package okhttp3.internal.connection;

import K1.b;
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.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import kotlin.jvm.internal.o;
import n2.AbstractC2613d;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Dispatcher;
import okhttp3.EventListener;
import okhttp3.EventListener$Companion$NONE$1;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.internal.Util;
import okhttp3.internal.cache.CacheInterceptor;
import okhttp3.internal.concurrent.TaskQueue;
import okhttp3.internal.http.BridgeInterceptor;
import okhttp3.internal.http.CallServerInterceptor;
import okhttp3.internal.http.RealInterceptorChain;
import okhttp3.internal.http.RetryAndFollowUpInterceptor;
import okhttp3.internal.platform.Platform;
import p5.C2736f;
import x4.AbstractC3257w;

/* JADX INFO: loaded from: classes3.dex */
public final class RealCall implements Call {

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final RealConnectionPool f22319c;

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final RealCall$timeout$1 f22321e;

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

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public ExchangeFinder f22324k;
    public RealConnection l;

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

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

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

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

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public volatile boolean f22329q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public volatile Exchange f22330r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public volatile RealConnection f22331s;

    public final class AsyncCall implements Runnable {

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

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public volatile AtomicInteger f22333b = new AtomicInteger(0);

        public AsyncCall(Callback callback) {
            this.f22332a = callback;
        }

        @Override // java.lang.Runnable
        public final void run() {
            Dispatcher dispatcher;
            Callback callback = this.f22332a;
            RealCall realCall = RealCall.this;
            String strConcat = "OkHttp ".concat(realCall.f22318b.f22125a.f());
            Thread threadCurrentThread = Thread.currentThread();
            String name = threadCurrentThread.getName();
            threadCurrentThread.setName(strConcat);
            try {
                RealCall$timeout$1 realCall$timeout$1 = realCall.f22321e;
                OkHttpClient okHttpClient = realCall.f22317a;
                realCall$timeout$1.h();
                boolean z6 = false;
                try {
                    try {
                        try {
                            callback.onResponse(realCall, realCall.f());
                            dispatcher = okHttpClient.f22072a;
                        } catch (IOException e6) {
                            e = e6;
                            z6 = true;
                            if (z6) {
                                Platform.Companion.get().log("Callback failure for " + RealCall.a(realCall), 4, e);
                            } else {
                                callback.onFailure(realCall, e);
                            }
                            dispatcher = okHttpClient.f22072a;
                        } catch (Throwable th) {
                            th = th;
                            z6 = true;
                            realCall.cancel();
                            if (!z6) {
                                IOException iOException = new IOException("canceled due to " + th);
                                AbstractC2613d.b(iOException, th);
                                callback.onFailure(realCall, iOException);
                            }
                            throw th;
                        }
                    } catch (Throwable th2) {
                        okHttpClient.f22072a.b(this);
                        throw th2;
                    }
                } catch (IOException e7) {
                    e = e7;
                } catch (Throwable th3) {
                    th = th3;
                }
                dispatcher.b(this);
            } finally {
                threadCurrentThread.setName(name);
            }
        }
    }

    public static final class CallReference extends WeakReference<RealCall> {

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

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

    /* JADX WARN: Type inference failed for: r4v6, types: [okhttp3.internal.connection.RealCall$timeout$1, p5.S] */
    public RealCall(OkHttpClient client, Request originalRequest) {
        o.h(client, "client");
        o.h(originalRequest, "originalRequest");
        this.f22317a = client;
        this.f22318b = originalRequest;
        this.f22319c = client.f22073b.f21975a;
        b bVar = client.f22076e;
        bVar.getClass();
        byte[] bArr = Util.f22179a;
        EventListener$Companion$NONE$1 this_asFactory = (EventListener$Companion$NONE$1) bVar.f4041b;
        o.h(this_asFactory, "$this_asFactory");
        this.f22320d = this_asFactory;
        ?? r42 = new C2736f() { // from class: okhttp3.internal.connection.RealCall$timeout$1
            @Override // p5.C2736f
            public final void j() {
                this.f22336m.cancel();
            }
        };
        r42.g(client.f22093z, TimeUnit.MILLISECONDS);
        this.f22321e = r42;
        this.f22322f = new AtomicBoolean();
        this.f22328p = true;
    }

    public static final String a(RealCall realCall) {
        StringBuilder sb = new StringBuilder();
        sb.append(realCall.f22329q ? "canceled " : "");
        sb.append("call");
        sb.append(" to ");
        sb.append(realCall.f22318b.f22125a.f());
        return sb.toString();
    }

    public final void b(RealConnection realConnection) {
        byte[] bArr = Util.f22179a;
        if (this.l != null) {
            throw new IllegalStateException("Check failed.");
        }
        this.l = realConnection;
        realConnection.f22350p.add(new CallReference(this, this.f22323j));
    }

    public final IOException c(IOException iOException) {
        IOException interruptedIOException;
        Socket socketJ;
        byte[] bArr = Util.f22179a;
        RealConnection realConnection = this.l;
        if (realConnection != null) {
            synchronized (realConnection) {
                socketJ = j();
            }
            if (this.l == null) {
                if (socketJ != null) {
                    Util.d(socketJ);
                }
                this.f22320d.getClass();
            } else if (socketJ != null) {
                throw new IllegalStateException("Check failed.");
            }
        }
        if (i()) {
            interruptedIOException = new InterruptedIOException("timeout");
            if (iOException != null) {
                interruptedIOException.initCause(iOException);
            }
        } else {
            interruptedIOException = iOException;
        }
        if (iOException == null) {
            this.f22320d.getClass();
            return interruptedIOException;
        }
        EventListener eventListener = this.f22320d;
        o.e(interruptedIOException);
        eventListener.getClass();
        return interruptedIOException;
    }

    @Override // okhttp3.Call
    public final void cancel() {
        Socket socket;
        if (this.f22329q) {
            return;
        }
        this.f22329q = true;
        Exchange exchange = this.f22330r;
        if (exchange != null) {
            exchange.f22293d.cancel();
        }
        RealConnection realConnection = this.f22331s;
        if (realConnection != null && (socket = realConnection.f22338c) != null) {
            Util.d(socket);
        }
        this.f22320d.getClass();
    }

    public final Object clone() {
        return new RealCall(this.f22317a, this.f22318b);
    }

    public final Response d() {
        if (!this.f22322f.compareAndSet(false, true)) {
            throw new IllegalStateException("Already Executed");
        }
        h();
        this.f22323j = Platform.Companion.get().getStackTraceForCloseable("response.body().close()");
        this.f22320d.getClass();
        try {
            Dispatcher dispatcher = this.f22317a.f22072a;
            synchronized (dispatcher) {
                dispatcher.f22007f.add(this);
            }
            return f();
        } finally {
            Dispatcher dispatcher2 = this.f22317a.f22072a;
            dispatcher2.getClass();
            dispatcher2.a(dispatcher2.f22007f, this);
        }
    }

    public final void e(boolean z6) {
        Exchange exchange;
        synchronized (this) {
            if (!this.f22328p) {
                throw new IllegalStateException("released");
            }
        }
        if (z6 && (exchange = this.f22330r) != null) {
            exchange.f22293d.cancel();
            exchange.f22290a.h(exchange, true, true, null);
        }
        this.f22325m = null;
    }

    /* JADX WARN: Removed duplicated region for block: B:16:0x007f  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Response f() {
        ArrayList arrayList = new ArrayList();
        AbstractC3257w.L(this.f22317a.f22074c, arrayList);
        arrayList.add(new RetryAndFollowUpInterceptor(this.f22317a));
        arrayList.add(new BridgeInterceptor(this.f22317a.f22080m));
        arrayList.add(new CacheInterceptor(this.f22317a.f22081n));
        arrayList.add(ConnectInterceptor.f22285a);
        AbstractC3257w.L(this.f22317a.f22075d, arrayList);
        arrayList.add(new CallServerInterceptor());
        Request request = this.f22318b;
        OkHttpClient okHttpClient = this.f22317a;
        boolean z6 = false;
        try {
            try {
                Response responseB = new RealInterceptorChain(this, arrayList, 0, null, request, okHttpClient.f22068A, okHttpClient.f22069B, okHttpClient.f22070C).b(this.f22318b);
                if (this.f22329q) {
                    Util.c(responseB);
                    throw new IOException("Canceled");
                }
                i(null);
                return responseB;
            } catch (IOException e6) {
                z6 = true;
                IOException iOExceptionI = i(e6);
                o.f(iOExceptionI, "null cannot be cast to non-null type kotlin.Throwable");
                throw iOExceptionI;
            }
        } catch (Throwable th) {
            if (!z6) {
            }
            throw th;
        }
        if (!z6) {
            i(null);
        }
        throw th;
    }

    @Override // okhttp3.Call
    public final void g(Callback callback) {
        AsyncCall asyncCall;
        if (!this.f22322f.compareAndSet(false, true)) {
            throw new IllegalStateException("Already Executed");
        }
        this.f22323j = Platform.Companion.get().getStackTraceForCloseable("response.body().close()");
        this.f22320d.getClass();
        Dispatcher dispatcher = this.f22317a.f22072a;
        AsyncCall asyncCall2 = new AsyncCall(callback);
        dispatcher.getClass();
        synchronized (dispatcher) {
            dispatcher.f22005d.add(asyncCall2);
            String str = this.f22318b.f22125a.f22027d;
            Iterator it = dispatcher.f22006e.iterator();
            while (true) {
                if (!it.hasNext()) {
                    Iterator it2 = dispatcher.f22005d.iterator();
                    while (true) {
                        if (!it2.hasNext()) {
                            asyncCall = null;
                            break;
                        } else {
                            asyncCall = (AsyncCall) it2.next();
                            if (o.c(RealCall.this.f22318b.f22125a.f22027d, str)) {
                                break;
                            }
                        }
                    }
                } else {
                    asyncCall = (AsyncCall) it.next();
                    if (o.c(RealCall.this.f22318b.f22125a.f22027d, str)) {
                        break;
                    }
                }
            }
            if (asyncCall != null) {
                asyncCall2.f22333b = asyncCall.f22333b;
            }
        }
        dispatcher.c();
    }

    public final IOException h(Exchange exchange, boolean z6, boolean z7, IOException iOException) {
        boolean z8;
        boolean z9;
        o.h(exchange, "exchange");
        if (exchange.equals(this.f22330r)) {
            synchronized (this) {
                z8 = false;
                if (z6) {
                    try {
                        if (!this.f22326n) {
                            if (z7 || !this.f22327o) {
                                z9 = false;
                            }
                        }
                        if (z6) {
                            this.f22326n = false;
                        }
                        if (z7) {
                            this.f22327o = false;
                        }
                        boolean z10 = this.f22326n;
                        boolean z11 = (z10 || this.f22327o) ? false : true;
                        if (!z10 && !this.f22327o) {
                            if (!this.f22328p) {
                                z8 = true;
                            }
                        }
                        z9 = z8;
                        z8 = z11;
                    } catch (Throwable th) {
                        throw th;
                    }
                } else {
                    if (z7) {
                    }
                    z9 = false;
                }
            }
            if (z8) {
                this.f22330r = null;
                RealConnection realConnection = this.l;
                if (realConnection != null) {
                    synchronized (realConnection) {
                        realConnection.f22347m++;
                    }
                }
            }
            if (z9) {
                return c(iOException);
            }
        }
        return iOException;
    }

    public final IOException i(IOException iOException) {
        boolean z6;
        synchronized (this) {
            z6 = false;
            if (this.f22328p) {
                this.f22328p = false;
                if (!this.f22326n) {
                    if (!this.f22327o) {
                        z6 = true;
                    }
                }
            }
        }
        return z6 ? c(iOException) : iOException;
    }

    @Override // okhttp3.Call
    public final boolean isCanceled() {
        return this.f22329q;
    }

    public final Socket j() {
        RealConnection realConnection = this.l;
        o.e(realConnection);
        byte[] bArr = Util.f22179a;
        ArrayList arrayList = realConnection.f22350p;
        Iterator it = arrayList.iterator();
        int i6 = 0;
        while (true) {
            if (!it.hasNext()) {
                i6 = -1;
                break;
            }
            if (o.c(((Reference) it.next()).get(), this)) {
                break;
            }
            i6++;
        }
        if (i6 == -1) {
            throw new IllegalStateException("Check failed.");
        }
        arrayList.remove(i6);
        this.l = null;
        if (!arrayList.isEmpty()) {
            return null;
        }
        realConnection.f22351q = System.nanoTime();
        RealConnectionPool realConnectionPool = this.f22319c;
        realConnectionPool.getClass();
        byte[] bArr2 = Util.f22179a;
        boolean z6 = realConnection.f22345j;
        TaskQueue taskQueue = realConnectionPool.f22359c;
        if (!z6 && realConnectionPool.f22357a != 0) {
            taskQueue.c(realConnectionPool.f22360d, 0L);
            return null;
        }
        realConnection.f22345j = true;
        ConcurrentLinkedQueue concurrentLinkedQueue = realConnectionPool.f22361e;
        concurrentLinkedQueue.remove(realConnection);
        if (concurrentLinkedQueue.isEmpty()) {
            taskQueue.a();
        }
        Socket socket = realConnection.f22339d;
        o.e(socket);
        return socket;
    }
}
