Fixed the decoder to prevent crash on buffer starving.

This commit is contained in:
ProfMobius committed 2014-10-03 03:18:03 +02:00
1 parent 6faaf46b80
commit f32a771e6b
2 files changed
+21 -17

No files matched your search

@@ -46,19 +46,19 @@ public class RConServer implements Runnable {
public final static RConServer instance = new RConServer();
public BiMap<FakePlayer, ChannelHandlerContext> fakePlayers = HashBiMap.create();
public ArrayList<Class> packetTypes = new ArrayList<Class>();
public BiMap<Byte, Class> 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));
}
}
@@ -12,17 +12,19 @@ import io.netty.handler.codec.ByteToMessageDecoder;
public class RconMsgColater extends ByteToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> 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);
}
}
}