package R4;

import java.io.Closeable;
import java.io.IOException;
import java.net.Socket;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import r4.AbstractC0983h;

/* JADX INFO: loaded from: classes.dex */
public final class q implements Closeable {
    public static final D J;

    /* JADX INFO: renamed from: A, reason: collision with root package name */
    public D f2705A;

    /* JADX INFO: renamed from: B, reason: collision with root package name */
    public long f2706B;

    /* JADX INFO: renamed from: C, reason: collision with root package name */
    public long f2707C;

    /* JADX INFO: renamed from: D, reason: collision with root package name */
    public long f2708D;

    /* JADX INFO: renamed from: E, reason: collision with root package name */
    public long f2709E;

    /* JADX INFO: renamed from: F, reason: collision with root package name */
    public final Socket f2710F;

    /* JADX INFO: renamed from: G, reason: collision with root package name */
    public final z f2711G;

    /* JADX INFO: renamed from: H, reason: collision with root package name */
    public final l f2712H;
    public final LinkedHashSet I;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final i f2713k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final LinkedHashMap f2714l = new LinkedHashMap();

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final String f2715m;

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

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public int f2717o;
    public boolean p;
    public final N4.d q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final N4.c f2718r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public final N4.c f2719s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public final N4.c f2720t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public final C f2721u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public long f2722v;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public long f2723w;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public long f2724x;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    public long f2725y;

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    public final D f2726z;

    static {
        D d3 = new D();
        d3.c(7, 65535);
        d3.c(5, 16384);
        J = d3;
    }

    public q(A2.p pVar) {
        this.f2713k = (i) pVar.f53f;
        String str = (String) pVar.f48a;
        if (str == null) {
            AbstractC0983h.i("connectionName");
            throw null;
        }
        this.f2715m = str;
        this.f2717o = 3;
        N4.d dVar = (N4.d) pVar.f49b;
        this.q = dVar;
        this.f2718r = dVar.e();
        this.f2719s = dVar.e();
        this.f2720t = dVar.e();
        this.f2721u = C.f2644a;
        D d3 = new D();
        d3.c(7, 16777216);
        this.f2726z = d3;
        this.f2705A = J;
        this.f2709E = r0.a();
        Socket socket = (Socket) pVar.f50c;
        if (socket == null) {
            AbstractC0983h.i("socket");
            throw null;
        }
        this.f2710F = socket;
        X4.n nVar = (X4.n) pVar.f52e;
        if (nVar == null) {
            AbstractC0983h.i("sink");
            throw null;
        }
        this.f2711G = new z(nVar);
        X4.o oVar = (X4.o) pVar.f51d;
        if (oVar == null) {
            AbstractC0983h.i("source");
            throw null;
        }
        this.f2712H = new l(this, new u(oVar));
        this.I = new LinkedHashSet();
    }

    public final void a(EnumC0183b enumC0183b, EnumC0183b enumC0183b2, IOException iOException) {
        int i;
        Object[] array;
        byte[] bArr = L4.b.f2100a;
        try {
            k(enumC0183b);
        } catch (IOException unused) {
        }
        synchronized (this) {
            if (this.f2714l.isEmpty()) {
                array = null;
            } else {
                array = this.f2714l.values().toArray(new y[0]);
                if (array == null) {
                    throw new NullPointerException("null cannot be cast to non-null type kotlin.Array<T of kotlin.collections.ArraysKt__ArraysJVMKt.toTypedArray>");
                }
                this.f2714l.clear();
            }
        }
        y[] yVarArr = (y[]) array;
        if (yVarArr != null) {
            for (y yVar : yVarArr) {
                try {
                    yVar.c(enumC0183b2, iOException);
                } catch (IOException unused2) {
                }
            }
        }
        try {
            this.f2711G.close();
        } catch (IOException unused3) {
        }
        try {
            this.f2710F.close();
        } catch (IOException unused4) {
        }
        this.f2718r.e();
        this.f2719s.e();
        this.f2720t.e();
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final void close() {
        a(EnumC0183b.NO_ERROR, EnumC0183b.CANCEL, null);
    }

    public final void e(IOException iOException) {
        EnumC0183b enumC0183b = EnumC0183b.PROTOCOL_ERROR;
        a(enumC0183b, enumC0183b, iOException);
    }

    public final synchronized y f(int i) {
        return (y) this.f2714l.get(Integer.valueOf(i));
    }

    public final void flush() {
        this.f2711G.flush();
    }

    public final synchronized y h(int i) {
        y yVar;
        yVar = (y) this.f2714l.remove(Integer.valueOf(i));
        notifyAll();
        return yVar;
    }

    public final void k(EnumC0183b enumC0183b) {
        synchronized (this.f2711G) {
            synchronized (this) {
                if (this.p) {
                    return;
                }
                this.p = true;
                this.f2711G.h(this.f2716n, enumC0183b, L4.b.f2100a);
            }
        }
    }

    public final synchronized void l(long j5) {
        long j6 = this.f2706B + j5;
        this.f2706B = j6;
        long j7 = j6 - this.f2707C;
        if (j7 >= this.f2726z.a() / 2) {
            o(j7, 0);
            this.f2707C += j7;
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:19:0x0035, code lost:
    
        r2 = java.lang.Math.min((int) java.lang.Math.min(r12, r6 - r4), r8.f2711G.f2769m);
        r6 = r2;
        r8.f2708D += r6;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public final void m(int r9, boolean r10, X4.e r11, long r12) {
        /*
            r8 = this;
            r0 = 0
            int r2 = (r12 > r0 ? 1 : (r12 == r0 ? 0 : -1))
            r3 = 0
            if (r2 != 0) goto Ld
            R4.z r12 = r8.f2711G
            r12.e(r10, r9, r11, r3)
            return
        Ld:
            int r2 = (r12 > r0 ? 1 : (r12 == r0 ? 0 : -1))
            if (r2 <= 0) goto L68
            monitor-enter(r8)
        L12:
            long r4 = r8.f2708D     // Catch: java.lang.Throwable -> L2a java.lang.InterruptedException -> L59
            long r6 = r8.f2709E     // Catch: java.lang.Throwable -> L2a java.lang.InterruptedException -> L59
            int r2 = (r4 > r6 ? 1 : (r4 == r6 ? 0 : -1))
            if (r2 < 0) goto L34
            java.util.LinkedHashMap r2 = r8.f2714l     // Catch: java.lang.Throwable -> L2a java.lang.InterruptedException -> L59
            java.lang.Integer r4 = java.lang.Integer.valueOf(r9)     // Catch: java.lang.Throwable -> L2a java.lang.InterruptedException -> L59
            boolean r2 = r2.containsKey(r4)     // Catch: java.lang.Throwable -> L2a java.lang.InterruptedException -> L59
            if (r2 == 0) goto L2c
            r8.wait()     // Catch: java.lang.Throwable -> L2a java.lang.InterruptedException -> L59
            goto L12
        L2a:
            r9 = move-exception
            goto L66
        L2c:
            java.io.IOException r9 = new java.io.IOException     // Catch: java.lang.Throwable -> L2a java.lang.InterruptedException -> L59
            java.lang.String r10 = "stream closed"
            r9.<init>(r10)     // Catch: java.lang.Throwable -> L2a java.lang.InterruptedException -> L59
            throw r9     // Catch: java.lang.Throwable -> L2a java.lang.InterruptedException -> L59
        L34:
            long r6 = r6 - r4
            long r4 = java.lang.Math.min(r12, r6)     // Catch: java.lang.Throwable -> L2a
            int r2 = (int) r4     // Catch: java.lang.Throwable -> L2a
            R4.z r4 = r8.f2711G     // Catch: java.lang.Throwable -> L2a
            int r4 = r4.f2769m     // Catch: java.lang.Throwable -> L2a
            int r2 = java.lang.Math.min(r2, r4)     // Catch: java.lang.Throwable -> L2a
            long r4 = r8.f2708D     // Catch: java.lang.Throwable -> L2a
            long r6 = (long) r2     // Catch: java.lang.Throwable -> L2a
            long r4 = r4 + r6
            r8.f2708D = r4     // Catch: java.lang.Throwable -> L2a
            monitor-exit(r8)
            long r12 = r12 - r6
            R4.z r4 = r8.f2711G
            if (r10 == 0) goto L54
            int r5 = (r12 > r0 ? 1 : (r12 == r0 ? 0 : -1))
            if (r5 != 0) goto L54
            r5 = 1
            goto L55
        L54:
            r5 = r3
        L55:
            r4.e(r5, r9, r11, r2)
            goto Ld
        L59:
            java.lang.Thread r9 = java.lang.Thread.currentThread()     // Catch: java.lang.Throwable -> L2a
            r9.interrupt()     // Catch: java.lang.Throwable -> L2a
            java.io.InterruptedIOException r9 = new java.io.InterruptedIOException     // Catch: java.lang.Throwable -> L2a
            r9.<init>()     // Catch: java.lang.Throwable -> L2a
            throw r9     // Catch: java.lang.Throwable -> L2a
        L66:
            monitor-exit(r8)
            throw r9
        L68:
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: R4.q.m(int, boolean, X4.e, long):void");
    }

    public final void n(int i, EnumC0183b enumC0183b) {
        this.f2718r.c(new o(this.f2715m + '[' + i + "] writeSynReset", this, i, enumC0183b), 0L);
    }

    public final void o(long j5, int i) {
        this.f2718r.c(new p(this.f2715m + '[' + i + "] windowUpdate", this, i, j5), 0L);
    }
}
