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;
}
}這是在MINA中編寫ProtocolDecoder的正確方法嗎?
任何人看到什麼毛病此代碼?當一次接收到大量數據包時,即使只連接了一個客戶端,其中一個數據包可能會在最後被切斷(例如12個字節,而不是15個字節),這顯然很糟糕。