Netty decoder with Sticky pack and unpacking

Source: Internet
Author: User

TCP is a "flow" protocol, a complete package may be split into multiple packets by TCP to send, or the small package into a large packet sent, which is called the TCP sticky packet and unpacking problems.

Assuming that the client sends packets D1 and D2 to the server individually, the following 4 scenarios may exist because the number of bytes read by the server is indeterminate.

    • 1. The service end points 2 times to read two separate packages, namely D1,D2, no sticky bags and unpacking;
    • 2. The service side received two packets at a time, D1 and D2 glued together, was become the TCP sticky packet;
    • 3. The service end points 2 reads to two packets, the first time reads to the complete D1 and the D2 packet the partial content, the second time reads to the D2 package the remainder, this is called the unpacking;
    • 4. The service end points 2 reads to two packets, first reads to the partial D1, reads the D1 remainder and the complete D2 packet for the second time;
    • 5. If the server side TCP receive sliding window is very small, and the packet D1 and D2 are very large, it is likely to send a fifth possibility, that is, the server multiple times to the D1 and D2 receive completely, during several times the unpacking situation.

Because the underlying TCP is unable to understand the upper layer of business logic, so at the bottom is not able to ensure that the packet is not split and reorganization, this problem can only be solved by the upper layer of application protocol stack design, according to the industry's mainstream protocol solution, summarized as follows:

    • 1. Message length, for example, the size of each message is a fixed length of 200 bytes, if not enough, empty space to fill;
    • 2. Add a carriage return line break at the end of the package to split;
    • 3. Divide the message into the message header and the message body, the message header contains the total length of the message (or message size) of the field, usually the design idea is the first field of the message header with an int to represent the total length of the message;
    • 4. More complex application-layer protocols;

Netty has abstracted the above 4 applications and provided 4 decoders

Linebasedframedecoder: Compile the Bytebuf in order to determine whether there is "\ n" or "\ r \ n", if so, at this position as the end position, from the readable index to the end of the interval of the byte is a row. It is a decoder that ends with a newline character, supports either a carry terminator or no terminator, and supports the maximum length of a single line. If a newline character is still not found after continuous reading to the maximum length, an exception is thrown and the exception stream that was previously read is ignored.

Fixedlengthframedecoder: A fixed length decoder that automatically decodes messages at a specified length, and developers do not need to consider issues such as TCP sticky packets. With Fixedlengthframedecoder decoding, regardless of how much data is received at one time, he is decoded according to the length set in the constructor, and if it is a half-packet message, Fixedlengthframedecoder caches the half-packet message and waits for the next packet. After arrival, make a bundle until the full package is read.

Delimiterbasedframedecoder: is a custom delimiter decoding, the first parameter of the constructor represents the maximum length of a single message, and the delimiter is still not found after reaching that length. Throws a toolongframeexception exception to prevent a memory overflow due to a missing delimiter in the exception stream.

Lengthfieldbasedframedecoder: distinguishes the entire package message by a fixed length. Message fixed length, the size of the packet, not enough space to complete, the sending and receiving parties follow the same convention, so that even if the adhesive packet through the receiver programming to achieve a fixed length message can also be distinguished.

Netty decoder with Sticky pack and unpacking

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.