package cr;

import java.io.EOFException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.SocketTimeoutException;
import java.util.ArrayDeque;
import kotlin.jvm.internal.t;
import kr.a0;
import kr.b0;
import kr.y;
import rm.n0;
import uq.u;
import vq.p;

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

    /* renamed from: o, reason: collision with root package name */
    public static final a f15640o = new a(null);

    /* renamed from: a, reason: collision with root package name */
    private final int f15641a;

    /* renamed from: b, reason: collision with root package name */
    private final f f15642b;

    /* renamed from: c, reason: collision with root package name */
    private long f15643c;

    /* renamed from: d, reason: collision with root package name */
    private long f15644d;

    /* renamed from: e, reason: collision with root package name */
    private long f15645e;

    /* renamed from: f, reason: collision with root package name */
    private long f15646f;

    /* renamed from: g, reason: collision with root package name */
    private final ArrayDeque f15647g;

    /* renamed from: h, reason: collision with root package name */
    private boolean f15648h;

    /* renamed from: i, reason: collision with root package name */
    private final c f15649i;

    /* renamed from: j, reason: collision with root package name */
    private final b f15650j;

    /* renamed from: k, reason: collision with root package name */
    private final d f15651k;

    /* renamed from: l, reason: collision with root package name */
    private final d f15652l;

    /* renamed from: m, reason: collision with root package name */
    private cr.b f15653m;

    /* renamed from: n, reason: collision with root package name */
    private IOException f15654n;

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

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

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

        /* renamed from: p, reason: collision with root package name */
        private boolean f15655p;

        /* renamed from: q, reason: collision with root package name */
        private final kr.d f15656q = new kr.d();

        /* renamed from: r, reason: collision with root package name */
        private u f15657r;

        /* renamed from: s, reason: collision with root package name */
        private boolean f15658s;

        public b(boolean z10) {
            this.f15655p = z10;
        }

        private final void a(boolean z10) {
            long min;
            boolean z11;
            i iVar = i.this;
            synchronized (iVar) {
                iVar.u().v();
                while (iVar.t() >= iVar.s() && !this.f15655p && !this.f15658s && iVar.j() == null) {
                    try {
                        iVar.G();
                    } finally {
                        iVar.u().C();
                    }
                }
                iVar.u().C();
                iVar.d();
                min = Math.min(iVar.s() - iVar.t(), this.f15656q.X());
                iVar.D(iVar.t() + min);
                z11 = z10 && min == this.f15656q.X();
                n0 n0Var = n0.f34904a;
            }
            i.this.u().v();
            try {
                i.this.i().A0(i.this.l(), z11, this.f15656q, min);
            } finally {
                iVar = i.this;
            }
        }

        @Override // kr.y
        public void U(kr.d source, long j10) {
            t.g(source, "source");
            i iVar = i.this;
            if (!p.f40482e || !Thread.holdsLock(iVar)) {
                this.f15656q.U(source, j10);
                while (this.f15656q.X() >= 16384) {
                    a(false);
                }
            } else {
                throw new AssertionError("Thread " + Thread.currentThread().getName() + " MUST NOT hold lock on " + iVar);
            }
        }

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

        public final boolean c() {
            return this.f15655p;
        }

        @Override // kr.y, java.io.Closeable, java.lang.AutoCloseable
        public void close() {
            i iVar = i.this;
            if (p.f40482e && Thread.holdsLock(iVar)) {
                throw new AssertionError("Thread " + Thread.currentThread().getName() + " MUST NOT hold lock on " + iVar);
            }
            i iVar2 = i.this;
            synchronized (iVar2) {
                if (this.f15658s) {
                    return;
                }
                boolean z10 = iVar2.j() == null;
                n0 n0Var = n0.f34904a;
                if (!i.this.q().f15655p) {
                    boolean z11 = this.f15656q.X() > 0;
                    if (this.f15657r != null) {
                        while (this.f15656q.X() > 0) {
                            a(false);
                        }
                        f i10 = i.this.i();
                        int l10 = i.this.l();
                        u uVar = this.f15657r;
                        t.d(uVar);
                        i10.B0(l10, z10, p.q(uVar));
                    } else if (z11) {
                        while (this.f15656q.X() > 0) {
                            a(true);
                        }
                    } else if (z10) {
                        i.this.i().A0(i.this.l(), true, null, 0L);
                    }
                }
                i iVar3 = i.this;
                synchronized (iVar3) {
                    this.f15658s = true;
                    t.e(iVar3, "null cannot be cast to non-null type java.lang.Object");
                    iVar3.notifyAll();
                    n0 n0Var2 = n0.f34904a;
                }
                i.this.i().flush();
                i.this.c();
            }
        }

        @Override // kr.y, java.io.Flushable
        public void flush() {
            i iVar = i.this;
            if (p.f40482e && Thread.holdsLock(iVar)) {
                throw new AssertionError("Thread " + Thread.currentThread().getName() + " MUST NOT hold lock on " + iVar);
            }
            i iVar2 = i.this;
            synchronized (iVar2) {
                iVar2.d();
                n0 n0Var = n0.f34904a;
            }
            while (this.f15656q.X() > 0) {
                a(false);
                i.this.i().flush();
            }
        }

        @Override // kr.y
        public b0 l() {
            return i.this.u();
        }
    }

    /* loaded from: classes3.dex */
    public final class c implements a0 {

        /* renamed from: p, reason: collision with root package name */
        private final long f15660p;

        /* renamed from: q, reason: collision with root package name */
        private boolean f15661q;

        /* renamed from: r, reason: collision with root package name */
        private final kr.d f15662r = new kr.d();

        /* renamed from: s, reason: collision with root package name */
        private final kr.d f15663s = new kr.d();

        /* renamed from: t, reason: collision with root package name */
        private u f15664t;

        /* renamed from: u, reason: collision with root package name */
        private boolean f15665u;

        public c(long j10, boolean z10) {
            this.f15660p = j10;
            this.f15661q = z10;
        }

        private final void j(long j10) {
            i iVar = i.this;
            if (!p.f40482e || !Thread.holdsLock(iVar)) {
                i.this.i().y0(j10);
                return;
            }
            throw new AssertionError("Thread " + Thread.currentThread().getName() + " MUST NOT hold lock on " + iVar);
        }

        /* JADX WARN: Code restructure failed: missing block: B:52:0x00d0, code lost:
        
            throw new java.io.IOException("stream closed");
         */
        /* JADX WARN: Removed duplicated region for block: B:30:0x00ad A[Catch: all -> 0x00dc, DONT_GENERATE, TRY_ENTER, TryCatch #1 {, blocks: (B:8:0x0019, B:10:0x001f, B:30:0x00ad, B:31:0x00b4, B:57:0x00d4, B:58:0x00db, B:12:0x0026, B:14:0x002c, B:16:0x0030, B:18:0x0036, B:19:0x0044, B:21:0x0048, B:23:0x0054, B:25:0x0077, B:27:0x008a, B:44:0x009d, B:47:0x00a3, B:51:0x00c9, B:52:0x00d0), top: B:7:0x0019, inners: #0 }] */
        @Override // kr.a0
        /*
            Code decompiled incorrectly, please refer to instructions dump.
        */
        public long J0(kr.d sink, long j10) {
            IOException iOException;
            long j11;
            boolean z10;
            t.g(sink, "sink");
            long j12 = 0;
            if (!(j10 >= 0)) {
                throw new IllegalArgumentException(("byteCount < 0: " + j10).toString());
            }
            while (true) {
                i iVar = i.this;
                synchronized (iVar) {
                    boolean h10 = iVar.h();
                    if (h10) {
                        iVar.o().v();
                    }
                    try {
                        if (iVar.j() == null || this.f15661q) {
                            iOException = null;
                        } else {
                            iOException = iVar.k();
                            if (iOException == null) {
                                cr.b j13 = iVar.j();
                                t.d(j13);
                                iOException = new n(j13);
                            }
                        }
                        if (this.f15665u) {
                            break;
                        }
                        if (this.f15663s.X() > j12) {
                            kr.d dVar = this.f15663s;
                            j11 = dVar.J0(sink, Math.min(j10, dVar.X()));
                            iVar.C(iVar.n() + j11);
                            long n10 = iVar.n() - iVar.m();
                            if (iOException == null && n10 >= iVar.i().N().c() / 2) {
                                iVar.i().K0(iVar.l(), n10);
                                iVar.B(iVar.n());
                            }
                        } else if (this.f15661q || iOException != null) {
                            j11 = -1;
                        } else {
                            iVar.G();
                            j11 = -1;
                            z10 = true;
                            n0 n0Var = n0.f34904a;
                        }
                        z10 = false;
                        n0 n0Var2 = n0.f34904a;
                    } finally {
                        if (h10) {
                            iVar.o().C();
                        }
                    }
                }
                if (!z10) {
                    if (j11 != -1) {
                        j(j11);
                        return j11;
                    }
                    if (iOException == null) {
                        return -1L;
                    }
                    throw iOException;
                }
                j12 = 0;
            }
        }

        public final boolean a() {
            return this.f15665u;
        }

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

        public final kr.d c() {
            return this.f15663s;
        }

        @Override // kr.a0, java.io.Closeable, java.lang.AutoCloseable
        public void close() {
            long X;
            i iVar = i.this;
            synchronized (iVar) {
                this.f15665u = true;
                X = this.f15663s.X();
                this.f15663s.a();
                t.e(iVar, "null cannot be cast to non-null type java.lang.Object");
                iVar.notifyAll();
                n0 n0Var = n0.f34904a;
            }
            if (X > 0) {
                j(X);
            }
            i.this.c();
        }

        public final kr.d d() {
            return this.f15662r;
        }

        public final u e() {
            return this.f15664t;
        }

        public final void f(kr.f source, long j10) {
            boolean z10;
            boolean z11;
            boolean z12;
            long j11;
            t.g(source, "source");
            i iVar = i.this;
            if (p.f40482e && Thread.holdsLock(iVar)) {
                throw new AssertionError("Thread " + Thread.currentThread().getName() + " MUST NOT hold lock on " + iVar);
            }
            while (j10 > 0) {
                synchronized (i.this) {
                    z10 = this.f15661q;
                    z11 = true;
                    z12 = this.f15663s.X() + j10 > this.f15660p;
                    n0 n0Var = n0.f34904a;
                }
                if (z12) {
                    source.skip(j10);
                    i.this.g(cr.b.FLOW_CONTROL_ERROR);
                    return;
                }
                if (z10) {
                    source.skip(j10);
                    return;
                }
                long J0 = source.J0(this.f15662r, j10);
                if (J0 == -1) {
                    throw new EOFException();
                }
                j10 -= J0;
                i iVar2 = i.this;
                synchronized (iVar2) {
                    if (this.f15665u) {
                        j11 = this.f15662r.X();
                        this.f15662r.a();
                    } else {
                        if (this.f15663s.X() != 0) {
                            z11 = false;
                        }
                        this.f15663s.o0(this.f15662r);
                        if (z11) {
                            t.e(iVar2, "null cannot be cast to non-null type java.lang.Object");
                            iVar2.notifyAll();
                        }
                        j11 = 0;
                    }
                }
                if (j11 > 0) {
                    j(j11);
                }
            }
        }

        public final void g(boolean z10) {
            this.f15661q = z10;
        }

        public final void h(u uVar) {
            this.f15664t = uVar;
        }

        @Override // kr.a0
        public b0 l() {
            return i.this.o();
        }
    }

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

        @Override // kr.c
        protected void B() {
            i.this.g(cr.b.CANCEL);
            i.this.i().q0();
        }

        public final void C() {
            if (w()) {
                throw x(null);
            }
        }

        @Override // kr.c
        protected IOException x(IOException iOException) {
            SocketTimeoutException socketTimeoutException = new SocketTimeoutException("timeout");
            if (iOException != null) {
                socketTimeoutException.initCause(iOException);
            }
            return socketTimeoutException;
        }
    }

    public i(int i10, f connection, boolean z10, boolean z11, u uVar) {
        t.g(connection, "connection");
        this.f15641a = i10;
        this.f15642b = connection;
        this.f15646f = connection.Q().c();
        ArrayDeque arrayDeque = new ArrayDeque();
        this.f15647g = arrayDeque;
        this.f15649i = new c(connection.N().c(), z11);
        this.f15650j = new b(z10);
        this.f15651k = new d();
        this.f15652l = new d();
        if (uVar == null) {
            if (!v()) {
                throw new IllegalStateException("remotely-initiated streams should have headers".toString());
            }
        } else {
            if (!(!v())) {
                throw new IllegalStateException("locally-initiated streams shouldn't have headers yet".toString());
            }
            arrayDeque.add(uVar);
        }
    }

    private final boolean f(cr.b bVar, IOException iOException) {
        if (p.f40482e && Thread.holdsLock(this)) {
            throw new AssertionError("Thread " + Thread.currentThread().getName() + " MUST NOT hold lock on " + this);
        }
        synchronized (this) {
            if (this.f15653m != null) {
                return false;
            }
            if (this.f15649i.b() && this.f15650j.c()) {
                return false;
            }
            this.f15653m = bVar;
            this.f15654n = iOException;
            t.e(this, "null cannot be cast to non-null type java.lang.Object");
            notifyAll();
            n0 n0Var = n0.f34904a;
            this.f15642b.p0(this.f15641a);
            return true;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final boolean h() {
        return !this.f15642b.G() || this.f15650j.b() || this.f15650j.c();
    }

    public final synchronized void A(cr.b errorCode) {
        t.g(errorCode, "errorCode");
        if (this.f15653m == null) {
            this.f15653m = errorCode;
            t.e(this, "null cannot be cast to non-null type java.lang.Object");
            notifyAll();
        }
    }

    public final void B(long j10) {
        this.f15644d = j10;
    }

    public final void C(long j10) {
        this.f15643c = j10;
    }

    public final void D(long j10) {
        this.f15645e = j10;
    }

    /* JADX WARN: Code restructure failed: missing block: B:13:0x001a, code lost:
    
        r2.f15651k.v();
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final synchronized u E(boolean z10) {
        Object removeFirst;
        while (true) {
            boolean z11 = true;
            if (!this.f15647g.isEmpty() || this.f15653m != null) {
                break;
            }
            if (!z10 && !h()) {
                z11 = false;
            }
            try {
                G();
                if (z11) {
                    this.f15651k.C();
                }
            } catch (Throwable th2) {
                if (z11) {
                    this.f15651k.C();
                }
                throw th2;
            }
        }
        if (!(!this.f15647g.isEmpty())) {
            IOException iOException = this.f15654n;
            if (iOException != null) {
                throw iOException;
            }
            cr.b bVar = this.f15653m;
            t.d(bVar);
            throw new n(bVar);
        }
        removeFirst = this.f15647g.removeFirst();
        t.f(removeFirst, "headersQueue.removeFirst()");
        return (u) removeFirst;
    }

    public final synchronized u F() {
        u e10;
        if (!this.f15649i.b() || !this.f15649i.d().E() || !this.f15649i.c().E()) {
            if (this.f15653m == null) {
                throw new IllegalStateException("too early; can't read the trailers yet");
            }
            IOException iOException = this.f15654n;
            if (iOException != null) {
                throw iOException;
            }
            cr.b bVar = this.f15653m;
            t.d(bVar);
            throw new n(bVar);
        }
        e10 = this.f15649i.e();
        if (e10 == null) {
            e10 = p.f40478a;
        }
        return e10;
    }

    public final void G() {
        try {
            t.e(this, "null cannot be cast to non-null type java.lang.Object");
            wait();
        } catch (InterruptedException unused) {
            Thread.currentThread().interrupt();
            throw new InterruptedIOException();
        }
    }

    public final b0 H() {
        return this.f15652l;
    }

    public final void b(long j10) {
        this.f15646f += j10;
        if (j10 > 0) {
            t.e(this, "null cannot be cast to non-null type java.lang.Object");
            notifyAll();
        }
    }

    public final void c() {
        boolean z10;
        boolean w10;
        if (p.f40482e && Thread.holdsLock(this)) {
            throw new AssertionError("Thread " + Thread.currentThread().getName() + " MUST NOT hold lock on " + this);
        }
        synchronized (this) {
            z10 = !this.f15649i.b() && this.f15649i.a() && (this.f15650j.c() || this.f15650j.b());
            w10 = w();
            n0 n0Var = n0.f34904a;
        }
        if (z10) {
            e(cr.b.CANCEL, null);
        } else {
            if (w10) {
                return;
            }
            this.f15642b.p0(this.f15641a);
        }
    }

    public final void d() {
        if (this.f15650j.b()) {
            throw new IOException("stream closed");
        }
        if (this.f15650j.c()) {
            throw new IOException("stream finished");
        }
        if (this.f15653m != null) {
            IOException iOException = this.f15654n;
            if (iOException != null) {
                throw iOException;
            }
            cr.b bVar = this.f15653m;
            t.d(bVar);
            throw new n(bVar);
        }
    }

    public final void e(cr.b rstStatusCode, IOException iOException) {
        t.g(rstStatusCode, "rstStatusCode");
        if (f(rstStatusCode, iOException)) {
            this.f15642b.D0(this.f15641a, rstStatusCode);
        }
    }

    public final void g(cr.b errorCode) {
        t.g(errorCode, "errorCode");
        if (f(errorCode, null)) {
            this.f15642b.F0(this.f15641a, errorCode);
        }
    }

    public final f i() {
        return this.f15642b;
    }

    public final synchronized cr.b j() {
        return this.f15653m;
    }

    public final IOException k() {
        return this.f15654n;
    }

    public final int l() {
        return this.f15641a;
    }

    public final long m() {
        return this.f15644d;
    }

    public final long n() {
        return this.f15643c;
    }

    public final d o() {
        return this.f15651k;
    }

    /* JADX WARN: Removed duplicated region for block: B:10:0x0011 A[Catch: all -> 0x0023, TRY_LEAVE, TryCatch #0 {, blocks: (B:3:0x0001, B:5:0x0005, B:10:0x0011, B:15:0x0017, B:16:0x0022), top: B:2:0x0001 }] */
    /* JADX WARN: Removed duplicated region for block: B:15:0x0017 A[Catch: all -> 0x0023, TRY_ENTER, TryCatch #0 {, blocks: (B:3:0x0001, B:5:0x0005, B:10:0x0011, B:15:0x0017, B:16:0x0022), top: B:2:0x0001 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final y p() {
        boolean z10;
        synchronized (this) {
            if (!this.f15648h && !v()) {
                z10 = false;
                if (z10) {
                    throw new IllegalStateException("reply before requesting the sink".toString());
                }
                n0 n0Var = n0.f34904a;
            }
            z10 = true;
            if (z10) {
            }
        }
        return this.f15650j;
    }

    public final b q() {
        return this.f15650j;
    }

    public final c r() {
        return this.f15649i;
    }

    public final long s() {
        return this.f15646f;
    }

    public final long t() {
        return this.f15645e;
    }

    public final d u() {
        return this.f15652l;
    }

    public final boolean v() {
        return this.f15642b.G() == ((this.f15641a & 1) == 1);
    }

    public final synchronized boolean w() {
        if (this.f15653m != null) {
            return false;
        }
        if ((this.f15649i.b() || this.f15649i.a()) && (this.f15650j.c() || this.f15650j.b())) {
            if (this.f15648h) {
                return false;
            }
        }
        return true;
    }

    public final b0 x() {
        return this.f15651k;
    }

    public final void y(kr.f source, int i10) {
        t.g(source, "source");
        if (!p.f40482e || !Thread.holdsLock(this)) {
            this.f15649i.f(source, i10);
            return;
        }
        throw new AssertionError("Thread " + Thread.currentThread().getName() + " MUST NOT hold lock on " + this);
    }

    /* JADX WARN: Removed duplicated region for block: B:19:0x005d A[Catch: all -> 0x007b, TryCatch #0 {, blocks: (B:10:0x0038, B:12:0x003d, B:14:0x0045, B:17:0x004e, B:19:0x005d, B:20:0x0062, B:27:0x0054), top: B:9:0x0038 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void z(u headers, boolean z10) {
        boolean w10;
        t.g(headers, "headers");
        if (p.f40482e && Thread.holdsLock(this)) {
            throw new AssertionError("Thread " + Thread.currentThread().getName() + " MUST NOT hold lock on " + this);
        }
        synchronized (this) {
            if (this.f15648h && headers.e(":status") == null && headers.e(":method") == null) {
                this.f15649i.h(headers);
                if (z10) {
                    this.f15649i.g(true);
                }
                w10 = w();
                t.e(this, "null cannot be cast to non-null type java.lang.Object");
                notifyAll();
                n0 n0Var = n0.f34904a;
            }
            this.f15648h = true;
            this.f15647g.add(headers);
            if (z10) {
            }
            w10 = w();
            t.e(this, "null cannot be cast to non-null type java.lang.Object");
            notifyAll();
            n0 n0Var2 = n0.f34904a;
        }
        if (w10) {
            return;
        }
        this.f15642b.p0(this.f15641a);
    }
}
