package p447.p448.p452;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ProtocolException;
import java.net.Proxy;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.net.UnknownServiceException;
import java.security.cert.CertificateException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLPeerUnverifiedException;
import okhttp3.internal.connection.RouteException;
import okhttp3.internal.http2.ConnectionShutdownException;
import okhttp3.internal.http2.StreamResetException;
import p138.p139.p143.p144.C2803;
import p138.p176.p177.p242.p246.p251.C4303;
import p402.p411.p413.C6635;
import p447.AbstractC6894;
import p447.C6860;
import p447.C6881;
import p447.C6908;
import p447.C6938;
import p447.EnumC6924;
import p447.InterfaceC6868;
import p447.p448.C6943;
import p447.p448.p452.C6977;
import p447.p448.p455.C7011;
import p447.p448.p455.EnumC7003;
import p447.p448.p457.C7069;
import p461.InterfaceC7107;

/* JADX INFO: renamed from: ސ.ޣ.ބ.ށ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes.dex */
public final class C6963 {

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public C6977.C6979 f20250;

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public C6977 f20251;

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public C6968 f20252;

    /* JADX INFO: renamed from: ށ, reason: contains not printable characters */
    public int f20253;

    /* JADX INFO: renamed from: ނ, reason: contains not printable characters */
    public int f20254;

    /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
    public int f20255;

    /* JADX INFO: renamed from: ބ, reason: contains not printable characters */
    public C6938 f20256;

    /* JADX INFO: renamed from: ޅ, reason: contains not printable characters */
    public final C6973 f20257;

    /* JADX INFO: renamed from: ކ, reason: contains not printable characters */
    public final C6860 f20258;

    /* JADX INFO: renamed from: އ, reason: contains not printable characters */
    public final C6964 f20259;

    /* JADX INFO: renamed from: ވ, reason: contains not printable characters */
    public final AbstractC6894 f20260;

    public C6963(C6973 c6973, C6860 c6860, C6964 c6964, AbstractC6894 abstractC6894) {
        if (c6973 == null) {
            C6635.m9824("connectionPool");
            throw null;
        }
        if (c6860 == null) {
            C6635.m9824("address");
            throw null;
        }
        if (c6964 == null) {
            C6635.m9824("call");
            throw null;
        }
        if (abstractC6894 == null) {
            C6635.m9824("eventListener");
            throw null;
        }
        this.f20257 = c6973;
        this.f20258 = c6860;
        this.f20259 = c6964;
        this.f20260 = abstractC6894;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final C6968 m10208() {
        C6973 c6973 = this.f20257;
        if (!C6943.f20196 || Thread.holdsLock(c6973)) {
            return this.f20252;
        }
        StringBuilder sbM4889 = C2803.m4889("Thread ");
        Thread threadCurrentThread = Thread.currentThread();
        C6635.m9823((Object) threadCurrentThread, "Thread.currentThread()");
        sbM4889.append(threadCurrentThread.getName());
        sbM4889.append(" MUST hold lock on ");
        sbM4889.append(c6973);
        throw new AssertionError(sbM4889.toString());
    }

    /* JADX WARN: Removed duplicated region for block: B:247:0x03f9  */
    /* JADX WARN: Removed duplicated region for block: B:250:0x0400  */
    /* JADX WARN: Removed duplicated region for block: B:253:0x041c  */
    /* JADX WARN: Removed duplicated region for block: B:27:0x0061  */
    /* JADX WARN: Removed duplicated region for block: B:307:0x03ae A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:340:0x0465 A[SYNTHETIC] */
    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final C6968 m10209(int i, int i2, int i3, int i4, boolean z) throws Throwable {
        Socket socketM10223;
        C6968 c6968;
        C6968 c69682;
        C6938 c6938;
        boolean z2;
        boolean z3;
        List<C6938> list;
        C6968 c69683;
        Socket socket;
        Socket socket2;
        InetSocketAddress inetSocketAddress;
        Socket socketM10238;
        C6977.C6979 c6979;
        String hostName;
        int port;
        synchronized (this.f20257) {
            if (this.f20259.mo10027()) {
                throw new IOException("Canceled");
            }
            C6968 c69684 = this.f20259.f20266;
            socketM10223 = (c69684 == null || (!c69684.f20290 && m10212(c69684.f20299.f20176.f19900))) ? null : this.f20259.m10223();
            if (this.f20259.f20266 != null) {
                c69682 = this.f20259.f20266;
                c6968 = null;
            } else {
                c6968 = c69684;
                c69682 = null;
            }
            if (c69682 == null) {
                this.f20253 = 0;
                this.f20254 = 0;
                this.f20255 = 0;
                if (this.f20257.m10242(this.f20258, this.f20259, null, false)) {
                    c69682 = this.f20259.f20266;
                    c6938 = null;
                    z2 = true;
                } else {
                    if (this.f20256 != null) {
                        c6938 = this.f20256;
                        this.f20256 = null;
                    } else {
                        c6938 = null;
                    }
                    z2 = false;
                }
            }
        }
        if (socketM10223 != null) {
            C6943.m10165(socketM10223);
        }
        if (c6968 != null) {
            AbstractC6894 abstractC6894 = this.f20260;
            C6964 c6964 = this.f20259;
            if (c6968 == null) {
                C6635.m9822();
                throw null;
            }
            if (abstractC6894 == null) {
                throw null;
            }
            if (c6964 == null) {
                C6635.m9824("call");
                throw null;
            }
        }
        if (z2) {
            AbstractC6894 abstractC68942 = this.f20260;
            C6964 c69642 = this.f20259;
            if (c69682 == null) {
                C6635.m9822();
                throw null;
            }
            abstractC68942.m10060(c69642, c69682);
        }
        if (c69682 != null) {
            return c69682;
        }
        if (c6938 != null || ((c6979 = this.f20250) != null && c6979.m10248())) {
            z3 = false;
        } else {
            C6977 c6977 = this.f20251;
            if (c6977 == null) {
                C6860 c6860 = this.f20258;
                C6964 c69643 = this.f20259;
                c6977 = new C6977(c6860, c69643.f20275.f20087, c69643, this.f20260);
                this.f20251 = c6977;
            }
            if (!c6977.m10246()) {
                throw new NoSuchElementException();
            }
            ArrayList arrayList = new ArrayList();
            while (c6977.m10247()) {
                if (!c6977.m10247()) {
                    StringBuilder sbM4889 = C2803.m4889("No route to ");
                    sbM4889.append(c6977.f20317.f19900.f20017);
                    sbM4889.append("; exhausted proxy configurations: ");
                    sbM4889.append(c6977.f20313);
                    throw new SocketException(sbM4889.toString());
                }
                List<? extends Proxy> list2 = c6977.f20313;
                int i5 = c6977.f20314;
                c6977.f20314 = i5 + 1;
                Proxy proxy = list2.get(i5);
                ArrayList arrayList2 = new ArrayList();
                c6977.f20315 = arrayList2;
                if (proxy.type() == Proxy.Type.DIRECT || proxy.type() == Proxy.Type.SOCKS) {
                    C6908 c6908 = c6977.f20317.f19900;
                    hostName = c6908.f20017;
                    port = c6908.f20018;
                } else {
                    SocketAddress socketAddressAddress = proxy.address();
                    if (!(socketAddressAddress instanceof InetSocketAddress)) {
                        StringBuilder sbM48892 = C2803.m4889("Proxy.address() is not an InetSocketAddress: ");
                        sbM48892.append(socketAddressAddress.getClass());
                        throw new IllegalArgumentException(sbM48892.toString().toString());
                    }
                    InetSocketAddress inetSocketAddress2 = (InetSocketAddress) socketAddressAddress;
                    if (C6977.f20312 == null) {
                        throw null;
                    }
                    if (inetSocketAddress2 == null) {
                        C6635.m9824("$this$socketHost");
                        throw null;
                    }
                    InetAddress address = inetSocketAddress2.getAddress();
                    if (address != null) {
                        hostName = address.getHostAddress();
                        C6635.m9823((Object) hostName, "address.hostAddress");
                    } else {
                        hostName = inetSocketAddress2.getHostName();
                        C6635.m9823((Object) hostName, "hostName");
                    }
                    port = inetSocketAddress2.getPort();
                }
                if (1 > port || 65535 < port) {
                    throw new SocketException("No route to " + hostName + ':' + port + "; port is out of range");
                }
                if (proxy.type() == Proxy.Type.SOCKS) {
                    arrayList2.add(InetSocketAddress.createUnresolved(hostName, port));
                } else {
                    AbstractC6894 abstractC68943 = c6977.f20320;
                    InterfaceC6868 interfaceC6868 = c6977.f20319;
                    if (abstractC68943 == null) {
                        throw null;
                    }
                    if (interfaceC6868 == null) {
                        C6635.m9824("call");
                        throw null;
                    }
                    if (hostName == null) {
                        C6635.m9824("domainName");
                        throw null;
                    }
                    List<InetAddress> listMo10057 = c6977.f20317.f19903.mo10057(hostName);
                    if (listMo10057.isEmpty()) {
                        throw new UnknownHostException(c6977.f20317.f19903 + " returned no addresses for " + hostName);
                    }
                    AbstractC6894 abstractC68944 = c6977.f20320;
                    InterfaceC6868 interfaceC68682 = c6977.f20319;
                    if (abstractC68944 == null) {
                        throw null;
                    }
                    if (interfaceC68682 == null) {
                        C6635.m9824("call");
                        throw null;
                    }
                    Iterator<InetAddress> it = listMo10057.iterator();
                    while (it.hasNext()) {
                        arrayList2.add(new InetSocketAddress(it.next(), port));
                    }
                }
                Iterator<? extends InetSocketAddress> it2 = c6977.f20315.iterator();
                while (it2.hasNext()) {
                    C6938 c69382 = new C6938(c6977.f20317, proxy, it2.next());
                    if (c6977.f20318.m10245(c69382)) {
                        c6977.f20316.add(c69382);
                    } else {
                        arrayList.add(c69382);
                    }
                }
                if (!arrayList.isEmpty()) {
                    break;
                }
            }
            if (arrayList.isEmpty()) {
                C4303.m7131((Collection) arrayList, (Iterable) c6977.f20316);
                c6977.f20316.clear();
            }
            this.f20250 = new C6977.C6979(arrayList);
            z3 = true;
        }
        synchronized (this.f20257) {
            if (this.f20259.mo10027()) {
                throw new IOException("Canceled");
            }
            if (z3) {
                C6977.C6979 c69792 = this.f20250;
                if (c69792 == null) {
                    C6635.m9822();
                    throw null;
                }
                list = c69792.f20322;
                if (this.f20257.m10242(this.f20258, this.f20259, list, false)) {
                    c69682 = this.f20259.f20266;
                    z2 = true;
                }
            } else {
                list = null;
            }
            if (!z2) {
                if (c6938 == null) {
                    C6977.C6979 c69793 = this.f20250;
                    if (c69793 == null) {
                        C6635.m9822();
                        throw null;
                    }
                    if (!c69793.m10248()) {
                        throw new NoSuchElementException();
                    }
                    List<C6938> list3 = c69793.f20322;
                    int i6 = c69793.f20321;
                    c69793.f20321 = i6 + 1;
                    c6938 = list3.get(i6);
                }
                C6973 c6973 = this.f20257;
                if (c6938 == null) {
                    C6635.m9822();
                    throw null;
                }
                c69682 = new C6968(c6973, c6938);
                this.f20252 = c69682;
            }
            c69683 = c69682;
        }
        if (z2) {
            AbstractC6894 abstractC68945 = this.f20260;
            C6964 c69644 = this.f20259;
            if (c69683 != null) {
                abstractC68945.m10060(c69644, c69683);
                return c69683;
            }
            C6635.m9822();
            throw null;
        }
        if (c69683 == null) {
            C6635.m9822();
            throw null;
        }
        C6964 c69645 = this.f20259;
        AbstractC6894 abstractC68946 = this.f20260;
        if (c69645 == null) {
            C6635.m9824("call");
            throw null;
        }
        if (abstractC68946 == null) {
            C6635.m9824("eventListener");
            throw null;
        }
        if (!(c69683.f20286 == null)) {
            throw new IllegalStateException("already connected".toString());
        }
        List<C6881> list4 = c69683.f20299.f20176.f19902;
        C6959 c6959 = new C6959(list4);
        C6860 c68602 = c69683.f20299.f20176;
        if (c68602.f19905 == null) {
            if (!list4.contains(C6881.f19963)) {
                throw new RouteException(new UnknownServiceException("CLEARTEXT communication not enabled for client"));
            }
            String str = c69683.f20299.f20176.f19900.f20017;
            if (C7069.f20588 == null) {
                throw null;
            }
            if (!C7069.f20586.mo10369(str)) {
                throw new RouteException(new UnknownServiceException(C2803.m4880("CLEARTEXT communication to ", str, " not permitted by network security policy")));
            }
        } else if (c68602.f19901.contains(EnumC6924.H2_PRIOR_KNOWLEDGE)) {
            throw new RouteException(new UnknownServiceException("H2_PRIOR_KNOWLEDGE cannot be used with HTTPS"));
        }
        RouteException routeException = null;
        do {
            try {
            } catch (IOException e) {
                e = e;
            }
            try {
                if (c69683.f20299.m10139()) {
                    c69683.m10228(i, i2, i3, c69645, abstractC68946);
                    if (c69683.f20283 == null) {
                        if (!c69683.f20299.m10139() && c69683.f20283 == null) {
                            throw new RouteException(new ProtocolException("Too many tunnel connections attempted: 21"));
                        }
                        c69683.f20297 = System.nanoTime();
                        this.f20259.f20275.f20087.m10243(c69683.f20299);
                        synchronized (this.f20257) {
                            this.f20252 = null;
                            if (this.f20257.m10242(this.f20258, this.f20259, list, true)) {
                                c69683.f20290 = true;
                                socketM10238 = c69683.m10238();
                                c69683 = this.f20259.f20266;
                                this.f20256 = c6938;
                            } else {
                                this.f20257.m10241(c69683);
                                this.f20259.m10219(c69683);
                                socketM10238 = null;
                            }
                        }
                        if (socketM10238 != null) {
                            C6943.m10165(socketM10238);
                        }
                        AbstractC6894 abstractC68947 = this.f20260;
                        C6964 c69646 = this.f20259;
                        if (c69683 != null) {
                            abstractC68947.m10060(c69646, c69683);
                            return c69683;
                        }
                        C6635.m9822();
                        throw null;
                    }
                } else {
                    try {
                        c69683.m10229(i, i2, c69645, abstractC68946);
                    } catch (IOException e2) {
                        e = e2;
                        socket = c69683.f20284;
                        if (socket != null) {
                            C6943.m10165(socket);
                        }
                        socket2 = c69683.f20283;
                        if (socket2 != null) {
                            C6943.m10165(socket2);
                        }
                        c69683.f20284 = null;
                        c69683.f20283 = null;
                        c69683.f20288 = null;
                        c69683.f20289 = null;
                        c69683.f20285 = null;
                        c69683.f20286 = null;
                        c69683.f20287 = null;
                        c69683.f20295 = 1;
                        C6938 c69383 = c69683.f20299;
                        inetSocketAddress = c69383.f20178;
                        Proxy proxy2 = c69383.f20177;
                        if (inetSocketAddress != null) {
                            C6635.m9824("inetSocketAddress");
                            throw null;
                        }
                        if (proxy2 == null) {
                            C6635.m9824("proxy");
                            throw null;
                        }
                        if (routeException == null) {
                            routeException = new RouteException(e);
                        } else {
                            routeException.f2194.addSuppressed(e);
                            routeException.f2193 = e;
                        }
                        if (!z) {
                            throw routeException;
                        }
                        c6959.f20231 = true;
                    }
                }
                c69683.m10231(c6959, i4, c69645, abstractC68946);
                abstractC68946.m10059(c69645, c69683.f20299.f20178, c69683.f20299.f20177);
                if (!c69683.f20299.m10139()) {
                }
                c69683.f20297 = System.nanoTime();
                this.f20259.f20275.f20087.m10243(c69683.f20299);
                synchronized (this.f20257) {
                }
            } catch (IOException e3) {
                e = e3;
                socket = c69683.f20284;
                if (socket != null) {
                }
                socket2 = c69683.f20283;
                if (socket2 != null) {
                }
                c69683.f20284 = null;
                c69683.f20283 = null;
                c69683.f20288 = null;
                c69683.f20289 = null;
                c69683.f20285 = null;
                c69683.f20286 = null;
                c69683.f20287 = null;
                c69683.f20295 = 1;
                C6938 c693832 = c69683.f20299;
                inetSocketAddress = c693832.f20178;
                Proxy proxy22 = c693832.f20177;
                if (inetSocketAddress != null) {
                }
            }
        } while ((!c6959.f20230 || (e instanceof ProtocolException) || (e instanceof InterruptedIOException) || ((e instanceof SSLHandshakeException) && (e.getCause() instanceof CertificateException)) || (e instanceof SSLPeerUnverifiedException) || !(e instanceof SSLException)) ? false : true);
        throw routeException;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final C6968 m10210(int i, int i2, int i3, int i4, boolean z, boolean z2) throws Throwable {
        boolean zM10308;
        while (true) {
            C6968 c6968M10209 = m10209(i, i2, i3, i4, z);
            if (c6968M10209 == null) {
                throw null;
            }
            long jNanoTime = System.nanoTime();
            Socket socket = c6968M10209.f20283;
            if (socket == null) {
                C6635.m9822();
                throw null;
            }
            Socket socket2 = c6968M10209.f20284;
            if (socket2 == null) {
                C6635.m9822();
                throw null;
            }
            InterfaceC7107 interfaceC7107 = c6968M10209.f20288;
            if (interfaceC7107 == null) {
                C6635.m9822();
                throw null;
            }
            if (socket.isClosed() || socket2.isClosed() || socket2.isInputShutdown() || socket2.isOutputShutdown()) {
                zM10308 = false;
            } else {
                C7011 c7011 = c6968M10209.f20287;
                zM10308 = c7011 != null ? c7011.m10308(jNanoTime) : (jNanoTime - c6968M10209.f20297 < 10000000000L || !z2) ? true : C6943.m10168(socket2, interfaceC7107);
            }
            if (zM10308) {
                return c6968M10209;
            }
            c6968M10209.m10237();
            synchronized (this.f20257) {
                if (this.f20256 == null) {
                    C6977.C6979 c6979 = this.f20250;
                    if (!(c6979 != null ? c6979.m10248() : true)) {
                        C6977 c6977 = this.f20251;
                        if (!(c6977 != null ? c6977.m10246() : true)) {
                            throw new IOException("exhausted all routes");
                        }
                    }
                }
            }
        }
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final void m10211(IOException iOException) {
        if (iOException == null) {
            C6635.m9824("e");
            throw null;
        }
        C6973 c6973 = this.f20257;
        if (C6943.f20196 && Thread.holdsLock(c6973)) {
            StringBuilder sbM4889 = C2803.m4889("Thread ");
            Thread threadCurrentThread = Thread.currentThread();
            C6635.m9823((Object) threadCurrentThread, "Thread.currentThread()");
            sbM4889.append(threadCurrentThread.getName());
            sbM4889.append(" MUST NOT hold lock on ");
            sbM4889.append(c6973);
            throw new AssertionError(sbM4889.toString());
        }
        synchronized (this.f20257) {
            this.f20256 = null;
            if ((iOException instanceof StreamResetException) && ((StreamResetException) iOException).f2195 == EnumC7003.REFUSED_STREAM) {
                this.f20253++;
            } else if (iOException instanceof ConnectionShutdownException) {
                this.f20254++;
            } else {
                this.f20255++;
            }
        }
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final boolean m10212(C6908 c6908) {
        if (c6908 != null) {
            C6908 c69082 = this.f20258.f19900;
            return c6908.f20018 == c69082.f20018 && C6635.m9825((Object) c6908.f20017, (Object) c69082.f20017);
        }
        C6635.m9824("url");
        throw null;
    }

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public final boolean m10213() {
        C6968 c6968;
        synchronized (this.f20257) {
            boolean z = false;
            if (this.f20253 == 0 && this.f20254 == 0 && this.f20255 == 0) {
                return false;
            }
            if (this.f20256 != null) {
                return true;
            }
            if (this.f20253 <= 1 && this.f20254 <= 1 && this.f20255 <= 0 && (c6968 = this.f20259.f20266) != null && c6968.f20292 == 0 && C6943.m10169(c6968.f20299.f20176.f19900, this.f20258.f19900)) {
                z = true;
            }
            if (z) {
                C6968 c69682 = this.f20259.f20266;
                if (c69682 != null) {
                    this.f20256 = c69682.f20299;
                    return true;
                }
                C6635.m9822();
                throw null;
            }
            C6977.C6979 c6979 = this.f20250;
            if (c6979 != null && c6979.m10248()) {
                return true;
            }
            C6977 c6977 = this.f20251;
            if (c6977 != null) {
                return c6977.m10246();
            }
            return true;
        }
    }
}
