package okhttp3.n0.connection;

import io.netty.handler.codec.rtsp.RtspHeaders;
import io.netty.handler.ssl.ApplicationProtocolNames;
import java.io.IOException;
import java.lang.ref.Reference;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.net.SocketException;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import kotlin.Metadata;
import kotlin.jvm.internal.o;
import kotlin.reflect.w.internal.y0.n.c2.c;
import kotlin.text.j;
import kotlinx.serialization.json.internal.AbstractJsonLexerKt;
import okhttp3.Address;
import okhttp3.Call;
import okhttp3.CertificatePinner;
import okhttp3.Connection;
import okhttp3.ConnectionSpec;
import okhttp3.EventListener;
import okhttp3.Handshake;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.Route;
import okhttp3.n0.concurrent.TaskRunner;
import okhttp3.n0.concurrent.b;
import okhttp3.n0.http.ExchangeCodec;
import okhttp3.n0.http.RealInterceptorChain;
import okhttp3.n0.http1.Http1ExchangeCodec;
import okhttp3.n0.http2.ErrorCode;
import okhttp3.n0.http2.Http2;
import okhttp3.n0.http2.Http2Connection;
import okhttp3.n0.http2.Http2ExchangeCodec;
import okhttp3.n0.http2.Http2Stream;
import okhttp3.n0.http2.Http2Writer;
import okhttp3.n0.http2.Settings;
import okhttp3.n0.platform.Platform;
import okhttp3.n0.tls.OkHostnameVerifier;
import okio.BufferedSink;
import okio.BufferedSource;
import okio.Source;
import okio.Timeout;

/* JADX INFO: renamed from: y.n0.g.f, reason: from Kotlin metadata */
/* JADX INFO: loaded from: classes2.dex */
@Metadata(d1 = {"\u0000ì\u0001\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\b\n\u0000\n\u0002\u0010!\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0005\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\t\n\u0002\b\u0005\n\u0002\u0010\u000b\n\u0002\b\b\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0007\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0007\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0007\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010 \n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0006\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0005\n\u0002\u0010\u000e\n\u0002\b\u0005\u0018\u0000 {2\u00020\u00012\u00020\u0002:\u0001{B\u0015\u0012\u0006\u0010\u0003\u001a\u00020\u0004\u0012\u0006\u0010\u0005\u001a\u00020\u0006¢\u0006\u0002\u0010\u0007J\u0006\u00105\u001a\u000206J\u0018\u00107\u001a\u00020\u001d2\u0006\u00108\u001a\u0002092\u0006\u0010\u0012\u001a\u00020\u0013H\u0002J>\u0010:\u001a\u0002062\u0006\u0010;\u001a\u00020\t2\u0006\u0010<\u001a\u00020\t2\u0006\u0010=\u001a\u00020\t2\u0006\u0010>\u001a\u00020\t2\u0006\u0010?\u001a\u00020\u001d2\u0006\u0010@\u001a\u00020A2\u0006\u0010B\u001a\u00020CJ%\u0010D\u001a\u0002062\u0006\u0010E\u001a\u00020F2\u0006\u0010G\u001a\u00020\u00062\u0006\u0010H\u001a\u00020IH\u0000¢\u0006\u0002\bJJ(\u0010K\u001a\u0002062\u0006\u0010;\u001a\u00020\t2\u0006\u0010<\u001a\u00020\t2\u0006\u0010@\u001a\u00020A2\u0006\u0010B\u001a\u00020CH\u0002J\u0010\u0010L\u001a\u0002062\u0006\u0010M\u001a\u00020NH\u0002J0\u0010O\u001a\u0002062\u0006\u0010;\u001a\u00020\t2\u0006\u0010<\u001a\u00020\t2\u0006\u0010=\u001a\u00020\t2\u0006\u0010@\u001a\u00020A2\u0006\u0010B\u001a\u00020CH\u0002J*\u0010P\u001a\u0004\u0018\u00010Q2\u0006\u0010<\u001a\u00020\t2\u0006\u0010=\u001a\u00020\t2\u0006\u0010R\u001a\u00020Q2\u0006\u00108\u001a\u000209H\u0002J\b\u0010S\u001a\u00020QH\u0002J(\u0010T\u001a\u0002062\u0006\u0010M\u001a\u00020N2\u0006\u0010>\u001a\u00020\t2\u0006\u0010@\u001a\u00020A2\u0006\u0010B\u001a\u00020CH\u0002J\n\u0010\u0012\u001a\u0004\u0018\u00010\u0013H\u0016J\r\u0010U\u001a\u000206H\u0000¢\u0006\u0002\bVJ%\u0010W\u001a\u00020\u001d2\u0006\u0010X\u001a\u00020Y2\u000e\u0010Z\u001a\n\u0012\u0004\u0012\u00020\u0006\u0018\u00010[H\u0000¢\u0006\u0002\b\\J\u000e\u0010]\u001a\u00020\u001d2\u0006\u0010^\u001a\u00020\u001dJ\u001d\u0010_\u001a\u00020`2\u0006\u0010E\u001a\u00020F2\u0006\u0010a\u001a\u00020bH\u0000¢\u0006\u0002\bcJ\u0015\u0010d\u001a\u00020e2\u0006\u0010f\u001a\u00020gH\u0000¢\u0006\u0002\bhJ\r\u0010 \u001a\u000206H\u0000¢\u0006\u0002\biJ\r\u0010!\u001a\u000206H\u0000¢\u0006\u0002\bjJ\u0018\u0010k\u001a\u0002062\u0006\u0010l\u001a\u00020\u00152\u0006\u0010m\u001a\u00020nH\u0016J\u0010\u0010o\u001a\u0002062\u0006\u0010p\u001a\u00020qH\u0016J\b\u0010%\u001a\u00020&H\u0016J\b\u0010\u0005\u001a\u00020\u0006H\u0016J\u0016\u0010r\u001a\u00020\u001d2\f\u0010s\u001a\b\u0012\u0004\u0012\u00020\u00060[H\u0002J\b\u00101\u001a\u00020(H\u0016J\u0010\u0010t\u001a\u0002062\u0006\u0010>\u001a\u00020\tH\u0002J\u0010\u0010u\u001a\u00020\u001d2\u0006\u00108\u001a\u000209H\u0002J\b\u0010v\u001a\u00020wH\u0016J\u001f\u0010x\u001a\u0002062\u0006\u0010@\u001a\u00020\r2\b\u0010y\u001a\u0004\u0018\u00010IH\u0000¢\u0006\u0002\bzR\u000e\u0010\b\u001a\u00020\tX\u0082\u000e¢\u0006\u0002\n\u0000R\u001d\u0010\n\u001a\u000e\u0012\n\u0012\b\u0012\u0004\u0012\u00020\r0\f0\u000b¢\u0006\b\n\u0000\u001a\u0004\b\u000e\u0010\u000fR\u0011\u0010\u0003\u001a\u00020\u0004¢\u0006\b\n\u0000\u001a\u0004\b\u0010\u0010\u0011R\u0010\u0010\u0012\u001a\u0004\u0018\u00010\u0013X\u0082\u000e¢\u0006\u0002\n\u0000R\u0010\u0010\u0014\u001a\u0004\u0018\u00010\u0015X\u0082\u000e¢\u0006\u0002\n\u0000R\u001a\u0010\u0016\u001a\u00020\u0017X\u0080\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u0018\u0010\u0019\"\u0004\b\u001a\u0010\u001bR\u0014\u0010\u001c\u001a\u00020\u001d8@X\u0080\u0004¢\u0006\u0006\u001a\u0004\b\u001e\u0010\u001fR\u000e\u0010 \u001a\u00020\u001dX\u0082\u000e¢\u0006\u0002\n\u0000R\u001a\u0010!\u001a\u00020\u001dX\u0086\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\"\u0010\u001f\"\u0004\b#\u0010$R\u0010\u0010%\u001a\u0004\u0018\u00010&X\u0082\u000e¢\u0006\u0002\n\u0000R\u0010\u0010'\u001a\u0004\u0018\u00010(X\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010)\u001a\u00020\tX\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0005\u001a\u00020\u0006X\u0082\u0004¢\u0006\u0002\n\u0000R\u001a\u0010*\u001a\u00020\tX\u0080\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b+\u0010,\"\u0004\b-\u0010.R\u0010\u0010/\u001a\u0004\u0018\u000100X\u0082\u000e¢\u0006\u0002\n\u0000R\u0010\u00101\u001a\u0004\u0018\u00010(X\u0082\u000e¢\u0006\u0002\n\u0000R\u0010\u00102\u001a\u0004\u0018\u000103X\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u00104\u001a\u00020\tX\u0082\u000e¢\u0006\u0002\n\u0000¨\u0006|"}, d2 = {"Lokhttp3/internal/connection/RealConnection;", "Lokhttp3/internal/http2/Http2Connection$Listener;", "Lokhttp3/Connection;", "connectionPool", "Lokhttp3/internal/connection/RealConnectionPool;", "route", "Lokhttp3/Route;", "(Lokhttp3/internal/connection/RealConnectionPool;Lokhttp3/Route;)V", "allocationLimit", "", "calls", "", "Ljava/lang/ref/Reference;", "Lokhttp3/internal/connection/RealCall;", "getCalls", "()Ljava/util/List;", "getConnectionPool", "()Lokhttp3/internal/connection/RealConnectionPool;", "handshake", "Lokhttp3/Handshake;", "http2Connection", "Lokhttp3/internal/http2/Http2Connection;", "idleAtNs", "", "getIdleAtNs$okhttp", "()J", "setIdleAtNs$okhttp", "(J)V", "isMultiplexed", "", "isMultiplexed$okhttp", "()Z", "noCoalescedConnections", "noNewExchanges", "getNoNewExchanges", "setNoNewExchanges", "(Z)V", "protocol", "Lokhttp3/Protocol;", "rawSocket", "Ljava/net/Socket;", "refusedStreamCount", "routeFailureCount", "getRouteFailureCount$okhttp", "()I", "setRouteFailureCount$okhttp", "(I)V", "sink", "Lokio/BufferedSink;", "socket", "source", "Lokio/BufferedSource;", "successCount", "cancel", "", "certificateSupportHost", RtspHeaders.Values.URL, "Lokhttp3/HttpUrl;", "connect", "connectTimeout", "readTimeout", "writeTimeout", "pingIntervalMillis", "connectionRetryEnabled", "call", "Lokhttp3/Call;", "eventListener", "Lokhttp3/EventListener;", "connectFailed", "client", "Lokhttp3/OkHttpClient;", "failedRoute", "failure", "Ljava/io/IOException;", "connectFailed$okhttp", "connectSocket", "connectTls", "connectionSpecSelector", "Lokhttp3/internal/connection/ConnectionSpecSelector;", "connectTunnel", "createTunnel", "Lokhttp3/Request;", "tunnelRequest", "createTunnelRequest", "establishProtocol", "incrementSuccessCount", "incrementSuccessCount$okhttp", "isEligible", "address", "Lokhttp3/Address;", "routes", "", "isEligible$okhttp", "isHealthy", "doExtensiveChecks", "newCodec", "Lokhttp3/internal/http/ExchangeCodec;", "chain", "Lokhttp3/internal/http/RealInterceptorChain;", "newCodec$okhttp", "newWebSocketStreams", "Lokhttp3/internal/ws/RealWebSocket$Streams;", "exchange", "Lokhttp3/internal/connection/Exchange;", "newWebSocketStreams$okhttp", "noCoalescedConnections$okhttp", "noNewExchanges$okhttp", "onSettings", "connection", "settings", "Lokhttp3/internal/http2/Settings;", "onStream", "stream", "Lokhttp3/internal/http2/Http2Stream;", "routeMatchesAny", "candidates", "startHttp2", "supportsUrl", "toString", "", "trackFailure", "e", "trackFailure$okhttp", "Companion", "okhttp"}, k = 1, mv = {1, 6, 0}, xi = 48)
public final class RealConnection extends Http2Connection.b implements Connection {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Route f9788b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public Socket f9789c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public Socket f9790d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public Handshake f9791e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public Protocol f9792f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public Http2Connection f9793g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public BufferedSource f9794h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public BufferedSink f9795i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public boolean f9796j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public boolean f9797k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public int f9798l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public int f9799m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public int f9800n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public int f9801o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public final List<Reference<RealCall>> f9802p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public long f9803q;

    /* JADX INFO: renamed from: y.n0.g.f$a */
    @Metadata(k = 3, mv = {1, 6, 0}, xi = 48)
    public /* synthetic */ class a {
        public static final /* synthetic */ int[] a;

        static {
            int[] iArr = new int[Proxy.Type.values().length];
            iArr[Proxy.Type.DIRECT.ordinal()] = 1;
            iArr[Proxy.Type.HTTP.ordinal()] = 2;
            a = iArr;
        }
    }

    public RealConnection(RealConnectionPool realConnectionPool, Route route) {
        o.f(realConnectionPool, "connectionPool");
        o.f(route, "route");
        this.f9788b = route;
        this.f9801o = 1;
        this.f9802p = new ArrayList();
        this.f9803q = Long.MAX_VALUE;
    }

    @Override // okhttp3.n0.http2.Http2Connection.b
    public synchronized void a(Http2Connection http2Connection, Settings settings) {
        o.f(http2Connection, "connection");
        o.f(settings, "settings");
        this.f9801o = (settings.a & 16) != 0 ? settings.f10021b[4] : Integer.MAX_VALUE;
    }

    @Override // okhttp3.n0.http2.Http2Connection.b
    public void b(Http2Stream http2Stream) {
        o.f(http2Stream, "stream");
        http2Stream.c(ErrorCode.REFUSED_STREAM, null);
    }

    /* JADX WARN: Removed duplicated region for block: B:50:0x00e6  */
    /* JADX WARN: Removed duplicated region for block: B:54:0x00ee  */
    /* JADX WARN: Removed duplicated region for block: B:57:0x011a  */
    /* JADX WARN: Removed duplicated region for block: B:58:0x0120  */
    /* JADX WARN: Removed duplicated region for block: B:60:0x012c  */
    /* JADX WARN: Removed duplicated region for block: B:97:0x015d A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final void c(int r17, int r18, int r19, int r20, boolean r21, okhttp3.Call r22, okhttp3.EventListener r23) throws java.lang.Throwable {
        /*
            Method dump skipped, instruction units count: 375
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: okhttp3.n0.connection.RealConnection.c(int, int, int, int, boolean, y.f, y.u):void");
    }

    public final void d(OkHttpClient okHttpClient, Route route, IOException iOException) {
        o.f(okHttpClient, "client");
        o.f(route, "failedRoute");
        o.f(iOException, "failure");
        if (route.f9690b.type() != Proxy.Type.DIRECT) {
            Address address = route.a;
            address.f9540h.connectFailed(address.f9541i.g(), route.f9690b.address(), iOException);
        }
        RouteDatabase routeDatabase = okHttpClient.M;
        synchronized (routeDatabase) {
            o.f(route, "failedRoute");
            routeDatabase.a.add(route);
        }
    }

    public final void e(int i2, int i3, Call call, EventListener eventListener) throws IOException {
        Socket socketCreateSocket;
        Route route = this.f9788b;
        Proxy proxy = route.f9690b;
        Address address = route.a;
        Proxy.Type type = proxy.type();
        int i4 = type == null ? -1 : a.a[type.ordinal()];
        if (i4 == 1 || i4 == 2) {
            socketCreateSocket = address.f9534b.createSocket();
            o.c(socketCreateSocket);
        } else {
            socketCreateSocket = new Socket(proxy);
        }
        this.f9789c = socketCreateSocket;
        InetSocketAddress inetSocketAddress = this.f9788b.f9691c;
        Objects.requireNonNull(eventListener);
        o.f(call, "call");
        o.f(inetSocketAddress, "inetSocketAddress");
        o.f(proxy, "proxy");
        socketCreateSocket.setSoTimeout(i3);
        try {
            Platform.a aVar = Platform.a;
            Platform.f10049b.e(socketCreateSocket, this.f9788b.f9691c, i2);
            try {
                this.f9794h = c.m(c.C0(socketCreateSocket));
                this.f9795i = c.l(c.B0(socketCreateSocket));
            } catch (NullPointerException e2) {
                if (o.a(e2.getMessage(), "throw with null exception")) {
                    throw new IOException(e2);
                }
            }
        } catch (ConnectException e3) {
            ConnectException connectException = new ConnectException(o.l("Failed to connect to ", this.f9788b.f9691c));
            connectException.initCause(e3);
            throw connectException;
        }
    }

    public final void f(int i2, int i3, int i4, Call call, EventListener eventListener) throws IOException {
        int i5;
        Request.a aVar = new Request.a();
        aVar.g(this.f9788b.a.f9541i);
        aVar.c("CONNECT", null);
        boolean z2 = true;
        aVar.b("Host", okhttp3.n0.c.y(this.f9788b.a.f9541i, true));
        aVar.b("Proxy-Connection", "Keep-Alive");
        aVar.b("User-Agent", "okhttp/4.11.0");
        Request requestA = aVar.a();
        ArrayList arrayList = new ArrayList(20);
        o.f(requestA, "request");
        Protocol protocol = Protocol.HTTP_1_1;
        o.f(protocol, "protocol");
        o.f("Preemptive Authenticate", "message");
        ResponseBody responseBody = okhttp3.n0.c.f9713c;
        o.f("Proxy-Authenticate", "name");
        o.f("OkHttp-Preemptive", "value");
        o.f("Proxy-Authenticate", "name");
        o.f("OkHttp-Preemptive", "value");
        Headers.b bVar = Headers.f10085f;
        bVar.a("Proxy-Authenticate");
        bVar.b("OkHttp-Preemptive", "Proxy-Authenticate");
        o.f("Proxy-Authenticate", "name");
        int i6 = 0;
        while (i6 < arrayList.size()) {
            if (j.g("Proxy-Authenticate", (String) arrayList.get(i6), true)) {
                arrayList.remove(i6);
                arrayList.remove(i6);
                i6 -= 2;
            }
            i6 += 2;
        }
        o.f("Proxy-Authenticate", "name");
        o.f("OkHttp-Preemptive", "value");
        arrayList.add("Proxy-Authenticate");
        arrayList.add(j.Y("OkHttp-Preemptive").toString());
        int i7 = 0;
        Object[] array = arrayList.toArray(new String[0]);
        Objects.requireNonNull(array, "null cannot be cast to non-null type kotlin.Array<T of kotlin.collections.ArraysKt__ArraysJVMKt.toTypedArray>");
        Response response = new Response(requestA, protocol, "Preemptive Authenticate", 407, null, new Headers((String[]) array, null), responseBody, null, null, null, -1L, -1L, null);
        Route route = this.f9788b;
        Request requestA2 = route.a.f9538f.a(route, response);
        if (requestA2 != null) {
            requestA = requestA2;
        }
        HttpUrl httpUrl = requestA.a;
        while (i7 < 21) {
            int i8 = i7 + 1;
            e(i2, i3, call, eventListener);
            String str = "CONNECT " + okhttp3.n0.c.y(httpUrl, z2) + " HTTP/1.1";
            while (true) {
                BufferedSource bufferedSource = this.f9794h;
                o.c(bufferedSource);
                BufferedSink bufferedSink = this.f9795i;
                o.c(bufferedSink);
                Http1ExchangeCodec http1ExchangeCodec = new Http1ExchangeCodec(null, this, bufferedSource, bufferedSink);
                TimeUnit timeUnit = TimeUnit.MILLISECONDS;
                bufferedSource.getF10138m().g(i3, timeUnit);
                i5 = i8;
                bufferedSink.getF10143m().g(i4, timeUnit);
                http1ExchangeCodec.k(requestA.f9622c, str);
                http1ExchangeCodec.f9842d.flush();
                Response.a aVarD = http1ExchangeCodec.d(false);
                o.c(aVarD);
                aVarD.h(requestA);
                Response responseB = aVarD.b();
                o.f(responseB, "response");
                long jL = okhttp3.n0.c.l(responseB);
                if (jL != -1) {
                    Source sourceJ = http1ExchangeCodec.j(jL);
                    okhttp3.n0.c.w(sourceJ, Integer.MAX_VALUE, timeUnit);
                    ((Http1ExchangeCodec.d) sourceJ).close();
                }
                int i9 = responseB.f9644o;
                if (i9 != 200) {
                    if (i9 != 407) {
                        throw new IOException(o.l("Unexpected response code for CONNECT: ", Integer.valueOf(responseB.f9644o)));
                    }
                    Route route2 = this.f9788b;
                    Request requestA3 = route2.a.f9538f.a(route2, responseB);
                    if (requestA3 == null) {
                        throw new IOException("Failed to authenticate with proxy");
                    }
                    if (j.g("close", Response.a(responseB, "Connection", null, 2), true)) {
                        requestA = requestA3;
                        z2 = true;
                        break;
                    } else {
                        requestA = requestA3;
                        i8 = i5;
                    }
                } else {
                    if (!bufferedSource.getBuffer().f() || !bufferedSink.getBuffer().f()) {
                        throw new IOException("TLS tunnel buffered too many bytes!");
                    }
                    z2 = true;
                    requestA = null;
                }
            }
            if (requestA == null) {
                return;
            }
            Socket socket = this.f9789c;
            if (socket != null) {
                okhttp3.n0.c.f(socket);
            }
            this.f9789c = null;
            this.f9795i = null;
            this.f9794h = null;
            Route route3 = this.f9788b;
            InetSocketAddress inetSocketAddress = route3.f9691c;
            Proxy proxy = route3.f9690b;
            o.f(call, "call");
            o.f(inetSocketAddress, "inetSocketAddress");
            o.f(proxy, "proxy");
            i7 = i5;
        }
    }

    public final void g(ConnectionSpecSelector connectionSpecSelector, int i2, Call call, EventListener eventListener) throws Throwable {
        SSLSocket sSLSocket;
        String strF;
        Protocol protocol;
        Protocol protocol2 = Protocol.HTTP_2;
        Protocol protocol3 = Protocol.H2_PRIOR_KNOWLEDGE;
        Protocol protocol4 = Protocol.HTTP_1_1;
        Address address = this.f9788b.a;
        if (address.f9535c == null) {
            if (!address.f9542j.contains(protocol3)) {
                this.f9790d = this.f9789c;
                this.f9792f = protocol4;
                return;
            } else {
                this.f9790d = this.f9789c;
                this.f9792f = protocol3;
                m(i2);
                return;
            }
        }
        o.f(call, "call");
        Address address2 = this.f9788b.a;
        SSLSocketFactory sSLSocketFactory = address2.f9535c;
        try {
            o.c(sSLSocketFactory);
            Socket socket = this.f9789c;
            HttpUrl httpUrl = address2.f9541i;
            Socket socketCreateSocket = sSLSocketFactory.createSocket(socket, httpUrl.f10091f, httpUrl.f10092g, true);
            if (socketCreateSocket == null) {
                throw new NullPointerException("null cannot be cast to non-null type javax.net.ssl.SSLSocket");
            }
            sSLSocket = (SSLSocket) socketCreateSocket;
            try {
                ConnectionSpec connectionSpecA = connectionSpecSelector.a(sSLSocket);
                if (connectionSpecA.f9704f) {
                    Platform.a aVar = Platform.a;
                    Platform.f10049b.d(sSLSocket, address2.f9541i.f10091f, address2.f9542j);
                }
                sSLSocket.startHandshake();
                SSLSession session = sSLSocket.getSession();
                o.e(session, "sslSocketSession");
                Handshake handshakeA = Handshake.a(session);
                HostnameVerifier hostnameVerifier = address2.f9536d;
                o.c(hostnameVerifier);
                if (!hostnameVerifier.verify(address2.f9541i.f10091f, session)) {
                    List<Certificate> listC = handshakeA.c();
                    if (!(!listC.isEmpty())) {
                        throw new SSLPeerUnverifiedException("Hostname " + address2.f9541i.f10091f + " not verified (no certificates)");
                    }
                    X509Certificate x509Certificate = (X509Certificate) listC.get(0);
                    StringBuilder sb = new StringBuilder();
                    sb.append("\n              |Hostname ");
                    sb.append(address2.f9541i.f10091f);
                    sb.append(" not verified:\n              |    certificate: ");
                    sb.append(CertificatePinner.a.a(x509Certificate));
                    sb.append("\n              |    DN: ");
                    sb.append((Object) x509Certificate.getSubjectDN().getName());
                    sb.append("\n              |    subjectAltNames: ");
                    OkHostnameVerifier okHostnameVerifier = OkHostnameVerifier.a;
                    o.f(x509Certificate, "certificate");
                    sb.append(kotlin.collections.j.P(okHostnameVerifier.b(x509Certificate, 7), okHostnameVerifier.b(x509Certificate, 2)));
                    sb.append("\n              ");
                    throw new SSLPeerUnverifiedException(j.a0(sb.toString(), null, 1));
                }
                CertificatePinner certificatePinner = address2.f9537e;
                o.c(certificatePinner);
                this.f9791e = new Handshake(handshakeA.a, handshakeA.f10081b, handshakeA.f10082c, new g(certificatePinner, handshakeA, address2));
                certificatePinner.a(address2.f9541i.f10091f, new h(this));
                if (connectionSpecA.f9704f) {
                    Platform.a aVar2 = Platform.a;
                    strF = Platform.f10049b.f(sSLSocket);
                } else {
                    strF = null;
                }
                this.f9790d = sSLSocket;
                this.f9794h = c.m(c.C0(sSLSocket));
                this.f9795i = c.l(c.B0(sSLSocket));
                if (strF != null) {
                    o.f(strF, "protocol");
                    Protocol protocol5 = Protocol.HTTP_1_0;
                    if (o.a(strF, "http/1.0")) {
                        protocol = protocol5;
                    } else if (!o.a(strF, ApplicationProtocolNames.HTTP_1_1)) {
                        if (o.a(strF, "h2_prior_knowledge")) {
                            protocol = protocol3;
                        } else if (o.a(strF, ApplicationProtocolNames.HTTP_2)) {
                            protocol = protocol2;
                        } else {
                            Protocol protocol6 = Protocol.SPDY_3;
                            if (!o.a(strF, ApplicationProtocolNames.SPDY_3_1)) {
                                protocol6 = Protocol.QUIC;
                                if (!o.a(strF, "quic")) {
                                    throw new IOException(o.l("Unexpected protocol: ", strF));
                                }
                            }
                            protocol = protocol6;
                        }
                    }
                    protocol4 = protocol;
                }
                this.f9792f = protocol4;
                Platform.a aVar3 = Platform.a;
                Platform.f10049b.a(sSLSocket);
                o.f(call, "call");
                if (this.f9792f == protocol2) {
                    m(i2);
                }
            } catch (Throwable th) {
                th = th;
                if (sSLSocket != null) {
                    Platform.a aVar4 = Platform.a;
                    Platform.f10049b.a(sSLSocket);
                }
                if (sSLSocket != null) {
                    okhttp3.n0.c.f(sSLSocket);
                }
                throw th;
            }
        } catch (Throwable th2) {
            th = th2;
            sSLSocket = null;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:36:0x0083  */
    /* JADX WARN: Removed duplicated region for block: B:59:0x00d4 A[RETURN] */
    /* JADX WARN: Removed duplicated region for block: B:64:0x00d5 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final boolean h(okhttp3.Address r7, java.util.List<okhttp3.Route> r8) {
        /*
            Method dump skipped, instruction units count: 251
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: okhttp3.n0.connection.RealConnection.h(y.a, java.util.List):boolean");
    }

    /* JADX WARN: Code restructure failed: missing block: B:22:0x0047, code lost:
    
        if (r0 >= r2.E) goto L16;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final boolean i(boolean r10) {
        /*
            r9 = this;
            byte[] r0 = okhttp3.n0.c.a
            long r0 = java.lang.System.nanoTime()
            java.net.Socket r2 = r9.f9789c
            kotlin.jvm.internal.o.c(r2)
            java.net.Socket r3 = r9.f9790d
            kotlin.jvm.internal.o.c(r3)
            z.f r4 = r9.f9794h
            kotlin.jvm.internal.o.c(r4)
            boolean r2 = r2.isClosed()
            r5 = 0
            if (r2 != 0) goto L86
            boolean r2 = r3.isClosed()
            if (r2 != 0) goto L86
            boolean r2 = r3.isInputShutdown()
            if (r2 != 0) goto L86
            boolean r2 = r3.isOutputShutdown()
            if (r2 == 0) goto L2f
            goto L86
        L2f:
            y.n0.j.f r2 = r9.f9793g
            r6 = 1
            if (r2 == 0) goto L50
            monitor-enter(r2)
            boolean r10 = r2.f9915t     // Catch: java.lang.Throwable -> L4d
            if (r10 == 0) goto L3b
        L39:
            monitor-exit(r2)
            goto L4c
        L3b:
            long r3 = r2.C     // Catch: java.lang.Throwable -> L4d
            long r7 = r2.B     // Catch: java.lang.Throwable -> L4d
            int r10 = (r3 > r7 ? 1 : (r3 == r7 ? 0 : -1))
            if (r10 >= 0) goto L4a
            long r3 = r2.E     // Catch: java.lang.Throwable -> L4d
            int r10 = (r0 > r3 ? 1 : (r0 == r3 ? 0 : -1))
            if (r10 < 0) goto L4a
            goto L39
        L4a:
            monitor-exit(r2)
            r5 = r6
        L4c:
            return r5
        L4d:
            r10 = move-exception
            monitor-exit(r2)
            throw r10
        L50:
            monitor-enter(r9)
            long r7 = r9.f9803q     // Catch: java.lang.Throwable -> L83
            long r0 = r0 - r7
            monitor-exit(r9)
            r7 = 10000000000(0x2540be400, double:4.9406564584E-314)
            int r0 = (r0 > r7 ? 1 : (r0 == r7 ? 0 : -1))
            if (r0 < 0) goto L82
            if (r10 == 0) goto L82
            java.lang.String r10 = "<this>"
            kotlin.jvm.internal.o.f(r3, r10)
            java.lang.String r10 = "source"
            kotlin.jvm.internal.o.f(r4, r10)
            int r10 = r3.getSoTimeout()     // Catch: java.net.SocketTimeoutException -> L80 java.io.IOException -> L81
            r3.setSoTimeout(r6)     // Catch: java.lang.Throwable -> L7b
            boolean r0 = r4.f()     // Catch: java.lang.Throwable -> L7b
            r0 = r0 ^ r6
            r3.setSoTimeout(r10)     // Catch: java.net.SocketTimeoutException -> L80 java.io.IOException -> L81
            r5 = r0
            goto L81
        L7b:
            r0 = move-exception
            r3.setSoTimeout(r10)     // Catch: java.net.SocketTimeoutException -> L80 java.io.IOException -> L81
            throw r0     // Catch: java.net.SocketTimeoutException -> L80 java.io.IOException -> L81
        L80:
            r5 = r6
        L81:
            return r5
        L82:
            return r6
        L83:
            r10 = move-exception
            monitor-exit(r9)
            throw r10
        L86:
            return r5
        */
        throw new UnsupportedOperationException("Method not decompiled: okhttp3.n0.connection.RealConnection.i(boolean):boolean");
    }

    public final boolean j() {
        return this.f9793g != null;
    }

    public final ExchangeCodec k(OkHttpClient okHttpClient, RealInterceptorChain realInterceptorChain) throws SocketException {
        o.f(okHttpClient, "client");
        o.f(realInterceptorChain, "chain");
        Socket socket = this.f9790d;
        o.c(socket);
        BufferedSource bufferedSource = this.f9794h;
        o.c(bufferedSource);
        BufferedSink bufferedSink = this.f9795i;
        o.c(bufferedSink);
        Http2Connection http2Connection = this.f9793g;
        if (http2Connection != null) {
            return new Http2ExchangeCodec(okHttpClient, this, realInterceptorChain, http2Connection);
        }
        socket.setSoTimeout(realInterceptorChain.f9831g);
        Timeout timeout = bufferedSource.getF10138m();
        long j2 = realInterceptorChain.f9831g;
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        timeout.g(j2, timeUnit);
        bufferedSink.getF10143m().g(realInterceptorChain.f9832h, timeUnit);
        return new Http1ExchangeCodec(okHttpClient, this, bufferedSource, bufferedSink);
    }

    public final synchronized void l() {
        this.f9796j = true;
    }

    public final void m(int i2) throws SocketException {
        String strL;
        Socket socket = this.f9790d;
        o.c(socket);
        BufferedSource bufferedSource = this.f9794h;
        o.c(bufferedSource);
        BufferedSink bufferedSink = this.f9795i;
        o.c(bufferedSink);
        socket.setSoTimeout(0);
        TaskRunner taskRunner = TaskRunner.f9730b;
        Http2Connection.a aVar = new Http2Connection.a(true, taskRunner);
        String str = this.f9788b.a.f9541i.f10091f;
        o.f(socket, "socket");
        o.f(str, "peerName");
        o.f(bufferedSource, "source");
        o.f(bufferedSink, "sink");
        o.f(socket, "<set-?>");
        aVar.f9923c = socket;
        if (aVar.a) {
            strL = okhttp3.n0.c.f9718h + ' ' + str;
        } else {
            strL = o.l("MockWebServer ", str);
        }
        o.f(strL, "<set-?>");
        aVar.f9924d = strL;
        o.f(bufferedSource, "<set-?>");
        aVar.f9925e = bufferedSource;
        o.f(bufferedSink, "<set-?>");
        aVar.f9926f = bufferedSink;
        o.f(this, "listener");
        o.f(this, "<set-?>");
        aVar.f9927g = this;
        aVar.f9929i = i2;
        Http2Connection http2Connection = new Http2Connection(aVar);
        this.f9793g = http2Connection;
        Http2Connection http2Connection2 = Http2Connection.f9907f;
        Settings settings = Http2Connection.f9908m;
        this.f9801o = (settings.a & 16) != 0 ? settings.f10021b[4] : Integer.MAX_VALUE;
        o.f(taskRunner, "taskRunner");
        Http2Writer http2Writer = http2Connection.M;
        synchronized (http2Writer) {
            if (http2Writer.f10014q) {
                throw new IOException("closed");
            }
            if (http2Writer.f10011n) {
                Logger logger = Http2Writer.f10009f;
                if (logger.isLoggable(Level.FINE)) {
                    logger.fine(okhttp3.n0.c.j(o.l(">> CONNECTION ", Http2.f9903b.h()), new Object[0]));
                }
                http2Writer.f10010m.write(Http2.f9903b);
                http2Writer.f10010m.flush();
            }
        }
        Http2Writer http2Writer2 = http2Connection.M;
        Settings settings2 = http2Connection.F;
        synchronized (http2Writer2) {
            o.f(settings2, "settings");
            if (http2Writer2.f10014q) {
                throw new IOException("closed");
            }
            http2Writer2.d(0, Integer.bitCount(settings2.a) * 6, 4, 0);
            int i3 = 0;
            while (i3 < 10) {
                int i4 = i3 + 1;
                if (((1 << i3) & settings2.a) != 0) {
                    http2Writer2.f10010m.writeShort(i3 != 4 ? i3 != 7 ? i3 : 4 : 3);
                    http2Writer2.f10010m.writeInt(settings2.f10021b[i3]);
                }
                i3 = i4;
            }
            http2Writer2.f10010m.flush();
        }
        if (http2Connection.F.a() != 65535) {
            http2Connection.M.l(0, r0 - 65535);
        }
        taskRunner.f().c(new b(http2Connection.f9912q, true, http2Connection.N), 0L);
    }

    public String toString() {
        Object obj;
        StringBuilder sbF = g.a.a.a.a.F("Connection{");
        sbF.append(this.f9788b.a.f9541i.f10091f);
        sbF.append(AbstractJsonLexerKt.COLON);
        sbF.append(this.f9788b.a.f9541i.f10092g);
        sbF.append(", proxy=");
        sbF.append(this.f9788b.f9690b);
        sbF.append(" hostAddress=");
        sbF.append(this.f9788b.f9691c);
        sbF.append(" cipherSuite=");
        Handshake handshake = this.f9791e;
        if (handshake == null || (obj = handshake.f10081b) == null) {
            obj = "none";
        }
        sbF.append(obj);
        sbF.append(" protocol=");
        sbF.append(this.f9792f);
        sbF.append(AbstractJsonLexerKt.END_OBJ);
        return sbF.toString();
    }
}
