Over-the-long content is divided into multiple send issues lengthfieldbasedframedecoder use

Source: Internet
Author: User

This problem is more common, in high-concurrency large data transfer when the packet reception will be chaotic

In the Org.jboss.netty.handler.codec.frame package, there is a Lengthfieldbasedframedecoder class to parse a package with a length attribute, as long as we add the total length of the packet to the transport protocol (there may be a better way)

Specific methods:

1. You can add 4 bytes to the packet before the packet to indicate the total length of the package, for example:

/**
* Transfer Protocol
* |------------------------------------------
* | Total length 4byte |pkey length 4byte |
* |------------------------------------------
* | Value 4byte|name 4byte|zip 1 |
* |------------------------------------------
* |skey value 8byte (long-time, fixed) |
* |------------------------------------------------
* | Package Contents |
* |
* |------------------------------------------------

Before transmitting data through Netty, perform

2. In the accepted Channelpipeline, add decoder, before handler, for example:

[Java]View Plaincopy
  1. Bootstrap.setpipelinefactory (new Channelpipelinefactory () {
  2. Public channelpipeline Getpipeline () throws Exception {
  3. Channelpipeline pipeline = new Defaultchannelpipeline ();
  4. Pipeline.addfirst ("Decoder", new Lengthfieldbasedframedecoder (100000000,0,4,0,4));
  5. Pipeline.addlast ("Handler", new XXX (config));
  6. return pipeline;
  7. }
  8. });

Using Lengthfieldbasedframedecoder as the decoder implementation, the Lengthfieldbasedframedecoder constructor, the first parameter is the maximum length of information, exceeding this length return exception, The second parameter is the start (offset) bit of the length attribute, the length of our protocol is 0 to 3rd bytes, so here we write 0, the third parameter is the length of the length property, we are 4 bytes, so write 4, the fourth parameter is the length adjustment value, when the total length is defined as containing the header length, the length of the correction information The fifth parameter is the number of skipped bytes, and we skip the first 4 bytes as needed so that the receiving side directly accepts content that does not contain the length attribute.

At this point, the receiving end will not call handler to continue processing the information until the length specified by decoder is received intact.

Too long content is divided into multiple send issues Lengthfieldbasedframedecoder use

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.