Add encoder
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
package com.seibel.distanthorizons.core.network;
|
||||
|
||||
import com.seibel.distanthorizons.core.network.messageHandling.MessageDecoder;
|
||||
import com.seibel.distanthorizons.core.network.messageHandling.MessageHandler;
|
||||
import com.seibel.distanthorizons.core.network.messageHandling.MessageHandlerSide;
|
||||
import com.seibel.distanthorizons.core.network.messageHandling.MessageRegistry;
|
||||
import com.seibel.distanthorizons.core.network.protocol.*;
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
@@ -12,8 +9,10 @@ import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
|
||||
import io.netty.handler.codec.LengthFieldPrepender;
|
||||
import io.netty.handler.logging.LogLevel;
|
||||
import io.netty.handler.logging.LoggingHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class LodServer {
|
||||
// TODO move to config of some sort
|
||||
@@ -47,15 +46,18 @@ public class LodServer {
|
||||
private ChannelInitializer<SocketChannel> getInitializer() {
|
||||
return new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
public void initChannel(SocketChannel ch) {
|
||||
public void initChannel(@NotNull SocketChannel ch) {
|
||||
ChannelPipeline pipeline = ch.pipeline();
|
||||
|
||||
MessageRegistry messageRegistry = new MessageRegistry();
|
||||
|
||||
// Encoding
|
||||
pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 2, 0, 2));
|
||||
pipeline.addLast(new LengthFieldBasedFrameDecoder(Short.MAX_VALUE, 0, Short.BYTES, 0, Short.BYTES));
|
||||
pipeline.addLast(new MessageDecoder(messageRegistry));
|
||||
// TODO packet encoder
|
||||
|
||||
// Encoder
|
||||
pipeline.addLast(new LengthFieldPrepender(Short.BYTES));
|
||||
pipeline.addLast(new MessageEncoder(messageRegistry));
|
||||
|
||||
pipeline.addLast(new MessageHandler(MessageHandlerSide.SERVER));
|
||||
}
|
||||
|
||||
-6
@@ -1,6 +0,0 @@
|
||||
package com.seibel.distanthorizons.core.network.messageHandling;
|
||||
|
||||
public enum MessageHandlerSide {
|
||||
CLIENT,
|
||||
SERVER
|
||||
}
|
||||
+3
-3
@@ -5,17 +5,17 @@ import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
|
||||
// This message is critical to maintain backwards compatibility
|
||||
// as it's used to receive version BEFORE everything else.
|
||||
// as it's used to receive version before everything else.
|
||||
public class HelloMessage extends Message {
|
||||
public int version = ModInfo.PROTOCOL_VERSION;
|
||||
|
||||
@Override
|
||||
public void encode(ByteBuf out) {
|
||||
public void encode(ChannelHandlerContext ctx, ByteBuf out) {
|
||||
out.writeInt(version);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void decode(ByteBuf in) {
|
||||
public void decode(ChannelHandlerContext ctx, ByteBuf in) {
|
||||
version = in.readInt();
|
||||
}
|
||||
|
||||
|
||||
@@ -6,8 +6,8 @@ import io.netty.channel.ChannelHandlerContext;
|
||||
public abstract class Message {
|
||||
public Message() { }
|
||||
|
||||
public abstract void encode(ByteBuf out);
|
||||
public abstract void decode(ByteBuf in);
|
||||
public abstract void encode(ChannelHandlerContext ctx, ByteBuf out);
|
||||
public abstract void decode(ChannelHandlerContext ctx, ByteBuf in);
|
||||
|
||||
public void handle_Server(ChannelHandlerContext ctx) {
|
||||
throw new UnsupportedOperationException();
|
||||
|
||||
+3
-3
@@ -1,4 +1,4 @@
|
||||
package com.seibel.distanthorizons.core.network.messageHandling;
|
||||
package com.seibel.distanthorizons.core.network.protocol;
|
||||
|
||||
import com.seibel.distanthorizons.core.network.messages.Message;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
@@ -8,7 +8,7 @@ import io.netty.handler.codec.ByteToMessageDecoder;
|
||||
import java.util.List;
|
||||
|
||||
public class MessageDecoder extends ByteToMessageDecoder {
|
||||
private MessageRegistry messageRegistry;
|
||||
private final MessageRegistry messageRegistry;
|
||||
|
||||
public MessageDecoder(MessageRegistry messageRegistry) {
|
||||
this.messageRegistry = messageRegistry;
|
||||
@@ -17,7 +17,7 @@ public class MessageDecoder extends ByteToMessageDecoder {
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
|
||||
Message message = messageRegistry.createMessage(in.readShort());
|
||||
message.decode(in);
|
||||
message.decode(ctx, in);
|
||||
out.add(message);
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.seibel.distanthorizons.core.network.protocol;
|
||||
|
||||
import com.seibel.distanthorizons.core.network.messages.Message;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToByteEncoder;
|
||||
|
||||
public class MessageEncoder extends MessageToByteEncoder<Message> {
|
||||
private final MessageRegistry messageRegistry;
|
||||
|
||||
public MessageEncoder(MessageRegistry messageRegistry) {
|
||||
this.messageRegistry = messageRegistry;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf out) throws IllegalArgumentException {
|
||||
out.writeShort(messageRegistry.getMessageId(msg));
|
||||
msg.encode(ctx, out);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.seibel.distanthorizons.core.network.messageHandling;
|
||||
package com.seibel.distanthorizons.core.network.protocol;
|
||||
|
||||
import com.seibel.distanthorizons.core.network.messages.Message;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package com.seibel.distanthorizons.core.network.protocol;
|
||||
|
||||
public enum MessageHandlerSide {
|
||||
CLIENT,
|
||||
SERVER
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.seibel.distanthorizons.core.network.messageHandling;
|
||||
package com.seibel.distanthorizons.core.network.protocol;
|
||||
|
||||
import com.seibel.distanthorizons.core.network.messages.HelloMessage;
|
||||
import com.seibel.distanthorizons.core.network.messages.Message;
|
||||
Reference in New Issue
Block a user