@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, an Http2Frame object is created and propagated via ByteToMessageDecoder.channelRead(io.netty.channel.ChannelHandlerContext, java.lang.Object). Outbound Http2Frame
objects received via write(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 the Http2StreamFrame interface. The Http2FrameCodec is instantiated using the
Http2FrameCodecBuilder. It's recommended for channel handlers to inherit from the
Http2ChannelDuplexHandler, as it provides additional functionality like iterating over all active streams or
creating outbound streams.
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 the END_STREAM flag set. Each
Http2StreamFrame has a Http2FrameStream object attached that uniquely identifies a particular stream.
Http2StreamFrames read from the channel always a Http2FrameStream object set, while when writing a
Http2StreamFrame the application code needs to set a Http2FrameStream object using
Http2StreamFrame.stream(Http2FrameStream).
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 the
Http2Settings.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 to 0. By default the initial
connection-level flow control window is the same as initial stream-level flow control window.
The first frame of an HTTP/2 stream must be an Http2HeadersFrame, which will have an Http2FrameStream
object attached.
A outbound HTTP/2 stream can be created by first instantiating a new Http2FrameStream object via
Http2ChannelDuplexHandler.newStream(), and then writing a Http2HeadersFrame object with the stream
attached.
final Http2Stream2 stream = handler.newStream();
ctx.write(headersFrame.stream(stream)).addListener(new ChannelFutureListener() {ByteToMessageDecoder.CumulatorChannelHandler.Sharable| 限定符和类型 | 字段和说明 |
|---|---|
protected Http2Connection.PropertyKey |
streamKey |
COMPOSITE_CUMULATOR, MERGE_CUMULATOR| 限定符和类型 | 方法和说明 |
|---|---|
void |
handlerAdded(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.
|
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,
java.lang.Throwable cause,
Http2Exception http2Ex)
Handler for a connection error.
|
protected void |
onStreamError(ChannelHandlerContext ctx,
boolean outbound,
java.lang.Throwable cause,
Http2Exception.StreamException streamException)
Exceptions for unknown streams, that is streams that have no
Http2FrameStream object attached
are simply logged and replied to by sending a RST_STREAM frame. |
void |
userEventTriggered(ChannelHandlerContext ctx,
java.lang.Object evt)
Handles the cleartext HTTP upgrade event.
|
void |
write(ChannelHandlerContext ctx,
java.lang.Object msg,
ChannelPromise promise)
Processes all
Http2Frames. |
bind, channelActive, channelInactive, channelReadComplete, channelWritabilityChanged, close, closeStream, closeStreamLocal, closeStreamRemote, connect, connection, decode, decoder, deregister, disconnect, encoder, exceptionCaught, flush, frameWriter, goAway, gracefulShutdownTimeoutMillis, gracefulShutdownTimeoutMillis, handlerRemoved0, handleServerHeaderDecodeSizeError, onError, onHttpClientUpgrade, onHttpServerUpgrade, read, resetStreamactualReadableBytes, callDecode, channelRead, decodeLast, discardSomeReadBytes, handlerRemoved, internalBuffer, isSingleDecode, setCumulator, setDiscardAfterReads, setSingleDecodechannelRegistered, channelUnregisteredensureNotSharable, isSharableclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waithandlerRemovedprotected final Http2Connection.PropertyKey streamKey
public final void handlerAdded(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelHandlerAdapterhandlerAdded 在接口中 ChannelHandlerhandlerAdded 在类中 Http2ConnectionHandlerjava.lang.Exceptionpublic final void userEventTriggered(ChannelHandlerContext ctx, java.lang.Object evt) throws java.lang.Exception
userEventTriggered 在接口中 ChannelInboundHandleruserEventTriggered 在类中 ByteToMessageDecoderjava.lang.Exceptionpublic void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise)
Http2Frames. Http2StreamFrames may only originate in child
streams.write 在接口中 ChannelOutboundHandlerwrite 在类中 Http2ConnectionHandlerctx - the ChannelHandlerContext for which the write operation is mademsg - the message to writepromise - the ChannelPromise to notify once the operation completesprotected void onConnectionError(ChannelHandlerContext ctx, boolean outbound, java.lang.Throwable cause, Http2Exception http2Ex)
Http2ConnectionHandleronConnectionError 在类中 Http2ConnectionHandlerctx - the channel contextoutbound - true if the error was caused by an outbound operation.cause - the exception that was caughthttp2Ex - the Http2Exception that is embedded in the causality chain. This may
be null if it's an unknown exception.protected final void onStreamError(ChannelHandlerContext ctx, boolean outbound, java.lang.Throwable cause, Http2Exception.StreamException streamException)
Http2FrameStream object attached
are simply logged and replied to by sending a RST_STREAM frame.onStreamError 在类中 Http2ConnectionHandlerctx - the channel contextoutbound - true if the error was caused by an outbound operation.cause - the exception that was caughtstreamException - the Http2Exception.StreamException that is embedded in the causality chain.protected final boolean isGracefulShutdownComplete()
Http2ConnectionHandlertrue
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.