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 f6195a;

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

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public c f6204j;
    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 f6205a;

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

    public g(h hVar, g.a aVar, g.d dVar, n nVar, Object obj) {
        this.f6198d = hVar;
        this.f6195a = aVar;
        this.f6199e = dVar;
        this.f6200f = nVar;
        Objects.requireNonNull((w.a) g.i0.a.f6152a);
        this.f6202h = new f(aVar, hVar.f6137f, dVar, nVar);
        this.f6201g = obj;
    }

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

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

    /* 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.f6204j
            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.f6204j
            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.f6204j
            long r2 = java.lang.System.nanoTime()
            r5.o = r2
            g.i0.a r5 = g.i0.a.f6152a
            g.h r6 = r4.f6198d
            g.i0.f.c r2 = r4.f6204j
            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.f6133b
            if (r5 != 0) goto L64
            goto L69
        L64:
            r6.notifyAll()
            r7 = r1
            goto L6e
        L69:
            java.util.Deque<g.i0.f.c> r5 = r6.f6136e
            r5.remove(r2)
        L6e:
            if (r7 == 0) goto L75
            g.i0.f.c r5 = r4.f6204j
            java.net.Socket r5 = r5.f6176e
            goto L76
        L75:
            r5 = r0
        L76:
            r4.f6204j = 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.f6198d) {
            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.f6204j;
            g0Var = null;
            socketC = (cVar == null || !cVar.k) ? null : c(false, false, true);
            cVar2 = this.f6204j;
            if (cVar2 != null) {
                cVar = null;
            } else {
                cVar2 = null;
            }
            if (!this.k) {
                cVar = null;
            }
            if (cVar2 == null) {
                g.i0.a.f6152a.c(this.f6198d, this.f6195a, this, null);
                c cVar3 = this.f6204j;
                if (cVar3 != null) {
                    cVar2 = cVar3;
                    z2 = true;
                } else {
                    g0Var = this.f6197c;
                    z2 = false;
                }
            } else {
                z2 = false;
            }
        }
        g.i0.c.e(socketC);
        if (cVar != null) {
            Objects.requireNonNull(this.f6200f);
        }
        if (z2) {
            Objects.requireNonNull(this.f6200f);
        }
        if (cVar2 != null) {
            return cVar2;
        }
        if (g0Var != null || ((aVar = this.f6196b) != null && aVar.a())) {
            z3 = false;
        } else {
            f fVar = this.f6202h;
            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.f6185a.f6062a.f6457e);
                    sbC.append("; exhausted proxy configurations: ");
                    sbC.append(fVar.f6189e);
                    throw new SocketException(sbC.toString());
                }
                List<Proxy> list = fVar.f6189e;
                int i6 = fVar.f6190f;
                fVar.f6190f = i6 + 1;
                Proxy proxy = list.get(i6);
                fVar.f6191g = new ArrayList();
                if (proxy.type() == Proxy.Type.DIRECT || proxy.type() == Proxy.Type.SOCKS) {
                    s sVar = fVar.f6185a.f6062a;
                    hostName = sVar.f6457e;
                    port = sVar.f6458f;
                } 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.f6191g.add(InetSocketAddress.createUnresolved(hostName, port));
                } else {
                    Objects.requireNonNull(fVar.f6188d);
                    Objects.requireNonNull((m.a) fVar.f6185a.f6063b);
                    if (hostName == null) {
                        throw new UnknownHostException("hostname == null");
                    }
                    try {
                        List listAsList = Arrays.asList(InetAddress.getAllByName(hostName));
                        if (listAsList.isEmpty()) {
                            throw new UnknownHostException(fVar.f6185a.f6063b + " returned no addresses for " + hostName);
                        }
                        Objects.requireNonNull(fVar.f6188d);
                        int size = listAsList.size();
                        for (int i7 = 0; i7 < size; i7++) {
                            fVar.f6191g.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.f6191g.size();
                for (int i8 = 0; i8 < size2; i8++) {
                    g0 g0Var2 = new g0(fVar.f6185a, proxy, fVar.f6191g.get(i8));
                    d dVar = fVar.f6186b;
                    synchronized (dVar) {
                        zContains = dVar.f6182a.contains(g0Var2);
                    }
                    if (zContains) {
                        fVar.f6192h.add(g0Var2);
                    } else {
                        arrayList.add(g0Var2);
                    }
                }
                if (!arrayList.isEmpty()) {
                    break;
                }
            }
            if (arrayList.isEmpty()) {
                arrayList.addAll(fVar.f6192h);
                fVar.f6192h.clear();
            }
            this.f6196b = new f.a(arrayList);
            z3 = true;
        }
        synchronized (this.f6198d) {
            if (this.m) {
                throw new IOException("Canceled");
            }
            if (z3) {
                f.a aVar2 = this.f6196b;
                Objects.requireNonNull(aVar2);
                ArrayList arrayList2 = new ArrayList(aVar2.f6193a);
                int size3 = arrayList2.size();
                int i9 = 0;
                while (true) {
                    if (i9 >= size3) {
                        break;
                    }
                    g0 g0Var3 = (g0) arrayList2.get(i9);
                    g.i0.a.f6152a.c(this.f6198d, this.f6195a, this, g0Var3);
                    c cVar4 = this.f6204j;
                    if (cVar4 != null) {
                        this.f6197c = g0Var3;
                        z2 = true;
                        cVar2 = cVar4;
                        break;
                    }
                    i9++;
                }
            }
            if (!z2) {
                if (g0Var == null) {
                    f.a aVar3 = this.f6196b;
                    if (!aVar3.a()) {
                        throw new NoSuchElementException();
                    }
                    List<g0> list2 = aVar3.f6193a;
                    int i10 = aVar3.f6194b;
                    aVar3.f6194b = i10 + 1;
                    g0Var = list2.get(i10);
                }
                this.f6197c = g0Var;
                this.f6203i = 0;
                cVar2 = new c(this.f6198d, g0Var);
                a(cVar2, false);
            }
        }
        if (!z2) {
            cVar2.c(i2, i3, i4, i5, z, this.f6199e, this.f6200f);
            g.i0.a aVar4 = g.i0.a.f6152a;
            h hVar = this.f6198d;
            Objects.requireNonNull((w.a) aVar4);
            hVar.f6137f.a(cVar2.f6174c);
            synchronized (this.f6198d) {
                this.k = true;
                g.i0.a aVar5 = g.i0.a.f6152a;
                h hVar2 = this.f6198d;
                Objects.requireNonNull((w.a) aVar5);
                if (!hVar2.f6138g) {
                    hVar2.f6138g = true;
                    h.f6132a.execute(hVar2.f6135d);
                }
                hVar2.f6136e.add(cVar2);
                if (cVar2.h()) {
                    socketB = g.i0.a.f6152a.b(this.f6198d, this.f6195a, this);
                    cVar2 = this.f6204j;
                } else {
                    socketB = null;
                }
            }
            g.i0.c.e(socketB);
        }
        Objects.requireNonNull(this.f6200f);
        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.f6198d) {
                if (cVarD.l == 0) {
                    return cVarD;
                }
                boolean z4 = false;
                if (!cVarD.f6176e.isClosed() && !cVarD.f6176e.isInputShutdown() && !cVarD.f6176e.isOutputShutdown()) {
                    g.i0.i.g gVar = cVarD.f6179h;
                    if (gVar != null) {
                        synchronized (gVar) {
                            z3 = gVar.f6303i;
                        }
                        z4 = !z3;
                    } else if (z2) {
                        try {
                            soTimeout = cVarD.f6176e.getSoTimeout();
                            try {
                                cVarD.f6176e.setSoTimeout(1);
                            } catch (Throwable th) {
                                cVarD.f6176e.setSoTimeout(soTimeout);
                                throw th;
                            }
                        } catch (SocketTimeoutException unused) {
                        } catch (IOException unused2) {
                        }
                        if (cVarD.f6180i.q()) {
                            cVarD.f6176e.setSoTimeout(soTimeout);
                        } else {
                            cVarD.f6176e.setSoTimeout(soTimeout);
                            z4 = true;
                        }
                    } else {
                        z4 = true;
                    }
                }
                if (z4) {
                    return cVarD;
                }
                f();
            }
        }
    }

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

    public void g() {
        c cVar;
        Socket socketC;
        synchronized (this.f6198d) {
            cVar = this.f6204j;
            socketC = c(false, true, false);
            if (this.f6204j != null) {
                cVar = null;
            }
        }
        g.i0.c.e(socketC);
        if (cVar != null) {
            g.i0.a.f6152a.d(this.f6199e, null);
            Objects.requireNonNull(this.f6200f);
            Objects.requireNonNull(this.f6200f);
        }
    }

    /* 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.f6198d
            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.f6392b     // 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.f6203i     // Catch: java.lang.Throwable -> L60
            int r7 = r7 + r4
            r6.f6203i = 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.f6197c = r3     // Catch: java.lang.Throwable -> L60
            goto L41
        L21:
            g.i0.f.c r1 = r6.f6204j     // 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.f6204j     // Catch: java.lang.Throwable -> L60
            int r1 = r1.l     // Catch: java.lang.Throwable -> L60
            if (r1 != 0) goto L41
            g.g0 r1 = r6.f6197c     // Catch: java.lang.Throwable -> L60
            if (r1 == 0) goto L1e
            if (r7 == 0) goto L1e
            g.i0.f.f r5 = r6.f6202h     // 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.f6204j     // 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.f6204j     // 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.f6200f
            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.f6200f);
        synchronized (this.f6198d) {
            if (cVar != null) {
                if (cVar == this.n) {
                    if (!z) {
                        this.f6204j.l++;
                    }
                    cVar2 = this.f6204j;
                    socketC = c(z, false, true);
                    if (this.f6204j != 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.f6200f);
        }
        if (iOException != null) {
            g.i0.a.f6152a.d(this.f6199e, iOException);
            Objects.requireNonNull(this.f6200f);
        } else if (z2) {
            g.i0.a.f6152a.d(this.f6199e, null);
            Objects.requireNonNull(this.f6200f);
        }
    }

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