package r7;

import java.io.Closeable;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.Socket;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import x7.C1731g;

/* JADX INFO: loaded from: classes.dex */
public final class o implements Closeable, AutoCloseable {

    /* JADX INFO: renamed from: P, reason: collision with root package name */
    public static final A f16632P;

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

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

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

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

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

    /* JADX INFO: renamed from: G, reason: collision with root package name */
    public A f16638G;
    public long H;

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

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

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

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

    /* JADX INFO: renamed from: M, reason: collision with root package name */
    public final x f16643M;

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

    /* JADX INFO: renamed from: O, reason: collision with root package name */
    public final LinkedHashSet f16645O;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final h f16646q;

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

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

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

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

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

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public final n7.d f16652w;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public final n7.c f16653x;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    public final n7.c f16654y;

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    public final n7.c f16655z;

    static {
        A a3 = new A();
        a3.c(7, 65535);
        a3.c(5, 16384);
        f16632P = a3;
    }

    public o(O3.q qVar) {
        this.f16646q = (h) qVar.f5170f;
        String str = (String) qVar.f5165a;
        if (str == null) {
            P6.g.h("connectionName");
            throw null;
        }
        this.f16648s = str;
        this.f16650u = 3;
        n7.d dVar = (n7.d) qVar.f5166b;
        this.f16652w = dVar;
        this.f16653x = dVar.e();
        this.f16654y = dVar.e();
        this.f16655z = dVar.e();
        this.f16633A = z.f16707a;
        A a3 = new A();
        a3.c(7, 16777216);
        this.f16637F = a3;
        this.f16638G = f16632P;
        this.f16641K = r0.a();
        Socket socket = (Socket) qVar.f5167c;
        if (socket == null) {
            P6.g.h("socket");
            throw null;
        }
        this.f16642L = socket;
        x7.A a8 = (x7.A) qVar.f5169e;
        if (a8 == null) {
            P6.g.h("sink");
            throw null;
        }
        this.f16643M = new x(a8);
        x7.B b8 = (x7.B) qVar.f5168d;
        if (b8 == null) {
            P6.g.h("source");
            throw null;
        }
        this.f16644N = new k(this, new s(b8));
        this.f16645O = new LinkedHashSet();
    }

    public final void A(long j8, int i) {
        this.f16653x.c(new n(this.f16648s + '[' + i + "] windowUpdate", this, i, j8), 0L);
    }

    public final void a(int i, int i5, IOException iOException) {
        int i8;
        Object[] array;
        I1.a.t(i, "connectionCode");
        I1.a.t(i5, "streamCode");
        byte[] bArr = l7.b.f14412a;
        try {
            m(i);
        } catch (IOException unused) {
        }
        synchronized (this) {
            if (this.f16647r.isEmpty()) {
                array = null;
            } else {
                array = this.f16647r.values().toArray(new w[0]);
                this.f16647r.clear();
            }
        }
        w[] wVarArr = (w[]) array;
        if (wVarArr != null) {
            for (w wVar : wVarArr) {
                try {
                    wVar.c(i5, iOException);
                } catch (IOException unused2) {
                }
            }
        }
        try {
            this.f16643M.close();
        } catch (IOException unused3) {
        }
        try {
            this.f16642L.close();
        } catch (IOException unused4) {
        }
        this.f16653x.e();
        this.f16654y.e();
        this.f16655z.e();
    }

    public final synchronized w b(int i) {
        return (w) this.f16647r.get(Integer.valueOf(i));
    }

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

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

    public final synchronized boolean g(long j8) {
        if (this.f16651v) {
            return false;
        }
        if (this.D < this.f16635C) {
            if (j8 >= this.f16636E) {
                return false;
            }
        }
        return true;
    }

    public final synchronized w j(int i) {
        w wVar;
        wVar = (w) this.f16647r.remove(Integer.valueOf(i));
        notifyAll();
        return wVar;
    }

    public final void m(int i) {
        I1.a.t(i, "statusCode");
        synchronized (this.f16643M) {
            synchronized (this) {
                if (this.f16651v) {
                    return;
                }
                this.f16651v = true;
                this.f16643M.j(l7.b.f14412a, this.f16649t, i);
            }
        }
    }

    public final synchronized void n(long j8) {
        long j9 = this.H + j8;
        this.H = j9;
        long j10 = j9 - this.f16639I;
        if (j10 >= this.f16637F.a() / 2) {
            A(j10, 0);
            this.f16639I += j10;
        }
    }

    /* 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.f16643M.f16701s);
        r6 = r2;
        r8.f16640J += r6;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void r(int i, boolean z8, C1731g c1731g, long j8) {
        int iMin;
        long j9;
        if (j8 == 0) {
            this.f16643M.b(z8, i, c1731g, 0);
            return;
        }
        while (j8 > 0) {
            synchronized (this) {
                while (true) {
                    try {
                        try {
                            long j10 = this.f16640J;
                            long j11 = this.f16641K;
                            if (j10 < j11) {
                                break;
                            } else {
                                if (!this.f16647r.containsKey(Integer.valueOf(i))) {
                                    throw new IOException("stream closed");
                                }
                                wait();
                            }
                        } catch (InterruptedException unused) {
                            Thread.currentThread().interrupt();
                            throw new InterruptedIOException();
                        }
                    } catch (Throwable th) {
                        throw th;
                    }
                }
            }
            j8 -= j9;
            this.f16643M.b(z8 && j8 == 0, i, c1731g, iMin);
        }
    }

    public final void u(int i, int i5) {
        I1.a.t(i5, "errorCode");
        this.f16653x.c(new j(this.f16648s + '[' + i + "] writeSynReset", this, i, i5, 2), 0L);
    }
}
