- java.lang.Object
-
- io.netty5.channel.ChannelHandlerAdapter
-
- io.netty5.handler.codec.ByteToMessageDecoder
-
- io.netty5.handler.codec.http2.Http2ConnectionHandler
-
- io.netty5.handler.codec.http2.Http2FrameCodec
-
- All Implemented Interfaces:
ChannelHandler
,Http2LifecycleManager
@UnstableApi public class Http2FrameCodec extends Http2ConnectionHandler
This API is very immature. The Http2Connection-based API is currently preferred over this API. This API is targeted to eventually replace or reduce the need for the
Http2ConnectionHandler
API.An HTTP/2 handler that maps HTTP/2 frames to
Http2Frame
objects and vice versa. For every incoming HTTP/2 frame, anHttp2Frame
object is created and propagated viaByteToMessageDecoder.channelRead(io.netty5.channel.ChannelHandlerContext, java.lang.Object)
. OutboundHttp2Frame
objects received viawrite(io.netty5.channel.ChannelHandlerContext, java.lang.Object)
are converted to the HTTP/2 wire format. HTTP/2 frames specific to a stream implement theHttp2StreamFrame
interface. TheHttp2FrameCodec
is instantiated using theHttp2FrameCodecBuilder
. It's recommended for channel handlers to inherit from theHttp2ChannelDuplexHandler
, as it provides additional functionality like iterating over all active streams or creating outbound streams.Stream Lifecycle
The frame codec delivers and writes frames for active streams. An active stream is closed when either side sends a
RST_STREAM
frame or both sides send a frame with theEND_STREAM
flag set. EachHttp2StreamFrame
has aHttp2FrameStream
object attached that uniquely identifies a particular stream.Http2StreamFrame
s read from the channel always aHttp2FrameStream
object set, while when writing aHttp2StreamFrame
the application code needs to set aHttp2FrameStream
object usingHttp2StreamFrame.stream(Http2FrameStream)
.Flow control
The frame codec automatically increments stream and connection flow control windows.
Incoming flow controlled frames need to be consumed by writing a
Http2WindowUpdateFrame
with the consumed number of bytes and the corresponding stream identifier set to the frame codec.The local stream-level flow control window can be changed by writing a
Http2SettingsFrame
with theHttp2Settings.initialWindowSize()
set to the targeted value.The connection-level flow control window can be changed by writing a
Http2WindowUpdateFrame
with the desired window size increment in bytes and the stream identifier set to0
. By default the initial connection-level flow control window is the same as initial stream-level flow control window.New inbound Streams
The first frame of an HTTP/2 stream must be an
Http2HeadersFrame
, which will have anHttp2FrameStream
object attached.New outbound Streams
A outbound HTTP/2 stream can be created by first instantiating a new
Http2FrameStream
object viaHttp2ChannelDuplexHandler.newStream()
, and then writing aHttp2HeadersFrame
object with the stream attached.{@code final Http2Stream2 stream = handler.newStream(); ctx.write(headersFrame.stream(stream)).addListener(new FutureListener
() {
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class io.netty5.handler.codec.ByteToMessageDecoder
ByteToMessageDecoder.Cumulator
-
-
Field Summary
Fields Modifier and Type Field Description protected Http2Connection.PropertyKey
streamKey
-
Fields inherited from class io.netty5.handler.codec.ByteToMessageDecoder
COMPOSITE_CUMULATOR, MERGE_CUMULATOR
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
channelInboundEvent(ChannelHandlerContext ctx, Object evt)
Handles the cleartext HTTP upgrade event.void
handlerAdded0(ChannelHandlerContext ctx)
protected boolean
isGracefulShutdownComplete()
Called by the graceful shutdown logic to determine when it is safe to close the connection.protected void
onConnectionError(ChannelHandlerContext ctx, boolean outbound, Throwable cause, Http2Exception http2Ex)
Handler for a connection error.protected void
onStreamError(ChannelHandlerContext ctx, boolean outbound, Throwable cause, Http2Exception.StreamException streamException)
Exceptions for unknown streams, that is streams that have noHttp2FrameStream
object attached are simply logged and replied to by sending a RST_STREAM frame.Future<Void>
write(ChannelHandlerContext ctx, Object msg)
Processes allHttp2Frame
s.-
Methods inherited from class io.netty5.handler.codec.http2.Http2ConnectionHandler
channelActive, channelExceptionCaught, channelInactive, channelReadComplete, channelWritabilityChanged, close, closeStream, closeStreamLocal, closeStreamRemote, connection, decode, decoder, encoder, flush, frameWriter, goAway, gracefulShutdownTimeoutMillis, gracefulShutdownTimeoutMillis, handlerRemoved0, handleServerHeaderDecodeSizeError, onError, onHttpClientUpgrade, onHttpServerUpgrade, resetStream
-
Methods inherited from class io.netty5.handler.codec.ByteToMessageDecoder
actualReadableBytes, channelRead, channelShutdown, decodeLast, discardSomeReadBytes, handlerAdded, handlerRemoved, internalBuffer, isSharable, isSingleDecode, setSingleDecode
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty5.channel.ChannelHandler
bind, channelRegistered, channelUnregistered, connect, deregister, disconnect, pendingOutboundBytes, read, register, sendOutboundEvent, shutdown
-
-
-
-
Field Detail
-
streamKey
protected final Http2Connection.PropertyKey streamKey
-
-
Method Detail
-
handlerAdded0
public void handlerAdded0(ChannelHandlerContext ctx) throws Exception
- Overrides:
handlerAdded0
in classHttp2ConnectionHandler
- Throws:
Exception
-
channelInboundEvent
public final void channelInboundEvent(ChannelHandlerContext ctx, Object evt) throws Exception
Handles the cleartext HTTP upgrade event. If an upgrade occurred, sends a simple response via HTTP/2 on stream 1 (the stream specifically reserved for cleartext HTTP upgrade).- Throws:
Exception
-
write
public Future<Void> write(ChannelHandlerContext ctx, Object msg)
Processes allHttp2Frame
s.Http2StreamFrame
s may only originate in child streams.- Parameters:
ctx
- theChannelHandlerContext
for which the write operation is mademsg
- the message to write- Returns:
- the
Future
which will be notified once the operation completes.
-
onConnectionError
protected void onConnectionError(ChannelHandlerContext ctx, boolean outbound, Throwable cause, Http2Exception http2Ex)
Description copied from class:Http2ConnectionHandler
Handler for a connection error. Sends a GO_AWAY frame to the remote endpoint. Once all streams are closed, the connection is shut down.- Overrides:
onConnectionError
in classHttp2ConnectionHandler
- Parameters:
ctx
- the channel contextoutbound
-true
if the error was caused by an outbound operation.cause
- the exception that was caughthttp2Ex
- theHttp2Exception
that is embedded in the causality chain. This may benull
if it's an unknown exception.
-
onStreamError
protected final void onStreamError(ChannelHandlerContext ctx, boolean outbound, Throwable cause, Http2Exception.StreamException streamException)
Exceptions for unknown streams, that is streams that have noHttp2FrameStream
object attached are simply logged and replied to by sending a RST_STREAM frame.- Overrides:
onStreamError
in classHttp2ConnectionHandler
- Parameters:
ctx
- the channel contextoutbound
-true
if the error was caused by an outbound operation.cause
- the exception that was caughtstreamException
- theHttp2Exception.StreamException
that is embedded in the causality chain.
-
isGracefulShutdownComplete
protected final boolean isGracefulShutdownComplete()
Description copied from class:Http2ConnectionHandler
Called by the graceful shutdown logic to determine when it is safe to close the connection. Returnstrue
if the graceful shutdown has completed and the connection can be safely closed. This implementation just guarantees that there are no active streams. Subclasses may override to provide additional checks.- Overrides:
isGracefulShutdownComplete
in classHttp2ConnectionHandler
-
-