package g.i0.f;

import g.g0;
import g.h;
import g.i0.f.f;
import g.m;
import g.n;
import g.s;
import g.w;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;

/* JADX INFO: loaded from: classes.dex */
public final class g {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final g.a f6181a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public f.a f6182b;

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final h f6184d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final g.d f6185e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final n f6186f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final Object f6187g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final f f6188h;

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public c f6190j;
    public boolean k;
    public boolean l;
    public boolean m;
    public g.i0.g.c n;

    public static final class a extends WeakReference<g> {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public final Object f6191a;

        public a(g gVar, Object obj) {
            super(gVar);
            this.f6191a = obj;
        }
    }

    public g(h hVar, g.a aVar, g.d dVar, n nVar, Object obj) {
        this.f6184d = hVar;
        this.f6181a = aVar;
        this.f6185e = dVar;
        this.f6186f = nVar;
        Objects.requireNonNull((w.a) g.i0.a.f6138a);
        this.f6188h = new f(aVar, hVar.f6123f, dVar, nVar);
        this.f6187g = obj;
    }

    public void a(c cVar, boolean z) {
        if (this.f6190j != null) {
            throw new IllegalStateException();
        }
        this.f6190j = cVar;
        this.k = z;
        cVar.n.add(new a(this, this.f6187g));
    }

    public synchronized c b() {
        return this.f6190j;
    }

    /* JADX WARN: Removed duplicated region for block: B:33:0x0075  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final java.net.Socket c(boolean r5, boolean r6, boolean r7) {
        /*
            r4 = this;
            r0 = 0
            if (r7 == 0) goto L5
            r4.n = r0
        L5:
            r7 = 1
            if (r6 == 0) goto La
            r4.l = r7
        La:
            g.i0.f.c r6 = r4.f6190j
            if (r6 == 0) goto L83
            if (r5 == 0) goto L12
            r6.k = r7
        L12:
            g.i0.g.c r5 = r4.n
            if (r5 != 0) goto L83
            boolean r5 = r4.l
            if (r5 != 0) goto L1e
            boolean r5 = r6.k
            if (r5 == 0) goto L83
        L1e:
            java.util.List<java.lang.ref.Reference<g.i0.f.g>> r5 = r6.n
            int r5 = r5.size()
            r1 = 0
            r2 = r1
        L26:
            if (r2 >= r5) goto L7d
            java.util.List<java.lang.ref.Reference<g.i0.f.g>> r3 = r6.n
            java.lang.Object r3 = r3.get(r2)
            java.lang.ref.Reference r3 = (java.lang.ref.Reference) r3
            java.lang.Object r3 = r3.get()
            if (r3 != r4) goto L7a
            java.util.List<java.lang.ref.Reference<g.i0.f.g>> r5 = r6.n
            r5.remove(r2)
            g.i0.f.c r5 = r4.f6190j
            java.util.List<java.lang.ref.Reference<g.i0.f.g>> r5 = r5.n
            boolean r5 = r5.isEmpty()
            if (r5 == 0) goto L75
            g.i0.f.c r5 = r4.f6190j
            long r2 = java.lang.System.nanoTime()
            r5.o = r2
            g.i0.a r5 = g.i0.a.f6138a
            g.h r6 = r4.f6184d
            g.i0.f.c r2 = r4.f6190j
            g.w$a r5 = (g.w.a) r5
            java.util.Objects.requireNonNull(r5)
            java.util.Objects.requireNonNull(r6)
            boolean r5 = r2.k
            if (r5 != 0) goto L69
            int r5 = r6.f6119b
            if (r5 != 0) goto L64
            goto L69
        L64:
            r6.notifyAll()
            r7 = r1
            goto L6e
        L69:
            java.util.Deque<g.i0.f.c> r5 = r6.f6122e
            r5.remove(r2)
        L6e:
            if (r7 == 0) goto L75
            g.i0.f.c r5 = r4.f6190j
            java.net.Socket r5 = r5.f6162e
            goto L76
        L75:
            r5 = r0
        L76:
            r4.f6190j = r0
            r0 = r5
            goto L83
        L7a:
            int r2 = r2 + 1
            goto L26
        L7d:
            java.lang.IllegalStateException r5 = new java.lang.IllegalStateException
            r5.<init>()
            throw r5
        L83:
            return r0
        */
        throw new UnsupportedOperationException("Method not decompiled: g.i0.f.g.c(boolean, boolean, boolean):java.net.Socket");
    }

    public final c d(int i2, int i3, int i4, int i5, boolean z) throws Throwable {
        c cVar;
        g0 g0Var;
        Socket socketC;
        c cVar2;
        boolean z2;
        boolean z3;
        Socket socketB;
        f.a aVar;
        String hostName;
        int port;
        boolean zContains;
        synchronized (this.f6184d) {
            if (this.l) {
                throw new IllegalStateException("released");
            }
            if (this.n != null) {
                throw new IllegalStateException("codec != null");
            }
            if (this.m) {
                throw new IOException("Canceled");
            }
            cVar = this.f6190j;
            g0Var = null;
            socketC = (cVar == null || !cVar.k) ? null : c(false, false, true);
            cVar2 = this.f6190j;
            if (cVar2 != null) {
                cVar = null;
            } else {
                cVar2 = null;
            }
            if (!this.k) {
                cVar = null;
            }
            if (cVar2 == null) {
                g.i0.a.f6138a.c(this.f6184d, this.f6181a, this, null);
                c cVar3 = this.f6190j;
                if (cVar3 != null) {
                    cVar2 = cVar3;
                    z2 = true;
                } else {
                    g0Var = this.f6183c;
                    z2 = false;
                }
            } else {
                z2 = false;
            }
        }
        g.i0.c.e(socketC);
        if (cVar != null) {
            Objects.requireNonNull(this.f6186f);
        }
        if (z2) {
            Objects.requireNonNull(this.f6186f);
        }
        if (cVar2 != null) {
            return cVar2;
        }
        if (g0Var != null || ((aVar = this.f6182b) != null && aVar.a())) {
            z3 = false;
        } else {
            f fVar = this.f6188h;
            if (!fVar.b()) {
                throw new NoSuchElementException();
            }
            ArrayList arrayList = new ArrayList();
            while (fVar.c()) {
                if (!fVar.c()) {
                    StringBuilder sbC = a.b.c.a.a.C("No route to ");
                    sbC.append(fVar.f6171a.f6048a.f6443e);
                    sbC.append("; exhausted proxy configurations: ");
                    sbC.append(fVar.f6175e);
                    throw new SocketException(sbC.toString());
                }
                List<Proxy> list = fVar.f6175e;
                int i6 = fVar.f6176f;
                fVar.f6176f = i6 + 1;
                Proxy proxy = list.get(i6);
                fVar.f6177g = new ArrayList();
                if (proxy.type() == Proxy.Type.DIRECT || proxy.type() == Proxy.Type.SOCKS) {
                    s sVar = fVar.f6171a.f6048a;
                    hostName = sVar.f6443e;
                    port = sVar.f6444f;
                } else {
                    SocketAddress socketAddressAddress = proxy.address();
                    if (!(socketAddressAddress instanceof InetSocketAddress)) {
                        StringBuilder sbC2 = a.b.c.a.a.C("Proxy.address() is not an InetSocketAddress: ");
                        sbC2.append(socketAddressAddress.getClass());
                        throw new IllegalArgumentException(sbC2.toString());
                    }
                    InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddressAddress;
                    InetAddress address = inetSocketAddress.getAddress();
                    hostName = address == null ? inetSocketAddress.getHostName() : address.getHostAddress();
                    port = inetSocketAddress.getPort();
                }
                if (port < 1 || port > 65535) {
                    throw new SocketException("No route to " + hostName + ":" + port + "; port is out of range");
                }
                if (proxy.type() == Proxy.Type.SOCKS) {
                    fVar.f6177g.add(InetSocketAddress.createUnresolved(hostName, port));
                } else {
                    Objects.requireNonNull(fVar.f6174d);
                    Objects.requireNonNull((m.a) fVar.f6171a.f6049b);
                    if (hostName == null) {
                        throw new UnknownHostException("hostname == null");
                    }
                    try {
                        List listAsList = Arrays.asList(InetAddress.getAllByName(hostName));
                        if (listAsList.isEmpty()) {
                            throw new UnknownHostException(fVar.f6171a.f6049b + " returned no addresses for " + hostName);
                        }
                        Objects.requireNonNull(fVar.f6174d);
                        int size = listAsList.size();
                        for (int i7 = 0; i7 < size; i7++) {
                            fVar.f6177g.add(new InetSocketAddress((InetAddress) listAsList.get(i7), port));
                        }
                    } catch (NullPointerException e2) {
                        UnknownHostException unknownHostException = new UnknownHostException(a.b.c.a.a.p("Broken system behaviour for dns lookup of ", hostName));
                        unknownHostException.initCause(e2);
                        throw unknownHostException;
                    }
                }
                int size2 = fVar.f6177g.size();
                for (int i8 = 0; i8 < size2; i8++) {
                    g0 g0Var2 = new g0(fVar.f6171a, proxy, fVar.f6177g.get(i8));
                    d dVar = fVar.f6172b;
                    synchronized (dVar) {
                        zContains = dVar.f6168a.contains(g0Var2);
                    }
                    if (zContains) {
                        fVar.f6178h.add(g0Var2);
                    } else {
                        arrayList.add(g0Var2);
                    }
                }
                if (!arrayList.isEmpty()) {
                    break;
                }
            }
            if (arrayList.isEmpty()) {
                arrayList.addAll(fVar.f6178h);
                fVar.f6178h.clear();
            }
            this.f6182b = new f.a(arrayList);
            z3 = true;
        }
        synchronized (this.f6184d) {
            if (this.m) {
                throw new IOException("Canceled");
            }
            if (z3) {
                f.a aVar2 = this.f6182b;
                Objects.requireNonNull(aVar2);
                ArrayList arrayList2 = new ArrayList(aVar2.f6179a);
                int size3 = arrayList2.size();
                int i9 = 0;
                while (true) {
                    if (i9 >= size3) {
                        break;
                    }
                    g0 g0Var3 = (g0) arrayList2.get(i9);
                    g.i0.a.f6138a.c(this.f6184d, this.f6181a, this, g0Var3);
                    c cVar4 = this.f6190j;
                    if (cVar4 != null) {
                        this.f6183c = g0Var3;
                        z2 = true;
                        cVar2 = cVar4;
                        break;
                    }
                    i9++;
                }
            }
            if (!z2) {
                if (g0Var == null) {
                    f.a aVar3 = this.f6182b;
                    if (!aVar3.a()) {
                        throw new NoSuchElementException();
                    }
                    List<g0> list2 = aVar3.f6179a;
                    int i10 = aVar3.f6180b;
                    aVar3.f6180b = i10 + 1;
                    g0Var = list2.get(i10);
                }
                this.f6183c = g0Var;
                this.f6189i = 0;
                cVar2 = new c(this.f6184d, g0Var);
                a(cVar2, false);
            }
        }
        if (!z2) {
            cVar2.c(i2, i3, i4, i5, z, this.f6185e, this.f6186f);
            g.i0.a aVar4 = g.i0.a.f6138a;
            h hVar = this.f6184d;
            Objects.requireNonNull((w.a) aVar4);
            hVar.f6123f.a(cVar2.f6160c);
            synchronized (this.f6184d) {
                this.k = true;
                g.i0.a aVar5 = g.i0.a.f6138a;
                h hVar2 = this.f6184d;
                Objects.requireNonNull((w.a) aVar5);
                if (!hVar2.f6124g) {
                    hVar2.f6124g = true;
                    h.f6118a.execute(hVar2.f6121d);
                }
                hVar2.f6122e.add(cVar2);
                if (cVar2.h()) {
                    socketB = g.i0.a.f6138a.b(this.f6184d, this.f6181a, this);
                    cVar2 = this.f6190j;
                } else {
                    socketB = null;
                }
            }
            g.i0.c.e(socketB);
        }
        Objects.requireNonNull(this.f6186f);
        return cVar2;
    }

    public final c e(int i2, int i3, int i4, int i5, boolean z, boolean z2) throws Throwable {
        int soTimeout;
        boolean z3;
        while (true) {
            c cVarD = d(i2, i3, i4, i5, z);
            synchronized (this.f6184d) {
                if (cVarD.l == 0) {
                    return cVarD;
                }
                boolean z4 = false;
                if (!cVarD.f6162e.isClosed() && !cVarD.f6162e.isInputShutdown() && !cVarD.f6162e.isOutputShutdown()) {
                    g.i0.i.g gVar = cVarD.f6165h;
                    if (gVar != null) {
                        synchronized (gVar) {
                            z3 = gVar.f6289i;
                        }
                        z4 = !z3;
                    } else if (z2) {
                        try {
                            soTimeout = cVarD.f6162e.getSoTimeout();
                            try {
                                cVarD.f6162e.setSoTimeout(1);
                            } catch (Throwable th) {
                                cVarD.f6162e.setSoTimeout(soTimeout);
                                throw th;
                            }
                        } catch (SocketTimeoutException unused) {
                        } catch (IOException unused2) {
                        }
                        if (cVarD.f6166i.q()) {
                            cVarD.f6162e.setSoTimeout(soTimeout);
                        } else {
                            cVarD.f6162e.setSoTimeout(soTimeout);
                            z4 = true;
                        }
                    } else {
                        z4 = true;
                    }
                }
                if (z4) {
                    return cVarD;
                }
                f();
            }
        }
    }

    public void f() {
        c cVar;
        Socket socketC;
        synchronized (this.f6184d) {
            cVar = this.f6190j;
            socketC = c(true, false, false);
            if (this.f6190j != null) {
                cVar = null;
            }
        }
        g.i0.c.e(socketC);
        if (cVar != null) {
            Objects.requireNonNull(this.f6186f);
        }
    }

    public void g() {
        c cVar;
        Socket socketC;
        synchronized (this.f6184d) {
            cVar = this.f6190j;
            socketC = c(false, true, false);
            if (this.f6190j != null) {
                cVar = null;
            }
        }
        g.i0.c.e(socketC);
        if (cVar != null) {
            g.i0.a.f6138a.d(this.f6185e, null);
            Objects.requireNonNull(this.f6186f);
            Objects.requireNonNull(this.f6186f);
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:12:0x001c, code lost:
    
        if (r7 != g.i0.i.b.CANCEL) goto L13;
     */
    /* JADX WARN: Removed duplicated region for block: B:30:0x004e A[Catch: all -> 0x0060, TryCatch #0 {, blocks: (B:4:0x0003, B:6:0x000a, B:8:0x0012, B:13:0x001e, B:28:0x0044, B:30:0x004e, B:34:0x0054, B:11:0x001a, B:14:0x0021, B:16:0x0025, B:18:0x002b, B:20:0x002f, B:22:0x0035, B:25:0x003b), top: B:42:0x0003 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public void h(java.io.IOException r7) {
        /*
            r6 = this;
            g.h r0 = r6.f6184d
            monitor-enter(r0)
            boolean r1 = r7 instanceof g.i0.i.u     // Catch: java.lang.Throwable -> L60
            r2 = 0
            r3 = 0
            r4 = 1
            if (r1 == 0) goto L21
            g.i0.i.u r7 = (g.i0.i.u) r7     // Catch: java.lang.Throwable -> L60
            g.i0.i.b r7 = r7.f6378b     // Catch: java.lang.Throwable -> L60
            g.i0.i.b r1 = g.i0.i.b.REFUSED_STREAM     // Catch: java.lang.Throwable -> L60
            if (r7 != r1) goto L1a
            int r7 = r6.f6189i     // Catch: java.lang.Throwable -> L60
            int r7 = r7 + r4
            r6.f6189i = r7     // Catch: java.lang.Throwable -> L60
            if (r7 <= r4) goto L43
            goto L1e
        L1a:
            g.i0.i.b r1 = g.i0.i.b.CANCEL     // Catch: java.lang.Throwable -> L60
            if (r7 == r1) goto L43
        L1e:
            r6.f6183c = r3     // Catch: java.lang.Throwable -> L60
            goto L41
        L21:
            g.i0.f.c r1 = r6.f6190j     // Catch: java.lang.Throwable -> L60
            if (r1 == 0) goto L43
            boolean r1 = r1.h()     // Catch: java.lang.Throwable -> L60
            if (r1 == 0) goto L2f
            boolean r1 = r7 instanceof g.i0.i.a     // Catch: java.lang.Throwable -> L60
            if (r1 == 0) goto L43
        L2f:
            g.i0.f.c r1 = r6.f6190j     // Catch: java.lang.Throwable -> L60
            int r1 = r1.l     // Catch: java.lang.Throwable -> L60
            if (r1 != 0) goto L41
            g.g0 r1 = r6.f6183c     // Catch: java.lang.Throwable -> L60
            if (r1 == 0) goto L1e
            if (r7 == 0) goto L1e
            g.i0.f.f r5 = r6.f6188h     // Catch: java.lang.Throwable -> L60
            r5.a(r1, r7)     // Catch: java.lang.Throwable -> L60
            goto L1e
        L41:
            r7 = r4
            goto L44
        L43:
            r7 = r2
        L44:
            g.i0.f.c r1 = r6.f6190j     // Catch: java.lang.Throwable -> L60
            java.net.Socket r7 = r6.c(r7, r2, r4)     // Catch: java.lang.Throwable -> L60
            g.i0.f.c r2 = r6.f6190j     // Catch: java.lang.Throwable -> L60
            if (r2 != 0) goto L54
            boolean r2 = r6.k     // Catch: java.lang.Throwable -> L60
            if (r2 != 0) goto L53
            goto L54
        L53:
            r3 = r1
        L54:
            monitor-exit(r0)     // Catch: java.lang.Throwable -> L60
            g.i0.c.e(r7)
            if (r3 == 0) goto L5f
            g.n r7 = r6.f6186f
            java.util.Objects.requireNonNull(r7)
        L5f:
            return
        L60:
            r7 = move-exception
            monitor-exit(r0)     // Catch: java.lang.Throwable -> L60
            throw r7
        */
        throw new UnsupportedOperationException("Method not decompiled: g.i0.f.g.h(java.io.IOException):void");
    }

    public void i(boolean z, g.i0.g.c cVar, long j2, IOException iOException) {
        c cVar2;
        Socket socketC;
        boolean z2;
        Objects.requireNonNull(this.f6186f);
        synchronized (this.f6184d) {
            if (cVar != null) {
                if (cVar == this.n) {
                    if (!z) {
                        this.f6190j.l++;
                    }
                    cVar2 = this.f6190j;
                    socketC = c(z, false, true);
                    if (this.f6190j != null) {
                        cVar2 = null;
                    }
                    z2 = this.l;
                }
            }
            throw new IllegalStateException("expected " + this.n + " but was " + cVar);
        }
        g.i0.c.e(socketC);
        if (cVar2 != null) {
            Objects.requireNonNull(this.f6186f);
        }
        if (iOException != null) {
            g.i0.a.f6138a.d(this.f6185e, iOException);
            Objects.requireNonNull(this.f6186f);
        } else if (z2) {
            g.i0.a.f6138a.d(this.f6185e, null);
            Objects.requireNonNull(this.f6186f);
        }
    }

    public String toString() {
        c cVarB = b();
        return cVarB != null ? cVarB.toString() : this.f6181a.toString();
    }
}
