Class HTTP2Endpoint

java.lang.Object
org.omegazero.http.h2.HTTP2Endpoint
Direct Known Subclasses:
HTTP2Client

public abstract class HTTP2Endpoint extends Object
Represents any HTTP/2 endpoint (client or server).

A HTTP2Endpoint is initialized with a WritableSocket representing the underlying connection where frames are exchanged. Incoming data on this connection must be passed to processData(byte[]).

Since:
1.2.1
  • Field Details

    • connection

      protected final WritableSocket connection
      The underlying connection.
    • settings

      protected final HTTP2Settings settings
      The local settings.
    • hpack

      protected final HPackContext hpack
      The HPackContext used for this HTTP2Endpoint.
    • streams

      @Deprecated protected final Map<Integer,HTTP2Stream> streams
      Deprecated.
      Since 1.2.4, direct access is discouraged; use registerStream(HTTP2Stream) and getStream(int) instead
      Contains active and recently closed streams of this HTTP2Endpoint.
    • closeWaitStreams

      @Deprecated protected final Deque<MessageStream> closeWaitStreams
      Deprecated.
      Since 1.2.4, direct access is discouraged; use streamClosed(MessageStream) instead
      Contains closed streams which will soon be removed from streams.
    • highestStreamId

      protected int highestStreamId
      The stream ID of the latest stream that was processed.
    • peerInitiatedStreams

      protected final Set<MessageStream> peerInitiatedStreams
      The set of MessageStreams initiated by the peer. May be null if the peer has not initiated any streams yet.
      Since:
      1.4.1
    • closeWaitTimeout

      protected long closeWaitTimeout
      The time in nanoseconds after which closed streams in closeWaitTimeout are removed from streams.
  • Constructor Details

    • HTTP2Endpoint

      public HTTP2Endpoint(WritableSocket connection, HTTP2Settings settings, HPackContext.Session hpackSession, boolean useHuffmanEncoding)
      Creates a new HTTP2Endpoint instance.
      Parameters:
      connection - The underlying connection
      settings - The local settings
      hpackSession - The HPACK session to use
      useHuffmanEncoding - Whether to enable HPACK Huffman Coding
    • HTTP2Endpoint

      public HTTP2Endpoint(WritableSocket connection, HTTP2Settings settings, HPackContext hpack)
      Creates a new HTTP2Endpoint instance.
      Parameters:
      connection - The underlying connection
      settings - The local settings
      hpack - The HPackContext to use
  • Method Details

    • newStreamForFrame

      protected abstract HTTP2Stream newStreamForFrame(int streamId, int type, int flags, byte[] payload) throws HTTP2ConnectionError
      Processes a frame received on a previously nonexistent stream and creates a new HTTP2Stream for it, if appropriate.

      The returned stream is registered automatically using registerStream(HTTP2Stream, boolean). If null is returned (no stream was created), and the frame type is not PRIORITY, it will be treated as a connection error of type PROTOCOL_ERROR.

      When creating a stream, the implementation should call checkRemoteCreateStream() first.

      Parameters:
      streamId - The stream ID of the new stream
      type - The frame type
      flags - The frame flags
      payload - The frame payload
      Returns:
      The new stream, or null if no stream is appropriate
      Throws:
      HTTP2ConnectionError - If the received frame is invalid or unexpected
    • processData

      public void processData(byte[] data)
      Data received on the underlying connection of this HTTP2Endpoint is passed to this method for further processing.
      Parameters:
      data - The received data
    • processData0

      protected final int processData0(byte[] data, int index)
    • registerStream

      protected void registerStream(HTTP2Stream stream)
      Registers a locally created stream.

      Calls registerStream(stream, false).

      Parameters:
      stream - The stream
      See Also:
    • registerStream

      protected void registerStream(HTTP2Stream stream, boolean initiatedByPeer)
      Registers a new stream.

      If applicable, streamClosed(MessageStream) must be called with this stream when it closes.

      Parameters:
      stream - The stream
      initiatedByPeer - true if the given stream was initiated by the peer. Ignored for any stream type other than MessageStream
      See Also:
    • streamClosed

      protected void streamClosed(MessageStream stream)
      Notifies this HTTP2Endpoint that the given stream has closed. This method must be called for all message streams added using registerStream(HTTP2Stream) or returned by newStreamForFrame(int, int, int, byte[]) when the stream closes.
      Parameters:
      stream - The stream
      Throws:
      IllegalStateException - If the stream is not closed
      Since:
      1.2.4
    • getControlStream

      protected ControlStream getControlStream()
      Returns the connection control stream (stream with ID 0).

      Equivalent to:

      
                      (ControlStream) getStream(0);
       
      Returns:
      The control stream
    • getStream

      protected HTTP2Stream getStream(int streamId)
      Returns the stream with the given ID.
      Parameters:
      streamId - The stream ID
      Returns:
      The stream, or null if no stream with the given ID exists
      Since:
      1.2.4
      See Also:
    • getStreams

      protected Collection<HTTP2Stream> getStreams()
      Returns a set of all known streams, including recently closed streams.
      Returns:
      A set of all streams
      Since:
      1.2.4
      See Also:
    • getOpenMessageStreamCount

      public int getOpenMessageStreamCount()
      Returns the number of open MessageStreams.
      Returns:
      The number of open message streams
      Since:
      1.2.4
    • getOpenPeerInitiatedMessageStreamCount

      public int getOpenPeerInitiatedMessageStreamCount()
      Returns the number of open MessageStreams initiated by the peer.
      Returns:
      The number of open message streams initiated by the peer
      Since:
      1.4.1
    • getOpenLocallyInitiatedMessageStreamCount

      public int getOpenLocallyInitiatedMessageStreamCount()
      Returns the number of open MessageStreams initiated locally.
      Returns:
      The number of open message streams initiated locally
      Since:
      1.4.1
    • checkRemoteCreateStream

      protected void checkRemoteCreateStream() throws HTTP2ConnectionError
      Checks whether the remote endpoint may cause a new stream to be created according to the local MAX_CONCURRENT_STREAMS setting. If not, a HTTP2ConnectionError stream error with status code REFUSED_STREAM is thrown.
      Throws:
      HTTP2ConnectionError - If no new stream may be created
    • checkLocalCreateStream

      protected boolean checkLocalCreateStream()
      Checks whether a new MessageStream can be initiated locally according to the remote MAX_CONCURRENT_STREAMS setting.
      Returns:
      true if a new local stream can be created
    • handleConnectionWindowUpdate

      protected void handleConnectionWindowUpdate()
      Calls MessageStream.windowUpdate() for each active MessageStream.

      This method should be called by the ControlStream onWindowUpdate callback, and if the underlying connection is writable again, after being unwritable previously.

    • getConnection

      public WritableSocket getConnection()
      Returns the underlying connection as a WritableSocket.
      Returns:
      The connection