Search in sources :

Example 6 with StreamAllocation

use of okhttp3.internal.connection.StreamAllocation in project okhttp by square.

the class RealWebSocket method connect.

public void connect(OkHttpClient client) {
    client = client.newBuilder().protocols(ONLY_HTTP1).build();
    final int pingIntervalMillis = client.pingIntervalMillis();
    final Request request = originalRequest.newBuilder().header("Upgrade", "websocket").header("Connection", "Upgrade").header("Sec-WebSocket-Key", key).header("Sec-WebSocket-Version", "13").build();
    call = Internal.instance.newWebSocketCall(client, request);
    call.enqueue(new Callback() {

        @Override
        public void onResponse(Call call, Response response) {
            try {
                checkResponse(response);
            } catch (ProtocolException e) {
                failWebSocket(e, response);
                closeQuietly(response);
                return;
            }
            // Promote the HTTP streams into web socket streams.
            StreamAllocation streamAllocation = Internal.instance.streamAllocation(call);
            // Prevent connection pooling!
            streamAllocation.noNewStreams();
            Streams streams = streamAllocation.connection().newWebSocketStreams(streamAllocation);
            // Process all web socket messages.
            try {
                listener.onOpen(RealWebSocket.this, response);
                String name = "OkHttp WebSocket " + request.url().redact();
                initReaderAndWriter(name, pingIntervalMillis, streams);
                streamAllocation.connection().socket().setSoTimeout(0);
                loopReader();
            } catch (Exception e) {
                failWebSocket(e, null);
            }
        }

        @Override
        public void onFailure(Call call, IOException e) {
            failWebSocket(e, null);
        }
    });
}
Also used : Response(okhttp3.Response) StreamAllocation(okhttp3.internal.connection.StreamAllocation) Call(okhttp3.Call) ProtocolException(java.net.ProtocolException) Callback(okhttp3.Callback) Request(okhttp3.Request) ByteString(okio.ByteString) IOException(java.io.IOException) IOException(java.io.IOException) ProtocolException(java.net.ProtocolException)

Example 7 with StreamAllocation

use of okhttp3.internal.connection.StreamAllocation in project okhttp by square.

the class ConnectInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    RealInterceptorChain realChain = (RealInterceptorChain) chain;
    Request request = realChain.request();
    StreamAllocation streamAllocation = realChain.streamAllocation();
    // We need the network to satisfy this request. Possibly for validating a conditional GET.
    boolean doExtensiveHealthChecks = !request.method().equals("GET");
    HttpCodec httpCodec = streamAllocation.newStream(client, doExtensiveHealthChecks);
    RealConnection connection = streamAllocation.connection();
    return realChain.proceed(request, streamAllocation, httpCodec, connection);
}
Also used : HttpCodec(okhttp3.internal.http.HttpCodec) RealInterceptorChain(okhttp3.internal.http.RealInterceptorChain) Request(okhttp3.Request)

Example 8 with StreamAllocation

use of okhttp3.internal.connection.StreamAllocation in project okhttp by square.

the class ConnectionPoolTest method allocateAndLeakAllocation.

/** Use a helper method so there's no hidden reference remaining on the stack. */
private void allocateAndLeakAllocation(ConnectionPool pool, RealConnection connection) {
    synchronized (pool) {
        StreamAllocation leak = new StreamAllocation(pool, connection.route().address(), null);
        leak.acquire(connection);
    }
}
Also used : StreamAllocation(okhttp3.internal.connection.StreamAllocation)

Example 9 with StreamAllocation

use of okhttp3.internal.connection.StreamAllocation in project okhttp by square.

the class RealInterceptorChain method proceed.

public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec, RealConnection connection) throws IOException {
    if (index >= interceptors.size())
        throw new AssertionError();
    calls++;
    // If we already have a stream, confirm that the incoming request will use it.
    if (this.httpCodec != null && !this.connection.supportsUrl(request.url())) {
        throw new IllegalStateException("network interceptor " + interceptors.get(index - 1) + " must retain the same host and port");
    }
    // If we already have a stream, confirm that this is the only call to chain.proceed().
    if (this.httpCodec != null && calls > 1) {
        throw new IllegalStateException("network interceptor " + interceptors.get(index - 1) + " must call proceed() exactly once");
    }
    // Call the next interceptor in the chain.
    RealInterceptorChain next = new RealInterceptorChain(interceptors, streamAllocation, httpCodec, connection, index + 1, request);
    Interceptor interceptor = interceptors.get(index);
    Response response = interceptor.intercept(next);
    // Confirm that the next interceptor made its required call to chain.proceed().
    if (httpCodec != null && index + 1 < interceptors.size() && next.calls != 1) {
        throw new IllegalStateException("network interceptor " + interceptor + " must call proceed() exactly once");
    }
    // Confirm that the intercepted response isn't null.
    if (response == null) {
        throw new NullPointerException("interceptor " + interceptor + " returned null");
    }
    return response;
}
Also used : Response(okhttp3.Response) Interceptor(okhttp3.Interceptor)

Aggregations

StreamAllocation (okhttp3.internal.connection.StreamAllocation)6 Response (okhttp3.Response)5 Request (okhttp3.Request)4 IOException (java.io.IOException)3 ProtocolException (java.net.ProtocolException)3 EOFException (java.io.EOFException)1 InterruptedIOException (java.io.InterruptedIOException)1 HttpRetryException (java.net.HttpRetryException)1 Call (okhttp3.Call)1 Callback (okhttp3.Callback)1 Interceptor (okhttp3.Interceptor)1 RealConnection (okhttp3.internal.connection.RealConnection)1 RouteException (okhttp3.internal.connection.RouteException)1 HttpCodec (okhttp3.internal.http.HttpCodec)1 RealInterceptorChain (okhttp3.internal.http.RealInterceptorChain)1 StatusLine (okhttp3.internal.http.StatusLine)1 ConnectionShutdownException (okhttp3.internal.http2.ConnectionShutdownException)1 BufferedSink (okio.BufferedSink)1 ByteString (okio.ByteString)1 Sink (okio.Sink)1