Class Http2FrameCodec
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelInboundHandlerAdapter
-
- io.netty.handler.codec.ByteToMessageDecoder
-
- io.netty.handler.codec.http2.Http2ConnectionHandler
-
- io.netty.handler.codec.http2.Http2FrameCodec
-
- All Implemented Interfaces:
ChannelHandler,ChannelInboundHandler,ChannelOutboundHandler,Http2LifecycleManager
- Direct Known Subclasses:
Http2MultiplexCodec
@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
Http2ConnectionHandlerAPI.An HTTP/2 handler that maps HTTP/2 frames to
Http2Frameobjects and vice versa. For every incoming HTTP/2 frame, anHttp2Frameobject is created and propagated viaByteToMessageDecoder.channelRead(io.netty.channel.ChannelHandlerContext, java.lang.Object). OutboundHttp2Frameobjects received viawrite(io.netty.channel.ChannelHandlerContext, java.lang.Object, io.netty.channel.ChannelPromise)are converted to the HTTP/2 wire format. HTTP/2 frames specific to a stream implement theHttp2StreamFrameinterface. TheHttp2FrameCodecis 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 aRST_STREAMframe or both sides send a frame with theEND_STREAMflag set. EachHttp2StreamFramehas aHttp2FrameStreamobject attached that uniquely identifies a particular stream.Http2StreamFrames read from the channel always aHttp2FrameStreamobject set, while when writing aHttp2StreamFramethe application code needs to set aHttp2FrameStreamobject 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
Http2WindowUpdateFramewith 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
Http2SettingsFramewith theHttp2Settings.initialWindowSize()set to the targeted value.The connection-level flow control window can be changed by writing a
Http2WindowUpdateFramewith 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 anHttp2HeadersFrame, which will have anHttp2FrameStreamobject attached.New outbound Streams
A outbound HTTP/2 stream can be created by first instantiating a newHttp2FrameStreamobject viaHttp2ChannelDuplexHandler.newStream(), and then writing aHttp2HeadersFrameobject with the stream attached.final Http2Stream2 stream = handler.newStream(); ctx.write(headersFrame.stream(stream)).addListener(new ChannelFutureListener() {
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classHttp2FrameCodec.ConnectionListener(package private) static classHttp2FrameCodec.DefaultHttp2FrameStreamHttp2FrameStreamimplementation.private classHttp2FrameCodec.FrameListenerprivate classHttp2FrameCodec.Http2RemoteFlowControllerListener-
Nested classes/interfaces inherited from class io.netty.handler.codec.ByteToMessageDecoder
ByteToMessageDecoder.Cumulator
-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description (package private) ChannelHandlerContextctxprivate IntObjectMap<Http2FrameCodec.DefaultHttp2FrameStream>frameStreamToInitializeMapprivate java.lang.IntegerinitialFlowControlWindowSizeprivate static InternalLoggerLOGprivate intnumBufferedStreamsNumber of buffered streams if theStreamBufferingEncoderis used.protected Http2Connection.PropertyKeystreamKeyprivate Http2Connection.PropertyKeyupgradeKey-
Fields inherited from class io.netty.handler.codec.ByteToMessageDecoder
COMPOSITE_CUMULATOR, MERGE_CUMULATOR
-
-
Constructor Summary
Constructors Constructor Description Http2FrameCodec(Http2ConnectionEncoder encoder, Http2ConnectionDecoder decoder, Http2Settings initialSettings, boolean decoupleCloseAndGoAway)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) booleanconsumeBytes(int streamId, int bytes)(package private) voidforEachActiveStream(Http2FrameStreamVisitor streamVisitor)Iterates over all active HTTP/2 streams.private voidhandleHeaderFuture(ChannelFuture channelFuture, int streamId)voidhandlerAdded(ChannelHandlerContext ctx)Do nothing by default, sub-classes may override this method.(package private) voidhandlerAdded0(ChannelHandlerContext ctx)private voidincreaseInitialConnectionWindow(int deltaBytes)protected booleanisGracefulShutdownComplete()Called by the graceful shutdown logic to determine when it is safe to close the connection.(package private) Http2FrameCodec.DefaultHttp2FrameStreamnewStream()Creates a new outbound/local stream.(package private) intnumInitializingStreams()Retrieve the number of streams currently in the process of being initialized.protected voidonConnectionError(ChannelHandlerContext ctx, boolean outbound, java.lang.Throwable cause, Http2Exception http2Ex)Handler for a connection error.(package private) voidonHttp2Frame(ChannelHandlerContext ctx, Http2Frame frame)(package private) voidonHttp2FrameStreamException(ChannelHandlerContext ctx, Http2FrameStreamException cause)(package private) voidonHttp2StreamStateChanged(ChannelHandlerContext ctx, Http2FrameCodec.DefaultHttp2FrameStream stream)private voidonHttp2StreamWritabilityChanged(ChannelHandlerContext ctx, Http2FrameCodec.DefaultHttp2FrameStream stream, boolean writable)private voidonHttp2UnknownStreamError(ChannelHandlerContext ctx, java.lang.Throwable cause, Http2Exception.StreamException streamException)private voidonStreamActive0(Http2Stream stream)protected voidonStreamError(ChannelHandlerContext ctx, boolean outbound, java.lang.Throwable cause, Http2Exception.StreamException streamException)Exceptions for unknown streams, that is streams that have noHttp2FrameStreamobject attached are simply logged and replied to by sending a RST_STREAM frame.private voidonUpgradeEvent(ChannelHandlerContext ctx, HttpServerUpgradeHandler.UpgradeEvent evt)private voidtryExpandConnectionFlowControlWindow(Http2Connection connection)voiduserEventTriggered(ChannelHandlerContext ctx, java.lang.Object evt)Handles the cleartext HTTP upgrade event.voidwrite(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise)Processes allHttp2Frames.private voidwriteGoAwayFrame(ChannelHandlerContext ctx, Http2GoAwayFrame frame, ChannelPromise promise)private voidwriteHeadersFrame(ChannelHandlerContext ctx, Http2HeadersFrame headersFrame, ChannelPromise promise)-
Methods inherited from class io.netty.handler.codec.http2.Http2ConnectionHandler
bind, channelActive, channelInactive, channelReadComplete, channelReadComplete0, channelWritabilityChanged, close, closeStream, closeStreamLocal, closeStreamRemote, connect, connection, decode, decoder, deregister, disconnect, encoder, exceptionCaught, flush, frameWriter, goAway, gracefulShutdownTimeoutMillis, gracefulShutdownTimeoutMillis, handlerRemoved0, handleServerHeaderDecodeSizeError, onError, onHttpClientUpgrade, onHttpServerUpgrade, read, resetStream
-
Methods inherited from class io.netty.handler.codec.ByteToMessageDecoder
actualReadableBytes, callDecode, channelRead, decodeLast, discardSomeReadBytes, handlerRemoved, internalBuffer, isSingleDecode, setCumulator, setDiscardAfterReads, setSingleDecode
-
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelRegistered, channelUnregistered
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, isSharable
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty.channel.ChannelHandler
handlerRemoved
-
-
-
-
Field Detail
-
LOG
private static final InternalLogger LOG
-
streamKey
protected final Http2Connection.PropertyKey streamKey
-
upgradeKey
private final Http2Connection.PropertyKey upgradeKey
-
initialFlowControlWindowSize
private final java.lang.Integer initialFlowControlWindowSize
-
ctx
ChannelHandlerContext ctx
-
numBufferedStreams
private int numBufferedStreams
Number of buffered streams if theStreamBufferingEncoderis used.
-
frameStreamToInitializeMap
private final IntObjectMap<Http2FrameCodec.DefaultHttp2FrameStream> frameStreamToInitializeMap
-
-
Constructor Detail
-
Http2FrameCodec
Http2FrameCodec(Http2ConnectionEncoder encoder, Http2ConnectionDecoder decoder, Http2Settings initialSettings, boolean decoupleCloseAndGoAway)
-
-
Method Detail
-
newStream
Http2FrameCodec.DefaultHttp2FrameStream newStream()
Creates a new outbound/local stream.
-
forEachActiveStream
final void forEachActiveStream(Http2FrameStreamVisitor streamVisitor) throws Http2Exception
Iterates over all active HTTP/2 streams.This method must not be called outside of the event loop.
- Throws:
Http2Exception
-
numInitializingStreams
int numInitializingStreams()
Retrieve the number of streams currently in the process of being initialized. This is package-private for testing only.
-
handlerAdded
public final void handlerAdded(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerAddedin interfaceChannelHandler- Overrides:
handlerAddedin classHttp2ConnectionHandler- Throws:
java.lang.Exception
-
tryExpandConnectionFlowControlWindow
private void tryExpandConnectionFlowControlWindow(Http2Connection connection) throws Http2Exception
- Throws:
Http2Exception
-
handlerAdded0
void handlerAdded0(ChannelHandlerContext ctx) throws java.lang.Exception
- Throws:
java.lang.Exception
-
userEventTriggered
public final void userEventTriggered(ChannelHandlerContext ctx, java.lang.Object evt) throws java.lang.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).- Specified by:
userEventTriggeredin interfaceChannelInboundHandler- Overrides:
userEventTriggeredin classByteToMessageDecoder- Throws:
java.lang.Exception
-
write
public void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise)
Processes allHttp2Frames.Http2StreamFrames may only originate in child streams.- Specified by:
writein interfaceChannelOutboundHandler- Overrides:
writein classHttp2ConnectionHandler- Parameters:
ctx- theChannelHandlerContextfor which the write operation is mademsg- the message to writepromise- theChannelPromiseto notify once the operation completes
-
increaseInitialConnectionWindow
private void increaseInitialConnectionWindow(int deltaBytes) throws Http2Exception- Throws:
Http2Exception
-
consumeBytes
final boolean consumeBytes(int streamId, int bytes) throws Http2Exception- Throws:
Http2Exception
-
writeGoAwayFrame
private void writeGoAwayFrame(ChannelHandlerContext ctx, Http2GoAwayFrame frame, ChannelPromise promise)
-
writeHeadersFrame
private void writeHeadersFrame(ChannelHandlerContext ctx, Http2HeadersFrame headersFrame, ChannelPromise promise)
-
handleHeaderFuture
private void handleHeaderFuture(ChannelFuture channelFuture, int streamId)
-
onStreamActive0
private void onStreamActive0(Http2Stream stream)
-
onConnectionError
protected void onConnectionError(ChannelHandlerContext ctx, boolean outbound, java.lang.Throwable cause, Http2Exception http2Ex)
Description copied from class:Http2ConnectionHandlerHandler for a connection error. Sends a GO_AWAY frame to the remote endpoint. Once all streams are closed, the connection is shut down.- Overrides:
onConnectionErrorin classHttp2ConnectionHandler- Parameters:
ctx- the channel contextoutbound-trueif the error was caused by an outbound operation.cause- the exception that was caughthttp2Ex- theHttp2Exceptionthat is embedded in the causality chain. This may benullif it's an unknown exception.
-
onStreamError
protected final void onStreamError(ChannelHandlerContext ctx, boolean outbound, java.lang.Throwable cause, Http2Exception.StreamException streamException)
Exceptions for unknown streams, that is streams that have noHttp2FrameStreamobject attached are simply logged and replied to by sending a RST_STREAM frame.- Overrides:
onStreamErrorin classHttp2ConnectionHandler- Parameters:
ctx- the channel contextoutbound-trueif the error was caused by an outbound operation.cause- the exception that was caughtstreamException- theHttp2Exception.StreamExceptionthat is embedded in the causality chain.
-
onHttp2UnknownStreamError
private void onHttp2UnknownStreamError(ChannelHandlerContext ctx, java.lang.Throwable cause, Http2Exception.StreamException streamException)
-
isGracefulShutdownComplete
protected final boolean isGracefulShutdownComplete()
Description copied from class:Http2ConnectionHandlerCalled by the graceful shutdown logic to determine when it is safe to close the connection. Returnstrueif 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:
isGracefulShutdownCompletein classHttp2ConnectionHandler
-
onUpgradeEvent
private void onUpgradeEvent(ChannelHandlerContext ctx, HttpServerUpgradeHandler.UpgradeEvent evt)
-
onHttp2StreamWritabilityChanged
private void onHttp2StreamWritabilityChanged(ChannelHandlerContext ctx, Http2FrameCodec.DefaultHttp2FrameStream stream, boolean writable)
-
onHttp2StreamStateChanged
void onHttp2StreamStateChanged(ChannelHandlerContext ctx, Http2FrameCodec.DefaultHttp2FrameStream stream)
-
onHttp2Frame
void onHttp2Frame(ChannelHandlerContext ctx, Http2Frame frame)
-
onHttp2FrameStreamException
void onHttp2FrameStreamException(ChannelHandlerContext ctx, Http2FrameStreamException cause)
-
-