public final class Http2Connection
extends java.lang.Object
implements java.io.Closeable
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.
Modifier and Type | Class and Description |
---|---|
static class |
Http2Connection.Builder |
static class |
Http2Connection.Listener
Listener of streams and settings initiated by the peer.
|
(package private) class |
Http2Connection.PingRunnable |
(package private) class |
Http2Connection.ReaderRunnable
Methods in this class must not lock FrameWriter.
|
Modifier and Type | Field and Description |
---|---|
private boolean |
awaitingPong
True if we have sent a ping that is still awaiting a reply.
|
(package private) long |
bytesLeftInWriteWindow
Count of bytes that can be written on the connection before receiving a window update.
|
(package private) boolean |
client
True if this peer initiated the connection.
|
(package private) java.lang.String |
connectionName |
(package private) java.util.Set<java.lang.Integer> |
currentPushRequests |
(package private) int |
lastGoodStreamId |
(package private) Http2Connection.Listener |
listener
User code to run in response to incoming streams or settings.
|
private static java.util.concurrent.ExecutorService |
listenerExecutor
Shared executor to send notifications of incoming streams.
|
(package private) int |
nextStreamId |
(package private) static int |
OKHTTP_CLIENT_WINDOW_SIZE |
(package private) Settings |
okHttpSettings
Settings we communicate to the peer.
|
(package private) Settings |
peerSettings
Settings we receive from the peer.
|
private java.util.concurrent.ExecutorService |
pushExecutor
Ensures push promise callbacks events are sent in order per stream.
|
(package private) PushObserver |
pushObserver
User code to run in response to push promise events.
|
(package private) Http2Connection.ReaderRunnable |
readerRunnable |
(package private) boolean |
receivedInitialPeerSettings |
(package private) boolean |
shutdown |
(package private) java.net.Socket |
socket |
(package private) java.util.Map<java.lang.Integer,Http2Stream> |
streams |
(package private) long |
unacknowledgedBytesRead
The total number of bytes consumed by the application, but not yet acknowledged by sending a
WINDOW_UPDATE frame on this connection. |
(package private) Http2Writer |
writer |
private java.util.concurrent.ScheduledExecutorService |
writerExecutor
Asynchronously writes frames to the outgoing socket.
|
Constructor and Description |
---|
Http2Connection(Http2Connection.Builder builder) |
Modifier and Type | Method and Description |
---|---|
(package private) void |
awaitPong()
For testing: waits until
requiredPongCount pings have been received from the peer. |
void |
close()
Closes this connection.
|
(package private) void |
close(ErrorCode connectionCode,
ErrorCode streamCode,
java.io.IOException cause) |
private void |
failConnection(java.io.IOException e) |
void |
flush() |
(package private) Http2Stream |
getStream(int id) |
boolean |
isShutdown() |
int |
maxConcurrentStreams() |
private Http2Stream |
newStream(int associatedStreamId,
java.util.List<Header> requestHeaders,
boolean out) |
Http2Stream |
newStream(java.util.List<Header> requestHeaders,
boolean out)
Returns a new locally-initiated stream.
|
int |
openStreamCount()
Returns the number of
open streams on this connection. |
(package private) void |
pushDataLater(int streamId,
okio.BufferedSource source,
int byteCount,
boolean inFinished)
Eagerly reads
byteCount bytes from the source before launching a background task to
process the data. |
(package private) boolean |
pushedStream(int streamId)
Even, positive numbered streams are pushed streams in HTTP/2.
|
private void |
pushExecutorExecute(NamedRunnable namedRunnable) |
(package private) void |
pushHeadersLater(int streamId,
java.util.List<Header> requestHeaders,
boolean inFinished) |
(package private) void |
pushRequestLater(int streamId,
java.util.List<Header> requestHeaders) |
(package private) void |
pushResetLater(int streamId,
ErrorCode errorCode) |
Http2Stream |
pushStream(int associatedStreamId,
java.util.List<Header> requestHeaders,
boolean out)
Returns a new server-initiated stream.
|
(package private) Http2Stream |
removeStream(int streamId) |
void |
setSettings(Settings settings)
Merges
settings into this peer's settings and sends them to the remote peer. |
void |
shutdown(ErrorCode statusCode)
Degrades this connection such that new streams can neither be created locally, nor accepted
from the remote peer.
|
void |
start()
Sends any initial frames and starts reading frames from the remote peer.
|
(package private) void |
start(boolean sendConnectionPreface) |
(package private) void |
updateConnectionFlowControl(long read) |
void |
writeData(int streamId,
boolean outFinished,
okio.Buffer buffer,
long byteCount)
Callers of this method are not thread safe, and sometimes on application threads.
|
(package private) void |
writeHeaders(int streamId,
boolean outFinished,
java.util.List<Header> alternating) |
(package private) void |
writePing(boolean reply,
int payload1,
int payload2) |
(package private) void |
writePingAndAwaitPong()
For testing: sends a ping and waits for a pong.
|
(package private) void |
writeSynReset(int streamId,
ErrorCode statusCode) |
(package private) void |
writeSynResetLater(int streamId,
ErrorCode errorCode) |
(package private) void |
writeWindowUpdateLater(int streamId,
long unacknowledgedBytesRead) |
static final int OKHTTP_CLIENT_WINDOW_SIZE
private static final java.util.concurrent.ExecutorService listenerExecutor
final boolean client
final Http2Connection.Listener listener
listenerExecutor
.final java.util.Map<java.lang.Integer,Http2Stream> streams
final java.lang.String connectionName
int lastGoodStreamId
int nextStreamId
boolean shutdown
private final java.util.concurrent.ScheduledExecutorService writerExecutor
private final java.util.concurrent.ExecutorService pushExecutor
final PushObserver pushObserver
private boolean awaitingPong
long unacknowledgedBytesRead
WINDOW_UPDATE
frame on this connection.long bytesLeftInWriteWindow
Settings okHttpSettings
final Settings peerSettings
boolean receivedInitialPeerSettings
final java.net.Socket socket
final Http2Writer writer
final Http2Connection.ReaderRunnable readerRunnable
final java.util.Set<java.lang.Integer> currentPushRequests
Http2Connection(Http2Connection.Builder builder)
public int openStreamCount()
open streams
on this connection.Http2Stream getStream(int id)
Http2Stream removeStream(int streamId)
public int maxConcurrentStreams()
void updateConnectionFlowControl(long read)
public Http2Stream pushStream(int associatedStreamId, java.util.List<Header> requestHeaders, boolean out) throws java.io.IOException
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.
Corresponds to FLAG_FIN
.java.io.IOException
public Http2Stream newStream(java.util.List<Header> requestHeaders, boolean out) throws java.io.IOException
out
- true to create an output stream that we can use to send data to the remote peer.
Corresponds to FLAG_FIN
.java.io.IOException
private Http2Stream newStream(int associatedStreamId, java.util.List<Header> requestHeaders, boolean out) throws java.io.IOException
java.io.IOException
void writeHeaders(int streamId, boolean outFinished, java.util.List<Header> alternating) throws java.io.IOException
java.io.IOException
public void writeData(int streamId, boolean outFinished, okio.Buffer buffer, long byteCount) throws java.io.IOException
Writes are subject to the write window of the stream and the connection. Until there is a
window sufficient to send byteCount
, the caller will block. For example, a user of
HttpURLConnection
who flushes more bytes to the output stream than the connection's
write window will block.
Zero byteCount
writes are not subject to flow control and will not block. The only
use case for zero byteCount
is closing a flushed output stream.
java.io.IOException
void writeSynResetLater(int streamId, ErrorCode errorCode)
void writeSynReset(int streamId, ErrorCode statusCode) throws java.io.IOException
java.io.IOException
void writeWindowUpdateLater(int streamId, long unacknowledgedBytesRead)
void writePing(boolean reply, int payload1, int payload2)
void writePingAndAwaitPong() throws java.lang.InterruptedException
java.lang.InterruptedException
void awaitPong() throws java.lang.InterruptedException
requiredPongCount
pings have been received from the peer.java.lang.InterruptedException
public void flush() throws java.io.IOException
java.io.IOException
public void shutdown(ErrorCode statusCode) throws java.io.IOException
java.io.IOException
public void close()
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
void close(ErrorCode connectionCode, ErrorCode streamCode, @Nullable java.io.IOException cause)
private void failConnection(@Nullable java.io.IOException e)
public void start() throws java.io.IOException
Http2Connection.Builder.build()
for all new connections.java.io.IOException
void start(boolean sendConnectionPreface) throws java.io.IOException
sendConnectionPreface
- true to send connection preface frames. This should always be true
except for in tests that don't check for a connection preface.java.io.IOException
public void setSettings(Settings settings) throws java.io.IOException
settings
into this peer's settings and sends them to the remote peer.java.io.IOException
public boolean isShutdown()
boolean pushedStream(int streamId)
void pushRequestLater(int streamId, java.util.List<Header> requestHeaders)
void pushHeadersLater(int streamId, java.util.List<Header> requestHeaders, boolean inFinished)
void pushDataLater(int streamId, okio.BufferedSource source, int byteCount, boolean inFinished) throws java.io.IOException
byteCount
bytes from the source before launching a background task to
process the data. This avoids corrupting the stream.java.io.IOException
void pushResetLater(int streamId, ErrorCode errorCode)
private void pushExecutorExecute(NamedRunnable namedRunnable)