package g.i0.i;

import h.w;
import h.x;
import h.y;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.SocketTimeoutException;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Objects;
import javax.annotation.Nullable;

/* JADX INFO: loaded from: classes.dex */
public final class p {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public long f6355a = 0;

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final int f6357c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final g f6358d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final Deque<g.r> f6359e;

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final b f6361g;

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

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final c f6364j;
    public g.i0.i.b k;

    public final class a implements w {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final h.f f6365b = new h.f();

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

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

        public a() {
        }

        public final void a(boolean z) throws IOException {
            p pVar;
            long jMin;
            p pVar2;
            synchronized (p.this) {
                p.this.f6364j.i();
                while (true) {
                    try {
                        pVar = p.this;
                        if (pVar.f6356b > 0 || this.f6367d || this.f6366c || pVar.k != null) {
                            break;
                        } else {
                            pVar.j();
                        }
                    } finally {
                    }
                }
                pVar.f6364j.n();
                p.this.b();
                jMin = Math.min(p.this.f6356b, this.f6365b.f6543d);
                pVar2 = p.this;
                pVar2.f6356b -= jMin;
            }
            pVar2.f6364j.i();
            try {
                p pVar3 = p.this;
                pVar3.f6358d.N(pVar3.f6357c, z && jMin == this.f6365b.f6543d, this.f6365b, jMin);
            } finally {
            }
        }

        @Override // h.w, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            synchronized (p.this) {
                if (this.f6366c) {
                    return;
                }
                p pVar = p.this;
                if (!pVar.f6362h.f6367d) {
                    if (this.f6365b.f6543d > 0) {
                        while (this.f6365b.f6543d > 0) {
                            a(true);
                        }
                    } else {
                        pVar.f6358d.N(pVar.f6357c, true, null, 0L);
                    }
                }
                synchronized (p.this) {
                    this.f6366c = true;
                }
                p.this.f6358d.t.flush();
                p.this.a();
            }
        }

        @Override // h.w
        public y e() {
            return p.this.f6364j;
        }

        @Override // h.w, java.io.Flushable
        public void flush() throws IOException {
            synchronized (p.this) {
                p.this.b();
            }
            while (this.f6365b.f6543d > 0) {
                a(false);
                p.this.f6358d.flush();
            }
        }

        @Override // h.w
        public void h(h.f fVar, long j2) throws IOException {
            this.f6365b.h(fVar, j2);
            while (this.f6365b.f6543d >= 16384) {
                a(false);
            }
        }
    }

    public final class b implements x {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final h.f f6369b = new h.f();

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        public final h.f f6370c = new h.f();

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        public final long f6371d;

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

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

        public b(long j2) {
            this.f6371d = j2;
        }

        @Override // h.x, java.io.Closeable, java.lang.AutoCloseable
        public void close() {
            long j2;
            synchronized (p.this) {
                this.f6372e = true;
                h.f fVar = this.f6370c;
                j2 = fVar.f6543d;
                fVar.K();
                if (!p.this.f6359e.isEmpty()) {
                    Objects.requireNonNull(p.this);
                }
                p.this.notifyAll();
            }
            if (j2 > 0) {
                p.this.f6358d.M(j2);
            }
            p.this.a();
        }

        @Override // h.x
        public y e() {
            return p.this.f6363i;
        }

        /* JADX WARN: Removed duplicated region for block: B:34:0x0084  */
        /* JADX WARN: Removed duplicated region for block: B:36:0x008c  */
        @Override // h.x
        /*
            Code decompiled incorrectly, please refer to instructions dump.
            To view partially-correct add '--show-bad-code' argument
        */
        public long v(h.f r11, long r12) throws g.i0.i.u {
            /*
                r10 = this;
                r0 = 0
                int r2 = (r12 > r0 ? 1 : (r12 == r0 ? 0 : -1))
                if (r2 < 0) goto La9
            L6:
                r2 = 0
                g.i0.i.p r3 = g.i0.i.p.this
                monitor-enter(r3)
                g.i0.i.p r4 = g.i0.i.p.this     // Catch: java.lang.Throwable -> La6
                g.i0.i.p$c r4 = r4.f6363i     // Catch: java.lang.Throwable -> La6
                r4.i()     // Catch: java.lang.Throwable -> La6
                g.i0.i.p r4 = g.i0.i.p.this     // Catch: java.lang.Throwable -> L9d
                g.i0.i.b r5 = r4.k     // Catch: java.lang.Throwable -> L9d
                if (r5 == 0) goto L18
                r2 = r5
            L18:
                boolean r5 = r10.f6372e     // Catch: java.lang.Throwable -> L9d
                if (r5 != 0) goto L95
                java.util.Deque<g.r> r4 = r4.f6359e     // Catch: java.lang.Throwable -> L9d
                boolean r4 = r4.isEmpty()     // Catch: java.lang.Throwable -> L9d
                if (r4 != 0) goto L29
                g.i0.i.p r4 = g.i0.i.p.this     // Catch: java.lang.Throwable -> L9d
                java.util.Objects.requireNonNull(r4)     // Catch: java.lang.Throwable -> L9d
            L29:
                h.f r4 = r10.f6370c     // Catch: java.lang.Throwable -> L9d
                long r5 = r4.f6543d     // Catch: java.lang.Throwable -> L9d
                int r7 = (r5 > r0 ? 1 : (r5 == r0 ? 0 : -1))
                r8 = -1
                if (r7 <= 0) goto L63
                long r12 = java.lang.Math.min(r12, r5)     // Catch: java.lang.Throwable -> L9d
                long r11 = r4.v(r11, r12)     // Catch: java.lang.Throwable -> L9d
                g.i0.i.p r13 = g.i0.i.p.this     // Catch: java.lang.Throwable -> L9d
                long r4 = r13.f6355a     // Catch: java.lang.Throwable -> L9d
                long r4 = r4 + r11
                r13.f6355a = r4     // Catch: java.lang.Throwable -> L9d
                if (r2 != 0) goto L78
                g.i0.i.g r13 = r13.f6358d     // Catch: java.lang.Throwable -> L9d
                g.i0.i.t r13 = r13.p     // Catch: java.lang.Throwable -> L9d
                int r13 = r13.a()     // Catch: java.lang.Throwable -> L9d
                int r13 = r13 / 2
                long r6 = (long) r13     // Catch: java.lang.Throwable -> L9d
                int r13 = (r4 > r6 ? 1 : (r4 == r6 ? 0 : -1))
                if (r13 < 0) goto L78
                g.i0.i.p r13 = g.i0.i.p.this     // Catch: java.lang.Throwable -> L9d
                g.i0.i.g r4 = r13.f6358d     // Catch: java.lang.Throwable -> L9d
                int r5 = r13.f6357c     // Catch: java.lang.Throwable -> L9d
                long r6 = r13.f6355a     // Catch: java.lang.Throwable -> L9d
                r4.P(r5, r6)     // Catch: java.lang.Throwable -> L9d
                g.i0.i.p r13 = g.i0.i.p.this     // Catch: java.lang.Throwable -> L9d
                r13.f6355a = r0     // Catch: java.lang.Throwable -> L9d
                goto L78
            L63:
                boolean r4 = r10.f6373f     // Catch: java.lang.Throwable -> L9d
                if (r4 != 0) goto L77
                if (r2 != 0) goto L77
                g.i0.i.p r2 = g.i0.i.p.this     // Catch: java.lang.Throwable -> L9d
                r2.j()     // Catch: java.lang.Throwable -> L9d
                g.i0.i.p r2 = g.i0.i.p.this     // Catch: java.lang.Throwable -> La6
                g.i0.i.p$c r2 = r2.f6363i     // Catch: java.lang.Throwable -> La6
                r2.n()     // Catch: java.lang.Throwable -> La6
                monitor-exit(r3)     // Catch: java.lang.Throwable -> La6
                goto L6
            L77:
                r11 = r8
            L78:
                g.i0.i.p r13 = g.i0.i.p.this     // Catch: java.lang.Throwable -> La6
                g.i0.i.p$c r13 = r13.f6363i     // Catch: java.lang.Throwable -> La6
                r13.n()     // Catch: java.lang.Throwable -> La6
                monitor-exit(r3)     // Catch: java.lang.Throwable -> La6
                int r13 = (r11 > r8 ? 1 : (r11 == r8 ? 0 : -1))
                if (r13 == 0) goto L8c
                g.i0.i.p r13 = g.i0.i.p.this
                g.i0.i.g r13 = r13.f6358d
                r13.M(r11)
                return r11
            L8c:
                if (r2 != 0) goto L8f
                return r8
            L8f:
                g.i0.i.u r11 = new g.i0.i.u
                r11.<init>(r2)
                throw r11
            L95:
                java.io.IOException r11 = new java.io.IOException     // Catch: java.lang.Throwable -> L9d
                java.lang.String r12 = "stream closed"
                r11.<init>(r12)     // Catch: java.lang.Throwable -> L9d
                throw r11     // Catch: java.lang.Throwable -> L9d
            L9d:
                r11 = move-exception
                g.i0.i.p r12 = g.i0.i.p.this     // Catch: java.lang.Throwable -> La6
                g.i0.i.p$c r12 = r12.f6363i     // Catch: java.lang.Throwable -> La6
                r12.n()     // Catch: java.lang.Throwable -> La6
                throw r11     // Catch: java.lang.Throwable -> La6
            La6:
                r11 = move-exception
                monitor-exit(r3)     // Catch: java.lang.Throwable -> La6
                throw r11
            La9:
                java.lang.IllegalArgumentException r11 = new java.lang.IllegalArgumentException
                java.lang.StringBuilder r0 = new java.lang.StringBuilder
                r0.<init>()
                java.lang.String r1 = "byteCount < 0: "
                r0.append(r1)
                r0.append(r12)
                java.lang.String r12 = r0.toString()
                r11.<init>(r12)
                throw r11
            */
            throw new UnsupportedOperationException("Method not decompiled: g.i0.i.p.b.v(h.f, long):long");
        }
    }

    public class c extends h.c {
        public c() {
        }

        @Override // h.c
        public IOException l(IOException iOException) {
            SocketTimeoutException socketTimeoutException = new SocketTimeoutException("timeout");
            if (iOException != null) {
                socketTimeoutException.initCause(iOException);
            }
            return socketTimeoutException;
        }

        @Override // h.c
        public void m() {
            p.this.e(g.i0.i.b.CANCEL);
        }

        public void n() throws IOException {
            if (k()) {
                throw l(null);
            }
        }
    }

    public p(int i2, g gVar, boolean z, boolean z2, @Nullable g.r rVar) {
        ArrayDeque arrayDeque = new ArrayDeque();
        this.f6359e = arrayDeque;
        this.f6363i = new c();
        this.f6364j = new c();
        this.k = null;
        Objects.requireNonNull(gVar, "connection == null");
        this.f6357c = i2;
        this.f6358d = gVar;
        this.f6356b = gVar.q.a();
        b bVar = new b(gVar.p.a());
        this.f6361g = bVar;
        a aVar = new a();
        this.f6362h = aVar;
        bVar.f6373f = z2;
        aVar.f6367d = z;
        if (rVar != null) {
            arrayDeque.add(rVar);
        }
        if (g() && rVar != null) {
            throw new IllegalStateException("locally-initiated streams shouldn't have headers yet");
        }
        if (!g() && rVar == null) {
            throw new IllegalStateException("remotely-initiated streams should have headers");
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:12:0x0017  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public void a() {
        /*
            r2 = this;
            monitor-enter(r2)
            g.i0.i.p$b r0 = r2.f6361g     // Catch: java.lang.Throwable -> L2f
            boolean r1 = r0.f6373f     // Catch: java.lang.Throwable -> L2f
            if (r1 != 0) goto L17
            boolean r0 = r0.f6372e     // Catch: java.lang.Throwable -> L2f
            if (r0 == 0) goto L17
            g.i0.i.p$a r0 = r2.f6362h     // Catch: java.lang.Throwable -> L2f
            boolean r1 = r0.f6367d     // Catch: java.lang.Throwable -> L2f
            if (r1 != 0) goto L15
            boolean r0 = r0.f6366c     // Catch: java.lang.Throwable -> L2f
            if (r0 == 0) goto L17
        L15:
            r0 = 1
            goto L18
        L17:
            r0 = 0
        L18:
            boolean r1 = r2.h()     // Catch: java.lang.Throwable -> L2f
            monitor-exit(r2)     // Catch: java.lang.Throwable -> L2f
            if (r0 == 0) goto L25
            g.i0.i.b r0 = g.i0.i.b.CANCEL
            r2.c(r0)
            goto L2e
        L25:
            if (r1 != 0) goto L2e
            g.i0.i.g r0 = r2.f6358d
            int r1 = r2.f6357c
            r0.K(r1)
        L2e:
            return
        L2f:
            r0 = move-exception
            monitor-exit(r2)     // Catch: java.lang.Throwable -> L2f
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: g.i0.i.p.a():void");
    }

    public void b() throws IOException {
        a aVar = this.f6362h;
        if (aVar.f6366c) {
            throw new IOException("stream closed");
        }
        if (aVar.f6367d) {
            throw new IOException("stream finished");
        }
        if (this.k != null) {
            throw new u(this.k);
        }
    }

    public void c(g.i0.i.b bVar) {
        if (d(bVar)) {
            g gVar = this.f6358d;
            gVar.t.K(this.f6357c, bVar);
        }
    }

    public final boolean d(g.i0.i.b bVar) {
        synchronized (this) {
            if (this.k != null) {
                return false;
            }
            if (this.f6361g.f6373f && this.f6362h.f6367d) {
                return false;
            }
            this.k = bVar;
            notifyAll();
            this.f6358d.K(this.f6357c);
            return true;
        }
    }

    public void e(g.i0.i.b bVar) {
        if (d(bVar)) {
            this.f6358d.O(this.f6357c, bVar);
        }
    }

    public w f() {
        synchronized (this) {
            if (!this.f6360f && !g()) {
                throw new IllegalStateException("reply before requesting the sink");
            }
        }
        return this.f6362h;
    }

    public boolean g() {
        return this.f6358d.f6297c == ((this.f6357c & 1) == 1);
    }

    public synchronized boolean h() {
        if (this.k != null) {
            return false;
        }
        b bVar = this.f6361g;
        if (bVar.f6373f || bVar.f6372e) {
            a aVar = this.f6362h;
            if (aVar.f6367d || aVar.f6366c) {
                if (this.f6360f) {
                    return false;
                }
            }
        }
        return true;
    }

    public void i() {
        boolean zH;
        synchronized (this) {
            this.f6361g.f6373f = true;
            zH = h();
            notifyAll();
        }
        if (zH) {
            return;
        }
        this.f6358d.K(this.f6357c);
    }

    public void j() throws InterruptedIOException {
        try {
            wait();
        } catch (InterruptedException unused) {
            Thread.currentThread().interrupt();
            throw new InterruptedIOException();
        }
    }
}
