Package org.omegazero.http.h2.streams
Class MessageStream
java.lang.Object
org.omegazero.http.h2.streams.HTTP2Stream
org.omegazero.http.h2.streams.MessageStream
Represents any
HTTP2Stream
where HTTP messages are exchanged (streams with IDs higher than 0).- Since:
- 1.2.1
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
static final String
static final int
static final int
static final int
static final int
static final int
static final int
static final int
Fields inherited from class org.omegazero.http.h2.streams.HTTP2Stream
connection, localWindowSize, receiverWindowSize, streamId, windowSizeLock
-
Constructor Summary
ConstructorsConstructorDescriptionMessageStream
(int streamId, WritableSocket connection, ControlStream controlStream, HPackContext hpack) Creates aMessageStream
instance.MessageStream
(int streamId, WritableSocket connection, ControlStream controlStream, HTTP2Settings localSettings, HTTP2Settings remoteSettings, HPackContext hpack) Creates aMessageStream
instance. -
Method Summary
Modifier and TypeMethodDescriptionlong
Returns the time this stream closed, as returned bySystem.nanoTime()
.int
getState()
Returns the state of this stream.boolean
Returnstrue
if this stream is buffering data because more data was passed tosendData(byte[], boolean)
than the receiver can receive.boolean
isClosed()
boolean
Returnstrue
if this stream was closed because a RST_STREAM frame or a frame with the END_STREAM flag was sent, not received.boolean
void
preparePush
(boolean receive) Prepares this stream for the receipt or transmission of a promised server response.void
receiveFrame
(int type, int flags, byte[] data) This method is called if a frame is received on this HTTP/2 stream.void
rst
(int errorCode) Sends the given errorCode in a RST_STREAM frame to close the stream, if the underlying connection is still connected.boolean
sendData
(byte[] data, boolean endStream) Sends the given data on this stream in one or more DATA frames.void
sendHTTPMessage
(HTTPMessage message, boolean endStream) Sends the givenHTTPMessage
on this stream by encoding the data with the configuredHPackContext
and sending it in one HEADERS and zero or more CONTINUATION frames.void
sendPushPromise
(int promisedStreamId, HTTPRequest request) Sends the givenHTTPRequest
on this stream by encoding the data with the configuredHPackContext
and sending it in one PUSH_PROMISE and zero or more CONTINUATION frames.void
sendTrailers
(HTTPMessageTrailers trailers) Sends the given trailers on this stream by encoding the data with the configuredHPackContext
and sending it in one HEADERS and zero or more CONTINUATION frames.void
setOnClosed
(Consumer<Integer> onClosed) void
setOnData
(org.omegazero.common.util.function.SpecificThrowingConsumer<HTTP2ConnectionError, HTTPMessageData> onData) void
setOnDataFlushed
(Runnable onDataFlushed) void
setOnMessage
(org.omegazero.common.util.function.SpecificThrowingConsumer<HTTP2ConnectionError, HTTPMessageData> onMessage) void
setOnPushPromise
(org.omegazero.common.util.function.SpecificThrowingConsumer<HTTP2ConnectionError, HTTPRequest> onPushPromise) void
setOnTrailers
(org.omegazero.common.util.function.SpecificThrowingConsumer<HTTP2ConnectionError, HTTPMessageTrailers> onTrailers) void
setReceiveData
(boolean receiveData) Sets whether this stream should continue receiving flow-controlled data by sending WINDOW_UPDATE frames.void
setRequestSupplier
(HTTPRequestSupplier<?> requestSupplier) Sets theHTTPRequestSupplier
for this message stream.void
setResponseSupplier
(HTTPResponseSupplier<?> responseSupplier) Sets theHTTPResponseSupplier
for this message stream.void
This method is called when a window update frame is received.void
writeFrame
(int type, int flags, byte[] data, int offset, int length) Writes a frame with the given properties to theWritableSocket
passed in the constructor.Methods inherited from class org.omegazero.http.h2.streams.HTTP2Stream
getConnection, getStreamId, isFlowControlledFrameType, sendWindowSizeUpdate, writeFrame, writeFrame
-
Field Details
-
HTTP2_VERSION_NAME
- See Also:
-
ATTACHMENT_KEY_STREAM_ID
- See Also:
-
STATE_IDLE
public static final int STATE_IDLE- See Also:
-
STATE_OPEN
public static final int STATE_OPEN- See Also:
-
STATE_RESERVED_LOCAL
public static final int STATE_RESERVED_LOCAL- See Also:
-
STATE_RESERVED
public static final int STATE_RESERVED- See Also:
-
STATE_HALF_CLOSED_LOCAL
public static final int STATE_HALF_CLOSED_LOCAL- See Also:
-
STATE_HALF_CLOSED
public static final int STATE_HALF_CLOSED- See Also:
-
STATE_CLOSED
public static final int STATE_CLOSED- See Also:
-
-
Constructor Details
-
MessageStream
public MessageStream(int streamId, WritableSocket connection, ControlStream controlStream, HPackContext hpack) Creates aMessageStream
instance.The local and remote settings are retrieved from the given control stream.
- Parameters:
streamId
- The stream IDconnection
- The underlying connectioncontrolStream
- The control stream of the connectionhpack
- TheHPackContext
used for the connection
-
MessageStream
public MessageStream(int streamId, WritableSocket connection, ControlStream controlStream, HTTP2Settings localSettings, HTTP2Settings remoteSettings, HPackContext hpack) Creates aMessageStream
instance.- Parameters:
streamId
- The stream IDconnection
- The underlying connectioncontrolStream
- The control stream of the connectionlocalSettings
- The localHTTP2Settings
remoteSettings
- The remoteHTTP2Settings
hpack
- TheHPackContext
used for the connection
-
-
Method Details
-
sendPushPromise
Sends the givenHTTPRequest
on this stream by encoding the data with the configuredHPackContext
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 onrequest
- The request to send- Throws:
IllegalStateException
- If this stream is not inSTATE_HALF_CLOSED
- See Also:
-
sendHTTPMessage
Sends the givenHTTPMessage
on this stream by encoding the data with the configuredHPackContext
and sending it in one HEADERS and zero or more CONTINUATION frames.- Parameters:
message
- The message to sendendStream
- Whether the message should end the stream and no payload data will be sent- Throws:
IllegalStateException
- If this stream is not inSTATE_IDLE
(request) orSTATE_HALF_CLOSED
(response)
-
sendTrailers
Sends the given trailers on this stream by encoding the data with the configuredHPackContext
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 inSTATE_OPEN
(request) orSTATE_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 andhasDataBacklog()
will start returningtrue
, until a WINDOW_UPDATE frame is received.- Parameters:
data
- The data to sendendStream
- 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 inSTATE_OPEN
(request) orSTATE_HALF_CLOSED
(response)
-
receiveFrame
Description copied from class:HTTP2Stream
This method is called if a frame is received on this HTTP/2 stream.- Overrides:
receiveFrame
in classHTTP2Stream
- Parameters:
type
- The frame type numberflags
- The frame flagsdata
- 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 theWritableSocket
passed in the constructor.- Overrides:
writeFrame
in classHTTP2Stream
- Parameters:
type
- The frame typeflags
- The frame flagsdata
- The frame payloadoffset
- The index in data to start reading fromlength
- 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. TheHTTP2Stream.receiverWindowSize
is updated before this method is called.- Overrides:
windowUpdate
in classHTTP2Stream
-
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 toSTATE_CLOSED
and callsonClose
.- Parameters:
errorCode
- The status code to close the stream with
-
setRequestSupplier
Sets theHTTPRequestSupplier
for this message stream.- Parameters:
requestSupplier
- TheHTTPRequestSupplier
-
setResponseSupplier
Sets theHTTPResponseSupplier
for this message stream.- Parameters:
responseSupplier
- TheHTTPResponseSupplier
-
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
-
setOnClosed
-
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()Returnstrue
if this stream is buffering data because more data was passed tosendData(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 inSTATE_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
-
isClosed
public boolean isClosed()- Returns:
true
if this stream is closed
-
getCloseTime
public long getCloseTime()Returns the time this stream closed, as returned bySystem.nanoTime()
.- Returns:
- The time this stream closed
-
isCloseOutgoing
public boolean isCloseOutgoing()Returnstrue
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
-