Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

1. Overview of Decoder

A stream-like conversion of Bytes from one ByteBuf to another message type through a decoder

An implementation of Netty decoder is ChannelInboundHandlerAdapter. Basically, it decodes bytes data in the network into the type of message the user needs.

TIps: Decoders cannot be decorated with @sharable. Decoders can only decode for a single Channel. Decoding for multiple channels will result in data clutter

2. How does Decoder work

Important is a class ByteToMessageDecoder decoder, the class inherited ChannelInboundHandlerAdapter. So essentially the decoder is ChannelInboundHandler. Since it’s ChannelInboundHandler the data is read in the ChannelInboundHandler#channelRead method:

Tips: The reason why there is an if else branch is that if the developer ByteToMessageDecoder is set to the ServerBootstrap ServerSocketChannel handler, The Object type is SocketChannel and goes through the else branch. If it is childHandler set to ServerBootstrap then the Object type is ByteBuf and goes through the if branch. I’m just going to focus on the if branch here

2.1 Cumulator

The ByteToMessageDecoder class has a Cumulator interface:

public abstract class ByteToMessageDecoder extends ChannelInboundHandlerAdapter {
        public interface Cumulator {
        /**
         * Cumulate the given {@link ByteBuf}s and return the {@link ByteBuf} that holds the cumulated bytes.
         * The implementation is responsible to correctly handle the life-cycle of the given {@link ByteBuf}s and so
         * call {@link ByteBuf#release()} if a {@link ByteBuf} is fully consumed.
         */
        ByteBuf cumulate(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf in); }}Copy the code

Purpose: To accumulate incoming Byte data, and also to solve the sticky packet and half packet problem.

Suppose that the data we decode each time is ABCD, because there are sticky packets and half packets, the accumulator will perform the following operations (taking half packets as an example) :

The cumulation variable will be stored in ByteToMessageDecoder. The cumulation variable will be merged with the previous data in ByteToMessageDecoder. The cumulation variable will be merged with the previous data in ByteToMessageDecoder. It then decodes the desired Message. Then empty the cumulation that has been decoded.

2.2 Decoder work analysis

Bytes arrays read from the network are first processed by the ByteToMessageDecoder accumulator:

As shown in Figure 1: The accumulator processes the incoming data and merges it with the remaining data from the previous processing, then calls the decode:

**ByteToMessageDecoder#decode**

ByteToMessageDecoder#decode is an abstract method, so it depends on the implementation of the decoder, for example:

  • RedisDecoder
  • XmlDecoder

And so some Netty implementation of the decoder. Decoded structured objects, stored in **List**. When the current ChannelHandler decodes, subsequent ChannelHandlers are triggered

The value passed to the ChannelHandler’s channelRead method is the decoded Message type as shown in the following image:

Tips:

  • How can the incoming Bytefuf not be decoded to the desired type of Message, and these bytes are stored by the accumulator in the ByteBuf cumulation private variable of ByteToMessageDecoder
  • Any extra bytes will also be stored in the ByteBuf cumulation private variable ByteToMessageDecoder by the accumulator
  • The function of the accumulator is to solve the problem of sticky packets and half packets.

3. Summary

The key of the decoder is the accumulator, whose function is to solve the problem of sticky packets and half packets of data. Not enough data parsed into the corresponding Message data preserved, waiting for the follow-up data into a merger decode again, when the incoming data at a time but not too much can complete parsing into multiple (greater than 1), will the rest of the parsed data preserved, waiting for the subsequent incoming data and then to accumulate. Thus the whole decoding process is realized.

I am ant back elephant, the article is helpful to you like to pay attention to me, the article has incorrect place please give correct comments ~ thank you