java.lang.Object
org.miaixz.bus.http.metric.http.Http2Connection
- All Implemented Interfaces:
Closeable,AutoCloseable
A socket connection to a remote peer. Connections host streams which can send and receive data.
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic classListener of streams and settings initiated by the peer. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this connection.voidflush()Flushes all buffered data on the underlying writer.booleanisHealthy(long nowNs) Returns true if this connection is healthy.intReturns the maximum number of concurrent streams that the remote peer is willing to accept.newStream(List<Http2Header> requestHeaders, boolean out) Returns a new locally-initiated stream.intReturns the number ofopen streamson this connection.pushStream(int associatedStreamId, List<Http2Header> requestHeaders, boolean out) Returns a new server-initiated stream.voidsetSettings(Http2Settings settings) Mergessettingsinto this peer's settings and sends them to the remote peer.voidshutdown(Http2ErrorCode statusCode) Degrades this connection such that new streams can neither be created locally, nor accepted from the remote peer.voidstart()Sends any initial frames and starts reading frames from the remote peer.voidwriteData(int streamId, boolean outFinished, org.miaixz.bus.core.io.buffer.Buffer buffer, long byteCount) The caller of this method is not thread-safe and may be on an application thread.
-
Method Details
-
openStreamCount
public int openStreamCount()Returns the number ofopen streamson this connection.- Returns:
- the number of open streams.
-
maxConcurrentStreams
public int maxConcurrentStreams()Returns the maximum number of concurrent streams that the remote peer is willing to accept.- Returns:
- the maximum number of concurrent streams.
-
pushStream
public Http2Stream pushStream(int associatedStreamId, List<Http2Header> requestHeaders, boolean out) throws IOException Returns a new server-initiated stream.- Parameters:
associatedStreamId- The stream that triggered the sender to create this stream.requestHeaders- The request headers.out- True to create an output stream that we can use to send data to the remote peer. Corresponds toFLAG_FIN.- Returns:
- an HTTP/2 stream.
- Throws:
IOException- if an I/O error occurs.
-
newStream
Returns a new locally-initiated stream.- Parameters:
requestHeaders- The request headers.out- true to create an output stream that we can use to send data to the remote peer. Corresponds toFLAG_FIN.- Returns:
- an HTTP/2 stream.
- Throws:
IOException- if an I/O error occurs.
-
writeData
public void writeData(int streamId, boolean outFinished, org.miaixz.bus.core.io.buffer.Buffer buffer, long byteCount) throws IOException The caller of this method is not thread-safe and may be on an application thread. Typically, this method is called to send a buffer of data to the peer. The write is subject to the write window of the stream and the connection. Callers will block until there is enough window to sendbyteCount. For example, a user ofHttpURLConnectionwho flushes more bytes to the output stream than the connection's write window will block.- Parameters:
streamId- The stream ID.outFinished- Whether this is the last frame to be sent on this stream.buffer- The buffer containing the data.byteCount- The number of bytes to write.- Throws:
IOException- if an I/O error occurs.
-
flush
Flushes all buffered data on the underlying writer.- Throws:
IOException- if an I/O error occurs.
-
shutdown
Degrades this connection such that new streams can neither be created locally, nor accepted from the remote peer. Existing streams are not impacted. This is intended to permit an endpoint to gracefully stop accepting new requests without harming previously established streams.- Parameters:
statusCode- The error code to send in the GOAWAY frame.- Throws:
IOException- if an I/O error occurs.
-
close
public void close()Closes this connection. This cancels all open streams and unanswered pings. It closes the underlying input and output streams and shuts down internal executor services.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
start
Sends any initial frames and starts reading frames from the remote peer. This should be called afterHttp2Connection.Builder.build()for all new connections.- Throws:
IOException- if an I/O error occurs.
-
setSettings
Mergessettingsinto this peer's settings and sends them to the remote peer.- Parameters:
settings- The settings to apply.- Throws:
IOException- if an I/O error occurs.
-
isHealthy
public boolean isHealthy(long nowNs) Returns true if this connection is healthy. A connection is unhealthy if it has been shut down or if a degraded pong is overdue.- Parameters:
nowNs- The current time in nanoseconds.- Returns:
- true if the connection is healthy.
-