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

Introduction to the

Netty provides a lot of http2 packaging for us, so we can easily build a server that supports HTTP2. The only thing we need to customize is the HTTP2 handler.

In the previous article, we introduced a custom Http2Handler that inherits from Http2ConnectionHandler and implements an Http2FrameListener. Netty (Http2ConnectionHandler) netty (Http2ConnectionHandler) Netty (Http2ConnectionHandler) (Http2ConnectionHandler)

Today I’m going to introduce you to this implementation.

Http2FrameCodec

The core class for this implementation is Http2FrameCodec. In fact, Http2FrameCodec also inherits from http2Connect Handler.

Its main function is to map frames and Http2Frame objects in HTTP/2. Http2Frame encapsulates all http2 frames in Netty, so that the subsequent handler can focus on handling Http2Frame objects, thereby getting rid of the various details of the HTTP2 protocol, can reduce the workload of users.

For each incoming HTTP/2 frame, Http2FrameCodec creates an Http2Frame object and passes it to the channelRead method for processing that object.

The outbound Http2Frame can be converted to an Http2 frame format by calling the write method.

Http2Frame, Http2FrameStream, and Http2StreamFrame

Netty has three very similar classes, Http2Frame, Http2FrameStream, and Http2StreamFrame.

In netty, a TCP connection can create multiple streams. Http2FrameStream contains the stream ID and stream status.

A stream contains multiple messages, each consisting of multiple frames, so Http2Frame is the corresponding Netty class to these frames.

Http2StreamFrame is itself a frame, in fact it inherits from Http2Frame.

Why does this class exist? Because a frame itself is typically associated with a particular stream, Http2StreamFrame represents this association and can specify its associated stream through its set Stream method. The stream() method returns NULL if you want the frame to apply to the entire connection rather than to a specific stream, or if it is associated to the entire connection.

The structure of the Http2FrameCodec

Although Http2FrameCodec has a constructor, Netty recommends using Http2FrameCodecBuilder to construct it:

Http2FrameCodecBuilder. ForServer (). The build ();Copy the code

You can see that Http2Frame DecBuilder has a forServer and a forClient method. They are used on the server side and the client side.

It is mainly distinguished by the server attribute inside.

The Stream life cycle

Frame Codec will send and write frames to a valid stream. As I mentioned earlier, Http2StreamFrame is associated with an Http2FrameStream object.

For a valid stream, if either party sends an RST_STREAM frame, the stream will be closed.

Either the sender or the receiver sends a frame with an END_STREAM flag, and the stream is also closed.

Flow control

Http2FrameCodec provides automatic control over streams, but we still need to do something to update Windows.

In particular, when we receive an Http2DataFrame message, after processing the message, we need to increase the size of the window to indicate that the data has been processed and there is more room for the new data.

Http2WindowUpdateFrame = CTX; Http2WindowUpdateFrame = CTX; Http2WindowUpdateFrame = CTX;

Private static void onDataRead(ChannelHandlerContext CTX, Http2DataFrame data){ Http2FrameStream stream = data.stream(); if (data.isEndStream()) { sendResponse(ctx, stream, data.content()); } else {// End stream is not sent, but the reference data.release() needs to be released; } // After processing data, we need to update the window frame, Increase the size of processed Data CTX. Write (new DefaultHttp2WindowUpdateFrame (Data) initialFlowControlledBytes ()). The stream (stream)); }Copy the code

Example, we introduced into the corresponding to the DefaultHttp2WindowUpdateFrame stream id, if the stream id 0, says dealing with the connection, not separate a stream.

In addition to the Windows update frame, for a specific stream of the initial window can also send a Http2SettingsFrame, by setting the Http2Settings. InitialWindowSize () to initialize.

Receives the message

For each HTTP/2 stream, the frame contained in it can be divided into Http2HeadersFrame and Http2DataFrame, Http2HeadersFrame must be the first frame received, and this headerFrame is associated with a stream object: Http2FrameStream.

Therefore, we can handle these two different frames separately:

public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof Http2HeadersFrame) { onHeadersRead(ctx, (Http2HeadersFrame) msg); } else if (msg instanceof Http2DataFrame) { onDataRead(ctx, (Http2DataFrame) msg); } else { super.channelRead(ctx, msg); }}Copy the code

The custom handler

To use Http2FrameCodec, we just need to add Http2FrameCodec to our pipline and then add a custom handler:

ctx.pipeline().addLast(Http2FrameCodecBuilder.forServer().build(), new CustHttp2Handler());
Copy the code

Since Http2FrameCodec already converts frames in HTTP2, we only need to handle the custom logic in CustHttp2Handler.

Netty recommend a custom handler inherited from Http2ChannelDuplexHandler, Because it has a forEachActiveStream(Http2FrameStreamVisitor) method for creating newStream() and iterating through all valid streams than normal ChannelDuplexHandler.

conclusion

This article explains the principles of Http2FrameCodec and what to look for in its accompanying handler implementation.

Learn -netty4 for an example of this article

This article is available at www.flydean.com/31-netty-fr…

The most popular interpretation, the most profound dry goods, the most concise tutorial, many tips you didn’t know waiting for you to discover!

Welcome to pay attention to my public number: “procedures those things”, understand technology, more understand you!