Package 

Class Http2Connection

  • All Implemented Interfaces:
    java.io.Closeable , java.lang.AutoCloseable

    
    public final class Http2Connection
     implements Closeable
                        

    A socket connection to a remote peer. A connection hosts streams which can send and receive data.

    Many methods in this API are synchronous: the call is completed before the method returns. This is typical for Java but atypical for HTTP/2. This is motivated by exception transparency: an IOException that was triggered by a certain caller can be caught and handled by that caller.

    • Method Summary

      Modifier and Type Method Description
      Protocol getProtocol() The protocol as selected using ALPN.
      synchronized int openStreamCount() Returns the number of open streams on this connection.
      synchronized int maxConcurrentStreams()
      Http2Stream pushStream(int associatedStreamId, List<Header> requestHeaders, boolean out) Returns a new server-initiated stream.
      Http2Stream newStream(List<Header> requestHeaders, boolean out) Returns a new locally-initiated stream.
      void writeData(int streamId, boolean outFinished, Buffer buffer, long byteCount) Callers of this method are not thread safe, and sometimes on application threads.
      void flush()
      void shutdown(ErrorCode statusCode) Degrades this connection such that new streams can neither be created locally, nor acceptedfrom the remote peer.
      void close() Closes this connection.
      void start() Sends any initial frames and starts reading frames from the remote peer.
      void setSettings(Settings settings) Merges {@code settings} into this peer's settings and sends them to the remote peer.
      synchronized boolean isShutdown()
      • Methods inherited from class java.io.Closeable

        close
      • Methods inherited from class java.lang.Object

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

      • pushStream

         Http2Stream pushStream(int associatedStreamId, List<Header> requestHeaders, boolean out)

        Returns a new server-initiated stream.

        Parameters:
        associatedStreamId - the stream that triggered the sender to create this stream.
        out - true to create an output stream that we can use to send data to the remote peer.
      • newStream

         Http2Stream newStream(List<Header> requestHeaders, boolean out)

        Returns a new locally-initiated stream.

        Parameters:
        out - true to create an output stream that we can use to send data to the remote peer.
      • writeData

         void writeData(int streamId, boolean outFinished, Buffer buffer, long byteCount)

        Callers of this method are not thread safe, and sometimes on application threads. Most often,this method will be called to send a buffer worth of data to the peer.

        Writes are subject to the write window of the stream and the connection. Until there is awindow sufficient to send {@code byteCount}, the caller will block. For example, a user of {@code HttpURLConnection} who flushes more bytes to the output stream than the connection'swrite window will block.

        Zero {@code byteCount} writes are not subject to flow control and will not block. The onlyuse case for zero {@code byteCount} is closing a flushed output stream.

      • shutdown

         void shutdown(ErrorCode statusCode)

        Degrades this connection such that new streams can neither be created locally, nor acceptedfrom the remote peer. Existing streams are not impacted. This is intended to permit an endpointto gracefully stop accepting new requests without harming previously established streams.

      • close

         void close()

        Closes this connection. This cancels all open streams and unanswered pings. It closes theunderlying input and output streams and shuts down internal executor services.

      • start

         void start()

        Sends any initial frames and starts reading frames from the remote peer. This should be calledafter build for all new connections.

      • setSettings

         void setSettings(Settings settings)

        Merges {@code settings} into this peer's settings and sends them to the remote peer.