“This is the 27th day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.

Like again, form a habit 👏👏

Netty codec

Netty codec components include Channel, ChannelHandler, and ChannelPipe.

ChannelHandler

The ChannelHandler acts as the application logic container for processing inbound and outbound data. Realize ChannelInboundHandler interface (or ChannelInboundHandlerAdapter), for example, you can pick up inbound events and data, the data will then be your application’s business logic processing. You can also flush data from the ChannelInboundHandler when you want to send a response to a connected client. Your business logic is usually written in one or more ChannelinboundHandlers. ChannelOutboundHandler works the same way, except it handles outbound data.

ChannelPipeline

ChannelPipeline provides a container for the ChannelHandler chain. In the case of a client application, we call events outbound if they travel from client to server, That is, the data sent from the client to the server will pass through a series of ChannelOutboundHandler in the pipeline (ChannelOutboundHandler calls each handler from tail to head). And it’s handled by these handlers, or it’s called inbound, Inbound calls only the ChannelInboundHandler logic in the pipeline (ChannelInboundHandler calls each handler from head to tail).

When you send or receive a message via Netty, a data conversion occurs. Inbound messages are decoded: converted from bytes to another format (such as Java objects); If it is an outbound message, it is encoded in bytes.

Netty provides a number of useful codecs that implement the ChannelInboundHadnler or ChannelOutboundHandler interface. In these classes, the channelRead method has been overridden. In the inbound case, this method is called for every message read from an inbound Channel. It then calls the decode() method provided by the known decoder to decode and forward the decoded bytes to the next ChannelInboundHandler in the ChannelPipeline.

Netty provides many codecs, such as StringEncoder and StringDecoder for codec strings, ObjectEncoder and ObjectDecoder for codec objects, and so on.

Protobuf can be used for efficient codec, but with Protobuf it is difficult to maintain a large number of proto files, and protostuff is generally used today.

Protostuff is a serialization method based on the Protobuf implementation. The most obvious advantage over Protobuf is that it can be serialized with almost no performance loss without us writing.proto files.

Netty Sticking and unpacking packets

TCP is a stream protocol, just a long string of binary data without boundaries. TCP as transport layer protocol is not don’t understand the specific meaning of the upper business data, it will be according to the actual situation of TCP buffer packet classification, so that in our business is a complete package, may be the TCP split into multiple packages to send, it is also possible to multiple small bag packaging into a large packets to send, This is known as the TCP sticky and unpack problem. Flow – oriented communication is message – free.

As shown in the following figure, the client sends two packets, D1 and D2, but the server may receive data in the following cases.

Solution:

1) Fixed length of message: the size of transmitted data is fixed in length, for example, the length of each segment is fixed at 100 bytes. If there is not enough space, space will be filled

2) Add special delimiters to the end of data packets, such as underscores and hyphens. This method is easy to use, but when selecting delimiters, it must be careful that delimiters cannot appear inside each data packet.

3) Sending length: Send each piece of data together with the length of the data. For example, the first four bits of each piece of data can be selected as the length of the data. The application layer can determine the start and end of each piece of data according to the length.

Netty provides multiple decoders that can perform subcontracting operations as follows:

  • LineBasedFrameDecoder
  • DelimiterBasedFrameDecoder special separator (subcontracting)
  • FixedLengthFrameDecoder