package 㿎.ሽ.ඓ;

import com.facebook.internal.security.CertificateUtil;
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.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import okhttp3.internal.connection.RouteException;
import okhttp3.internal.http2.ConnectionShutdownException;
import okhttp3.internal.http2.ErrorCode;
import okhttp3.internal.http2.StreamResetException;
import ඓ.K.K.K.C1293;
import 㿎.AbstractC2533;
import 㿎.AbstractC2537;
import 㿎.AbstractC2661;
import 㿎.C2531;
import 㿎.C2539;
import 㿎.C2625;
import 㿎.C2640;
import 㿎.C2655;
import 㿎.ሽ.AbstractC2570;
import 㿎.ሽ.C2540;
import 㿎.ሽ.ඓ.C2544;
import 㿎.ሽ.㴹.AbstractC2576;
import 㿎.ሽ.㴹.C2583;

/* renamed from: 㿎.ሽ.ඓ.㴹  reason: contains not printable characters */
/* compiled from: StreamAllocation */
public final class C2548 {

    /* renamed from: ʬ  reason: contains not printable characters */
    public C2539 f8608;

    /* renamed from: Ԕ  reason: contains not printable characters */
    public AbstractC2576 f8609;

    /* renamed from: ඓ  reason: contains not printable characters */
    public final AbstractC2537 f8610;

    /* renamed from: ᕣ  reason: contains not printable characters */
    public boolean f8611;

    /* renamed from: ᨨ  reason: contains not printable characters */
    public int f8612;

    /* renamed from: ῲ  reason: contains not printable characters */
    public C2544.C2545 f8613;

    /* renamed from: ⁱ  reason: contains not printable characters */
    public final Object f8614;

    /* renamed from: K  reason: contains not printable characters */
    public final C2640 f8615;

    /* renamed from: 㑃  reason: contains not printable characters */
    public boolean f8616;

    /* renamed from: 㳳  reason: contains not printable characters */
    public C2543 f8617;

    /* renamed from: 㴹  reason: contains not printable characters */
    public final AbstractC2533 f8618;

    /* renamed from: 㸾  reason: contains not printable characters */
    public final C2655 f8619;

    /* renamed from: 㿎  reason: contains not printable characters */
    public final C2544 f8620;

    /* renamed from: 䁗  reason: contains not printable characters */
    public boolean f8621;

    /* renamed from: 㿎.ሽ.ඓ.㴹$K  reason: contains not printable characters */
    /* compiled from: StreamAllocation */
    public static final class C2549 extends WeakReference<C2548> {

        /* renamed from: K  reason: contains not printable characters */
        public final Object f8622;

        public C2549(C2548 r1, Object obj) {
            super(r1);
            this.f8622 = obj;
        }
    }

    public C2548(C2655 r3, C2640 k, AbstractC2537 r5, AbstractC2533 r6, Object obj) {
        this.f8619 = r3;
        this.f8615 = k;
        this.f8610 = r5;
        this.f8618 = r6;
        this.f8620 = new C2544(k, AbstractC2570.f8676.m6760(this.f8619), r5, r6);
        this.f8614 = obj;
    }

    public String toString() {
        C2543 r0 = m6676();
        return r0 != null ? r0.toString() : this.f8615.toString();
    }

    /* renamed from: ʬ  reason: contains not printable characters */
    public synchronized C2543 m6676() {
        return this.f8617;
    }

    /* renamed from: ඓ  reason: contains not printable characters */
    public void m6677() {
        C2543 r1;
        Socket r2;
        synchronized (this.f8619) {
            r1 = this.f8617;
            r2 = m6679(false, true, false);
            if (this.f8617 != null) {
                r1 = null;
            }
        }
        C2540.m6652(r2);
        if (r1 != null) {
            this.f8618.m6625();
            this.f8618.m6618();
        }
    }

    /* renamed from: ῲ  reason: contains not printable characters */
    public AbstractC2576 m6678() {
        AbstractC2576 r1;
        synchronized (this.f8619) {
            r1 = this.f8609;
        }
        return r1;
    }

    /* renamed from: K  reason: contains not printable characters */
    public AbstractC2576 m6682(C2625 r9, AbstractC2661.AbstractC2662 k, boolean z) {
        C2583 r0 = (C2583) k;
        try {
            AbstractC2576 r92 = m6681(((C2583) k).f8692, r0.f8697, r0.f8691, r9.f8893, r9.f8895, z).m6658(r9, k, this);
            synchronized (this.f8619) {
                this.f8609 = r92;
            }
            return r92;
        } catch (IOException e) {
            throw new RouteException(e);
        }
    }

    /* renamed from: 㸾  reason: contains not printable characters */
    public void m6687() {
        C2543 r1;
        Socket r2;
        synchronized (this.f8619) {
            r1 = this.f8617;
            r2 = m6679(true, false, false);
            if (this.f8617 != null) {
                r1 = null;
            }
        }
        C2540.m6652(r2);
        if (r1 != null) {
            this.f8618.m6625();
        }
    }

    /* JADX INFO: finally extract failed */
    /* JADX WARNING: Code restructure failed: missing block: B:11:0x001d, code lost:
        if (r0.f8581.isInputShutdown() != false) goto L_0x005d;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:13:0x0025, code lost:
        if (r0.f8581.isOutputShutdown() == false) goto L_0x0028;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:14:0x0028, code lost:
        r1 = r0.f8591;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:15:0x002b, code lost:
        if (r1 == null) goto L_0x0034;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:16:0x002d, code lost:
        r2 = !r1.m6823();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:17:0x0034, code lost:
        if (r12 == false) goto L_0x005c;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:19:?, code lost:
        r1 = r0.f8581.getSoTimeout();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:21:?, code lost:
        r0.f8581.setSoTimeout(1);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:22:0x0047, code lost:
        if (r0.f8583.m3821() == false) goto L_0x004f;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:23:0x0049, code lost:
        r0.f8581.setSoTimeout(r1);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:24:0x004f, code lost:
        r0.f8581.setSoTimeout(r1);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:25:0x0055, code lost:
        r4 = move-exception;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:26:0x0056, code lost:
        r0.f8581.setSoTimeout(r1);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:27:0x005b, code lost:
        throw r4;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:8:0x000e, code lost:
        r2 = false;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:9:0x0015, code lost:
        if (r0.f8581.isClosed() != false) goto L_0x005d;
     */
    /* JADX WARNING: Removed duplicated region for block: B:32:0x005f A[LOOP:0: B:0:0x0000->B:32:0x005f, LOOP_END] */
    /* JADX WARNING: Removed duplicated region for block: B:38:0x0063 A[SYNTHETIC] */
    /* renamed from: K  reason: contains not printable characters */
    public final C2543 m6681(int i, int i2, int i3, int i4, boolean z, boolean z2) throws IOException {
        C2543 r0;
        boolean z3;
        while (true) {
            r0 = m6680(i, i2, i3, i4, z);
            synchronized (this.f8619) {
                if (r0.f8586 == 0) {
                    return r0;
                }
            }
            m6687();
        }
        z3 = true;
        if (!z3) {
            return r0;
        }
        m6687();
        if (!z3) {
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public final C2543 m6680(int i, int i2, int i3, int i4, boolean z) throws IOException {
        C2539 r4;
        Socket r3;
        C2543 r7;
        C2543 r0;
        boolean z2;
        boolean z3;
        C2543 r32;
        Socket socket;
        C2544.C2545 k;
        String str;
        int i5;
        synchronized (this.f8619) {
            if (this.f8616) {
                throw new IllegalStateException("released");
            } else if (this.f8609 != null) {
                throw new IllegalStateException("codec != null");
            } else if (!this.f8621) {
                C2543 r02 = this.f8617;
                C2543 r33 = this.f8617;
                r4 = null;
                r3 = (r33 == null || !r33.f8582) ? null : m6679(false, false, true);
                if (this.f8617 != null) {
                    r0 = this.f8617;
                    r7 = null;
                } else {
                    r7 = r02;
                    r0 = null;
                }
                if (!this.f8611) {
                    r7 = null;
                }
                if (r0 == null) {
                    AbstractC2570.f8676.m6759(this.f8619, this.f8615, this, null);
                    if (this.f8617 != null) {
                        r0 = this.f8617;
                        z2 = true;
                    } else {
                        r4 = this.f8608;
                    }
                }
                z2 = false;
            } else {
                throw new IOException("Canceled");
            }
        }
        C2540.m6652(r3);
        if (r7 != null) {
            this.f8618.m6625();
        }
        if (z2) {
            this.f8618.m6617();
        }
        if (r0 != null) {
            return r0;
        }
        if (r4 != null || ((k = this.f8613) != null && k.m6672())) {
            z3 = false;
        } else {
            C2544 r2 = this.f8620;
            if (r2.m6671()) {
                ArrayList arrayList = new ArrayList();
                while (r2.m6669()) {
                    if (r2.m6669()) {
                        List<Proxy> list = r2.f8594;
                        int i6 = r2.f8598;
                        r2.f8598 = i6 + 1;
                        Proxy proxy = list.get(i6);
                        r2.f8596 = new ArrayList();
                        if (proxy.type() == Proxy.Type.DIRECT || proxy.type() == Proxy.Type.SOCKS) {
                            C2531 r10 = r2.f8597.f8969;
                            str = r10.f8542;
                            i5 = r10.f8536;
                        } else {
                            SocketAddress address = proxy.address();
                            if (address instanceof InetSocketAddress) {
                                InetSocketAddress inetSocketAddress = (InetSocketAddress) address;
                                InetAddress address2 = inetSocketAddress.getAddress();
                                if (address2 == null) {
                                    str = inetSocketAddress.getHostName();
                                } else {
                                    str = address2.getHostAddress();
                                }
                                i5 = inetSocketAddress.getPort();
                            } else {
                                StringBuilder r22 = C1293.m3416("Proxy.address() is not an InetSocketAddress: ");
                                r22.append(address.getClass());
                                throw new IllegalArgumentException(r22.toString());
                            }
                        }
                        if (i5 >= 1 && i5 <= 65535) {
                            if (proxy.type() == Proxy.Type.SOCKS) {
                                r2.f8596.add(InetSocketAddress.createUnresolved(str, i5));
                            } else {
                                r2.f8599.m6622();
                                List<InetAddress> r9 = r2.f8597.f8967.m6974(str);
                                if (!r9.isEmpty()) {
                                    r2.f8599.m6615();
                                    int size = r9.size();
                                    for (int i7 = 0; i7 < size; i7++) {
                                        r2.f8596.add(new InetSocketAddress(r9.get(i7), i5));
                                    }
                                } else {
                                    throw new UnknownHostException(r2.f8597.f8967 + " returned no addresses for " + str);
                                }
                            }
                            int size2 = r2.f8596.size();
                            for (int i8 = 0; i8 < size2; i8++) {
                                C2539 r11 = new C2539(r2.f8597, proxy, r2.f8596.get(i8));
                                if (r2.f8595.m6688(r11)) {
                                    r2.f8600.add(r11);
                                } else {
                                    arrayList.add(r11);
                                }
                            }
                            if (!arrayList.isEmpty()) {
                                break;
                            }
                        } else {
                            throw new SocketException("No route to " + str + CertificateUtil.DELIMITER + i5 + "; port is out of range");
                        }
                    } else {
                        StringBuilder r34 = C1293.m3416("No route to ");
                        r34.append(r2.f8597.f8969.f8542);
                        r34.append("; exhausted proxy configurations: ");
                        r34.append(r2.f8594);
                        throw new SocketException(r34.toString());
                    }
                }
                if (arrayList.isEmpty()) {
                    arrayList.addAll(r2.f8600);
                    r2.f8600.clear();
                }
                this.f8613 = new C2544.C2545(arrayList);
                z3 = true;
            } else {
                throw new NoSuchElementException();
            }
        }
        synchronized (this.f8619) {
            if (!this.f8621) {
                if (z3) {
                    List<C2539> r23 = this.f8613.m6673();
                    int size3 = r23.size();
                    int i9 = 0;
                    while (true) {
                        if (i9 >= size3) {
                            break;
                        }
                        C2539 r102 = r23.get(i9);
                        AbstractC2570.f8676.m6759(this.f8619, this.f8615, this, r102);
                        if (this.f8617 != null) {
                            r0 = this.f8617;
                            this.f8608 = r102;
                            z2 = true;
                            break;
                        }
                        i9++;
                    }
                }
                if (!z2) {
                    if (r4 == null) {
                        C2544.C2545 k2 = this.f8613;
                        if (k2.m6672()) {
                            List<C2539> list2 = k2.f8602;
                            int i10 = k2.f8601;
                            k2.f8601 = i10 + 1;
                            r4 = list2.get(i10);
                        } else {
                            throw new NoSuchElementException();
                        }
                    }
                    this.f8608 = r4;
                    this.f8612 = 0;
                    r0 = new C2543(this.f8619, r4);
                    m6685(r0, false);
                }
            } else {
                throw new IOException("Canceled");
            }
        }
        if (z2) {
            this.f8618.m6617();
            return r0;
        }
        r0.m6660(i, i2, i3, i4, z, this.f8610, this.f8618);
        AbstractC2570.f8676.m6760(this.f8619).m6690(r0.f8579);
        synchronized (this.f8619) {
            this.f8611 = true;
            AbstractC2570.f8676.m6756(this.f8619, r0);
            if (r0.m6666()) {
                socket = AbstractC2570.f8676.m6758(this.f8619, this.f8615, this);
                r32 = this.f8617;
            } else {
                r32 = r0;
                socket = null;
            }
        }
        C2540.m6652(socket);
        this.f8618.m6617();
        return r32;
    }

    /* renamed from: K  reason: contains not printable characters */
    public void m6686(boolean z, AbstractC2576 r3, long j, IOException iOException) {
        C2543 r5;
        Socket r2;
        boolean z2;
        this.f8618.m6620();
        synchronized (this.f8619) {
            if (r3 != null) {
                if (r3 == this.f8609) {
                    if (!z) {
                        this.f8617.f8586++;
                    }
                    r5 = this.f8617;
                    r2 = m6679(z, false, true);
                    if (this.f8617 != null) {
                        r5 = null;
                    }
                    z2 = this.f8616;
                }
            }
            throw new IllegalStateException("expected " + this.f8609 + " but was " + r3);
        }
        C2540.m6652(r2);
        if (r5 != null) {
            this.f8618.m6625();
        }
        if (iOException != null) {
            this.f8618.m6616();
        } else if (z2) {
            this.f8618.m6618();
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public final Socket m6679(boolean z, boolean z2, boolean z3) {
        Socket socket;
        if (z3) {
            this.f8609 = null;
        }
        if (z2) {
            this.f8616 = true;
        }
        C2543 r4 = this.f8617;
        if (r4 != null) {
            if (z) {
                r4.f8582 = true;
            }
            if (this.f8609 == null && (this.f8616 || this.f8617.f8582)) {
                C2543 r3 = this.f8617;
                int size = r3.f8580.size();
                for (int i = 0; i < size; i++) {
                    if (r3.f8580.get(i).get() == this) {
                        r3.f8580.remove(i);
                        if (this.f8617.f8580.isEmpty()) {
                            this.f8617.f8587 = System.nanoTime();
                            if (AbstractC2570.f8676.m6765(this.f8619, this.f8617)) {
                                socket = this.f8617.f8581;
                                this.f8617 = null;
                                return socket;
                            }
                        }
                        socket = null;
                        this.f8617 = null;
                        return socket;
                    }
                }
                throw new IllegalStateException();
            }
        }
        return null;
    }

    /* renamed from: K  reason: contains not printable characters */
    public void m6683() {
        AbstractC2576 r1;
        C2543 r2;
        synchronized (this.f8619) {
            this.f8621 = true;
            r1 = this.f8609;
            r2 = this.f8617;
        }
        if (r1 != null) {
            r1.cancel();
        } else if (r2 != null) {
            C2540.m6652(r2.f8590);
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public void m6684(IOException iOException) {
        boolean z;
        C2543 r1;
        Socket r7;
        synchronized (this.f8619) {
            if (iOException instanceof StreamResetException) {
                ErrorCode errorCode = ((StreamResetException) iOException).errorCode;
                if (errorCode == ErrorCode.REFUSED_STREAM) {
                    this.f8612++;
                    if (this.f8612 > 1) {
                        this.f8608 = null;
                    }
                    z = false;
                    r1 = this.f8617;
                    r7 = m6679(z, false, true);
                    if (this.f8617 != null || !this.f8611) {
                        r1 = null;
                    }
                } else {
                    if (errorCode != ErrorCode.CANCEL) {
                        this.f8608 = null;
                    }
                    z = false;
                    r1 = this.f8617;
                    r7 = m6679(z, false, true);
                    r1 = null;
                }
            } else {
                if (this.f8617 != null && (!this.f8617.m6666() || (iOException instanceof ConnectionShutdownException))) {
                    if (this.f8617.f8586 == 0) {
                        if (!(this.f8608 == null || iOException == null)) {
                            this.f8620.m6670(this.f8608, iOException);
                        }
                        this.f8608 = null;
                    }
                }
                z = false;
                r1 = this.f8617;
                r7 = m6679(z, false, true);
                r1 = null;
            }
            z = true;
            r1 = this.f8617;
            r7 = m6679(z, false, true);
            r1 = null;
        }
        C2540.m6652(r7);
        if (r1 != null) {
            this.f8618.m6625();
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public void m6685(C2543 r2, boolean z) {
        if (this.f8617 == null) {
            this.f8617 = r2;
            this.f8611 = z;
            r2.f8580.add(new C2549(this, this.f8614));
            return;
        }
        throw new IllegalStateException();
    }
}
