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 Stringstatic final Stringstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intFields 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 aMessageStreaminstance.MessageStream(int streamId, WritableSocket connection, ControlStream controlStream, HTTP2Settings localSettings, HTTP2Settings remoteSettings, HPackContext hpack) Creates aMessageStreaminstance. -
Method Summary
Modifier and TypeMethodDescriptionlongReturns the time this stream closed, as returned bySystem.nanoTime().intgetState()Returns the state of this stream.booleanReturnstrueif this stream is buffering data because more data was passed tosendData(byte[], boolean)than the receiver can receive.booleanisClosed()booleanReturnstrueif this stream was closed because a RST_STREAM frame or a frame with the END_STREAM flag was sent, not received.booleanvoidpreparePush(boolean receive) Prepares this stream for the receipt or transmission of a promised server response.voidreceiveFrame(int type, int flags, byte[] data) This method is called if a frame is received on this HTTP/2 stream.voidrst(int errorCode) Sends the given errorCode in a RST_STREAM frame to close the stream, if the underlying connection is still connected.booleansendData(byte[] data, boolean endStream) Sends the given data on this stream in one or more DATA frames.voidsendHTTPMessage(HTTPMessage message, boolean endStream) Sends the givenHTTPMessageon this stream by encoding the data with the configuredHPackContextand sending it in one HEADERS and zero or more CONTINUATION frames.voidsendPushPromise(int promisedStreamId, HTTPRequest request) Sends the givenHTTPRequeston this stream by encoding the data with the configuredHPackContextand sending it in one PUSH_PROMISE and zero or more CONTINUATION frames.voidsendTrailers(HTTPMessageTrailers trailers) Sends the given trailers on this stream by encoding the data with the configuredHPackContextand sending it in one HEADERS and zero or more CONTINUATION frames.voidsetOnClosed(Consumer<Integer> onClosed) voidsetOnData(org.omegazero.common.util.function.SpecificThrowingConsumer<HTTP2ConnectionError, HTTPMessageData> onData) voidsetOnDataFlushed(Runnable onDataFlushed) voidsetOnMessage(org.omegazero.common.util.function.SpecificThrowingConsumer<HTTP2ConnectionError, HTTPMessageData> onMessage) voidsetOnPushPromise(org.omegazero.common.util.function.SpecificThrowingConsumer<HTTP2ConnectionError, HTTPRequest> onPushPromise) voidsetOnTrailers(org.omegazero.common.util.function.SpecificThrowingConsumer<HTTP2ConnectionError, HTTPMessageTrailers> onTrailers) voidsetReceiveData(boolean receiveData) Sets whether this stream should continue receiving flow-controlled data by sending WINDOW_UPDATE frames.voidsetRequestSupplier(HTTPRequestSupplier<?> requestSupplier) Sets theHTTPRequestSupplierfor this message stream.voidsetResponseSupplier(HTTPResponseSupplier<?> responseSupplier) Sets theHTTPResponseSupplierfor this message stream.voidThis method is called when a window update frame is received.voidwriteFrame(int type, int flags, byte[] data, int offset, int length) Writes a frame with the given properties to theWritableSocketpassed 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 aMessageStreaminstance.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- TheHPackContextused for the connection
-
MessageStream
public MessageStream(int streamId, WritableSocket connection, ControlStream controlStream, HTTP2Settings localSettings, HTTP2Settings remoteSettings, HPackContext hpack) Creates aMessageStreaminstance.- Parameters:
streamId- The stream IDconnection- The underlying connectioncontrolStream- The control stream of the connectionlocalSettings- The localHTTP2SettingsremoteSettings- The remoteHTTP2Settingshpack- TheHPackContextused for the connection
-
-
Method Details
-
sendPushPromise
Sends the givenHTTPRequeston this stream by encoding the data with the configuredHPackContextand 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 givenHTTPMessageon this stream by encoding the data with the configuredHPackContextand 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 configuredHPackContextand 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:
trueIf 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:HTTP2StreamThis method is called if a frame is received on this HTTP/2 stream.- Overrides:
receiveFramein 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:HTTP2StreamWrites a frame with the given properties to theWritableSocketpassed in the constructor.- Overrides:
writeFramein 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:HTTP2StreamThis method is called when a window update frame is received. TheHTTP2Stream.receiverWindowSizeis updated before this method is called.- Overrides:
windowUpdatein 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_CLOSEDand callsonClose.- Parameters:
errorCode- The status code to close the stream with
-
setRequestSupplier
Sets theHTTPRequestSupplierfor this message stream.- Parameters:
requestSupplier- TheHTTPRequestSupplier
-
setResponseSupplier
Sets theHTTPResponseSupplierfor 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-trueto continue receiving flow-controlled data
-
hasDataBacklog
public boolean hasDataBacklog()Returnstrueif this stream is buffering data because more data was passed tosendData(byte[], boolean)than the receiver can receive.- Returns:
trueif 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-trueif this stream is about to receive a pushed response,falseotherwise- Throws:
IllegalStateException- If this stream is not inSTATE_IDLEor 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:
trueif this stream is expecting a response
-
isClosed
public boolean isClosed()- Returns:
trueif 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()Returnstrueif this stream was closed because a RST_STREAM frame or a frame with the END_STREAM flag was sent, not received.- Returns:
trueif this stream was closed by an outgoing frame
-