Class FramedConnection
- java.lang.Object
-
- com.squareup.okhttp.internal.framed.FramedConnection
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public final class FramedConnection extends Object 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 SPDY. This is motivated by exception transparency: an IOException that was triggered by a certain caller can be caught and handled by that caller.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classFramedConnection.Builder
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes this connection.voidflush()longgetIdleStartTimeNs()Returns the time in ns when this connection became idle or Long.MAX_VALUE if connection is not idle.ProtocolgetProtocol()The protocol as selected using ALPN.booleanisIdle()Returns true if this connection is idle.FramedStreamnewStream(List<Header> requestHeaders, boolean out, boolean in)Returns a new locally-initiated stream.intopenStreamCount()Returns the number ofopen streamson this connection.Pingping()Sends a ping frame to the peer.FramedStreampushStream(int associatedStreamId, List<Header> requestHeaders, boolean out)Returns a new server-initiated stream.voidsendConnectionPreface()Sends a connection header if the current variant requires it.voidshutdown(ErrorCode statusCode)Degrades this connection such that new streams can neither be created locally, nor accepted from the remote peer.voidwriteData(int streamId, boolean outFinished, Buffer buffer, long byteCount)Callers of this method are not thread safe, and sometimes on application threads.
-
-
-
Method Detail
-
getProtocol
public Protocol getProtocol()
The protocol as selected using ALPN.
-
openStreamCount
public int openStreamCount()
Returns the number ofopen streamson this connection.
-
isIdle
public boolean isIdle()
Returns true if this connection is idle.
-
getIdleStartTimeNs
public long getIdleStartTimeNs()
Returns the time in ns when this connection became idle or Long.MAX_VALUE if connection is not idle.
-
pushStream
public FramedStream pushStream(int associatedStreamId, List<Header> requestHeaders, boolean out) throws IOException
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. Corresponds toFLAG_FIN.- Throws:
IOException
-
newStream
public FramedStream newStream(List<Header> requestHeaders, boolean out, boolean in) throws IOException
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. Corresponds toFLAG_FIN.in- true to create an input stream that the remote peer can use to send data to us. Corresponds toFLAG_UNIDIRECTIONAL.- Throws:
IOException
-
writeData
public void writeData(int streamId, boolean outFinished, Buffer buffer, long byteCount) throws IOExceptionCallers 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 a window sufficient to send
byteCount, the caller will block. For example, a user ofHttpURLConnectionwho flushes more bytes to the output stream than the connection's write window will block.Zero
byteCountwrites are not subject to flow control and will not block. The only use case for zerobyteCountis closing a flushed output stream.- Throws:
IOException
-
ping
public Ping ping() throws IOException
Sends a ping frame to the peer. Use the returned object to await the ping's response and observe its round trip time.- Throws:
IOException
-
flush
public void flush() throws IOException- Throws:
IOException
-
shutdown
public void shutdown(ErrorCode 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.- Throws:
IOException
-
close
public void close() throws IOExceptionCloses 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- Throws:
IOException
-
sendConnectionPreface
public void sendConnectionPreface() throws IOExceptionSends a connection header if the current variant requires it. This should be called afterFramedConnection.Builder.build()for all new connections.- Throws:
IOException
-
-