Package io.netty.handler.codec.http2
Class DefaultHttp2ConnectionEncoder
java.lang.Object
io.netty.handler.codec.http2.DefaultHttp2ConnectionEncoder
- All Implemented Interfaces:
Http2ConnectionEncoder
,Http2DataWriter
,Http2FrameWriter
,Http2SettingsReceivedConsumer
,Closeable
,AutoCloseable
@UnstableApi
public class DefaultHttp2ConnectionEncoder
extends Object
implements Http2ConnectionEncoder, Http2SettingsReceivedConsumer
Default implementation of
Http2ConnectionEncoder
.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclass
Common base type for payloads to deliver via flow-control.private final class
Wrap a DATA frame so it can be written subject to flow-control.private final class
Wrap headers so they can be written subject to flow-control.Nested classes/interfaces inherited from interface io.netty.handler.codec.http2.Http2FrameWriter
Http2FrameWriter.Configuration
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Http2Connection
private final Http2FrameWriter
private Http2LifecycleManager
private final Queue
<Http2Settings> private Queue
<Http2Settings> -
Constructor Summary
ConstructorsConstructorDescriptionDefaultHttp2ConnectionEncoder
(Http2Connection connection, Http2FrameWriter frameWriter) -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes this writer and frees any allocated resources.Get the configuration related elements for thisHttp2FrameWriter
Provides direct access to the underlying connection.void
consumeReceivedSettings
(Http2Settings settings) Consume the most recently received but not yet ACKed settings.Provides the remote flow controller for managing outbound traffic.Provides direct access to the underlying frame writer object.void
lifecycleManager
(Http2LifecycleManager lifecycleManager) Sets the lifecycle manager.private void
Gets the local settings on the top of the queue that has been sent but not ACKed.void
remoteSettings
(Http2Settings settings) Sets the settings for the remote endpoint of the HTTP/2 connection.private Http2Stream
requireStream
(int streamId) private static ChannelFuture
sendHeaders
(Http2FrameWriter frameWriter, ChannelHandlerContext ctx, int streamId, Http2Headers headers, boolean hasPriority, int streamDependency, short weight, boolean exclusive, int padding, boolean endOfStream, ChannelPromise promise) Write headers viaHttp2FrameWriter
.private static boolean
validateHeadersSentState
(Http2Stream stream, Http2Headers headers, boolean isServer, boolean endOfStream) writeData
(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream, ChannelPromise promise) Writes aDATA
frame to the remote endpoint.writeFrame
(ChannelHandlerContext ctx, byte frameType, int streamId, Http2Flags flags, ByteBuf payload, ChannelPromise promise) Writes the given data to the internalHttp2FrameWriter
without performing any state checks on the connection/stream.writeGoAway
(ChannelHandlerContext ctx, int lastStreamId, long errorCode, ByteBuf debugData, ChannelPromise promise) Writes a GO_AWAY frame to the remote endpoint.writeHeaders
(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding, boolean endStream, ChannelPromise promise) Writes a HEADERS frame to the remote endpoint.writeHeaders
(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endOfStream, ChannelPromise promise) Writes a HEADERS frame with priority specified to the remote endpoint.private ChannelFuture
writeHeaders0
(ChannelHandlerContext ctx, int streamId, Http2Headers headers, boolean hasPriority, int streamDependency, short weight, boolean exclusive, int padding, boolean endOfStream, ChannelPromise promise) writePing
(ChannelHandlerContext ctx, boolean ack, long data, ChannelPromise promise) Writes a PING frame to the remote endpoint.writePriority
(ChannelHandlerContext ctx, int streamId, int streamDependency, short weight, boolean exclusive, ChannelPromise promise) Writes a PRIORITY frame to the remote endpoint.writePushPromise
(ChannelHandlerContext ctx, int streamId, int promisedStreamId, Http2Headers headers, int padding, ChannelPromise promise) Writes a PUSH_PROMISE frame to the remote endpoint.writeRstStream
(ChannelHandlerContext ctx, int streamId, long errorCode, ChannelPromise promise) Writes a RST_STREAM frame to the remote endpoint.writeSettings
(ChannelHandlerContext ctx, Http2Settings settings, ChannelPromise promise) Writes a SETTINGS frame to the remote endpoint.writeSettingsAck
(ChannelHandlerContext ctx, ChannelPromise promise) Writes a SETTINGS acknowledgment to the remote endpoint.writeWindowUpdate
(ChannelHandlerContext ctx, int streamId, int windowSizeIncrement, ChannelPromise promise) Writes a WINDOW_UPDATE frame to the remote endpoint.
-
Field Details
-
frameWriter
-
connection
-
lifecycleManager
-
outstandingLocalSettingsQueue
-
outstandingRemoteSettingsQueue
-
-
Constructor Details
-
DefaultHttp2ConnectionEncoder
-
-
Method Details
-
lifecycleManager
Description copied from interface:Http2ConnectionEncoder
Sets the lifecycle manager. Must be called as part of initialization before the encoder is used.- Specified by:
lifecycleManager
in interfaceHttp2ConnectionEncoder
-
frameWriter
Description copied from interface:Http2ConnectionEncoder
Provides direct access to the underlying frame writer object.- Specified by:
frameWriter
in interfaceHttp2ConnectionEncoder
-
connection
Description copied from interface:Http2ConnectionEncoder
Provides direct access to the underlying connection.- Specified by:
connection
in interfaceHttp2ConnectionEncoder
-
flowController
Description copied from interface:Http2ConnectionEncoder
Provides the remote flow controller for managing outbound traffic.- Specified by:
flowController
in interfaceHttp2ConnectionEncoder
-
remoteSettings
Description copied from interface:Http2ConnectionEncoder
Sets the settings for the remote endpoint of the HTTP/2 connection.- Specified by:
remoteSettings
in interfaceHttp2ConnectionEncoder
- Throws:
Http2Exception
-
writeData
public ChannelFuture writeData(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream, ChannelPromise promise) Description copied from interface:Http2DataWriter
Writes aDATA
frame to the remote endpoint. This will result in one or more frames being written to the context.- Specified by:
writeData
in interfaceHttp2DataWriter
- Parameters:
ctx
- the context to use for writing.streamId
- the stream for which to send the frame.data
- the payload of the frame. This will be released by this method.padding
- additional bytes that should be added to obscure the true content size. Must be between 0 and 256 (inclusive). A 1 byte padding is encoded as just the pad length field with value 0. A 256 byte padding is encoded as the pad length field with value 255 and 255 padding bytes appended to the end of the frame.endOfStream
- indicates if this is the last frame to be sent for the stream.promise
- the promise for the write.- Returns:
- the future for the write.
-
writeHeaders
public ChannelFuture writeHeaders(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding, boolean endStream, ChannelPromise promise) Description copied from interface:Http2FrameWriter
Writes a HEADERS frame to the remote endpoint.- Specified by:
writeHeaders
in interfaceHttp2FrameWriter
- Parameters:
ctx
- the context to use for writing.streamId
- the stream for which to send the frame.headers
- the headers to be sent.padding
- additional bytes that should be added to obscure the true content size. Must be between 0 and 256 (inclusive).endStream
- indicates if this is the last frame to be sent for the stream.promise
- the promise for the write.- Returns:
- the future for the write.
Section 10.5.1 states the following:
The header block MUST be processed to ensure a consistent connection state, unless the connection is closed.
If this call has modified the HPACK header state you MUST throw a connection error.If this call has NOT modified the HPACK header state you are free to throw a stream error.
-
validateHeadersSentState
private static boolean validateHeadersSentState(Http2Stream stream, Http2Headers headers, boolean isServer, boolean endOfStream) -
writeHeaders
public ChannelFuture writeHeaders(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endOfStream, ChannelPromise promise) Description copied from interface:Http2FrameWriter
Writes a HEADERS frame with priority specified to the remote endpoint.- Specified by:
writeHeaders
in interfaceHttp2FrameWriter
- Parameters:
ctx
- the context to use for writing.streamId
- the stream for which to send the frame.headers
- the headers to be sent.streamDependency
- the stream on which this stream should depend, or 0 if it should depend on the connection.weight
- the weight for this stream.exclusive
- whether this stream should be the exclusive dependant of its parent.padding
- additional bytes that should be added to obscure the true content size. Must be between 0 and 256 (inclusive).endOfStream
- indicates if this is the last frame to be sent for the stream.promise
- the promise for the write.- Returns:
- the future for the write.
Section 10.5.1 states the following:
The header block MUST be processed to ensure a consistent connection state, unless the connection is closed.
If this call has modified the HPACK header state you MUST throw a connection error.If this call has NOT modified the HPACK header state you are free to throw a stream error.
-
sendHeaders
private static ChannelFuture sendHeaders(Http2FrameWriter frameWriter, ChannelHandlerContext ctx, int streamId, Http2Headers headers, boolean hasPriority, int streamDependency, short weight, boolean exclusive, int padding, boolean endOfStream, ChannelPromise promise) Write headers viaHttp2FrameWriter
. IfhasPriority
isfalse
it will ignore thestreamDependency
,weight
andexclusive
parameters. -
writeHeaders0
private ChannelFuture writeHeaders0(ChannelHandlerContext ctx, int streamId, Http2Headers headers, boolean hasPriority, int streamDependency, short weight, boolean exclusive, int padding, boolean endOfStream, ChannelPromise promise) -
writePriority
public ChannelFuture writePriority(ChannelHandlerContext ctx, int streamId, int streamDependency, short weight, boolean exclusive, ChannelPromise promise) Description copied from interface:Http2FrameWriter
Writes a PRIORITY frame to the remote endpoint.- Specified by:
writePriority
in interfaceHttp2FrameWriter
- Parameters:
ctx
- the context to use for writing.streamId
- the stream for which to send the frame.streamDependency
- the stream on which this stream should depend, or 0 if it should depend on the connection.weight
- the weight for this stream.exclusive
- whether this stream should be the exclusive dependant of its parent.promise
- the promise for the write.- Returns:
- the future for the write.
-
writeRstStream
public ChannelFuture writeRstStream(ChannelHandlerContext ctx, int streamId, long errorCode, ChannelPromise promise) Description copied from interface:Http2FrameWriter
Writes a RST_STREAM frame to the remote endpoint.- Specified by:
writeRstStream
in interfaceHttp2FrameWriter
- Parameters:
ctx
- the context to use for writing.streamId
- the stream for which to send the frame.errorCode
- the error code indicating the nature of the failure.promise
- the promise for the write.- Returns:
- the future for the write.
-
writeSettings
public ChannelFuture writeSettings(ChannelHandlerContext ctx, Http2Settings settings, ChannelPromise promise) Description copied from interface:Http2FrameWriter
Writes a SETTINGS frame to the remote endpoint.- Specified by:
writeSettings
in interfaceHttp2FrameWriter
- Parameters:
ctx
- the context to use for writing.settings
- the settings to be sent.promise
- the promise for the write.- Returns:
- the future for the write.
-
writeSettingsAck
Description copied from interface:Http2FrameWriter
Writes a SETTINGS acknowledgment to the remote endpoint.- Specified by:
writeSettingsAck
in interfaceHttp2FrameWriter
- Parameters:
ctx
- the context to use for writing.promise
- the promise for the write.- Returns:
- the future for the write.
-
writePing
public ChannelFuture writePing(ChannelHandlerContext ctx, boolean ack, long data, ChannelPromise promise) Description copied from interface:Http2FrameWriter
Writes a PING frame to the remote endpoint.- Specified by:
writePing
in interfaceHttp2FrameWriter
- Parameters:
ctx
- the context to use for writing.ack
- indicates whether this is an ack of a PING frame previously received from the remote endpoint.data
- the payload of the frame.promise
- the promise for the write.- Returns:
- the future for the write.
-
writePushPromise
public ChannelFuture writePushPromise(ChannelHandlerContext ctx, int streamId, int promisedStreamId, Http2Headers headers, int padding, ChannelPromise promise) Description copied from interface:Http2FrameWriter
Writes a PUSH_PROMISE frame to the remote endpoint.- Specified by:
writePushPromise
in interfaceHttp2FrameWriter
- Parameters:
ctx
- the context to use for writing.streamId
- the stream for which to send the frame.promisedStreamId
- the ID of the promised stream.headers
- the headers to be sent.padding
- additional bytes that should be added to obscure the true content size. Must be between 0 and 256 (inclusive).promise
- the promise for the write.- Returns:
- the future for the write.
Section 10.5.1 states the following:
The header block MUST be processed to ensure a consistent connection state, unless the connection is closed.
If this call has modified the HPACK header state you MUST throw a connection error.If this call has NOT modified the HPACK header state you are free to throw a stream error.
-
writeGoAway
public ChannelFuture writeGoAway(ChannelHandlerContext ctx, int lastStreamId, long errorCode, ByteBuf debugData, ChannelPromise promise) Description copied from interface:Http2FrameWriter
Writes a GO_AWAY frame to the remote endpoint.- Specified by:
writeGoAway
in interfaceHttp2FrameWriter
- Parameters:
ctx
- the context to use for writing.lastStreamId
- the last known stream of this endpoint.errorCode
- the error code, if the connection was abnormally terminated.debugData
- application-defined debug data. This will be released by this method.promise
- the promise for the write.- Returns:
- the future for the write.
-
writeWindowUpdate
public ChannelFuture writeWindowUpdate(ChannelHandlerContext ctx, int streamId, int windowSizeIncrement, ChannelPromise promise) Description copied from interface:Http2FrameWriter
Writes a WINDOW_UPDATE frame to the remote endpoint.- Specified by:
writeWindowUpdate
in interfaceHttp2FrameWriter
- Parameters:
ctx
- the context to use for writing.streamId
- the stream for which to send the frame.windowSizeIncrement
- the number of bytes by which the local inbound flow control window is increasing.promise
- the promise for the write.- Returns:
- the future for the write.
-
writeFrame
public ChannelFuture writeFrame(ChannelHandlerContext ctx, byte frameType, int streamId, Http2Flags flags, ByteBuf payload, ChannelPromise promise) Description copied from interface:Http2ConnectionEncoder
Writes the given data to the internalHttp2FrameWriter
without performing any state checks on the connection/stream.- Specified by:
writeFrame
in interfaceHttp2ConnectionEncoder
- Specified by:
writeFrame
in interfaceHttp2FrameWriter
- Parameters:
ctx
- the context to use for writing.frameType
- the frame type identifier.streamId
- the stream for which to send the frame.flags
- the flags to write for this frame.payload
- the payload to write for this frame. This will be released by this method.promise
- the promise for the write.- Returns:
- the future for the write.
-
close
public void close()Description copied from interface:Http2FrameWriter
Closes this writer and frees any allocated resources.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceHttp2FrameWriter
-
pollSentSettings
Description copied from interface:Http2ConnectionEncoder
Gets the local settings on the top of the queue that has been sent but not ACKed. This may returnnull
.- Specified by:
pollSentSettings
in interfaceHttp2ConnectionEncoder
-
configuration
Description copied from interface:Http2FrameWriter
Get the configuration related elements for thisHttp2FrameWriter
- Specified by:
configuration
in interfaceHttp2FrameWriter
-
requireStream
-
consumeReceivedSettings
Description copied from interface:Http2SettingsReceivedConsumer
Consume the most recently received but not yet ACKed settings.- Specified by:
consumeReceivedSettings
in interfaceHttp2SettingsReceivedConsumer
-
notifyLifecycleManagerOnError
-