I am currently evaluating Netty to handle socket comms for a Java client to integrate with a C++ server. The messaging protocol has the following structure –
- (Type)(SubType)(Length)(MessageBody) with size
- <4bytes><4bytes><4bytes><…> – The Length includes the header.
Following the Netty api I subclass LengthFieldBasedFrameDecoder to receive a valid full packet and then decode each packet depending on the type received. From docs I’m using –
- lengthFieldOffset = 8
- lengthFieldLength = 4
- lengthAdjustment = -12 (= the length of HDR1 + LEN, negative)
- initialBytesToStrip = 0
It works fine for about 5 minutes (I’m getting one message every 5 seconds or so) and then the decode event contains a ChannelBuffer that is much shorter than the size of the message. (I have received this message multiple times before the crash). I then obviously get an BufferUnderflowException in my internal decode code. Am I doing something wrong? Should I be guaranteed the correct sized buffer for the message when using LengthFieldBasedFrameDecoder?
LengthFieldBasedFrameDecoder class –
public class CisPacketDecoder extends LengthFieldBasedFrameDecoder
{
public CisPacketDecoder(int maxFrameLength, int lengthFieldOffset,
int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip) {
super(maxFrameLength, lengthFieldOffset, lengthFieldLength, lengthAdjustment,
initialBytesToStrip);
}
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf)
throws Exception
{
CisMessage message = null;
int type = buf.getInt(0); //Type is always first int
CisMessageType messageType = CisMessageType.fromIntToType(type);
if(messageType != null)
{
message = messageType.getObject();
if(message != null)
{
message.decode(buf.toByteBuffer());
}
else
{
System.out.println("Unable to create message for type " + type);
}
}
//mark the Channel buf as read by moving reader index
buf.readerIndex(buf.capacity());
return message;
}
}
And instantiated here.
public class PmcPipelineFactory implements ChannelPipelineFactory
{
@Override
public ChannelPipeline getPipeline() throws Exception
{
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("encoder", new CisPacketEncoder());
pipeline.addLast("decoder", new CisPacketDecoder(1024, 8, 4, -12, 0));
pipeline.addLast("handler", new MsgClientHandler());
return pipeline;
}
}
You need to call super.decode(..) and operate on the returned ChannelBuffer in your decode(..) method.
So it would be like this;
Be sure to check the UPPERCASE comments .