public class CustomProtocolDecoder extends CumulativeProtocolDecoder{
byte currentCmd = -1;
int currentSize = -1;
boolean isFirst = false;
@Override
protected boolean doDecode(IoSession is, ByteBuffer bb, ProtocolDecoderOutput pdo) throws Exception {
if(currentCmd == -1)
{
currentCmd = bb.get();
currentSize = Packet.getSize(currentCmd);
isFirst = true;
}
while(bb.remaining() > 0)
{
if(!isFirst)
{
currentCmd = bb.get();
currentSize = Packet.getSize(currentCmd);
}
else
isFirst = false;
//System.err.println(currentCmd + " " + bb.remaining() + " " + currentSize);
if(bb.remaining() >= currentSize - 1)
{
Packet p = PacketDecoder.decodePacket(bb, currentCmd);
pdo.write(p);
}
else
{
bb.flip();
return false;
}
}
if(bb.remaining() == 0)
return true;
else
return false;
}
}
Anyone see anything wrong with this code? When a lot of packets are received at once, even when only one client is connected, one of them might get cut off at the end (12 bytes instead of 15 bytes, for example) which is obviously bad.