Deutro
Deutro

Reputation: 3323

Need to reassemble tcp packets with netty

currently I am working with an inhouse protocol where I send a request to our hardware and receive the answer with netty. In the message which I receive are several bytes which tell me how many bytes the answer will contain. In my channelRead method I wait until the readable bytes of the recieved message are equal or greater than the expected bytes to make sure I get all data.

if (((ByteBuf) msg).readableBytes() >= dataSize) {
        //do something with the bytes
        ctx.close();
        ((ByteBuf) msg).release();
}

This works fine if I receive exactly one tcp package from the hardware. Sometimes the hardware splits the TCP frame into several packages and my channelRead waits for ever.

Is there a simple way in netty to reassemble these packets in the channelRead method?

Upvotes: 1

Views: 456

Answers (1)

Norman Maurer
Norman Maurer

Reputation: 23567

Just extend ByteToMessageDecoder. This will handle all the buffering for you. Check the javadocs for more details and an example.

Upvotes: 2

Related Questions