package com.mbridge.msdk.thrid.okhttp.internal.connection; import com.mbridge.msdk.thrid.okhttp.Address; import com.mbridge.msdk.thrid.okhttp.Call; import com.mbridge.msdk.thrid.okhttp.ConnectionPool; import com.mbridge.msdk.thrid.okhttp.EventListener; import com.mbridge.msdk.thrid.okhttp.Interceptor; import com.mbridge.msdk.thrid.okhttp.OkHttpClient; import com.mbridge.msdk.thrid.okhttp.Route; import com.mbridge.msdk.thrid.okhttp.internal.Internal; import com.mbridge.msdk.thrid.okhttp.internal.Util; import com.mbridge.msdk.thrid.okhttp.internal.connection.RouteSelector; import com.mbridge.msdk.thrid.okhttp.internal.http.HttpCodec; import com.mbridge.msdk.thrid.okhttp.internal.http2.ConnectionShutdownException; import com.mbridge.msdk.thrid.okhttp.internal.http2.ErrorCode; import com.mbridge.msdk.thrid.okhttp.internal.http2.StreamResetException; import java.io.IOException; import java.lang.ref.WeakReference; import java.net.Socket; import java.util.List; public final class StreamAllocation { static final /* synthetic */ boolean $assertionsDisabled = false; public final Address address; public final Call call; private final Object callStackTrace; private boolean canceled; private HttpCodec codec; private RealConnection connection; private final ConnectionPool connectionPool; public final EventListener eventListener; private int refusedStreamCount; private boolean released; private boolean reportedAcquired; private Route route; private RouteSelector.Selection routeSelection; private final RouteSelector routeSelector; public StreamAllocation(ConnectionPool connectionPool2, Address address2, Call call2, EventListener eventListener2, Object obj) { this.connectionPool = connectionPool2; this.address = address2; this.call = call2; this.eventListener = eventListener2; this.routeSelector = new RouteSelector(address2, routeDatabase(), call2, eventListener2); this.callStackTrace = obj; } public HttpCodec newStream(OkHttpClient okHttpClient, Interceptor.Chain chain, boolean z) { try { HttpCodec newCodec = findHealthyConnection(chain.connectTimeoutMillis(), chain.readTimeoutMillis(), chain.writeTimeoutMillis(), okHttpClient.pingIntervalMillis(), okHttpClient.retryOnConnectionFailure(), z).newCodec(okHttpClient, chain, this); synchronized (this.connectionPool) { this.codec = newCodec; } return newCodec; } catch (IOException e) { throw new RouteException(e); } } /* JADX WARNING: Code restructure failed: missing block: B:11:0x0018, code lost: if (r0.isHealthy(r9) != false) goto L_0x001e; */ /* JADX WARNING: Code restructure failed: missing block: B:13:0x001e, code lost: return r0; */ private RealConnection findHealthyConnection(int i, int i2, int i3, int i4, boolean z, boolean z2) throws IOException { while (true) { RealConnection findConnection = findConnection(i, i2, i3, i4, z); synchronized (this.connectionPool) { if (findConnection.successCount == 0 && !findConnection.isMultiplexed()) { return findConnection; } } noNewStreams(); } } private RealConnection findConnection(int i, int i2, int i3, int i4, boolean z) throws IOException { RealConnection realConnection; Socket releaseIfNoNewStreams; RealConnection realConnection2; Socket socket; boolean z2; Route route2; boolean z3; RouteSelector.Selection selection; synchronized (this.connectionPool) { if (this.released) { throw new IllegalStateException("released"); } else if (this.codec != null) { throw new IllegalStateException("codec != null"); } else if (!this.canceled) { realConnection = this.connection; releaseIfNoNewStreams = releaseIfNoNewStreams(); realConnection2 = this.connection; socket = null; if (realConnection2 != null) { realConnection = null; } else { realConnection2 = null; } if (!this.reportedAcquired) { realConnection = null; } if (realConnection2 == null) { Internal.instance.get(this.connectionPool, this.address, this, null); RealConnection realConnection3 = this.connection; if (realConnection3 != null) { realConnection2 = realConnection3; z2 = true; route2 = null; } else { route2 = this.route; } } else { route2 = null; } z2 = false; } else { throw new IOException("Canceled"); } } Util.closeQuietly(releaseIfNoNewStreams); if (realConnection != null) { this.eventListener.connectionReleased(this.call, realConnection); } if (z2) { this.eventListener.connectionAcquired(this.call, realConnection2); } if (realConnection2 != null) { this.route = this.connection.route(); return realConnection2; } if (route2 != null || ((selection = this.routeSelection) != null && selection.hasNext())) { z3 = false; } else { this.routeSelection = this.routeSelector.next(); z3 = true; } synchronized (this.connectionPool) { if (!this.canceled) { if (z3) { List<Route> all = this.routeSelection.getAll(); int size = all.size(); int i5 = 0; while (true) { if (i5 >= size) { break; } Route route3 = all.get(i5); Internal.instance.get(this.connectionPool, this.address, this, route3); RealConnection realConnection4 = this.connection; if (realConnection4 != null) { this.route = route3; realConnection2 = realConnection4; z2 = true; break; } i5++; } } if (!z2) { if (route2 == null) { route2 = this.routeSelection.next(); } this.route = route2; this.refusedStreamCount = 0; realConnection2 = new RealConnection(this.connectionPool, route2); acquire(realConnection2, false); } } else { throw new IOException("Canceled"); } } if (z2) { this.eventListener.connectionAcquired(this.call, realConnection2); return realConnection2; } realConnection2.connect(i, i2, i3, i4, z, this.call, this.eventListener); routeDatabase().connected(realConnection2.route()); synchronized (this.connectionPool) { this.reportedAcquired = true; Internal.instance.put(this.connectionPool, realConnection2); if (realConnection2.isMultiplexed()) { socket = Internal.instance.deduplicate(this.connectionPool, this.address, this); realConnection2 = this.connection; } } Util.closeQuietly(socket); this.eventListener.connectionAcquired(this.call, realConnection2); return realConnection2; } private Socket releaseIfNoNewStreams() { RealConnection realConnection = this.connection; if (realConnection == null || !realConnection.noNewStreams) { return null; } return deallocate(false, false, true); } public void streamFinished(boolean z, HttpCodec httpCodec, long j, IOException iOException) { RealConnection realConnection; Socket deallocate; boolean z2; this.eventListener.responseBodyEnd(this.call, j); synchronized (this.connectionPool) { if (httpCodec != null) { if (httpCodec == this.codec) { if (!z) { this.connection.successCount++; } realConnection = this.connection; deallocate = deallocate(z, false, true); if (this.connection != null) { realConnection = null; } z2 = this.released; } } throw new IllegalStateException("expected " + this.codec + " but was " + httpCodec); } Util.closeQuietly(deallocate); if (realConnection != null) { this.eventListener.connectionReleased(this.call, realConnection); } if (iOException != null) { this.eventListener.callFailed(this.call, Internal.instance.timeoutExit(this.call, iOException)); } else if (z2) { Internal.instance.timeoutExit(this.call, null); this.eventListener.callEnd(this.call); } } public HttpCodec codec() { HttpCodec httpCodec; synchronized (this.connectionPool) { httpCodec = this.codec; } return httpCodec; } private RouteDatabase routeDatabase() { return Internal.instance.routeDatabase(this.connectionPool); } public Route route() { return this.route; } public synchronized RealConnection connection() { return this.connection; } public void release() { RealConnection realConnection; Socket deallocate; synchronized (this.connectionPool) { realConnection = this.connection; deallocate = deallocate(false, true, false); if (this.connection != null) { realConnection = null; } } Util.closeQuietly(deallocate); if (realConnection != null) { Internal.instance.timeoutExit(this.call, null); this.eventListener.connectionReleased(this.call, realConnection); this.eventListener.callEnd(this.call); } } public void noNewStreams() { RealConnection realConnection; Socket deallocate; synchronized (this.connectionPool) { realConnection = this.connection; deallocate = deallocate(true, false, false); if (this.connection != null) { realConnection = null; } } Util.closeQuietly(deallocate); if (realConnection != null) { this.eventListener.connectionReleased(this.call, realConnection); } } private Socket deallocate(boolean z, boolean z2, boolean z3) { Socket socket; if (z3) { this.codec = null; } if (z2) { this.released = true; } RealConnection realConnection = this.connection; if (realConnection == null) { return null; } if (z) { realConnection.noNewStreams = true; } if (this.codec != null) { return null; } if (!this.released && !this.connection.noNewStreams) { return null; } release(this.connection); if (this.connection.allocations.isEmpty()) { this.connection.idleAtNanos = System.nanoTime(); if (Internal.instance.connectionBecameIdle(this.connectionPool, this.connection)) { socket = this.connection.socket(); this.connection = null; return socket; } } socket = null; this.connection = null; return socket; } public void cancel() { HttpCodec httpCodec; RealConnection realConnection; synchronized (this.connectionPool) { this.canceled = true; httpCodec = this.codec; realConnection = this.connection; } if (httpCodec != null) { httpCodec.cancel(); } else if (realConnection != null) { realConnection.cancel(); } } /* JADX WARNING: Removed duplicated region for block: B:30:0x0051 */ public void streamFailed(IOException iOException) { RealConnection realConnection; boolean z; Socket deallocate; synchronized (this.connectionPool) { realConnection = null; if (iOException instanceof StreamResetException) { ErrorCode errorCode = ((StreamResetException) iOException).errorCode; if (errorCode == ErrorCode.REFUSED_STREAM) { int i = this.refusedStreamCount + 1; this.refusedStreamCount = i; if (i > 1) { this.route = null; } z = false; RealConnection realConnection2 = this.connection; deallocate = deallocate(z, false, true); if (this.connection == null) { if (this.reportedAcquired) { realConnection = realConnection2; } } } else { if (errorCode != ErrorCode.CANCEL) { this.route = null; } z = false; RealConnection realConnection22 = this.connection; deallocate = deallocate(z, false, true); if (this.connection == null) { } } } else { RealConnection realConnection3 = this.connection; if (realConnection3 != null && (!realConnection3.isMultiplexed() || (iOException instanceof ConnectionShutdownException))) { if (this.connection.successCount == 0) { Route route2 = this.route; if (!(route2 == null || iOException == null)) { this.routeSelector.connectFailed(route2, iOException); } this.route = null; } } z = false; RealConnection realConnection222 = this.connection; deallocate = deallocate(z, false, true); if (this.connection == null) { } } z = true; RealConnection realConnection2222 = this.connection; deallocate = deallocate(z, false, true); if (this.connection == null) { } } Util.closeQuietly(deallocate); if (realConnection != null) { this.eventListener.connectionReleased(this.call, realConnection); } } public void acquire(RealConnection realConnection, boolean z) { if (this.connection == null) { this.connection = realConnection; this.reportedAcquired = z; realConnection.allocations.add(new StreamAllocationReference(this, this.callStackTrace)); return; } throw new IllegalStateException(); } private void release(RealConnection realConnection) { int size = realConnection.allocations.size(); for (int i = 0; i < size; i++) { if (realConnection.allocations.get(i).get() == this) { realConnection.allocations.remove(i); return; } } throw new IllegalStateException(); } public Socket releaseAndAcquire(RealConnection realConnection) { if (this.codec == null && this.connection.allocations.size() == 1) { Socket deallocate = deallocate(true, false, false); this.connection = realConnection; realConnection.allocations.add(this.connection.allocations.get(0)); return deallocate; } throw new IllegalStateException(); } public boolean hasMoreRoutes() { RouteSelector.Selection selection; return this.route != null || ((selection = this.routeSelection) != null && selection.hasNext()) || this.routeSelector.hasNext(); } public String toString() { RealConnection connection2 = connection(); return connection2 != null ? connection2.toString() : this.address.toString(); } public static final class StreamAllocationReference extends WeakReference<StreamAllocation> { public final Object callStackTrace; StreamAllocationReference(StreamAllocation streamAllocation, Object obj) { super(streamAllocation); this.callStackTrace = obj; } } }