Small taping you know Netty series seven (those out-of-the-box Channelhandler).

Source: Internet
Author: User
Tags starttls

First, preface
Netty provides a number of general-purpose protocols for XXX and processors, almost out-of-the-box, which reduces the amount of time and effort you would have spent on quite tedious matters. In addition, this article, does not involve Netty to the WebSocket agreement support, because involves the space is a bit big, will make a concrete introduction in next article.

Back to Top
Second, SSL protocol
The SSL protocol is a security protocol that stacks on top of other protocols. To support SSL/TLS, Java provides the JAVAX.NET.SSL package, and its sslcontext and Sslengine classes make decryption and encryption fairly straightforward. Netty uses this API through a channelhandler called Sslhandler, where Sslhandler uses sslengine internally to do the actual work. Describes the Sslhandler data flow.

Copy Code br/> @Override

Bytebufallocator bytebufallocator = Ch.alloc ();
For each Sslhandler instance, the Channel Bytebufallocator is used to obtain a new sslengine from Sslcontext.
Sslengine sslengine = Context.newengine (bytebufallocator);
Server-side mode, client mode set to True
Sslengine.setuseclientmode (FALSE);
Do not need to authenticate the client, the client does not set the item
Sslengine.setneedclientauth (FALSE);
To set Sslhandler to the first Channelhandler. This ensures that encryption is done only after all other channelhandler have applied their logic to the data.
StartTls If True, the first message written will not be encrypted (the client should be set to true)
Ch.pipeline (). AddFirst ("SSL", New Sslhandler (Sslengine, StartTls));
}
Copy Code
Tips: The order in which Channelhandler is executed in the Channelpipeline chain-the sequence of inbound events, the order in which outbound events are executed.

Back to Top
Third, the HTTP protocol
HTTP is based on the request/response pattern: The client sends an HTTP request to the server, and the server returns an HTTP response. Shows the components of HTTP requests and responses in Netty:

Netty 对 HTTP 协议的支持主要提供了以下 ChannelHandler:

Httpresponsedecoder:xxx, for the client, decodes the response from the server.
Httprequestencoder: Encoder, User Client, encoding the request sent to the server.
Httprequestdecoder:xxx, for the service side, decodes the request from the client.
Httpresponseencoder: Encoder, for service-side, encodes the response to the client.
HTTPCLIENTCODEC: Compile xxx, User client, effect equals Httpresponsedecoder + Httprequestencoder.
HTTPSERVERCODEC: Compile xxx, User Service side, effect equals Httprequestdecoder + Httpresponseencoder.
Httpobjectaggregator: Aggregator, because HTTP requests and responses may consist of many parts that need to be aggregated to form a complete message, Httpobjectaggregator can merge multiple message parts into Fullhttprequest or fullhttpresponse messages.
Httpcontentcompressor: compression, User Service side, compress the data to be transferred, support gzip and deflate compression format.
Httpcontentdecompressor: Decompression, for the client, to decompress the data transmitted by the service side.

Copy Code br/> @Override
Channelpipeline pipeline = Ch.pipeline ();
Sslengine sslengine = Sslcontext.newengine (Ch.alloc ());
if (isclient) {
Use HTTPS to add SSL authentication
Pipeline.addfirst ("SSL", New Sslhandler (Sslengine, true));
Pipeline.addlast ("Codec", new Httpclientcodec ());
1. It is recommended to turn on the compression function to reduce the size of transmitted data as much as possible
2. Client processes compressed content from the server
Pipeline.addlast ("Decompressor", New Httpcontentdecompressor ());
}else {
Pipeline.addfirst ("SSL", New Sslhandler (Sslengine));
Httpservercodec: Converts an HTTP client request to an HttpRequest object, encodes the HttpResponse object into an HTTP response, and sends it to the client.
Pipeline.addlast ("Codec", new Httpservercodec ());
Server-side, compressed data
Pipeline.addlast ("Compressor", New Httpcontentcompressor ());
}
Purpose multiple messages are converted to a single fullhttprequest or fullhttpresponse
Add Httpobjectaggregator with maximum message 512KB to Channelpipeline
A Toolongframeexception exception is thrown after the message is larger than this.
Pipeline.addlast ("Aggregator", New Httpobjectaggregator (512 * 1024));
}
Copy Code
Tips: When using HTTP, it is recommended to turn on compression to minimize the amount of data transferred. While compression can lead to some CPU clock cycles overhead.

Back to Top
Iv. solutions for unpacking and sticking packages
TCP transmission process, the client sent two packets, and the server only received a packet, the client's two packets stuck together, known as sticky packet;

TCP 传输过程中,客户端发送了两个数据包,服务端虽然收到了两个数据包,但是两个数据包都是不完整的,或多了数据,或少了数据,称为拆包;发生TCP粘包、拆包主要是由于下面一些原因:

1. The application writes more data than the socket buffer size, which will occur when the package is split.
2, the application write data is less than the socket buffer size, the network card will apply the data written to the network, which will occur sticky packets.
3, the MSS (maximum message length) size of the TCP segment, when the TCP message length-tcp head length >MSS will occur when unpacking.
4, the receiving method does not read the socket buffer data in time, this will occur sticky packets.

Netty 预定义了一些×××用于解决粘包和拆包现象,其中大体分为两类:

Delimiter-based protocol: use defined characters between packets to mark the beginning or end of a message or message segment. This allows the receiver to split the different packets by this character.
Length-based protocol: Send the packet header to each packet, the head should contain at least the length of the packet, so that the receiving end after receiving the data, by reading the length of the header section of the field, we know the actual length of each packet.

基于分隔符的协议

Copy Code
public class Linebasedhandlerinitializer extends Channelinitializer<channel> {

@Overrideprotected void initChannel(Channel ch) throws Exception {    ch.pipeline().addLast(            // 将提取到的桢转发给下一个Channelhandler            new LineBasedFrameDecoder(64 * 1024),            // 添加 FrameHandler 以接收帧            new FrameHandler()    );}public static final class FrameHandler extends SimpleChannelInboundHandler<ByteBuf> {    @Override    protected void messageReceived(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {        //Do something with the data extracted from the frame    }}

}
Copy Code
Length-based protocol

LengthFieldBasedFrameDecoder 是 Netty 基于长度协议解决拆包粘包问题的一个重要的类,主要结构就是 header+body 结构。我们只需要传入正确的参数就可以发送和接收正确的数据,那吗重点就在于这几个参数的意义。下面我们就具体了解一下这几个参数的意义。先来看一下LengthFieldBasedFrameDecoder主要的构造方法:

Public Lengthfieldbasedframedecoder (
int Maxframelength,
int lengthfieldoffset, int lengthfieldlength,
int lengthadjustment, int initialbytestostrip)
Maxframelength: Maximum frame length. That is, the maximum length of data that can be received. If exceeded, this data will be discarded.
Lengthfieldoffset: Length field offset. This means that several bytes of data start may not represent the length of the data, and a few bytes are required to be the length field.
Lengthfieldlength: Length domain number of bytes. Use a few bytes to represent the length of the data.
Lengthadjustment: Data length correction. Because length fields specify a length that can make the entire length of the header+body, or just the length of the body. If the entire length of the header+body is represented, then we need to fix the data length.
Initialbytestostrip: The number of bytes skipped. If you need to receive all of Header+body's data, this value is 0, and if you only want to receive the body data, you need to skip the number of bytes that the header occupies.

Copy Code
public class Lengthbasedinitializer extends Channelinitializer<channel> {

@Overrideprotected void initChannel(Channel ch) throws Exception {    ch.pipeline().addLast(            new LengthFieldBasedFrameDecoder(64 * 1024, 0, 8),            new FrameHandler()    );}public static final class FrameHandler extends SimpleChannelInboundHandler<ByteBuf> {    @Override    protected void messageReceived(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {        //处理桢的数据    }}

}
Copy Code
TIPS:UDP protocol does not occur in the packet or unpacking phenomenon, because UDP is based on the message sent, the UDP header uses 16bit to indicate the length of the UDP data packets, so in the application layer can be very good to separate the different data message area.

Back to Top
V. Other
Due to the possibility of network saturation, how to write large chunks of data efficiently in an asynchronous framework is a special problem. Netty is implemented through an Fileregion interface, which is defined in the API documentation for Netty: "File area sent by Channel that supports 0 copies of file transfer." However, this interface only applies to the direct transfer of file content, and does not include any processing of file data by the application.

View Code
If large chunks of data are to be copied from the file system into the user's memory, a chunkedwritehandler can be installed and written to the file data using the Chunkedinput implementation. It supports asynchronously writing large data streams without causing a lot of memory consumption.

Chunkedwritehandlerinitializer.java

Netty提供的用于和JDK进行互操作的序列化类 :Netty提供的用于和 JBoss Marshalling 进行互操作的序列化类 :

Marshallinginitializer.java
Netty provides a serialization class for interoperating with Protocol buffers:

Small taping you know Netty series seven (those out-of-the-box Channelhandler).

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.