package okhttp3;

import c.k;
import c.q.d.g;
import c.q.d.j;
import d.b0;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
import okhttp3.internal.connection.Transmitter;
import okhttp3.internal.platform.Platform;

/* JADX INFO: compiled from: RealCall.kt */
/* JADX INFO: loaded from: classes.dex */
public final class RealCall implements Call {
    public static final Companion Companion = new Companion(null);
    private final OkHttpClient client;
    private boolean executed;
    private final boolean forWebSocket;
    private final Request originalRequest;
    private Transmitter transmitter;

    /* JADX INFO: compiled from: RealCall.kt */
    public final class AsyncCall implements Runnable {
        private volatile AtomicInteger callsPerHost;
        private final Callback responseCallback;
        final /* synthetic */ RealCall this$0;

        public AsyncCall(RealCall realCall, Callback callback) {
            j.c(callback, "responseCallback");
            this.this$0 = realCall;
            this.responseCallback = callback;
            this.callsPerHost = new AtomicInteger(0);
        }

        public final AtomicInteger callsPerHost() {
            return this.callsPerHost;
        }

        public final void executeOn(ExecutorService executorService) {
            j.c(executorService, "executorService");
            boolean z = !Thread.holdsLock(this.this$0.getClient().dispatcher());
            if (k.f1418a && !z) {
                throw new AssertionError("Assertion failed");
            }
            try {
                try {
                    executorService.execute(this);
                } catch (RejectedExecutionException e2) {
                    InterruptedIOException interruptedIOException = new InterruptedIOException("executor rejected");
                    interruptedIOException.initCause(e2);
                    RealCall.access$getTransmitter$p(this.this$0).noMoreExchanges(interruptedIOException);
                    this.responseCallback.onFailure(this.this$0, interruptedIOException);
                    this.this$0.getClient().dispatcher().finished$okhttp(this);
                }
            } catch (Throwable th) {
                this.this$0.getClient().dispatcher().finished$okhttp(this);
                throw th;
            }
        }

        public final RealCall get() {
            return this.this$0;
        }

        public final String host() {
            return this.this$0.getOriginalRequest().url().host();
        }

        public final Request request() {
            return this.this$0.getOriginalRequest();
        }

        public final void reuseCallsPerHostFrom(AsyncCall asyncCall) {
            j.c(asyncCall, "other");
            this.callsPerHost = asyncCall.callsPerHost;
        }

        @Override // java.lang.Runnable
        public void run() {
            boolean z;
            IOException e2;
            Dispatcher dispatcher;
            String str = "OkHttp " + this.this$0.redactedUrl();
            Thread threadCurrentThread = Thread.currentThread();
            j.b(threadCurrentThread, "currentThread");
            String name = threadCurrentThread.getName();
            threadCurrentThread.setName(str);
            try {
                RealCall.access$getTransmitter$p(this.this$0).timeoutEnter();
                try {
                    try {
                        z = true;
                    } catch (IOException e3) {
                        z = false;
                        e2 = e3;
                    }
                    try {
                        this.responseCallback.onResponse(this.this$0, this.this$0.getResponseWithInterceptorChain());
                        dispatcher = this.this$0.getClient().dispatcher();
                    } catch (IOException e4) {
                        e2 = e4;
                        if (z) {
                            Platform.Companion.get().log(4, "Callback failure for " + this.this$0.toLoggableString(), e2);
                        } else {
                            this.responseCallback.onFailure(this.this$0, e2);
                        }
                        dispatcher = this.this$0.getClient().dispatcher();
                    }
                    dispatcher.finished$okhttp(this);
                } catch (Throwable th) {
                    this.this$0.getClient().dispatcher().finished$okhttp(this);
                    throw th;
                }
            } finally {
                threadCurrentThread.setName(name);
            }
        }
    }

    /* JADX INFO: compiled from: RealCall.kt */
    public static final class Companion {
        private Companion() {
        }

        public final RealCall newRealCall(OkHttpClient okHttpClient, Request request, boolean z) {
            j.c(okHttpClient, "client");
            j.c(request, "originalRequest");
            RealCall realCall = new RealCall(okHttpClient, request, z, null);
            realCall.transmitter = new Transmitter(okHttpClient, realCall);
            return realCall;
        }

        public /* synthetic */ Companion(g gVar) {
            this();
        }
    }

    private RealCall(OkHttpClient okHttpClient, Request request, boolean z) {
        this.client = okHttpClient;
        this.originalRequest = request;
        this.forWebSocket = z;
    }

    public static final /* synthetic */ Transmitter access$getTransmitter$p(RealCall realCall) {
        Transmitter transmitter = realCall.transmitter;
        if (transmitter != null) {
            return transmitter;
        }
        j.j("transmitter");
        throw null;
    }

    @Override // okhttp3.Call
    public void cancel() {
        Transmitter transmitter = this.transmitter;
        if (transmitter != null) {
            transmitter.cancel();
        } else {
            j.j("transmitter");
            throw null;
        }
    }

    @Override // okhttp3.Call
    public void enqueue(Callback callback) {
        j.c(callback, "responseCallback");
        synchronized (this) {
            if (!(!this.executed)) {
                throw new IllegalStateException("Already Executed".toString());
            }
            this.executed = true;
            c.j jVar = c.j.f1417a;
        }
        Transmitter transmitter = this.transmitter;
        if (transmitter == null) {
            j.j("transmitter");
            throw null;
        }
        transmitter.callStart();
        this.client.dispatcher().enqueue$okhttp(new AsyncCall(this, callback));
    }

    @Override // okhttp3.Call
    public Response execute() {
        synchronized (this) {
            if (!(!this.executed)) {
                throw new IllegalStateException("Already Executed".toString());
            }
            this.executed = true;
            c.j jVar = c.j.f1417a;
        }
        Transmitter transmitter = this.transmitter;
        if (transmitter == null) {
            j.j("transmitter");
            throw null;
        }
        transmitter.timeoutEnter();
        Transmitter transmitter2 = this.transmitter;
        if (transmitter2 == null) {
            j.j("transmitter");
            throw null;
        }
        transmitter2.callStart();
        try {
            this.client.dispatcher().executed$okhttp(this);
            return getResponseWithInterceptorChain();
        } finally {
            this.client.dispatcher().finished$okhttp(this);
        }
    }

    public final OkHttpClient getClient() {
        return this.client;
    }

    public final boolean getExecuted() {
        return this.executed;
    }

    public final boolean getForWebSocket() {
        return this.forWebSocket;
    }

    public final Request getOriginalRequest() {
        return this.originalRequest;
    }

    /* JADX WARN: Removed duplicated region for block: B:38:0x00c1  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final okhttp3.Response getResponseWithInterceptorChain() throws java.lang.Throwable {
        /*
            Method dump skipped, instruction units count: 209
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: okhttp3.RealCall.getResponseWithInterceptorChain():okhttp3.Response");
    }

    @Override // okhttp3.Call
    public boolean isCanceled() {
        Transmitter transmitter = this.transmitter;
        if (transmitter != null) {
            return transmitter.isCanceled();
        }
        j.j("transmitter");
        throw null;
    }

    @Override // okhttp3.Call
    public synchronized boolean isExecuted() {
        return this.executed;
    }

    public final String redactedUrl() {
        return this.originalRequest.url().redact();
    }

    @Override // okhttp3.Call
    public Request request() {
        return this.originalRequest;
    }

    public final void setExecuted(boolean z) {
        this.executed = z;
    }

    @Override // okhttp3.Call
    public b0 timeout() {
        Transmitter transmitter = this.transmitter;
        if (transmitter != null) {
            return transmitter.timeout();
        }
        j.j("transmitter");
        throw null;
    }

    public final String toLoggableString() {
        StringBuilder sb = new StringBuilder();
        sb.append(isCanceled() ? "canceled " : "");
        sb.append(this.forWebSocket ? "web socket" : "call");
        sb.append(" to ");
        sb.append(redactedUrl());
        return sb.toString();
    }

    public /* synthetic */ RealCall(OkHttpClient okHttpClient, Request request, boolean z, g gVar) {
        this(okHttpClient, request, z);
    }

    @Override // okhttp3.Call
    public RealCall clone() {
        return Companion.newRealCall(this.client, this.originalRequest, this.forWebSocket);
    }
}
