Class MessageStream

java.lang.Object
org.omegazero.http.h2.streams.HTTP2Stream
org.omegazero.http.h2.streams.MessageStream

public class MessageStream extends HTTP2Stream
Represents any HTTP2Stream where HTTP messages are exchanged (streams with IDs higher than 0).
Since:
1.2.1
  • Field Details

  • Constructor Details

    • MessageStream

      public MessageStream(int streamId, WritableSocket connection, ControlStream controlStream, HPackContext hpack)
      Creates a MessageStream instance.

      The local and remote settings are retrieved from the given control stream.

      Parameters:
      streamId - The stream ID
      connection - The underlying connection
      controlStream - The control stream of the connection
      hpack - The HPackContext used for the connection
    • MessageStream

      public MessageStream(int streamId, WritableSocket connection, ControlStream controlStream, HTTP2Settings localSettings, HTTP2Settings remoteSettings, HPackContext hpack)
      Creates a MessageStream instance.
      Parameters:
      streamId - The stream ID
      connection - The underlying connection
      controlStream - The control stream of the connection
      localSettings - The local HTTP2Settings
      remoteSettings - The remote HTTP2Settings
      hpack - The HPackContext used for the connection
  • Method Details

    • sendPushPromise

      public void sendPushPromise(int promisedStreamId, HTTPRequest request)
      Sends the given HTTPRequest on this stream by encoding the data with the configured HPackContext and sending it in one PUSH_PROMISE and zero or more CONTINUATION frames.
      Parameters:
      promisedStreamId - The promised stream ID where the promise response is going to be sent on
      request - The request to send
      Throws:
      IllegalStateException - If this stream is not in STATE_HALF_CLOSED
      See Also:
    • sendHTTPMessage

      public void sendHTTPMessage(HTTPMessage message, boolean endStream)
      Sends the given HTTPMessage on this stream by encoding the data with the configured HPackContext and sending it in one HEADERS and zero or more CONTINUATION frames.
      Parameters:
      message - The message to send
      endStream - Whether the message should end the stream and no payload data will be sent
      Throws:
      IllegalStateException - If this stream is not in STATE_IDLE (request) or STATE_HALF_CLOSED (response)
    • sendTrailers

      public void sendTrailers(HTTPMessageTrailers trailers)
      Sends the given trailers on this stream by encoding the data with the configured HPackContext and sending it in one HEADERS and zero or more CONTINUATION frames. This will end the stream.
      Parameters:
      trailers - The trailers to send
      Throws:
      IllegalStateException - If the stream is not in STATE_OPEN (request) or STATE_HALF_CLOSED (response)
    • sendData

      public boolean sendData(byte[] data, boolean endStream)
      Sends the given data on this stream in one or more DATA frames. If the receiver window size if not large enough to receive all data, it will be buffered and hasDataBacklog() will start returning true, until a WINDOW_UPDATE frame is received.
      Parameters:
      data - The data to send
      endStream - Whether this is the last data packet sent on this stream
      Returns:
      true If all data could be written because the receiver window size is large enough
      Throws:
      IllegalStateException - If the stream is not in STATE_OPEN (request) or STATE_HALF_CLOSED (response)
    • receiveFrame

      public void receiveFrame(int type, int flags, byte[] data) throws HTTP2ConnectionError
      Description copied from class: HTTP2Stream
      This method is called if a frame is received on this HTTP/2 stream.
      Overrides:
      receiveFrame in class HTTP2Stream
      Parameters:
      type - The frame type number
      flags - The frame flags
      data - The frame payload
      Throws:
      HTTP2ConnectionError - If a HTTP/2 connection error occurs, for example because the frame payload is invalid
    • writeFrame

      public void writeFrame(int type, int flags, byte[] data, int offset, int length)
      Description copied from class: HTTP2Stream
      Writes a frame with the given properties to the WritableSocket passed in the constructor.
      Overrides:
      writeFrame in class HTTP2Stream
      Parameters:
      type - The frame type
      flags - The frame flags
      data - The frame payload
      offset - The index in data to start reading from
      length - The total amount of bytes in data to write
    • windowUpdate

      public void windowUpdate()
      Description copied from class: HTTP2Stream
      This method is called when a window update frame is received. The HTTP2Stream.receiverWindowSize is updated before this method is called.
      Overrides:
      windowUpdate in class HTTP2Stream
    • rst

      public void rst(int errorCode)
      Sends the given errorCode in a RST_STREAM frame to close the stream, if the underlying connection is still connected. This also immediately changed the state to STATE_CLOSED and calls onClose.
      Parameters:
      errorCode - The status code to close the stream with
    • setRequestSupplier

      public void setRequestSupplier(HTTPRequestSupplier<?> requestSupplier)
      Sets the HTTPRequestSupplier for this message stream.
      Parameters:
      requestSupplier - The HTTPRequestSupplier
    • setResponseSupplier

      public void setResponseSupplier(HTTPResponseSupplier<?> responseSupplier)
      Sets the HTTPResponseSupplier for this message stream.
      Parameters:
      responseSupplier - The HTTPResponseSupplier
    • setOnPushPromise

      public void setOnPushPromise(org.omegazero.common.util.function.SpecificThrowingConsumer<HTTP2ConnectionError,HTTPRequest> onPushPromise)
    • setOnMessage

      public void setOnMessage(org.omegazero.common.util.function.SpecificThrowingConsumer<HTTP2ConnectionError,HTTPMessageData> onMessage)
    • setOnData

      public void setOnData(org.omegazero.common.util.function.SpecificThrowingConsumer<HTTP2ConnectionError,HTTPMessageData> onData)
    • setOnTrailers

      public void setOnTrailers(org.omegazero.common.util.function.SpecificThrowingConsumer<HTTP2ConnectionError,HTTPMessageTrailers> onTrailers)
    • setOnDataFlushed

      public void setOnDataFlushed(Runnable onDataFlushed)
    • setOnClosed

      public void setOnClosed(Consumer<Integer> onClosed)
    • setReceiveData

      public void setReceiveData(boolean receiveData)
      Sets whether this stream should continue receiving flow-controlled data by sending WINDOW_UPDATE frames.
      Parameters:
      receiveData - true to continue receiving flow-controlled data
    • hasDataBacklog

      public boolean hasDataBacklog()
      Returns true if this stream is buffering data because more data was passed to sendData(byte[], boolean) than the receiver can receive.
      Returns:
      true if this stream is buffering data
    • preparePush

      public void preparePush(boolean receive)
      Prepares this stream for the receipt or transmission of a promised server response.
      Parameters:
      receive - true if this stream is about to receive a pushed response, false otherwise
      Throws:
      IllegalStateException - If this stream is not in STATE_IDLE or the stream ID is an odd number
    • getState

      public int getState()
      Returns the state of this stream.
      Returns:
      The stream state
    • isExpectingResponse

      public boolean isExpectingResponse()
      Returns true if this stream is expecting a response (the state is STATE_HALF_CLOSED).
      Returns:
      true if this stream is expecting a response
    • isClosed

      public boolean isClosed()
      Returns true if this stream is closed (the state is STATE_CLOSED).
      Returns:
      true if this stream is closed
    • getCloseTime

      public long getCloseTime()
      Returns the time this stream closed, as returned by System.nanoTime().
      Returns:
      The time this stream closed
    • isCloseOutgoing

      public boolean isCloseOutgoing()
      Returns true if this stream was closed because a RST_STREAM frame or a frame with the END_STREAM flag was sent, not received.
      Returns:
      true if this stream was closed by an outgoing frame