diff --git a/src/main/java/mcp/mobius/opis/network/rconserver/RConServer.java b/src/main/java/mcp/mobius/opis/network/rconserver/RConServer.java index 95c86c3..6b3ef4e 100644 --- a/src/main/java/mcp/mobius/opis/network/rconserver/RConServer.java +++ b/src/main/java/mcp/mobius/opis/network/rconserver/RConServer.java @@ -46,19 +46,19 @@ public class RConServer implements Runnable { public final static RConServer instance = new RConServer(); public BiMap fakePlayers = HashBiMap.create(); - public ArrayList packetTypes = new ArrayList(); + public BiMap packetTypes = HashBiMap.create(); private int port = -1; private RConServer(){ this.port = modOpis.rconport; - packetTypes.add(PacketReqChunks.class); - packetTypes.add(PacketReqData.class); - packetTypes.add(NetDataCommand.class); - packetTypes.add(NetDataList.class); - packetTypes.add(NetDataValue.class); - packetTypes.add(PacketChunks.class); + packetTypes.put((byte)0, PacketReqChunks.class); + packetTypes.put((byte)1, PacketReqData.class); + packetTypes.put((byte)2, NetDataCommand.class); + packetTypes.put((byte)3, NetDataList.class); + packetTypes.put((byte)4, NetDataValue.class); + packetTypes.put((byte)5, PacketChunks.class); } @Override @@ -96,12 +96,14 @@ public class RConServer implements Runnable { packet.encode(output); byte[] data = output.toByteArray(); - ByteBuf buf = ctx.alloc().buffer(data.length); + ByteBuf buf = ctx.alloc().buffer(data.length + 4 + 1); buf.writeInt(data.length); - buf.writeByte(packetTypes.indexOf(packet.getClass())); + buf.writeByte(packetTypes.inverse().get(packet.getClass())); buf.writeBytes(data); ctx.write(buf); ctx.flush(); + + modOpis.log.info(String.format("%s", data.length)); } } diff --git a/src/main/java/mcp/mobius/opis/network/rconserver/RconMsgColater.java b/src/main/java/mcp/mobius/opis/network/rconserver/RconMsgColater.java index 8ede2ee..2d2a6af 100644 --- a/src/main/java/mcp/mobius/opis/network/rconserver/RconMsgColater.java +++ b/src/main/java/mcp/mobius/opis/network/rconserver/RconMsgColater.java @@ -12,17 +12,19 @@ import io.netty.handler.codec.ByteToMessageDecoder; public class RconMsgColater extends ByteToMessageDecoder { - @Override - protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { + @Override + protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { if (in.readableBytes() < 4) { return; } // Packet starts with the size (int) - int packetSize = in.readInt(); // We read the packet size (not including the packet type byte) - if (in.readableBytes() < 1 + packetSize) { return ;} // We check if there is at least packet type + packetSize available bytes - byte packetType = in.readByte(); // We read the header - + int packetSize = in.getInt(in.readerIndex()); // We read the packet size (not including the packet type byte) + + if (in.readableBytes() < 4 + 1 + packetSize) { return ;} // We check if there is at least packet type + packetSize available bytes + in.readInt(); + byte packetType = in.readByte(); // We read the header + // Here we should get a list of potential packets and decode for the corresponding byte - + PacketBase packet = (PacketBase) RConServer.instance.packetTypes.get(packetType).newInstance(); packet.decode(ByteStreams.newDataInput(in.readBytes(packetSize).array())); out.add(packet); - } + } }