Is this the right way to write a ProtocolDecoder in MINA?
Posted
by phpscriptcoder
on Stack Overflow
See other posts from Stack Overflow
or by phpscriptcoder
Published on 2010-05-22T18:06:42Z
Indexed on
2010/05/22
18:10 UTC
Read the original article
Hit count: 236
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.
© Stack Overflow or respective owner