what-the-bank/sources/okhttp3/repackaged/internal/http/StreamAllocation.java

270 lines
9.1 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package okhttp3.repackaged.internal.http;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.lang.ref.WeakReference;
import java.net.ProtocolException;
import java.net.SocketTimeoutException;
import java.security.cert.CertificateException;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLPeerUnverifiedException;
import o.giX;
import okhttp3.repackaged.Address;
import okhttp3.repackaged.ConnectionPool;
import okhttp3.repackaged.Route;
import okhttp3.repackaged.internal.Internal;
import okhttp3.repackaged.internal.RouteDatabase;
import okhttp3.repackaged.internal.Util;
import okhttp3.repackaged.internal.io.RealConnection;
/* loaded from: classes6.dex */
public final class StreamAllocation {
public final Address address;
private final ConnectionPool aio;
private boolean aiu;
private Route amD;
private RouteSelector amE;
private RealConnection amF;
private boolean amG;
private HttpStream amH;
public StreamAllocation(ConnectionPool connectionPool, Address address) {
this.aio = connectionPool;
this.address = address;
this.amE = new RouteSelector(address, vi());
}
public final HttpStream newStream(int i, int i2, int i3, boolean z, boolean z2) throws RouteException, IOException {
HttpStream http1xStream;
try {
RealConnection a = a(i, i2, i3, z, z2);
if (a.framedConnection != null) {
http1xStream = new Http2xStream(this, a.framedConnection);
} else {
a.socket().setSoTimeout(i2);
a.source.timeout().timeout(i2, TimeUnit.MILLISECONDS);
a.sink.timeout().timeout(i3, TimeUnit.MILLISECONDS);
http1xStream = new Http1xStream(this, a.source, a.sink);
}
synchronized (this.aio) {
this.amH = http1xStream;
}
return http1xStream;
} catch (IOException e) {
throw new RouteException(e);
}
}
private RealConnection a(int i, int i2, int i3, boolean z, boolean z2) throws IOException, RouteException {
while (true) {
RealConnection a = a(i, i2, i3, z);
synchronized (this.aio) {
if (a.successCount == 0) {
return a;
}
if (a.isHealthy(z2)) {
return a;
}
connectionFailed(new IOException());
}
}
}
private RealConnection a(int i, int i2, int i3, boolean z) throws IOException, RouteException {
synchronized (this.aio) {
if (this.amG) {
throw new IllegalStateException("released");
}
if (this.amH != null) {
throw new IllegalStateException("stream != null");
}
if (this.aiu) {
throw new IOException("Canceled");
}
RealConnection realConnection = this.amF;
if (realConnection != null && !realConnection.noNewStreams) {
return realConnection;
}
RealConnection realConnection2 = Internal.instance.get(this.aio, this.address, this);
if (realConnection2 != null) {
this.amF = realConnection2;
return realConnection2;
}
Route route = this.amD;
if (route == null) {
route = this.amE.next();
synchronized (this.aio) {
this.amD = route;
}
}
RealConnection realConnection3 = new RealConnection(route);
acquire(realConnection3);
synchronized (this.aio) {
Internal.instance.put(this.aio, realConnection3);
this.amF = realConnection3;
if (this.aiu) {
throw new IOException("Canceled");
}
}
realConnection3.connect(i, i2, i3, this.address.connectionSpecs(), z);
vi().connected(realConnection3.route());
return realConnection3;
}
}
public final void streamFinished(boolean z, HttpStream httpStream) {
synchronized (this.aio) {
if (httpStream != null) {
if (httpStream == this.amH) {
if (!z) {
this.amF.successCount++;
}
}
}
StringBuilder sb = new StringBuilder("expected ");
sb.append(this.amH);
sb.append(" but was ");
sb.append(httpStream);
throw new IllegalStateException(sb.toString());
}
a(z, false, true);
}
public final HttpStream stream() {
HttpStream httpStream;
synchronized (this.aio) {
httpStream = this.amH;
}
return httpStream;
}
private RouteDatabase vi() {
return Internal.instance.routeDatabase(this.aio);
}
public final RealConnection connection() {
RealConnection realConnection;
synchronized (this) {
realConnection = this.amF;
}
return realConnection;
}
public final void release() {
a(false, true, false);
}
public final void noNewStreams() {
a(true, false, false);
}
private void a(boolean z, boolean z2, boolean z3) {
RealConnection realConnection;
RealConnection realConnection2;
synchronized (this.aio) {
realConnection = null;
if (z3) {
try {
this.amH = null;
} catch (Throwable th) {
throw th;
}
}
if (z2) {
this.amG = true;
}
RealConnection realConnection3 = this.amF;
if (realConnection3 != null) {
if (z) {
realConnection3.noNewStreams = true;
}
if (this.amH == null && (this.amG || this.amF.noNewStreams)) {
c(this.amF);
if (this.amF.allocations.isEmpty()) {
this.amF.idleAtNanos = System.nanoTime();
if (Internal.instance.connectionBecameIdle(this.aio, this.amF)) {
realConnection2 = this.amF;
this.amF = null;
realConnection = realConnection2;
}
}
realConnection2 = null;
this.amF = null;
realConnection = realConnection2;
}
}
}
if (realConnection != null) {
Util.closeQuietly(realConnection.socket());
}
}
public final void cancel() {
HttpStream httpStream;
RealConnection realConnection;
synchronized (this.aio) {
this.aiu = true;
httpStream = this.amH;
realConnection = this.amF;
}
if (httpStream != null) {
httpStream.cancel();
} else if (realConnection != null) {
realConnection.cancel();
}
}
public final void connectionFailed(IOException iOException) {
synchronized (this.aio) {
RealConnection realConnection = this.amF;
if (realConnection != null && realConnection.successCount == 0) {
Route route = this.amD;
if (route != null && iOException != null) {
this.amE.connectFailed(route, iOException);
}
this.amD = null;
}
}
a(true, false, true);
}
public final void acquire(RealConnection realConnection) {
realConnection.allocations.add(new WeakReference(this));
}
private void c(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 final boolean recover(IOException iOException, giX gix) {
if (this.amF != null) {
connectionFailed(iOException);
}
boolean z = gix == null || (gix instanceof RetryableSink);
RouteSelector routeSelector = this.amE;
return (routeSelector == null || routeSelector.hasNext()) && a(iOException) && z;
}
private boolean a(IOException iOException) {
if (iOException instanceof ProtocolException) {
return false;
}
if (iOException instanceof InterruptedIOException) {
return iOException instanceof SocketTimeoutException;
}
return (((iOException instanceof SSLHandshakeException) && (iOException.getCause() instanceof CertificateException)) || (iOException instanceof SSLPeerUnverifiedException)) ? false : true;
}
public final String toString() {
return this.address.toString();
}
}