Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

“Welcome to the discussion in the comments section. The nuggets will be giving away 100 nuggets in the comments section after the diggnation project. See the event article for details.”

Introduction to the

No matter what protocol, if you really want to be used, need to convert the protocol into the corresponding language is good to really apply, this article will start from the http2 message structure, discuss netty to http2 message encapsulation, take you to appreciate the real framework should do what degree.

Structure of http2 messages

Http2 differs from HTTP1.1 in that it uses a new binary frame division to establish a data stream between the client and server for message exchange. The data stream is a bidirectional byte stream used to send one or more messages.

A message is a logically complete piece of data sent by a client and a server. Depending on the size of the data, messages can be divided into different frames. That is, messages are made up of different frames.

Frame is the smallest unit of http2 communication. From the previous section, we know that there are several frames:

  • DATA frame
  • HEADERS frame
  • PRIORITY frame
  • RST_STREAM frame
  • SETTINGS acknowledgment frame
  • SETTINGS frame
  • PING frame
  • PING acknowledgment
  • PUSH_PROMISE frame
  • GO_AWAY frame
  • WINDOW_UPDATE frame
  • Unknown Frame

Let’s take a look at the general structure of a stream and frame in HTTP2:

In HTTP2, a TCP connection can host multiple streams of data, and different frames in the streams can be sent interlaced.

Each frame is identified by its stream ID as the stream to which it belongs.

With the basic concepts of HTTP2 in mind, let’s look at netty’s encapsulation of HTTP2.

Netty encapsulates HTTP2

Http2Stream

As the largest unit of stream under a TCP connection, Netty provides the Http2Stream interface. Note that Http2Stream is an interface that has two implementation classes, DefaultStream and ConnectionStream.

There are two very important properties in Http2Stream: ID and state.

Id, described earlier, is the unique tag for stream. Note here that Stream ids created by the client must be odd and Stream ids created by the server must be even. CONNECTION_STREAM_ID (1); HTTP_UPGRADE_STREAM_ID (1);

State represents the stream’s state. Specifically, a stream has the following states:

        IDLE(false, false),
        RESERVED_LOCAL(false, false),
        RESERVED_REMOTE(false, false),
        OPEN(true, true),
        HALF_CLOSED_LOCAL(false, true),
        HALF_CLOSED_REMOTE(true, false),
        CLOSED(false, false);
Copy the code

Why do states need to distinguish between local and remote? This is because of the two ends of the stream connection, so there are two states.

The counterpart to the stream state is the http2 lifecycle. Netty provides Http2LifecycleManager to represent management of the HTTP2 lifecycle:

    void closeStreamLocal(Http2Stream stream, ChannelFuture future);
    void closeStreamRemote(Http2Stream stream, ChannelFuture future);
    void closeStream(Http2Stream stream, ChannelFuture future);
    ChannelFuture resetStream(ChannelHandlerContext ctx, int streamId, long errorCode,
            ChannelPromise promise);
    ChannelFuture goAway(ChannelHandlerContext ctx, int lastStreamId, long errorCode,
            ByteBuf debugData, ChannelPromise promise);
    void onError(ChannelHandlerContext ctx, boolean outbound, Throwable cause);
Copy the code

Close stream, reset stream, reject new stream: goAway, and handle error state.

Http2Frame

Stream is followed by the actual Http2Frame that carries the HTTP2 message. In Netty, Http2Frame is an interface that has many concrete implementations.

Direct subclasses of Http2Frame include HTTP2GoAwayFrame, HTTPPingFrame, Http2SettingsFrame, and HTTP2SettingsAckFrame.

GoAway indicates that no new stream is accepted, and ping is used for heartbeat detection. SETTINGS are used to modify the configuration of a connection or Stream.

Netty has an Http2Settings class for this.

There are some special setting names defined in this class:

SETTINGS name meaning
SETTINGS_HEADER_TABLE_SIZE Maximum size of the peer index table
SETTINGS_ENABLE_PUSH Whether to enable the server push function
SETTINGS_MAX_CONCURRENT_STREAMS The maximum number of concurrent streams allowed by the receiver
SETTINGS_INITIAL_WINDOW_SIZE The size of the sender window for stream-level flow control
SETTINGS_MAX_FRAME_SIZE Sets the maximum frame size
SETTINGS_MAX_HEADER_LIST_SIZE The maximum size of the peer header index table

In addition to the four frames mentioned above, Other frame to realize all inherit from Http2StreamFrame, specifically PriorityFrame, ResetFrame, HeadersFrame, DataFrame, WindowUpdateFrame, PushPromiseFrame and U NknownFrame.

Each frame represents a different function. The most important ones here are Http2HeadersFrame and Http2DataFrame.

Http2HeadersFrame is primarily an HTTP2 request sent from the client to the server.

In addition to the standard HTTP1.1 headers, http2 also supports the following headers:

      METHOD(":method", true),

        SCHEME(":scheme", true),

        AUTHORITY(":authority", true),

        PATH(":path", true),

        STATUS(":status", false),

        PROTOCOL(":protocol", true);
Copy the code

For Http2DataFrame, it is itself a ByteBufHolder, used to pass specific data information. The Payload of a data frame is stored directly in ByteBuf.

conclusion

So that’s netty encapsulating http2 messages.

Learn -netty4 for an example of this article

This article is available at www.flydean.com/28-netty-wr…

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!