Search code examples
nettychunkedapache-plc4x

Netty MessageToMessageCodec decode method receives only 512byte chunk of large response


I'm working on the Apache PLC4X project where we implement industry PLC protocols using Netty. We are currently encountering a problem with decoding responses as soon as the size of these exceed a limit of 512 bytes.

As soon as the size of the response exceeds these 512 bytes, the decode method of our MessageToMessageCodec based protocol layer implementation only receives a 512 byte large ByteBuf. Directly after this, the method is called again with the rest of the packet.

How would be the correct way to handle this?


Solution

  • You should use the ByteToMessageDecoder here which will allow you to buffer data. See the javadocs for more details on how you can archive this. But basically its something like:

    YourDecoder extends ByteToMessageDecoder {
        @Override
        protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
            if (in.readableBytes() < 1024) {
                return;
            }
            ....
        }
    }