package okhttp3.internal.connection;

import java.io.IOException;
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.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import kotlin.jvm.internal.o;
import n2.AbstractC2612c;
import okhttp3.Address;
import okhttp3.Call;
import okhttp3.EventListener;
import okhttp3.HttpUrl;
import okhttp3.Route;
import okhttp3.internal.Util;
import okhttp3.internal.connection.RouteSelector;
import okhttp3.internal.http2.ConnectionShutdownException;
import okhttp3.internal.http2.ErrorCode;
import okhttp3.internal.http2.StreamResetException;
import x4.AbstractC3257w;

/* JADX INFO: loaded from: classes3.dex */
public final class ExchangeFinder {

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

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

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public RouteSelector.Selection f22311e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public RouteSelector f22312f;

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

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

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

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

    public ExchangeFinder(RealConnectionPool connectionPool, Address address, RealCall call, EventListener eventListener) {
        o.h(connectionPool, "connectionPool");
        o.h(call, "call");
        o.h(eventListener, "eventListener");
        this.f22307a = connectionPool;
        this.f22308b = address;
        this.f22309c = call;
        this.f22310d = eventListener;
    }

    /* JADX WARN: Removed duplicated region for block: B:178:0x02d5 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:31:0x0065  */
    /* JADX WARN: Removed duplicated region for block: B:32:0x007a  */
    /* JADX WARN: Type inference failed for: r6v11, types: [java.lang.Object, java.util.List] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final RealConnection a(int i6, boolean z6, boolean z7, int i7, int i8) throws IOException {
        ArrayList arrayList;
        String hostName;
        int port;
        List listQ;
        boolean zContains;
        RouteDatabase routeDatabase;
        Socket socketJ;
        while (!this.f22309c.f22329q) {
            RealConnection realConnection = this.f22309c.l;
            if (realConnection != null) {
                synchronized (realConnection) {
                    try {
                        socketJ = (realConnection.f22345j || !b(realConnection.f22337b.f22168a.f21896h)) ? this.f22309c.j() : null;
                    } catch (Throwable th) {
                        throw th;
                    }
                }
                if (this.f22309c.l == null) {
                    if (socketJ != null) {
                        Util.d(socketJ);
                    }
                    EventListener eventListener = this.f22310d;
                    RealCall call = this.f22309c;
                    eventListener.getClass();
                    o.h(call, "call");
                    this.f22313g = 0;
                    this.f22314h = 0;
                    this.f22315i = 0;
                    if (this.f22307a.a(this.f22308b, this.f22309c, null, false)) {
                        Route route = this.f22316j;
                        try {
                            if (route != null) {
                                this.f22316j = null;
                            } else {
                                RouteSelector.Selection selection = this.f22311e;
                                if (selection == null || !selection.a()) {
                                    RouteSelector routeSelector = this.f22312f;
                                    if (routeSelector == null) {
                                        Address address = this.f22308b;
                                        RealCall realCall = this.f22309c;
                                        routeSelector = new RouteSelector(address, realCall.f22317a.f22071D, realCall, this.f22310d);
                                        this.f22312f = routeSelector;
                                    }
                                    if (!routeSelector.a()) {
                                        throw new NoSuchElementException();
                                    }
                                    arrayList = new ArrayList();
                                    while (routeSelector.f22372f < routeSelector.f22371e.size()) {
                                        boolean z8 = routeSelector.f22372f < routeSelector.f22371e.size();
                                        Address address2 = routeSelector.f22367a;
                                        if (!z8) {
                                            throw new SocketException("No route to " + address2.f21896h.f22027d + "; exhausted proxy configurations: " + routeSelector.f22371e);
                                        }
                                        List list = routeSelector.f22371e;
                                        int i9 = routeSelector.f22372f;
                                        routeSelector.f22372f = i9 + 1;
                                        Proxy proxy = (Proxy) list.get(i9);
                                        ArrayList arrayList2 = new ArrayList();
                                        routeSelector.f22373g = arrayList2;
                                        if (proxy.type() == Proxy.Type.DIRECT || proxy.type() == Proxy.Type.SOCKS) {
                                            HttpUrl httpUrl = address2.f21896h;
                                            hostName = httpUrl.f22027d;
                                            port = httpUrl.f22028e;
                                        } else {
                                            SocketAddress proxyAddress = proxy.address();
                                            if (!(proxyAddress instanceof InetSocketAddress)) {
                                                throw new IllegalArgumentException(("Proxy.address() is not an InetSocketAddress: " + proxyAddress.getClass()).toString());
                                            }
                                            o.g(proxyAddress, "proxyAddress");
                                            InetSocketAddress inetSocketAddress = (InetSocketAddress) proxyAddress;
                                            RouteSelector.f22366i.getClass();
                                            o.h(inetSocketAddress, "<this>");
                                            InetAddress address3 = inetSocketAddress.getAddress();
                                            if (address3 == null) {
                                                hostName = inetSocketAddress.getHostName();
                                                o.g(hostName, "hostName");
                                            } else {
                                                hostName = address3.getHostAddress();
                                                o.g(hostName, "address.hostAddress");
                                            }
                                            port = inetSocketAddress.getPort();
                                        }
                                        if (1 > port || port >= 65536) {
                                            throw new SocketException("No route to " + hostName + ':' + port + "; port is out of range");
                                        }
                                        if (proxy.type() == Proxy.Type.SOCKS) {
                                            arrayList2.add(InetSocketAddress.createUnresolved(hostName, port));
                                        } else {
                                            byte[] bArr = Util.f22179a;
                                            o.h(hostName, "<this>");
                                            if (Util.f22184f.d(hostName)) {
                                                listQ = AbstractC2612c.q(InetAddress.getByName(hostName));
                                            } else {
                                                routeSelector.f22370d.getClass();
                                                Call call2 = routeSelector.f22369c;
                                                o.h(call2, "call");
                                                List listA = address2.f21889a.a(hostName);
                                                if (listA.isEmpty()) {
                                                    throw new UnknownHostException(address2.f21889a + " returned no addresses for " + hostName);
                                                }
                                                listQ = listA;
                                            }
                                            Iterator it = listQ.iterator();
                                            while (it.hasNext()) {
                                                arrayList2.add(new InetSocketAddress((InetAddress) it.next(), port));
                                            }
                                        }
                                        Iterator it2 = routeSelector.f22373g.iterator();
                                        while (it2.hasNext()) {
                                            Route route2 = new Route(routeSelector.f22367a, proxy, (InetSocketAddress) it2.next());
                                            RouteDatabase routeDatabase2 = routeSelector.f22368b;
                                            synchronized (routeDatabase2) {
                                                zContains = routeDatabase2.f22363a.contains(route2);
                                            }
                                            if (zContains) {
                                                routeSelector.f22374h.add(route2);
                                            } else {
                                                arrayList.add(route2);
                                            }
                                        }
                                        if (!arrayList.isEmpty()) {
                                            break;
                                        }
                                    }
                                    if (arrayList.isEmpty()) {
                                        AbstractC3257w.L(routeSelector.f22374h, arrayList);
                                        routeSelector.f22374h.clear();
                                    }
                                    RouteSelector.Selection selection2 = new RouteSelector.Selection(arrayList);
                                    this.f22311e = selection2;
                                    if (this.f22309c.f22329q) {
                                        throw new IOException("Canceled");
                                    }
                                    if (this.f22307a.a(this.f22308b, this.f22309c, arrayList, false)) {
                                        realConnection = this.f22309c.l;
                                        o.e(realConnection);
                                        EventListener eventListener2 = this.f22310d;
                                        RealCall call3 = this.f22309c;
                                        eventListener2.getClass();
                                        o.h(call3, "call");
                                    } else {
                                        if (!selection2.a()) {
                                            throw new NoSuchElementException();
                                        }
                                        int i10 = selection2.f22376b;
                                        selection2.f22376b = i10 + 1;
                                        route = (Route) arrayList.get(i10);
                                        RealConnection realConnection2 = new RealConnection(this.f22307a, route);
                                        this.f22309c.f22331s = realConnection2;
                                        realConnection2.c(i6, i7, i8, z6, this.f22309c, this.f22310d);
                                        this.f22309c.f22331s = null;
                                        routeDatabase = this.f22309c.f22317a.f22071D;
                                        Route route3 = realConnection2.f22337b;
                                        synchronized (routeDatabase) {
                                            o.h(route3, "route");
                                            routeDatabase.f22363a.remove(route3);
                                        }
                                        if (this.f22307a.a(this.f22308b, this.f22309c, arrayList, true)) {
                                            realConnection = this.f22309c.l;
                                            o.e(realConnection);
                                            this.f22316j = route;
                                            Socket socket = realConnection2.f22339d;
                                            o.e(socket);
                                            Util.d(socket);
                                            EventListener eventListener3 = this.f22310d;
                                            RealCall call4 = this.f22309c;
                                            eventListener3.getClass();
                                            o.h(call4, "call");
                                        } else {
                                            synchronized (realConnection2) {
                                                RealConnectionPool realConnectionPool = this.f22307a;
                                                realConnectionPool.getClass();
                                                byte[] bArr2 = Util.f22179a;
                                                realConnectionPool.f22361e.add(realConnection2);
                                                realConnectionPool.f22359c.c(realConnectionPool.f22360d, 0L);
                                                this.f22309c.b(realConnection2);
                                            }
                                            EventListener eventListener4 = this.f22310d;
                                            RealCall call5 = this.f22309c;
                                            eventListener4.getClass();
                                            o.h(call5, "call");
                                            realConnection = realConnection2;
                                        }
                                    }
                                } else {
                                    RouteSelector.Selection selection3 = this.f22311e;
                                    o.e(selection3);
                                    if (!selection3.a()) {
                                        throw new NoSuchElementException();
                                    }
                                    int i11 = selection3.f22376b;
                                    selection3.f22376b = i11 + 1;
                                    route = (Route) selection3.f22375a.get(i11);
                                }
                            }
                            realConnection2.c(i6, i7, i8, z6, this.f22309c, this.f22310d);
                            this.f22309c.f22331s = null;
                            routeDatabase = this.f22309c.f22317a.f22071D;
                            Route route32 = realConnection2.f22337b;
                            synchronized (routeDatabase) {
                            }
                        } catch (Throwable th2) {
                            this.f22309c.f22331s = null;
                            throw th2;
                        }
                        arrayList = null;
                        RealConnection realConnection22 = new RealConnection(this.f22307a, route);
                        this.f22309c.f22331s = realConnection22;
                    } else {
                        realConnection = this.f22309c.l;
                        o.e(realConnection);
                        EventListener eventListener5 = this.f22310d;
                        RealCall call6 = this.f22309c;
                        eventListener5.getClass();
                        o.h(call6, "call");
                    }
                } else if (socketJ != null) {
                    throw new IllegalStateException("Check failed.");
                }
            } else {
                this.f22313g = 0;
                this.f22314h = 0;
                this.f22315i = 0;
                if (this.f22307a.a(this.f22308b, this.f22309c, null, false)) {
                }
            }
            if (realConnection.i(z7)) {
                return realConnection;
            }
            realConnection.k();
            if (this.f22316j == null) {
                RouteSelector.Selection selection4 = this.f22311e;
                if (selection4 != null ? selection4.a() : true) {
                    continue;
                } else {
                    RouteSelector routeSelector2 = this.f22312f;
                    if (!(routeSelector2 != null ? routeSelector2.a() : true)) {
                        throw new IOException("exhausted all routes");
                    }
                }
            }
        }
        throw new IOException("Canceled");
    }

    public final boolean b(HttpUrl url) {
        o.h(url, "url");
        HttpUrl httpUrl = this.f22308b.f21896h;
        return url.f22028e == httpUrl.f22028e && o.c(url.f22027d, httpUrl.f22027d);
    }

    public final void c(IOException e6) {
        o.h(e6, "e");
        this.f22316j = null;
        if (e6 instanceof StreamResetException) {
            if (((StreamResetException) e6).f22594a == ErrorCode.REFUSED_STREAM) {
                this.f22313g++;
                return;
            }
        }
        if (e6 instanceof ConnectionShutdownException) {
            this.f22314h++;
        } else {
            this.f22315i++;
        }
    }
}
