Package io.netty.handler.codec.http2
Class DefaultHttp2Connection
- java.lang.Object
-
- io.netty.handler.codec.http2.DefaultHttp2Connection
-
- All Implemented Interfaces:
Http2Connection
@UnstableApi public class DefaultHttp2Connection extends java.lang.Object implements Http2Connection
Simple implementation ofHttp2Connection
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
DefaultHttp2Connection.ActiveStreams
Manages the list of currently active streams.private class
DefaultHttp2Connection.ConnectionStream
Stream class representing the connection, itself.private class
DefaultHttp2Connection.DefaultEndpoint<F extends Http2FlowController>
Simple endpoint implementation.(package private) class
DefaultHttp2Connection.DefaultPropertyKey
Implementation ofHttp2Connection.PropertyKey
that specifies the index position of the property.private class
DefaultHttp2Connection.DefaultStream
Simple stream implementation.(package private) static interface
DefaultHttp2Connection.Event
Allows events which would modify the collection of active streams to be queued while iterating viaforEachActiveStream(Http2StreamVisitor)
.private class
DefaultHttp2Connection.PropertyKeyRegistry
A registry of all stream property keys known by this connection.-
Nested classes/interfaces inherited from interface io.netty.handler.codec.http2.Http2Connection
Http2Connection.Endpoint<F extends Http2FlowController>, Http2Connection.Listener, Http2Connection.PropertyKey
-
-
Field Summary
Fields Modifier and Type Field Description (package private) DefaultHttp2Connection.ActiveStreams
activeStreams
(package private) Promise<java.lang.Void>
closePromise
(package private) DefaultHttp2Connection.ConnectionStream
connectionStream
(package private) java.util.List<Http2Connection.Listener>
listeners
We chose aList
over aSet
to avoid allocating anIterator
objects when iterating over the listeners.(package private) DefaultHttp2Connection.DefaultEndpoint<Http2LocalFlowController>
localEndpoint
private static InternalLogger
logger
(package private) DefaultHttp2Connection.PropertyKeyRegistry
propertyKeyRegistry
(package private) DefaultHttp2Connection.DefaultEndpoint<Http2RemoteFlowController>
remoteEndpoint
(package private) IntObjectMap<Http2Stream>
streamMap
-
Constructor Summary
Constructors Constructor Description DefaultHttp2Connection(boolean server)
Creates a new connection with the given settings.DefaultHttp2Connection(boolean server, int maxReservedStreams)
Creates a new connection with the given settings.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) static Http2Stream.State
activeState(int streamId, Http2Stream.State initialState, boolean isLocal, boolean halfClosed)
void
addListener(Http2Connection.Listener listener)
Adds a listener of stream life-cycle events.Future<java.lang.Void>
close(Promise<java.lang.Void> promise)
Close this connection.private void
closeStreamsGreaterThanLastKnownStreamId(int lastKnownStream, DefaultHttp2Connection.DefaultEndpoint<?> endpoint)
Http2Stream
connectionStream()
Gets the stream object representing the connection, itself (i.e.Http2Stream
forEachActiveStream(Http2StreamVisitor visitor)
Provide a means of iterating over the collection of active streams.boolean
goAwayReceived()
Indicates whether or not aGOAWAY
was received from the remote endpoint.void
goAwayReceived(int lastKnownStream, long errorCode, ByteBuf debugData)
Indicates that aGOAWAY
was received from the remote endpoint and sets the last known stream.boolean
goAwaySent()
Indicates whether or not aGOAWAY
was sent to the remote endpoint.boolean
goAwaySent(int lastKnownStream, long errorCode, ByteBuf debugData)
Updates the local state of thisHttp2Connection
as a result of aGOAWAY
to send to the remote endpoint.(package private) boolean
isClosed()
Determine ifclose(Promise)
has been called and no more streams are allowed to be created.boolean
isServer()
Indicates whether or not the local endpoint for this connection is the server.private boolean
isStreamMapEmpty()
Determine ifstreamMap
only contains the connection stream.Http2Connection.Endpoint<Http2LocalFlowController>
local()
Gets a view of this connection from the localHttp2Connection.Endpoint
.Http2Connection.PropertyKey
newKey()
Creates a new key that is unique within thisHttp2Connection
.(package private) void
notifyClosed(Http2Stream stream)
(package private) void
notifyHalfClosed(Http2Stream stream)
int
numActiveStreams()
Gets the number of streams that are actively in use (i.e.Http2Connection.Endpoint<Http2RemoteFlowController>
remote()
Gets a view of this connection from the remoteHttp2Connection.Endpoint
.void
removeListener(Http2Connection.Listener listener)
Removes a listener of stream life-cycle events.(package private) void
removeStream(DefaultHttp2Connection.DefaultStream stream, java.util.Iterator<?> itr)
Remove a stream from thestreamMap
.Http2Stream
stream(int streamId)
Gets the stream if it exists.boolean
streamMayHaveExisted(int streamId)
Indicates whether or not the given stream may have existed within this connection.(package private) DefaultHttp2Connection.DefaultPropertyKey
verifyKey(Http2Connection.PropertyKey key)
Verifies that the key is valid and returns it as the internalDefaultHttp2Connection.DefaultPropertyKey
type.
-
-
-
Field Detail
-
logger
private static final InternalLogger logger
-
streamMap
final IntObjectMap<Http2Stream> streamMap
-
propertyKeyRegistry
final DefaultHttp2Connection.PropertyKeyRegistry propertyKeyRegistry
-
connectionStream
final DefaultHttp2Connection.ConnectionStream connectionStream
-
localEndpoint
final DefaultHttp2Connection.DefaultEndpoint<Http2LocalFlowController> localEndpoint
-
remoteEndpoint
final DefaultHttp2Connection.DefaultEndpoint<Http2RemoteFlowController> remoteEndpoint
-
listeners
final java.util.List<Http2Connection.Listener> listeners
We chose aList
over aSet
to avoid allocating anIterator
objects when iterating over the listeners.Initial size of 4 because the default configuration currently has 3 listeners (local/remote flow controller and
StreamByteDistributor
) and we leave room for 1 extra. We could be more aggressive but the ArrayList resize will double the size if we are too small.
-
activeStreams
final DefaultHttp2Connection.ActiveStreams activeStreams
-
closePromise
Promise<java.lang.Void> closePromise
-
-
Constructor Detail
-
DefaultHttp2Connection
public DefaultHttp2Connection(boolean server)
Creates a new connection with the given settings.- Parameters:
server
- whether or not this end-point is the server-side of the HTTP/2 connection.
-
DefaultHttp2Connection
public DefaultHttp2Connection(boolean server, int maxReservedStreams)
Creates a new connection with the given settings.- Parameters:
server
- whether or not this end-point is the server-side of the HTTP/2 connection.maxReservedStreams
- The maximum amount of streams which can exist in the reserved state for each endpoint.
-
-
Method Detail
-
isClosed
final boolean isClosed()
Determine ifclose(Promise)
has been called and no more streams are allowed to be created.
-
close
public Future<java.lang.Void> close(Promise<java.lang.Void> promise)
Description copied from interface:Http2Connection
Close this connection. No more new streams can be created after this point and all streams that exists (active or otherwise) will be closed and removed.Note if iterating active streams via
Http2Connection.forEachActiveStream(Http2StreamVisitor)
and an exception is thrown it is necessary to call this method again to ensure the close completes.- Specified by:
close
in interfaceHttp2Connection
- Parameters:
promise
- Will be completed when all streams have been removed, and listeners have been notified.- Returns:
- A future that will be completed when all streams have been removed, and listeners have been notified.
-
addListener
public void addListener(Http2Connection.Listener listener)
Description copied from interface:Http2Connection
Adds a listener of stream life-cycle events.- Specified by:
addListener
in interfaceHttp2Connection
-
removeListener
public void removeListener(Http2Connection.Listener listener)
Description copied from interface:Http2Connection
Removes a listener of stream life-cycle events. If the same listener was added multiple times then only the first occurrence gets removed.- Specified by:
removeListener
in interfaceHttp2Connection
-
isServer
public boolean isServer()
Description copied from interface:Http2Connection
Indicates whether or not the local endpoint for this connection is the server.- Specified by:
isServer
in interfaceHttp2Connection
-
connectionStream
public Http2Stream connectionStream()
Description copied from interface:Http2Connection
Gets the stream object representing the connection, itself (i.e. stream zero). This object always exists.- Specified by:
connectionStream
in interfaceHttp2Connection
-
stream
public Http2Stream stream(int streamId)
Description copied from interface:Http2Connection
Gets the stream if it exists. If not, returnsnull
.- Specified by:
stream
in interfaceHttp2Connection
-
streamMayHaveExisted
public boolean streamMayHaveExisted(int streamId)
Description copied from interface:Http2Connection
Indicates whether or not the given stream may have existed within this connection. This is a short form for callingHttp2Connection.Endpoint.mayHaveCreatedStream(int)
on both endpoints.- Specified by:
streamMayHaveExisted
in interfaceHttp2Connection
-
numActiveStreams
public int numActiveStreams()
Description copied from interface:Http2Connection
Gets the number of streams that are actively in use (i.e.OPEN
orHALF CLOSED
).- Specified by:
numActiveStreams
in interfaceHttp2Connection
-
forEachActiveStream
public Http2Stream forEachActiveStream(Http2StreamVisitor visitor) throws Http2Exception
Description copied from interface:Http2Connection
Provide a means of iterating over the collection of active streams.- Specified by:
forEachActiveStream
in interfaceHttp2Connection
- Parameters:
visitor
- The visitor which will visit each active stream.- Returns:
- The stream before iteration stopped or
null
if iteration went past the end. - Throws:
Http2Exception
-
local
public Http2Connection.Endpoint<Http2LocalFlowController> local()
Description copied from interface:Http2Connection
Gets a view of this connection from the localHttp2Connection.Endpoint
.- Specified by:
local
in interfaceHttp2Connection
-
remote
public Http2Connection.Endpoint<Http2RemoteFlowController> remote()
Description copied from interface:Http2Connection
Gets a view of this connection from the remoteHttp2Connection.Endpoint
.- Specified by:
remote
in interfaceHttp2Connection
-
goAwayReceived
public boolean goAwayReceived()
Description copied from interface:Http2Connection
Indicates whether or not aGOAWAY
was received from the remote endpoint.- Specified by:
goAwayReceived
in interfaceHttp2Connection
-
goAwayReceived
public void goAwayReceived(int lastKnownStream, long errorCode, ByteBuf debugData) throws Http2Exception
Description copied from interface:Http2Connection
Indicates that aGOAWAY
was received from the remote endpoint and sets the last known stream.- Specified by:
goAwayReceived
in interfaceHttp2Connection
- Parameters:
lastKnownStream
- The Last-Stream-ID in the GOAWAY frame.errorCode
- the Error Code in the GOAWAY frame.debugData
- The Additional Debug Data in the GOAWAY frame. Note that reference count ownership belongs to the caller (ownership is not transferred to this method).- Throws:
Http2Exception
-
goAwaySent
public boolean goAwaySent()
Description copied from interface:Http2Connection
Indicates whether or not aGOAWAY
was sent to the remote endpoint.- Specified by:
goAwaySent
in interfaceHttp2Connection
-
goAwaySent
public boolean goAwaySent(int lastKnownStream, long errorCode, ByteBuf debugData) throws Http2Exception
Description copied from interface:Http2Connection
Updates the local state of thisHttp2Connection
as a result of aGOAWAY
to send to the remote endpoint.- Specified by:
goAwaySent
in interfaceHttp2Connection
- Parameters:
lastKnownStream
- The Last-Stream-ID in the GOAWAY frame.errorCode
- the Error Code in the GOAWAY frame. GOAWAY frame. Note that reference count ownership belongs to the caller (ownership is not transferred to this method).- Returns:
true
if the correspondingGOAWAY
frame should be sent to the remote endpoint.- Throws:
Http2Exception
-
closeStreamsGreaterThanLastKnownStreamId
private void closeStreamsGreaterThanLastKnownStreamId(int lastKnownStream, DefaultHttp2Connection.DefaultEndpoint<?> endpoint) throws Http2Exception
- Throws:
Http2Exception
-
isStreamMapEmpty
private boolean isStreamMapEmpty()
Determine ifstreamMap
only contains the connection stream.
-
removeStream
void removeStream(DefaultHttp2Connection.DefaultStream stream, java.util.Iterator<?> itr)
Remove a stream from thestreamMap
.- Parameters:
stream
- the stream to remove.itr
- an iterator that may be pointing to the stream during iteration andIterator.remove()
will be used if non-null
.
-
activeState
static Http2Stream.State activeState(int streamId, Http2Stream.State initialState, boolean isLocal, boolean halfClosed) throws Http2Exception
- Throws:
Http2Exception
-
notifyHalfClosed
void notifyHalfClosed(Http2Stream stream)
-
notifyClosed
void notifyClosed(Http2Stream stream)
-
newKey
public Http2Connection.PropertyKey newKey()
Description copied from interface:Http2Connection
Creates a new key that is unique within thisHttp2Connection
.- Specified by:
newKey
in interfaceHttp2Connection
-
verifyKey
final DefaultHttp2Connection.DefaultPropertyKey verifyKey(Http2Connection.PropertyKey key)
Verifies that the key is valid and returns it as the internalDefaultHttp2Connection.DefaultPropertyKey
type.- Throws:
java.lang.NullPointerException
- if the key isnull
.java.lang.ClassCastException
- if the key is not of typeDefaultHttp2Connection.DefaultPropertyKey
.java.lang.IllegalArgumentException
- if the key was not created by this connection.
-
-