Home / Class/ PrefaceDecoder Class — netty Architecture

PrefaceDecoder Class — netty Architecture

Architecture documentation for the PrefaceDecoder class in Http2ConnectionHandler.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  83642a58_aebe_1c6a_ae68_9c2017752662["PrefaceDecoder"]
  9c883019_01e3_e30b_1c79_24fbb727545e["Http2ConnectionHandler.java"]
  83642a58_aebe_1c6a_ae68_9c2017752662 -->|defined in| 9c883019_01e3_e30b_1c79_24fbb727545e
  73375026_56ea_312c_baa4_40200ab8c7dd["PrefaceDecoder()"]
  83642a58_aebe_1c6a_ae68_9c2017752662 -->|method| 73375026_56ea_312c_baa4_40200ab8c7dd
  757a3b1c_aa01_c10d_4d8e_8d76310493c1["prefaceSent()"]
  83642a58_aebe_1c6a_ae68_9c2017752662 -->|method| 757a3b1c_aa01_c10d_4d8e_8d76310493c1
  7a6947b9_edc8_6ed8_f6ad_f257a2519043["decode()"]
  83642a58_aebe_1c6a_ae68_9c2017752662 -->|method| 7a6947b9_edc8_6ed8_f6ad_f257a2519043
  f123ca8d_4393_d5af_4aa4_bd6bee27eb0e["channelActive()"]
  83642a58_aebe_1c6a_ae68_9c2017752662 -->|method| f123ca8d_4393_d5af_4aa4_bd6bee27eb0e
  68787620_1297_7012_76dd_f35cf0f76325["channelInactive()"]
  83642a58_aebe_1c6a_ae68_9c2017752662 -->|method| 68787620_1297_7012_76dd_f35cf0f76325
  2c2407ea_daae_cefc_2c70_e617573e655a["handlerRemoved()"]
  83642a58_aebe_1c6a_ae68_9c2017752662 -->|method| 2c2407ea_daae_cefc_2c70_e617573e655a
  c1d1be9f_9652_da73_3f65_f6f0e074ccdf["cleanup()"]
  83642a58_aebe_1c6a_ae68_9c2017752662 -->|method| c1d1be9f_9652_da73_3f65_f6f0e074ccdf
  52c5c05c_e3b5_8aff_b159_87aaed064f8f["readClientPrefaceString()"]
  83642a58_aebe_1c6a_ae68_9c2017752662 -->|method| 52c5c05c_e3b5_8aff_b159_87aaed064f8f
  c95ea991_3080_fce0_ff9f_9e2917aa74d9["verifyFirstFrameIsSettings()"]
  83642a58_aebe_1c6a_ae68_9c2017752662 -->|method| c95ea991_3080_fce0_ff9f_9e2917aa74d9
  fedde1dc_4baf_9ad9_9fa9_576c4ae34102["sendPreface()"]
  83642a58_aebe_1c6a_ae68_9c2017752662 -->|method| fedde1dc_4baf_9ad9_9fa9_576c4ae34102

Relationship Graph

Source Code

codec-http2/src/main/java/io/netty/handler/codec/http2/Http2ConnectionHandler.java lines 226–394

    private final class PrefaceDecoder extends BaseDecoder {
        private ByteBuf clientPrefaceString;
        private boolean prefaceSent;

        PrefaceDecoder(ChannelHandlerContext ctx) throws Exception {
            clientPrefaceString = clientPrefaceString(encoder.connection());
            // This handler was just added to the context. In case it was handled after
            // the connection became active, send the connection preface now.
            sendPreface(ctx);
        }

        @Override
        public boolean prefaceSent() {
            return prefaceSent;
        }

        @Override
        public void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
            try {
                if (ctx.channel().isActive() && readClientPrefaceString(in) && verifyFirstFrameIsSettings(in)) {
                    // After the preface is read, it is time to hand over control to the post initialized decoder.
                    byteDecoder = new FrameDecoder();
                    byteDecoder.decode(ctx, in, out);
                }
            } catch (Throwable e) {
                if (byteDecoder != null) {
                    // Skip all bytes before we report the exception as
                    in.skipBytes(in.readableBytes());
                }
                onError(ctx, false, e);
            }
        }

        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            // The channel just became active - send the connection preface to the remote endpoint.
            sendPreface(ctx);

            if (flushPreface) {
                // As we don't know if any channelReadComplete() events will be triggered at all we need to ensure we
                // also flush. Otherwise the remote peer might never see the preface / settings frame.
                // See https://github.com/netty/netty/issues/12089
                ctx.flush();
            }
        }

        @Override
        public void channelInactive(ChannelHandlerContext ctx) throws Exception {
            cleanup();
            super.channelInactive(ctx);
        }

        /**
         * Releases the {@code clientPrefaceString}. Any active streams will be left in the open.
         */
        @Override
        public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
            cleanup();
        }

        /**
         * Releases the {@code clientPrefaceString}. Any active streams will be left in the open.
         */
        private void cleanup() {
            if (clientPrefaceString != null) {
                clientPrefaceString.release();
                clientPrefaceString = null;
            }
        }

        /**
         * Decodes the client connection preface string from the input buffer.
         *
         * @return {@code true} if processing of the client preface string is complete. Since client preface strings can
         *         only be received by servers, returns true immediately for client endpoints.
         */
        private boolean readClientPrefaceString(ByteBuf in) throws Http2Exception {
            if (clientPrefaceString == null) {
                return true;
            }

Frequently Asked Questions

What is the PrefaceDecoder class?
PrefaceDecoder is a class in the netty codebase, defined in codec-http2/src/main/java/io/netty/handler/codec/http2/Http2ConnectionHandler.java.
Where is PrefaceDecoder defined?
PrefaceDecoder is defined in codec-http2/src/main/java/io/netty/handler/codec/http2/Http2ConnectionHandler.java at line 226.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free