Module bus.http

Class Http2Connection

java.lang.Object
org.miaixz.bus.http.metric.http.Http2Connection
All Implemented Interfaces:
Closeable, AutoCloseable

public class Http2Connection extends Object implements Closeable
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 Classes
    Modifier and Type
    Class
    Description
    static class 
     
    static class 
    Listener of streams and settings initiated by the peer.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes this connection.
    void
    Flushes all buffered data on the underlying writer.
    boolean
    isHealthy(long nowNs)
    Returns true if this connection is healthy.
    int
    Returns 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.
    int
    Returns the number of open streams on this connection.
    pushStream(int associatedStreamId, List<Http2Header> requestHeaders, boolean out)
    Returns a new server-initiated stream.
    void
    Merges settings into this peer's settings and sends them to the remote peer.
    void
    Degrades this connection such that new streams can neither be created locally, nor accepted from the remote peer.
    void
    Sends any initial frames and starts reading frames from the remote peer.
    void
    writeData(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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • openStreamCount

      public int openStreamCount()
      Returns the number of open streams on 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 to FLAG_FIN.
      Returns:
      an HTTP/2 stream.
      Throws:
      IOException - if an I/O error occurs.
    • newStream

      public Http2Stream newStream(List<Http2Header> requestHeaders, boolean out) throws IOException
      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 to FLAG_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 send byteCount. For example, a user of HttpURLConnection who 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

      public void flush() throws IOException
      Flushes all buffered data on the underlying writer.
      Throws:
      IOException - if an I/O error occurs.
    • shutdown

      public void shutdown(Http2ErrorCode statusCode) throws IOException
      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:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • start

      public void start() throws IOException
      Sends any initial frames and starts reading frames from the remote peer. This should be called after Http2Connection.Builder.build() for all new connections.
      Throws:
      IOException - if an I/O error occurs.
    • setSettings

      public void setSettings(Http2Settings settings) throws IOException
      Merges settings into 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.