package zq;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSocketFactory;
import kotlin.jvm.internal.t;
import rm.n0;
import uq.b0;
import uq.d0;
import uq.r;
import uq.v;
import uq.z;
import zq.n;

/* loaded from: classes3.dex */
public final class h implements uq.e, Cloneable {
    private zq.c A;
    private boolean B;
    private boolean C;
    private boolean D;
    private volatile boolean E;
    private volatile zq.c F;
    private final CopyOnWriteArrayList G;

    /* renamed from: p, reason: collision with root package name */
    private final z f53660p;

    /* renamed from: q, reason: collision with root package name */
    private final b0 f53661q;

    /* renamed from: r, reason: collision with root package name */
    private final boolean f53662r;

    /* renamed from: s, reason: collision with root package name */
    private final j f53663s;

    /* renamed from: t, reason: collision with root package name */
    private final r f53664t;

    /* renamed from: u, reason: collision with root package name */
    private final c f53665u;

    /* renamed from: v, reason: collision with root package name */
    private final AtomicBoolean f53666v;

    /* renamed from: w, reason: collision with root package name */
    private Object f53667w;

    /* renamed from: x, reason: collision with root package name */
    private d f53668x;

    /* renamed from: y, reason: collision with root package name */
    private i f53669y;

    /* renamed from: z, reason: collision with root package name */
    private boolean f53670z;

    /* loaded from: classes3.dex */
    public final class a implements Runnable {

        /* renamed from: p, reason: collision with root package name */
        private final uq.f f53671p;

        /* renamed from: q, reason: collision with root package name */
        private volatile AtomicInteger f53672q;

        /* renamed from: r, reason: collision with root package name */
        final /* synthetic */ h f53673r;

        public a(h hVar, uq.f responseCallback) {
            t.g(responseCallback, "responseCallback");
            this.f53673r = hVar;
            this.f53671p = responseCallback;
            this.f53672q = new AtomicInteger(0);
        }

        public static /* synthetic */ void c(a aVar, RejectedExecutionException rejectedExecutionException, int i10, Object obj) {
            if ((i10 & 1) != 0) {
                rejectedExecutionException = null;
            }
            aVar.b(rejectedExecutionException);
        }

        public final void a(ExecutorService executorService) {
            t.g(executorService, "executorService");
            uq.p n10 = this.f53673r.l().n();
            if (vq.p.f40482e && Thread.holdsLock(n10)) {
                throw new AssertionError("Thread " + Thread.currentThread().getName() + " MUST NOT hold lock on " + n10);
            }
            try {
                try {
                    executorService.execute(this);
                } catch (RejectedExecutionException e10) {
                    b(e10);
                    this.f53673r.l().n().f(this);
                }
            } catch (Throwable th2) {
                this.f53673r.l().n().f(this);
                throw th2;
            }
        }

        public final void b(RejectedExecutionException rejectedExecutionException) {
            InterruptedIOException interruptedIOException = new InterruptedIOException("executor rejected");
            interruptedIOException.initCause(rejectedExecutionException);
            this.f53673r.x(interruptedIOException);
            this.f53671p.onFailure(this.f53673r, interruptedIOException);
        }

        public final h d() {
            return this.f53673r;
        }

        public final AtomicInteger e() {
            return this.f53672q;
        }

        public final String f() {
            return this.f53673r.s().k().i();
        }

        public final void g(a other) {
            t.g(other, "other");
            this.f53672q = other.f53672q;
        }

        @Override // java.lang.Runnable
        public void run() {
            boolean z10;
            Throwable th2;
            IOException e10;
            uq.p n10;
            String str = "OkHttp " + this.f53673r.y();
            h hVar = this.f53673r;
            Thread currentThread = Thread.currentThread();
            String name = currentThread.getName();
            currentThread.setName(str);
            try {
                hVar.f53665u.v();
                try {
                    try {
                        z10 = true;
                        try {
                            this.f53671p.onResponse(hVar, hVar.u());
                            n10 = hVar.l().n();
                        } catch (IOException e11) {
                            e10 = e11;
                            if (z10) {
                                dr.h.f16598a.g().j("Callback failure for " + hVar.D(), 4, e10);
                            } else {
                                this.f53671p.onFailure(hVar, e10);
                            }
                            n10 = hVar.l().n();
                            n10.f(this);
                        } catch (Throwable th3) {
                            th2 = th3;
                            hVar.cancel();
                            if (!z10) {
                                IOException iOException = new IOException("canceled due to " + th2);
                                rm.h.a(iOException, th2);
                                this.f53671p.onFailure(hVar, iOException);
                            }
                            throw th2;
                        }
                    } catch (Throwable th4) {
                        hVar.l().n().f(this);
                        throw th4;
                    }
                } catch (IOException e12) {
                    z10 = false;
                    e10 = e12;
                } catch (Throwable th5) {
                    z10 = false;
                    th2 = th5;
                }
                n10.f(this);
            } finally {
                currentThread.setName(name);
            }
        }
    }

    /* loaded from: classes3.dex */
    public static final class b extends WeakReference {

        /* renamed from: a, reason: collision with root package name */
        private final Object f53674a;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public b(h referent, Object obj) {
            super(referent);
            t.g(referent, "referent");
            this.f53674a = obj;
        }

        public final Object a() {
            return this.f53674a;
        }
    }

    /* loaded from: classes3.dex */
    public static final class c extends kr.c {
        c() {
        }

        @Override // kr.c
        protected void B() {
            h.this.cancel();
        }
    }

    public h(z client, b0 originalRequest, boolean z10) {
        t.g(client, "client");
        t.g(originalRequest, "originalRequest");
        this.f53660p = client;
        this.f53661q = originalRequest;
        this.f53662r = z10;
        this.f53663s = client.k().a();
        this.f53664t = client.p().a(this);
        c cVar = new c();
        cVar.g(client.g(), TimeUnit.MILLISECONDS);
        this.f53665u = cVar;
        this.f53666v = new AtomicBoolean();
        this.D = true;
        this.G = new CopyOnWriteArrayList();
    }

    private final IOException C(IOException iOException) {
        if (this.f53670z || !this.f53665u.w()) {
            return iOException;
        }
        InterruptedIOException interruptedIOException = new InterruptedIOException("timeout");
        if (iOException != null) {
            interruptedIOException.initCause(iOException);
        }
        return interruptedIOException;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final String D() {
        StringBuilder sb2 = new StringBuilder();
        sb2.append(e() ? "canceled " : "");
        sb2.append(this.f53662r ? "web socket" : "call");
        sb2.append(" to ");
        sb2.append(y());
        return sb2.toString();
    }

    private final IOException d(IOException iOException) {
        Socket z10;
        boolean z11 = vq.p.f40482e;
        if (z11 && Thread.holdsLock(this)) {
            throw new AssertionError("Thread " + Thread.currentThread().getName() + " MUST NOT hold lock on " + this);
        }
        i iVar = this.f53669y;
        if (iVar != null) {
            if (z11 && Thread.holdsLock(iVar)) {
                throw new AssertionError("Thread " + Thread.currentThread().getName() + " MUST NOT hold lock on " + iVar);
            }
            synchronized (iVar) {
                z10 = z();
            }
            if (this.f53669y == null) {
                if (z10 != null) {
                    vq.p.g(z10);
                }
                this.f53664t.connectionReleased(this, iVar);
            } else {
                if (!(z10 == null)) {
                    throw new IllegalStateException("Check failed.".toString());
                }
            }
        }
        IOException C = C(iOException);
        if (iOException != null) {
            r rVar = this.f53664t;
            t.d(C);
            rVar.callFailed(this, C);
        } else {
            this.f53664t.callEnd(this);
        }
        return C;
    }

    private final void f() {
        this.f53667w = dr.h.f16598a.g().h("response.body().close()");
        this.f53664t.callStart(this);
    }

    private final uq.a i(v vVar) {
        SSLSocketFactory sSLSocketFactory;
        HostnameVerifier hostnameVerifier;
        uq.g gVar;
        if (vVar.j()) {
            sSLSocketFactory = this.f53660p.J();
            hostnameVerifier = this.f53660p.v();
            gVar = this.f53660p.i();
        } else {
            sSLSocketFactory = null;
            hostnameVerifier = null;
            gVar = null;
        }
        return new uq.a(vVar.i(), vVar.o(), this.f53660p.o(), this.f53660p.I(), sSLSocketFactory, hostnameVerifier, gVar, this.f53660p.E(), this.f53660p.D(), this.f53660p.C(), this.f53660p.l(), this.f53660p.F());
    }

    public final boolean A() {
        zq.c cVar = this.F;
        if (cVar != null && cVar.k()) {
            d dVar = this.f53668x;
            t.d(dVar);
            n b10 = dVar.b();
            zq.c cVar2 = this.F;
            if (b10.d(cVar2 != null ? cVar2.h() : null)) {
                return true;
            }
        }
        return false;
    }

    public final void B() {
        if (!(!this.f53670z)) {
            throw new IllegalStateException("Check failed.".toString());
        }
        this.f53670z = true;
        this.f53665u.w();
    }

    @Override // uq.e
    public void J(uq.f responseCallback) {
        t.g(responseCallback, "responseCallback");
        if (!this.f53666v.compareAndSet(false, true)) {
            throw new IllegalStateException("Already Executed".toString());
        }
        f();
        this.f53660p.n().a(new a(this, responseCallback));
    }

    @Override // uq.e
    public boolean Q() {
        return this.f53666v.get();
    }

    public final void c(i connection) {
        t.g(connection, "connection");
        if (!vq.p.f40482e || Thread.holdsLock(connection)) {
            if (!(this.f53669y == null)) {
                throw new IllegalStateException("Check failed.".toString());
            }
            this.f53669y = connection;
            connection.i().add(new b(this, this.f53667w));
            return;
        }
        throw new AssertionError("Thread " + Thread.currentThread().getName() + " MUST hold lock on " + connection);
    }

    @Override // uq.e
    public void cancel() {
        if (this.E) {
            return;
        }
        this.E = true;
        zq.c cVar = this.F;
        if (cVar != null) {
            cVar.b();
        }
        Iterator it = this.G.iterator();
        while (it.hasNext()) {
            ((n.b) it.next()).cancel();
        }
        this.f53664t.canceled(this);
    }

    @Override // uq.e
    public boolean e() {
        return this.E;
    }

    @Override // uq.e
    public b0 g() {
        return this.f53661q;
    }

    /* renamed from: h, reason: merged with bridge method [inline-methods] */
    public uq.e clone() {
        return new h(this.f53660p, this.f53661q, this.f53662r);
    }

    public final void j(b0 request, boolean z10, ar.g chain) {
        t.g(request, "request");
        t.g(chain, "chain");
        if (!(this.A == null)) {
            throw new IllegalStateException("Check failed.".toString());
        }
        synchronized (this) {
            if (!(!this.C)) {
                throw new IllegalStateException("cannot make a new request because the previous response is still open: please call response.close()".toString());
            }
            if (!(!this.B)) {
                throw new IllegalStateException("Check failed.".toString());
            }
            n0 n0Var = n0.f34904a;
        }
        if (z10) {
            k kVar = new k(this.f53660p, i(request.k()), this, chain);
            this.f53668x = this.f53660p.q() ? new f(kVar, this.f53660p.u()) : new p(kVar);
        }
    }

    public final void k(boolean z10) {
        zq.c cVar;
        synchronized (this) {
            if (!this.D) {
                throw new IllegalStateException("released".toString());
            }
            n0 n0Var = n0.f34904a;
        }
        if (z10 && (cVar = this.F) != null) {
            cVar.d();
        }
        this.A = null;
    }

    public final z l() {
        return this.f53660p;
    }

    @Override // uq.e
    public d0 m() {
        if (!this.f53666v.compareAndSet(false, true)) {
            throw new IllegalStateException("Already Executed".toString());
        }
        this.f53665u.v();
        f();
        try {
            this.f53660p.n().b(this);
            return u();
        } finally {
            this.f53660p.n().g(this);
        }
    }

    public final i n() {
        return this.f53669y;
    }

    public final r o() {
        return this.f53664t;
    }

    public final boolean p() {
        return this.f53662r;
    }

    public final zq.c q() {
        return this.A;
    }

    public final b0 s() {
        return this.f53661q;
    }

    public final CopyOnWriteArrayList t() {
        return this.G;
    }

    /* JADX WARN: Removed duplicated region for block: B:23:0x00a1  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final d0 u() {
        ArrayList arrayList = new ArrayList();
        sm.z.A(arrayList, this.f53660p.w());
        arrayList.add(new ar.j(this.f53660p));
        arrayList.add(new ar.a(this.f53660p.m()));
        this.f53660p.f();
        arrayList.add(new xq.a(null));
        arrayList.add(zq.a.f53608a);
        if (!this.f53662r) {
            sm.z.A(arrayList, this.f53660p.y());
        }
        arrayList.add(new ar.b(this.f53662r));
        boolean z10 = false;
        try {
            d0 b10 = new ar.g(this, arrayList, 0, null, this.f53661q, this.f53660p.j(), this.f53660p.G(), this.f53660p.L()).b(this.f53661q);
            if (e()) {
                vq.m.f(b10);
                throw new IOException("Canceled");
            }
            x(null);
            return b10;
        } catch (IOException e10) {
            try {
                IOException x10 = x(e10);
                t.e(x10, "null cannot be cast to non-null type kotlin.Throwable");
                throw x10;
            } catch (Throwable th2) {
                th = th2;
                z10 = true;
                if (!z10) {
                    x(null);
                }
                throw th;
            }
        } catch (Throwable th3) {
            th = th3;
            if (!z10) {
            }
            throw th;
        }
    }

    public final zq.c v(ar.g chain) {
        t.g(chain, "chain");
        synchronized (this) {
            if (!this.D) {
                throw new IllegalStateException("released".toString());
            }
            if (!(!this.C)) {
                throw new IllegalStateException("Check failed.".toString());
            }
            if (!(!this.B)) {
                throw new IllegalStateException("Check failed.".toString());
            }
            n0 n0Var = n0.f34904a;
        }
        d dVar = this.f53668x;
        t.d(dVar);
        zq.c cVar = new zq.c(this, this.f53664t, dVar, dVar.a().r(this.f53660p, chain));
        this.A = cVar;
        this.F = cVar;
        synchronized (this) {
            this.B = true;
            this.C = true;
        }
        if (this.E) {
            throw new IOException("Canceled");
        }
        return cVar;
    }

    /* JADX WARN: Removed duplicated region for block: B:12:0x0021 A[Catch: all -> 0x0017, TryCatch #0 {all -> 0x0017, blocks: (B:44:0x0012, B:12:0x0021, B:14:0x0025, B:15:0x0027, B:17:0x002c, B:21:0x0035, B:23:0x0039, B:27:0x0042, B:9:0x001b), top: B:43:0x0012 }] */
    /* JADX WARN: Removed duplicated region for block: B:14:0x0025 A[Catch: all -> 0x0017, TryCatch #0 {all -> 0x0017, blocks: (B:44:0x0012, B:12:0x0021, B:14:0x0025, B:15:0x0027, B:17:0x002c, B:21:0x0035, B:23:0x0039, B:27:0x0042, B:9:0x001b), top: B:43:0x0012 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final IOException w(zq.c exchange, boolean z10, boolean z11, IOException iOException) {
        boolean z12;
        boolean z13;
        boolean z14;
        t.g(exchange, "exchange");
        if (!t.b(exchange, this.F)) {
            return iOException;
        }
        synchronized (this) {
            z12 = false;
            if (z10) {
                try {
                    if (!this.B) {
                    }
                    if (z10) {
                        this.B = false;
                    }
                    if (z11) {
                        this.C = false;
                    }
                    z14 = this.B;
                    boolean z15 = z14 && !this.C;
                    if (!z14 && !this.C && !this.D) {
                        z12 = true;
                    }
                    z13 = z12;
                    z12 = z15;
                    n0 n0Var = n0.f34904a;
                } catch (Throwable th2) {
                    throw th2;
                }
            }
            if (!z11 || !this.C) {
                z13 = false;
                n0 n0Var2 = n0.f34904a;
            }
            if (z10) {
            }
            if (z11) {
            }
            z14 = this.B;
            if (z14) {
            }
            if (!z14) {
                z12 = true;
            }
            z13 = z12;
            z12 = z15;
            n0 n0Var22 = n0.f34904a;
        }
        if (z12) {
            this.F = null;
            i iVar = this.f53669y;
            if (iVar != null) {
                iVar.n();
            }
        }
        return z13 ? d(iOException) : iOException;
    }

    public final IOException x(IOException iOException) {
        boolean z10;
        synchronized (this) {
            z10 = false;
            if (this.D) {
                this.D = false;
                if (!this.B && !this.C) {
                    z10 = true;
                }
            }
            n0 n0Var = n0.f34904a;
        }
        return z10 ? d(iOException) : iOException;
    }

    public final String y() {
        return this.f53661q.k().q();
    }

    public final Socket z() {
        i iVar = this.f53669y;
        t.d(iVar);
        if (vq.p.f40482e && !Thread.holdsLock(iVar)) {
            throw new AssertionError("Thread " + Thread.currentThread().getName() + " MUST hold lock on " + iVar);
        }
        List i10 = iVar.i();
        Iterator it = i10.iterator();
        int i11 = 0;
        while (true) {
            if (!it.hasNext()) {
                i11 = -1;
                break;
            }
            if (t.b(((Reference) it.next()).get(), this)) {
                break;
            }
            i11++;
        }
        if (!(i11 != -1)) {
            throw new IllegalStateException("Check failed.".toString());
        }
        i10.remove(i11);
        this.f53669y = null;
        if (i10.isEmpty()) {
            iVar.w(System.nanoTime());
            if (this.f53663s.c(iVar)) {
                return iVar.y();
            }
        }
        return null;
    }
}
