package cr;

import cr.h;
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 java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import kotlin.jvm.internal.m0;
import kotlin.jvm.internal.o0;
import kotlin.jvm.internal.t;
import kotlin.jvm.internal.v;
import rm.n0;
import vq.p;

/* loaded from: classes3.dex */
public final class f implements Closeable {
    public static final c R = new c(null);
    private static final m S;
    private final cr.l A;
    private long B;
    private long C;
    private long D;
    private long E;
    private long F;
    private long G;
    private final m H;
    private m I;
    private long J;
    private long K;
    private long L;
    private long M;
    private final Socket N;
    private final cr.j O;
    private final e P;
    private final Set Q;

    /* renamed from: p */
    private final boolean f15560p;

    /* renamed from: q */
    private final d f15561q;

    /* renamed from: r */
    private final Map f15562r;

    /* renamed from: s */
    private final String f15563s;

    /* renamed from: t */
    private int f15564t;

    /* renamed from: u */
    private int f15565u;

    /* renamed from: v */
    private boolean f15566v;

    /* renamed from: w */
    private final yq.d f15567w;

    /* renamed from: x */
    private final yq.c f15568x;

    /* renamed from: y */
    private final yq.c f15569y;

    /* renamed from: z */
    private final yq.c f15570z;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes3.dex */
    public static final class a extends v implements dn.a {

        /* renamed from: q */
        final /* synthetic */ long f15572q;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        a(long j10) {
            super(0);
            this.f15572q = j10;
        }

        @Override // dn.a
        /* renamed from: a */
        public final Long invoke() {
            boolean z10;
            f fVar = f.this;
            synchronized (fVar) {
                if (fVar.C < fVar.B) {
                    z10 = true;
                } else {
                    fVar.B++;
                    z10 = false;
                }
            }
            if (z10) {
                f.this.C(null);
                return -1L;
            }
            f.this.C0(false, 1, 0);
            return Long.valueOf(this.f15572q);
        }
    }

    /* loaded from: classes3.dex */
    public static final class b {

        /* renamed from: a */
        private boolean f15573a;

        /* renamed from: b */
        private final yq.d f15574b;

        /* renamed from: c */
        public Socket f15575c;

        /* renamed from: d */
        public String f15576d;

        /* renamed from: e */
        public kr.f f15577e;

        /* renamed from: f */
        public kr.e f15578f;

        /* renamed from: g */
        private d f15579g;

        /* renamed from: h */
        private cr.l f15580h;

        /* renamed from: i */
        private int f15581i;

        public b(boolean z10, yq.d taskRunner) {
            t.g(taskRunner, "taskRunner");
            this.f15573a = z10;
            this.f15574b = taskRunner;
            this.f15579g = d.f15583b;
            this.f15580h = cr.l.f15684b;
        }

        public final f a() {
            return new f(this);
        }

        public final boolean b() {
            return this.f15573a;
        }

        public final String c() {
            String str = this.f15576d;
            if (str != null) {
                return str;
            }
            t.y("connectionName");
            return null;
        }

        public final d d() {
            return this.f15579g;
        }

        public final int e() {
            return this.f15581i;
        }

        public final cr.l f() {
            return this.f15580h;
        }

        public final kr.e g() {
            kr.e eVar = this.f15578f;
            if (eVar != null) {
                return eVar;
            }
            t.y("sink");
            return null;
        }

        public final Socket h() {
            Socket socket = this.f15575c;
            if (socket != null) {
                return socket;
            }
            t.y("socket");
            return null;
        }

        public final kr.f i() {
            kr.f fVar = this.f15577e;
            if (fVar != null) {
                return fVar;
            }
            t.y("source");
            return null;
        }

        public final yq.d j() {
            return this.f15574b;
        }

        public final b k(d listener) {
            t.g(listener, "listener");
            this.f15579g = listener;
            return this;
        }

        public final b l(int i10) {
            this.f15581i = i10;
            return this;
        }

        public final void m(String str) {
            t.g(str, "<set-?>");
            this.f15576d = str;
        }

        public final void n(kr.e eVar) {
            t.g(eVar, "<set-?>");
            this.f15578f = eVar;
        }

        public final void o(Socket socket) {
            t.g(socket, "<set-?>");
            this.f15575c = socket;
        }

        public final void p(kr.f fVar) {
            t.g(fVar, "<set-?>");
            this.f15577e = fVar;
        }

        public final b q(Socket socket, String peerName, kr.f source, kr.e sink) {
            String str;
            t.g(socket, "socket");
            t.g(peerName, "peerName");
            t.g(source, "source");
            t.g(sink, "sink");
            o(socket);
            if (this.f15573a) {
                str = p.f40483f + ' ' + peerName;
            } else {
                str = "MockWebServer " + peerName;
            }
            m(str);
            p(source);
            n(sink);
            return this;
        }
    }

    /* loaded from: classes3.dex */
    public static final class c {
        private c() {
        }

        public /* synthetic */ c(kotlin.jvm.internal.k kVar) {
            this();
        }

        public final m a() {
            return f.S;
        }
    }

    /* loaded from: classes3.dex */
    public static abstract class d {

        /* renamed from: a */
        public static final b f15582a = new b(null);

        /* renamed from: b */
        public static final d f15583b = new a();

        /* loaded from: classes3.dex */
        public static final class a extends d {
            a() {
            }

            @Override // cr.f.d
            public void c(cr.i stream) {
                t.g(stream, "stream");
                stream.e(cr.b.REFUSED_STREAM, null);
            }
        }

        /* loaded from: classes3.dex */
        public static final class b {
            private b() {
            }

            public /* synthetic */ b(kotlin.jvm.internal.k kVar) {
                this();
            }
        }

        public void b(f connection, m settings) {
            t.g(connection, "connection");
            t.g(settings, "settings");
        }

        public abstract void c(cr.i iVar);
    }

    /* loaded from: classes3.dex */
    public final class e implements h.c, dn.a {

        /* renamed from: p */
        private final cr.h f15584p;

        /* renamed from: q */
        final /* synthetic */ f f15585q;

        /* loaded from: classes3.dex */
        public static final class a extends v implements dn.a {

            /* renamed from: p */
            final /* synthetic */ f f15586p;

            /* renamed from: q */
            final /* synthetic */ o0 f15587q;

            /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
            a(f fVar, o0 o0Var) {
                super(0);
                this.f15586p = fVar;
                this.f15587q = o0Var;
            }

            @Override // dn.a
            public /* bridge */ /* synthetic */ Object invoke() {
                m227invoke();
                return n0.f34904a;
            }

            /* renamed from: invoke */
            public final void m227invoke() {
                this.f15586p.K().b(this.f15586p, (m) this.f15587q.f26199p);
            }
        }

        /* loaded from: classes3.dex */
        static final class b extends v implements dn.a {

            /* renamed from: p */
            final /* synthetic */ f f15588p;

            /* renamed from: q */
            final /* synthetic */ cr.i f15589q;

            /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
            b(f fVar, cr.i iVar) {
                super(0);
                this.f15588p = fVar;
                this.f15589q = iVar;
            }

            @Override // dn.a
            public /* bridge */ /* synthetic */ Object invoke() {
                m228invoke();
                return n0.f34904a;
            }

            /* renamed from: invoke */
            public final void m228invoke() {
                try {
                    this.f15588p.K().c(this.f15589q);
                } catch (IOException e10) {
                    dr.h.f16598a.g().j("Http2Connection.Listener failure for " + this.f15588p.I(), 4, e10);
                    try {
                        this.f15589q.e(cr.b.PROTOCOL_ERROR, e10);
                    } catch (IOException unused) {
                    }
                }
            }
        }

        /* loaded from: classes3.dex */
        static final class c extends v implements dn.a {

            /* renamed from: p */
            final /* synthetic */ f f15590p;

            /* renamed from: q */
            final /* synthetic */ int f15591q;

            /* renamed from: r */
            final /* synthetic */ int f15592r;

            /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
            c(f fVar, int i10, int i11) {
                super(0);
                this.f15590p = fVar;
                this.f15591q = i10;
                this.f15592r = i11;
            }

            @Override // dn.a
            public /* bridge */ /* synthetic */ Object invoke() {
                m229invoke();
                return n0.f34904a;
            }

            /* renamed from: invoke */
            public final void m229invoke() {
                this.f15590p.C0(true, this.f15591q, this.f15592r);
            }
        }

        /* loaded from: classes3.dex */
        static final class d extends v implements dn.a {

            /* renamed from: q */
            final /* synthetic */ boolean f15594q;

            /* renamed from: r */
            final /* synthetic */ m f15595r;

            /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
            d(boolean z10, m mVar) {
                super(0);
                this.f15594q = z10;
                this.f15595r = mVar;
            }

            @Override // dn.a
            public /* bridge */ /* synthetic */ Object invoke() {
                m230invoke();
                return n0.f34904a;
            }

            /* renamed from: invoke */
            public final void m230invoke() {
                e.this.p(this.f15594q, this.f15595r);
            }
        }

        public e(f fVar, cr.h reader) {
            t.g(reader, "reader");
            this.f15585q = fVar;
            this.f15584p = reader;
        }

        @Override // cr.h.c
        public void a(boolean z10, int i10, kr.f source, int i11) {
            t.g(source, "source");
            if (this.f15585q.o0(i10)) {
                this.f15585q.g0(i10, source, i11, z10);
                return;
            }
            cr.i S = this.f15585q.S(i10);
            if (S == null) {
                this.f15585q.F0(i10, cr.b.PROTOCOL_ERROR);
                long j10 = i11;
                this.f15585q.y0(j10);
                source.skip(j10);
                return;
            }
            S.y(source, i11);
            if (z10) {
                S.z(p.f40478a, true);
            }
        }

        @Override // cr.h.c
        public void b() {
        }

        @Override // cr.h.c
        public void c(boolean z10, int i10, int i11, List headerBlock) {
            t.g(headerBlock, "headerBlock");
            if (this.f15585q.o0(i10)) {
                this.f15585q.j0(i10, headerBlock, z10);
                return;
            }
            f fVar = this.f15585q;
            synchronized (fVar) {
                cr.i S = fVar.S(i10);
                if (S != null) {
                    n0 n0Var = n0.f34904a;
                    S.z(p.r(headerBlock), z10);
                    return;
                }
                if (fVar.f15566v) {
                    return;
                }
                if (i10 <= fVar.J()) {
                    return;
                }
                if (i10 % 2 == fVar.L() % 2) {
                    return;
                }
                cr.i iVar = new cr.i(i10, fVar, false, z10, p.r(headerBlock));
                fVar.s0(i10);
                fVar.T().put(Integer.valueOf(i10), iVar);
                yq.c.d(fVar.f15567w.i(), fVar.I() + '[' + i10 + "] onStream", 0L, false, new b(fVar, iVar), 6, null);
            }
        }

        @Override // cr.h.c
        public void d(int i10, long j10) {
            if (i10 == 0) {
                f fVar = this.f15585q;
                synchronized (fVar) {
                    fVar.M = fVar.V() + j10;
                    t.e(fVar, "null cannot be cast to non-null type java.lang.Object");
                    fVar.notifyAll();
                    n0 n0Var = n0.f34904a;
                }
                return;
            }
            cr.i S = this.f15585q.S(i10);
            if (S != null) {
                synchronized (S) {
                    S.b(j10);
                    n0 n0Var2 = n0.f34904a;
                }
            }
        }

        @Override // cr.h.c
        public void g(int i10, cr.b errorCode) {
            t.g(errorCode, "errorCode");
            if (this.f15585q.o0(i10)) {
                this.f15585q.n0(i10, errorCode);
                return;
            }
            cr.i p02 = this.f15585q.p0(i10);
            if (p02 != null) {
                p02.A(errorCode);
            }
        }

        @Override // cr.h.c
        public void h(boolean z10, int i10, int i11) {
            if (!z10) {
                yq.c.d(this.f15585q.f15568x, this.f15585q.I() + " ping", 0L, false, new c(this.f15585q, i10, i11), 6, null);
                return;
            }
            f fVar = this.f15585q;
            synchronized (fVar) {
                if (i10 == 1) {
                    fVar.C++;
                } else if (i10 != 2) {
                    if (i10 == 3) {
                        fVar.F++;
                        t.e(fVar, "null cannot be cast to non-null type java.lang.Object");
                        fVar.notifyAll();
                    }
                    n0 n0Var = n0.f34904a;
                } else {
                    fVar.E++;
                }
            }
        }

        @Override // cr.h.c
        public void i(int i10, int i11, int i12, boolean z10) {
        }

        @Override // dn.a
        public /* bridge */ /* synthetic */ Object invoke() {
            q();
            return n0.f34904a;
        }

        @Override // cr.h.c
        public void l(boolean z10, m settings) {
            t.g(settings, "settings");
            yq.c.d(this.f15585q.f15568x, this.f15585q.I() + " applyAndAckSettings", 0L, false, new d(z10, settings), 6, null);
        }

        @Override // cr.h.c
        public void m(int i10, int i11, List requestHeaders) {
            t.g(requestHeaders, "requestHeaders");
            this.f15585q.k0(i11, requestHeaders);
        }

        @Override // cr.h.c
        public void n(int i10, cr.b errorCode, kr.g debugData) {
            int i11;
            Object[] array;
            t.g(errorCode, "errorCode");
            t.g(debugData, "debugData");
            debugData.size();
            f fVar = this.f15585q;
            synchronized (fVar) {
                array = fVar.T().values().toArray(new cr.i[0]);
                t.e(array, "null cannot be cast to non-null type kotlin.Array<T of kotlin.collections.ArraysKt__ArraysJVMKt.toTypedArray>");
                fVar.f15566v = true;
                n0 n0Var = n0.f34904a;
            }
            for (cr.i iVar : (cr.i[]) array) {
                if (iVar.l() > i10 && iVar.v()) {
                    iVar.A(cr.b.REFUSED_STREAM);
                    this.f15585q.p0(iVar.l());
                }
            }
        }

        public final void p(boolean z10, m settings) {
            long c10;
            int i10;
            cr.i[] iVarArr;
            t.g(settings, "settings");
            o0 o0Var = new o0();
            cr.j X = this.f15585q.X();
            f fVar = this.f15585q;
            synchronized (X) {
                synchronized (fVar) {
                    m Q = fVar.Q();
                    if (!z10) {
                        m mVar = new m();
                        mVar.g(Q);
                        mVar.g(settings);
                        settings = mVar;
                    }
                    o0Var.f26199p = settings;
                    c10 = settings.c() - Q.c();
                    if (c10 != 0 && !fVar.T().isEmpty()) {
                        Object[] array = fVar.T().values().toArray(new cr.i[0]);
                        t.e(array, "null cannot be cast to non-null type kotlin.Array<T of kotlin.collections.ArraysKt__ArraysJVMKt.toTypedArray>");
                        iVarArr = (cr.i[]) array;
                        fVar.t0((m) o0Var.f26199p);
                        yq.c.d(fVar.f15570z, fVar.I() + " onSettings", 0L, false, new a(fVar, o0Var), 6, null);
                        n0 n0Var = n0.f34904a;
                    }
                    iVarArr = null;
                    fVar.t0((m) o0Var.f26199p);
                    yq.c.d(fVar.f15570z, fVar.I() + " onSettings", 0L, false, new a(fVar, o0Var), 6, null);
                    n0 n0Var2 = n0.f34904a;
                }
                try {
                    fVar.X().a((m) o0Var.f26199p);
                } catch (IOException e10) {
                    fVar.C(e10);
                }
                n0 n0Var3 = n0.f34904a;
            }
            if (iVarArr != null) {
                for (cr.i iVar : iVarArr) {
                    synchronized (iVar) {
                        iVar.b(c10);
                        n0 n0Var4 = n0.f34904a;
                    }
                }
            }
        }

        public void q() {
            cr.b bVar;
            cr.b bVar2 = cr.b.INTERNAL_ERROR;
            IOException e10 = null;
            try {
                this.f15584p.c(this);
                do {
                } while (this.f15584p.b(false, this));
                bVar = cr.b.NO_ERROR;
                try {
                    try {
                        this.f15585q.A(bVar, cr.b.CANCEL, null);
                    } catch (IOException e11) {
                        e10 = e11;
                        cr.b bVar3 = cr.b.PROTOCOL_ERROR;
                        this.f15585q.A(bVar3, bVar3, e10);
                        vq.m.f(this.f15584p);
                    }
                } catch (Throwable th2) {
                    th = th2;
                    this.f15585q.A(bVar, bVar2, e10);
                    vq.m.f(this.f15584p);
                    throw th;
                }
            } catch (IOException e12) {
                e10 = e12;
                bVar = bVar2;
            } catch (Throwable th3) {
                th = th3;
                bVar = bVar2;
                this.f15585q.A(bVar, bVar2, e10);
                vq.m.f(this.f15584p);
                throw th;
            }
            vq.m.f(this.f15584p);
        }
    }

    /* renamed from: cr.f$f */
    /* loaded from: classes3.dex */
    public static final class C0340f extends v implements dn.a {

        /* renamed from: q */
        final /* synthetic */ int f15597q;

        /* renamed from: r */
        final /* synthetic */ kr.d f15598r;

        /* renamed from: s */
        final /* synthetic */ int f15599s;

        /* renamed from: t */
        final /* synthetic */ boolean f15600t;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        C0340f(int i10, kr.d dVar, int i11, boolean z10) {
            super(0);
            this.f15597q = i10;
            this.f15598r = dVar;
            this.f15599s = i11;
            this.f15600t = z10;
        }

        @Override // dn.a
        public /* bridge */ /* synthetic */ Object invoke() {
            m231invoke();
            return n0.f34904a;
        }

        /* renamed from: invoke */
        public final void m231invoke() {
            f fVar = f.this;
            int i10 = this.f15597q;
            kr.d dVar = this.f15598r;
            int i11 = this.f15599s;
            boolean z10 = this.f15600t;
            try {
                boolean d10 = fVar.A.d(i10, dVar, i11, z10);
                if (d10) {
                    fVar.X().p(i10, cr.b.CANCEL);
                }
                if (d10 || z10) {
                    synchronized (fVar) {
                        fVar.Q.remove(Integer.valueOf(i10));
                    }
                }
            } catch (IOException unused) {
            }
        }
    }

    /* loaded from: classes3.dex */
    public static final class g extends v implements dn.a {

        /* renamed from: q */
        final /* synthetic */ int f15602q;

        /* renamed from: r */
        final /* synthetic */ List f15603r;

        /* renamed from: s */
        final /* synthetic */ boolean f15604s;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        g(int i10, List list, boolean z10) {
            super(0);
            this.f15602q = i10;
            this.f15603r = list;
            this.f15604s = z10;
        }

        @Override // dn.a
        public /* bridge */ /* synthetic */ Object invoke() {
            m232invoke();
            return n0.f34904a;
        }

        /* renamed from: invoke */
        public final void m232invoke() {
            boolean b10 = f.this.A.b(this.f15602q, this.f15603r, this.f15604s);
            f fVar = f.this;
            int i10 = this.f15602q;
            boolean z10 = this.f15604s;
            if (b10) {
                try {
                    fVar.X().p(i10, cr.b.CANCEL);
                } catch (IOException unused) {
                    return;
                }
            }
            if (b10 || z10) {
                synchronized (fVar) {
                    fVar.Q.remove(Integer.valueOf(i10));
                }
            }
        }
    }

    /* loaded from: classes3.dex */
    public static final class h extends v implements dn.a {

        /* renamed from: q */
        final /* synthetic */ int f15606q;

        /* renamed from: r */
        final /* synthetic */ List f15607r;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        h(int i10, List list) {
            super(0);
            this.f15606q = i10;
            this.f15607r = list;
        }

        @Override // dn.a
        public /* bridge */ /* synthetic */ Object invoke() {
            m233invoke();
            return n0.f34904a;
        }

        /* renamed from: invoke */
        public final void m233invoke() {
            boolean a10 = f.this.A.a(this.f15606q, this.f15607r);
            f fVar = f.this;
            int i10 = this.f15606q;
            if (a10) {
                try {
                    fVar.X().p(i10, cr.b.CANCEL);
                    synchronized (fVar) {
                        fVar.Q.remove(Integer.valueOf(i10));
                    }
                } catch (IOException unused) {
                }
            }
        }
    }

    /* loaded from: classes3.dex */
    public static final class i extends v implements dn.a {

        /* renamed from: q */
        final /* synthetic */ int f15609q;

        /* renamed from: r */
        final /* synthetic */ cr.b f15610r;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        i(int i10, cr.b bVar) {
            super(0);
            this.f15609q = i10;
            this.f15610r = bVar;
        }

        @Override // dn.a
        public /* bridge */ /* synthetic */ Object invoke() {
            m234invoke();
            return n0.f34904a;
        }

        /* renamed from: invoke */
        public final void m234invoke() {
            f.this.A.c(this.f15609q, this.f15610r);
            f fVar = f.this;
            int i10 = this.f15609q;
            synchronized (fVar) {
                fVar.Q.remove(Integer.valueOf(i10));
                n0 n0Var = n0.f34904a;
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes3.dex */
    public static final class j extends v implements dn.a {
        j() {
            super(0);
        }

        @Override // dn.a
        public /* bridge */ /* synthetic */ Object invoke() {
            m235invoke();
            return n0.f34904a;
        }

        /* renamed from: invoke */
        public final void m235invoke() {
            f.this.C0(false, 2, 0);
        }
    }

    /* loaded from: classes3.dex */
    public static final class k extends v implements dn.a {

        /* renamed from: q */
        final /* synthetic */ int f15613q;

        /* renamed from: r */
        final /* synthetic */ cr.b f15614r;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        k(int i10, cr.b bVar) {
            super(0);
            this.f15613q = i10;
            this.f15614r = bVar;
        }

        @Override // dn.a
        public /* bridge */ /* synthetic */ Object invoke() {
            m236invoke();
            return n0.f34904a;
        }

        /* renamed from: invoke */
        public final void m236invoke() {
            try {
                f.this.D0(this.f15613q, this.f15614r);
            } catch (IOException e10) {
                f.this.C(e10);
            }
        }
    }

    /* loaded from: classes3.dex */
    public static final class l extends v implements dn.a {

        /* renamed from: q */
        final /* synthetic */ int f15616q;

        /* renamed from: r */
        final /* synthetic */ long f15617r;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        l(int i10, long j10) {
            super(0);
            this.f15616q = i10;
            this.f15617r = j10;
        }

        @Override // dn.a
        public /* bridge */ /* synthetic */ Object invoke() {
            m237invoke();
            return n0.f34904a;
        }

        /* renamed from: invoke */
        public final void m237invoke() {
            try {
                f.this.X().s(this.f15616q, this.f15617r);
            } catch (IOException e10) {
                f.this.C(e10);
            }
        }
    }

    static {
        m mVar = new m();
        mVar.h(7, 65535);
        mVar.h(5, 16384);
        S = mVar;
    }

    public f(b builder) {
        t.g(builder, "builder");
        boolean b10 = builder.b();
        this.f15560p = b10;
        this.f15561q = builder.d();
        this.f15562r = new LinkedHashMap();
        String c10 = builder.c();
        this.f15563s = c10;
        this.f15565u = builder.b() ? 3 : 2;
        yq.d j10 = builder.j();
        this.f15567w = j10;
        yq.c i10 = j10.i();
        this.f15568x = i10;
        this.f15569y = j10.i();
        this.f15570z = j10.i();
        this.A = builder.f();
        m mVar = new m();
        if (builder.b()) {
            mVar.h(7, 16777216);
        }
        this.H = mVar;
        this.I = S;
        this.M = r2.c();
        this.N = builder.h();
        this.O = new cr.j(builder.g(), b10);
        this.P = new e(this, new cr.h(builder.i(), b10));
        this.Q = new LinkedHashSet();
        if (builder.e() != 0) {
            long nanos = TimeUnit.MILLISECONDS.toNanos(builder.e());
            i10.k(c10 + " ping", nanos, new a(nanos));
        }
    }

    public final void C(IOException iOException) {
        cr.b bVar = cr.b.PROTOCOL_ERROR;
        A(bVar, bVar, iOException);
    }

    /* JADX WARN: Removed duplicated region for block: B:21:0x0048 A[Catch: all -> 0x0081, TryCatch #0 {, blocks: (B:6:0x0007, B:8:0x000e, B:9:0x0013, B:11:0x0017, B:13:0x002a, B:15:0x0032, B:19:0x0042, B:21:0x0048, B:22:0x0051, B:37:0x007b, B:38:0x0080), top: B:5:0x0007, outer: #1 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private final cr.i a0(int i10, List list, boolean z10) {
        int i11;
        cr.i iVar;
        boolean z11;
        boolean z12 = !z10;
        synchronized (this.O) {
            synchronized (this) {
                if (this.f15565u > 1073741823) {
                    u0(cr.b.REFUSED_STREAM);
                }
                if (this.f15566v) {
                    throw new cr.a();
                }
                i11 = this.f15565u;
                this.f15565u = i11 + 2;
                iVar = new cr.i(i11, this, z12, false, null);
                if (z10 && this.L < this.M && iVar.t() < iVar.s()) {
                    z11 = false;
                    if (iVar.w()) {
                        this.f15562r.put(Integer.valueOf(i11), iVar);
                    }
                    n0 n0Var = n0.f34904a;
                }
                z11 = true;
                if (iVar.w()) {
                }
                n0 n0Var2 = n0.f34904a;
            }
            if (i10 == 0) {
                this.O.g(z12, i11, list);
            } else {
                if (!(true ^ this.f15560p)) {
                    throw new IllegalArgumentException("client streams shouldn't have associated stream IDs".toString());
                }
                this.O.m(i10, i11, list);
            }
        }
        if (z11) {
            this.O.flush();
        }
        return iVar;
    }

    public static /* synthetic */ void x0(f fVar, boolean z10, int i10, Object obj) {
        if ((i10 & 1) != 0) {
            z10 = true;
        }
        fVar.w0(z10);
    }

    public final void A(cr.b connectionCode, cr.b streamCode, IOException iOException) {
        int i10;
        Object[] objArr;
        t.g(connectionCode, "connectionCode");
        t.g(streamCode, "streamCode");
        if (p.f40482e && Thread.holdsLock(this)) {
            throw new AssertionError("Thread " + Thread.currentThread().getName() + " MUST NOT hold lock on " + this);
        }
        try {
            u0(connectionCode);
        } catch (IOException unused) {
        }
        synchronized (this) {
            if (!this.f15562r.isEmpty()) {
                objArr = this.f15562r.values().toArray(new cr.i[0]);
                t.e(objArr, "null cannot be cast to non-null type kotlin.Array<T of kotlin.collections.ArraysKt__ArraysJVMKt.toTypedArray>");
                this.f15562r.clear();
            } else {
                objArr = null;
            }
            n0 n0Var = n0.f34904a;
        }
        cr.i[] iVarArr = (cr.i[]) objArr;
        if (iVarArr != null) {
            for (cr.i iVar : iVarArr) {
                try {
                    iVar.e(streamCode, iOException);
                } catch (IOException unused2) {
                }
            }
        }
        try {
            this.O.close();
        } catch (IOException unused3) {
        }
        try {
            this.N.close();
        } catch (IOException unused4) {
        }
        this.f15568x.q();
        this.f15569y.q();
        this.f15570z.q();
    }

    /* JADX WARN: Code restructure failed: missing block: B:17:0x0036, code lost:
    
        throw new java.io.IOException("stream closed");
     */
    /* JADX WARN: Code restructure failed: missing block: B:21:0x0038, code lost:
    
        r2 = java.lang.Math.min((int) java.lang.Math.min(r12, r6 - r4), r8.O.h());
        r6 = r2;
        r8.L += r6;
        r4 = rm.n0.f34904a;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void A0(int i10, boolean z10, kr.d dVar, long j10) {
        int min;
        long j11;
        if (j10 == 0) {
            this.O.c(z10, i10, dVar, 0);
            return;
        }
        loop0: while (j10 > 0) {
            synchronized (this) {
                while (true) {
                    try {
                        long j12 = this.L;
                        long j13 = this.M;
                        if (j12 < j13) {
                            break;
                        }
                        if (!this.f15562r.containsKey(Integer.valueOf(i10))) {
                            break loop0;
                        }
                        t.e(this, "null cannot be cast to non-null type java.lang.Object");
                        wait();
                    } catch (InterruptedException unused) {
                        Thread.currentThread().interrupt();
                        throw new InterruptedIOException();
                    }
                }
            }
            j10 -= j11;
            this.O.c(z10 && j10 == 0, i10, dVar, min);
        }
    }

    public final void B0(int i10, boolean z10, List alternating) {
        t.g(alternating, "alternating");
        this.O.g(z10, i10, alternating);
    }

    public final void C0(boolean z10, int i10, int i11) {
        try {
            this.O.j(z10, i10, i11);
        } catch (IOException e10) {
            C(e10);
        }
    }

    public final void D0(int i10, cr.b statusCode) {
        t.g(statusCode, "statusCode");
        this.O.p(i10, statusCode);
    }

    public final void F0(int i10, cr.b errorCode) {
        t.g(errorCode, "errorCode");
        yq.c.d(this.f15568x, this.f15563s + '[' + i10 + "] writeSynReset", 0L, false, new k(i10, errorCode), 6, null);
    }

    public final boolean G() {
        return this.f15560p;
    }

    public final String I() {
        return this.f15563s;
    }

    public final int J() {
        return this.f15564t;
    }

    public final d K() {
        return this.f15561q;
    }

    public final void K0(int i10, long j10) {
        yq.c.d(this.f15568x, this.f15563s + '[' + i10 + "] windowUpdate", 0L, false, new l(i10, j10), 6, null);
    }

    public final int L() {
        return this.f15565u;
    }

    public final m N() {
        return this.H;
    }

    public final m Q() {
        return this.I;
    }

    public final synchronized cr.i S(int i10) {
        return (cr.i) this.f15562r.get(Integer.valueOf(i10));
    }

    public final Map T() {
        return this.f15562r;
    }

    public final long V() {
        return this.M;
    }

    public final cr.j X() {
        return this.O;
    }

    public final synchronized boolean Y(long j10) {
        if (this.f15566v) {
            return false;
        }
        if (this.E < this.D) {
            if (j10 >= this.G) {
                return false;
            }
        }
        return true;
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public void close() {
        A(cr.b.NO_ERROR, cr.b.CANCEL, null);
    }

    public final cr.i e0(List requestHeaders, boolean z10) {
        t.g(requestHeaders, "requestHeaders");
        return a0(0, requestHeaders, z10);
    }

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

    public final void g0(int i10, kr.f source, int i11, boolean z10) {
        t.g(source, "source");
        kr.d dVar = new kr.d();
        long j10 = i11;
        source.L0(j10);
        source.J0(dVar, j10);
        yq.c.d(this.f15569y, this.f15563s + '[' + i10 + "] onData", 0L, false, new C0340f(i10, dVar, i11, z10), 6, null);
    }

    public final void j0(int i10, List requestHeaders, boolean z10) {
        t.g(requestHeaders, "requestHeaders");
        yq.c.d(this.f15569y, this.f15563s + '[' + i10 + "] onHeaders", 0L, false, new g(i10, requestHeaders, z10), 6, null);
    }

    public final void k0(int i10, List requestHeaders) {
        t.g(requestHeaders, "requestHeaders");
        synchronized (this) {
            if (this.Q.contains(Integer.valueOf(i10))) {
                F0(i10, cr.b.PROTOCOL_ERROR);
                return;
            }
            this.Q.add(Integer.valueOf(i10));
            yq.c.d(this.f15569y, this.f15563s + '[' + i10 + "] onRequest", 0L, false, new h(i10, requestHeaders), 6, null);
        }
    }

    public final void n0(int i10, cr.b errorCode) {
        t.g(errorCode, "errorCode");
        yq.c.d(this.f15569y, this.f15563s + '[' + i10 + "] onReset", 0L, false, new i(i10, errorCode), 6, null);
    }

    public final boolean o0(int i10) {
        return i10 != 0 && (i10 & 1) == 0;
    }

    public final synchronized cr.i p0(int i10) {
        cr.i iVar;
        iVar = (cr.i) this.f15562r.remove(Integer.valueOf(i10));
        t.e(this, "null cannot be cast to non-null type java.lang.Object");
        notifyAll();
        return iVar;
    }

    public final void q0() {
        synchronized (this) {
            long j10 = this.E;
            long j11 = this.D;
            if (j10 < j11) {
                return;
            }
            this.D = j11 + 1;
            this.G = System.nanoTime() + 1000000000;
            n0 n0Var = n0.f34904a;
            yq.c.d(this.f15568x, this.f15563s + " ping", 0L, false, new j(), 6, null);
        }
    }

    public final void s0(int i10) {
        this.f15564t = i10;
    }

    public final void t0(m mVar) {
        t.g(mVar, "<set-?>");
        this.I = mVar;
    }

    public final void u0(cr.b statusCode) {
        t.g(statusCode, "statusCode");
        synchronized (this.O) {
            m0 m0Var = new m0();
            synchronized (this) {
                if (this.f15566v) {
                    return;
                }
                this.f15566v = true;
                int i10 = this.f15564t;
                m0Var.f26197p = i10;
                n0 n0Var = n0.f34904a;
                this.O.f(i10, statusCode, vq.m.f40470a);
            }
        }
    }

    public final void w0(boolean z10) {
        if (z10) {
            this.O.b();
            this.O.r(this.H);
            if (this.H.c() != 65535) {
                this.O.s(0, r9 - 65535);
            }
        }
        yq.c.d(this.f15567w.i(), this.f15563s, 0L, false, this.P, 6, null);
    }

    public final synchronized void y0(long j10) {
        long j11 = this.J + j10;
        this.J = j11;
        long j12 = j11 - this.K;
        if (j12 >= this.H.c() / 2) {
            K0(0, j12);
            this.K += j12;
        }
    }
}
