package okhttp3.internal.connection; import java.io.IOException; import java.net.Socket; import java.util.List; import okhttp3.Address; import okhttp3.Call; import okhttp3.EventListener; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Route; import okhttp3.internal.Util; import okhttp3.internal.connection.RouteSelector; import okhttp3.internal.http.ExchangeCodec; /* loaded from: classes5.dex */ public final class ExchangeFinder { public final Address address; public final Call call; public RealConnection connectingConnection; public final RealConnectionPool connectionPool; public final EventListener eventListener; public boolean hasStreamFailure; public Route nextRouteToTry; public RouteSelector.Selection routeSelection; public final RouteSelector routeSelector; public final Transmitter transmitter; public RealConnection connectingConnection() { return this.connectingConnection; } public ExchangeFinder(Transmitter transmitter, RealConnectionPool realConnectionPool, Address address, Call call, EventListener eventListener) { this.transmitter = transmitter; this.connectionPool = realConnectionPool; this.address = address; this.call = call; this.eventListener = eventListener; this.routeSelector = new RouteSelector(address, realConnectionPool.routeDatabase, call, eventListener); } public ExchangeCodec find(OkHttpClient okHttpClient, Interceptor.Chain chain, boolean z) { try { return findHealthyConnection(chain.connectTimeoutMillis(), chain.readTimeoutMillis(), chain.writeTimeoutMillis(), okHttpClient.pingIntervalMillis(), okHttpClient.retryOnConnectionFailure(), z).newCodec(okHttpClient, chain); } catch (IOException e) { trackFailure(); throw new RouteException(e); } catch (RouteException e2) { trackFailure(); throw e2; } } public final RealConnection findHealthyConnection(int i, int i2, int i3, int i4, boolean z, boolean z2) { while (true) { RealConnection findConnection = findConnection(i, i2, i3, i4, z); synchronized (this.connectionPool) { try { if (findConnection.successCount == 0 && !findConnection.isMultiplexed()) { return findConnection; } if (findConnection.isHealthy(z2)) { return findConnection; } findConnection.noNewExchanges(); } catch (Throwable th) { throw th; } } } } public final RealConnection findConnection(int i, int i2, int i3, int i4, boolean z) { RealConnection realConnection; Socket socket; Socket releaseConnectionNoEvents; RealConnection realConnection2; boolean z2; Route route; boolean z3; List list; RouteSelector.Selection selection; synchronized (this.connectionPool) { try { if (this.transmitter.isCanceled()) { throw new IOException("Canceled"); } this.hasStreamFailure = false; Transmitter transmitter = this.transmitter; realConnection = transmitter.connection; socket = null; releaseConnectionNoEvents = (realConnection == null || !realConnection.noNewExchanges) ? null : transmitter.releaseConnectionNoEvents(); Transmitter transmitter2 = this.transmitter; realConnection2 = transmitter2.connection; if (realConnection2 != null) { realConnection = null; } else { realConnection2 = null; } if (realConnection2 == null) { if (this.connectionPool.transmitterAcquirePooledConnection(this.address, transmitter2, null, false)) { realConnection2 = this.transmitter.connection; route = null; z2 = true; } else { route = this.nextRouteToTry; if (route != null) { this.nextRouteToTry = null; } else if (retryCurrentRoute()) { route = this.transmitter.connection.route(); } z2 = false; } } z2 = false; route = null; } finally { } } Util.closeQuietly(releaseConnectionNoEvents); if (realConnection != null) { this.eventListener.connectionReleased(this.call, realConnection); } if (z2) { this.eventListener.connectionAcquired(this.call, realConnection2); } if (realConnection2 != null) { return realConnection2; } if (route != null || ((selection = this.routeSelection) != null && selection.hasNext())) { z3 = false; } else { this.routeSelection = this.routeSelector.next(); z3 = true; } synchronized (this.connectionPool) { try { if (this.transmitter.isCanceled()) { throw new IOException("Canceled"); } if (z3) { list = this.routeSelection.getAll(); if (this.connectionPool.transmitterAcquirePooledConnection(this.address, this.transmitter, list, false)) { realConnection2 = this.transmitter.connection; z2 = true; } } else { list = null; } if (!z2) { if (route == null) { route = this.routeSelection.next(); } realConnection2 = new RealConnection(this.connectionPool, route); this.connectingConnection = realConnection2; } } finally { } } if (z2) { this.eventListener.connectionAcquired(this.call, realConnection2); return realConnection2; } realConnection2.connect(i, i2, i3, i4, z, this.call, this.eventListener); this.connectionPool.routeDatabase.connected(realConnection2.route()); synchronized (this.connectionPool) { try { this.connectingConnection = null; if (this.connectionPool.transmitterAcquirePooledConnection(this.address, this.transmitter, list, true)) { realConnection2.noNewExchanges = true; socket = realConnection2.socket(); realConnection2 = this.transmitter.connection; this.nextRouteToTry = route; } else { this.connectionPool.put(realConnection2); this.transmitter.acquireConnectionNoEvents(realConnection2); } } finally { } } Util.closeQuietly(socket); this.eventListener.connectionAcquired(this.call, realConnection2); return realConnection2; } public void trackFailure() { synchronized (this.connectionPool) { this.hasStreamFailure = true; } } public boolean hasStreamFailure() { boolean z; synchronized (this.connectionPool) { z = this.hasStreamFailure; } return z; } public boolean hasRouteToTry() { synchronized (this.connectionPool) { try { boolean z = true; if (this.nextRouteToTry != null) { return true; } if (retryCurrentRoute()) { this.nextRouteToTry = this.transmitter.connection.route(); return true; } RouteSelector.Selection selection = this.routeSelection; if ((selection == null || !selection.hasNext()) && !this.routeSelector.hasNext()) { z = false; } return z; } finally { } } } public final boolean retryCurrentRoute() { RealConnection realConnection = this.transmitter.connection; return realConnection != null && realConnection.routeFailureCount == 0 && Util.sameConnection(realConnection.route().address().url(), this.address.url()); } }