@Override public void cancel() { RealConnection connection = streamAllocation.connection(); if (connection != null) connection.cancel(); }
/** * Finds a connection and returns it if it is healthy. If it is unhealthy the process is repeated * until a healthy connection is found. */ private RealConnection findHealthyConnection(int connectTimeout, int readTimeout, int writeTimeout, boolean connectionRetryEnabled, boolean doExtensiveHealthChecks) throws IOException, RouteException { while (true) { RealConnection candidate = findConnection(connectTimeout, readTimeout, writeTimeout, connectionRetryEnabled); // If this is a brand new connection, we can skip the extensive health checks. synchronized (connectionPool) { if (candidate.successCount == 0) { return candidate; } } // Otherwise do a potentially-slow check to confirm that the pooled connection is still good. if (candidate.isHealthy(doExtensiveHealthChecks)) { return candidate; } connectionFailed(new IOException()); } }
/** Returns a recycled connection to {@code address}, or null if no such connection exists. */ RealConnection get(Address address, StreamAllocation streamAllocation) { assert (Thread.holdsLock(this)); for (RealConnection connection : connections) { // TODO(jwilson): this is awkward. We're already holding a lock on 'this', and // connection.allocationLimit() may also lock the FramedConnection. if (connection.allocations.size() < connection.allocationLimit() && address.equals(connection.getRoute().address) && !connection.noNewStreams) { streamAllocation.acquire(connection); return connection; } } return null; }
RealConnection newConnection = new RealConnection(route); acquire(newConnection); newConnection.connect(connectTimeout, readTimeout, writeTimeout, address.connectionSpecs(), connectionRetryEnabled); routeDatabase().connected(newConnection.getRoute());
RealConnection newConnection = new RealConnection(selectedRoute); acquire(newConnection); newConnection.connect(connectTimeout, readTimeout, writeTimeout, address.connectionSpecs(), connectionRetryEnabled); routeDatabase().connected(newConnection.route());
/** Does all the work necessary to build a full HTTP or HTTPS connection on a raw socket. */ private void connectSocket(int connectTimeout, int readTimeout, int writeTimeout, ConnectionSpecSelector connectionSpecSelector) throws IOException { rawSocket.setSoTimeout(readTimeout); try { Platform.get().connectSocket(rawSocket, route.socketAddress(), connectTimeout); } catch (ConnectException e) { throw new ConnectException("Failed to connect to " + route.socketAddress()); } source = Okio.buffer(Okio.source(rawSocket)); sink = Okio.buffer(Okio.sink(rawSocket)); if (route.address().sslSocketFactory() != null) { connectTls(readTimeout, writeTimeout, connectionSpecSelector); } else { protocol = Protocol.HTTP_1_1; socket = rawSocket; } if (protocol == Protocol.SPDY_3 || protocol == Protocol.HTTP_2) { socket.setSoTimeout(0); // Framed connection timeouts are set per-stream. FramedConnection framedConnection = new FramedConnection.Builder(true) .socket(socket, route.address().url().host(), source, sink) .protocol(protocol) .build(); framedConnection.sendConnectionPreface(); // Only assign the framed connection once the preface has been sent successfully. this.framedConnection = framedConnection; } }
? address.socketFactory().createSocket() : new Socket(proxy); connectSocket(connectTimeout, readTimeout, writeTimeout, connectionSpecSelector); } catch (IOException e) { closeQuietly(socket);
Request tunnelRequest = createTunnelRequest(); HttpUrl url = tunnelRequest.url(); String requestLine = "CONNECT " + url.host() + ":" + url.port() + " HTTP/1.1";
private void connectTls(int readTimeout, int writeTimeout, ConnectionSpecSelector connectionSpecSelector) throws IOException { if (route.requiresTunnel()) { createTunnel(readTimeout, writeTimeout);
private Response readNetworkResponse() throws IOException { httpStream.finishRequest(); Response networkResponse = httpStream.readResponseHeaders() .request(networkRequest) .handshake(streamAllocation.connection().getHandshake()) .header(OkHeaders.SENT_MILLIS, Long.toString(sentRequestMillis)) .header(OkHeaders.RECEIVED_MILLIS, Long.toString(System.currentTimeMillis())) .build(); if (!forWebSocket) { networkResponse = networkResponse.newBuilder() .body(httpStream.openResponseBody(networkResponse)) .build(); } if ("close".equalsIgnoreCase(networkResponse.request().header("Connection")) || "close".equalsIgnoreCase(networkResponse.header("Connection"))) { streamAllocation.noNewStreams(); } return networkResponse; }
RealConnection newConnection = new RealConnection(route); acquire(newConnection); newConnection.connect(connectTimeout, readTimeout, writeTimeout, address.connectionSpecs(), connectionRetryEnabled); routeDatabase().connected(newConnection.getRoute());
/** Does all the work necessary to build a full HTTP or HTTPS connection on a raw socket. */ private void connectSocket(int connectTimeout, int readTimeout, int writeTimeout, ConnectionSpecSelector connectionSpecSelector) throws IOException { rawSocket.setSoTimeout(readTimeout); try { Platform.get().connectSocket(rawSocket, route.socketAddress(), connectTimeout); } catch (ConnectException e) { throw new ConnectException("Failed to connect to " + route.socketAddress()); } source = Okio.buffer(Okio.source(rawSocket)); sink = Okio.buffer(Okio.sink(rawSocket)); if (route.address().sslSocketFactory() != null) { connectTls(readTimeout, writeTimeout, connectionSpecSelector); } else { protocol = Protocol.HTTP_1_1; socket = rawSocket; } if (protocol == Protocol.SPDY_3 || protocol == Protocol.HTTP_2) { socket.setSoTimeout(0); // Framed connection timeouts are set per-stream. FramedConnection framedConnection = new FramedConnection.Builder(true) .socket(socket, route.address().url().host(), source, sink) .protocol(protocol) .build(); framedConnection.sendConnectionPreface(); // Only assign the framed connection once the preface has been sent successfully. this.framedConnection = framedConnection; } }
? address.socketFactory().createSocket() : new Socket(proxy); connectSocket(connectTimeout, readTimeout, writeTimeout, connectionSpecSelector); } catch (IOException e) { closeQuietly(socket);
Request tunnelRequest = createTunnelRequest(); HttpUrl url = tunnelRequest.url(); String requestLine = "CONNECT " + url.host() + ":" + url.port() + " HTTP/1.1";
private void connectTls(int readTimeout, int writeTimeout, ConnectionSpecSelector connectionSpecSelector) throws IOException { if (route.requiresTunnel()) { createTunnel(readTimeout, writeTimeout);
private Response readNetworkResponse() throws IOException { httpStream.finishRequest(); Response networkResponse = httpStream.readResponseHeaders() .request(networkRequest) .handshake(streamAllocation.connection().getHandshake()) .header(OkHeaders.SENT_MILLIS, Long.toString(sentRequestMillis)) .header(OkHeaders.RECEIVED_MILLIS, Long.toString(System.currentTimeMillis())) .build(); if (!forWebSocket) { networkResponse = networkResponse.newBuilder() .body(httpStream.openResponseBody(networkResponse)) .build(); } if ("close".equalsIgnoreCase(networkResponse.request().header("Connection")) || "close".equalsIgnoreCase(networkResponse.header("Connection"))) { streamAllocation.noNewStreams(); } return networkResponse; }
@Override public void cancel() { RealConnection connection = streamAllocation.connection(); if (connection != null) connection.cancel(); }
/** Returns a recycled connection to {@code address}, or null if no such connection exists. */ RealConnection get(Address address, StreamAllocation streamAllocation) { assert (Thread.holdsLock(this)); for (RealConnection connection : connections) { // TODO(jwilson): this is awkward. We're already holding a lock on 'this', and // connection.allocationLimit() may also lock the FramedConnection. if (connection.allocations.size() < connection.allocationLimit() && address.equals(connection.getRoute().address) && !connection.noNewStreams) { streamAllocation.acquire(connection); return connection; } } return null; }
/** * Finds a connection and returns it if it is healthy. If it is unhealthy the process is repeated * until a healthy connection is found. */ private RealConnection findHealthyConnection(int connectTimeout, int readTimeout, int writeTimeout, boolean connectionRetryEnabled, boolean doExtensiveHealthChecks) throws IOException, RouteException { while (true) { RealConnection candidate = findConnection( connectTimeout, readTimeout, writeTimeout, connectionRetryEnabled); if (connection.isHealthy(doExtensiveHealthChecks)) { return candidate; } connectionFailed(); } }
connectTls(readTimeout, writeTimeout, connectionSpecSelector); } else { protocol = Protocol.HTTP_1_1;