package okhttp3.internal.http2;

import J4.a;
import java.io.Closeable;
import java.io.EOFException;
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 kotlin.jvm.internal.o;
import okhttp3.internal.Util;
import okhttp3.internal.concurrent.Task;
import okhttp3.internal.concurrent.TaskQueue;
import okhttp3.internal.concurrent.TaskRunner;
import okhttp3.internal.http2.Http2Connection;
import okhttp3.internal.http2.Http2Reader;
import okhttp3.internal.http2.Http2Stream;
import okhttp3.internal.http2.PushObserver;
import okhttp3.internal.platform.Platform;
import p5.C2740j;
import p5.I;
import p5.J;
import w4.C3137A;

/* JADX INFO: loaded from: classes3.dex */
public final class Http2Connection implements Closeable {

    /* JADX INFO: renamed from: D, reason: collision with root package name */
    public static final Companion f22468D = new Companion(0);

    /* JADX INFO: renamed from: E, reason: collision with root package name */
    public static final Settings f22469E;

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

    /* JADX INFO: renamed from: B, reason: collision with root package name */
    public final ReaderRunnable f22471B;

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final TaskRunner f22479j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final TaskQueue f22480k;
    public final TaskQueue l;

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

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public final PushObserver f22482n;

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

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

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

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

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

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public final Settings f22488t;

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

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

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

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

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

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

    public static final class Builder {

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

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

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

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

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

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

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

        public Builder(TaskRunner taskRunner) {
            o.h(taskRunner, "taskRunner");
            this.f22495a = taskRunner;
            this.f22500f = Listener.f22502a;
            this.f22501g = PushObserver.f22590a;
        }
    }

    public static final class Companion {
        public /* synthetic */ Companion(int i6) {
            this();
        }

        private Companion() {
        }
    }

    public static abstract class Listener {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public static final Http2Connection$Listener$Companion$REFUSE_INCOMING_STREAMS$1 f22502a;

        public static final class Companion {
            public /* synthetic */ Companion(int i6) {
                this();
            }

            private Companion() {
            }
        }

        /* JADX WARN: Type inference failed for: r0v1, types: [okhttp3.internal.http2.Http2Connection$Listener$Companion$REFUSE_INCOMING_STREAMS$1] */
        static {
            new Companion(0);
            f22502a = new Listener() { // from class: okhttp3.internal.http2.Http2Connection$Listener$Companion$REFUSE_INCOMING_STREAMS$1
                @Override // okhttp3.internal.http2.Http2Connection.Listener
                public final void b(Http2Stream http2Stream) {
                    http2Stream.c(ErrorCode.REFUSED_STREAM, null);
                }
            };
        }

        public void a(Http2Connection connection, Settings settings) {
            o.h(connection, "connection");
            o.h(settings, "settings");
        }

        public abstract void b(Http2Stream http2Stream);
    }

    public final class ReaderRunnable implements Http2Reader.Handler, a {

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

        public ReaderRunnable(Http2Reader http2Reader) {
            this.f22503a = http2Reader;
        }

        /* JADX WARN: Code restructure failed: missing block: B:57:0x00f2, code lost:
        
            if (r19 == false) goto L68;
         */
        /* JADX WARN: Code restructure failed: missing block: B:58:0x00f4, code lost:
        
            r4.i(okhttp3.internal.Util.f22180b, true);
         */
        /* JADX WARN: Code restructure failed: missing block: B:59:0x00f9, code lost:
        
            return;
         */
        /* JADX WARN: Code restructure failed: missing block: B:68:?, code lost:
        
            return;
         */
        /*
            Code decompiled incorrectly, please refer to instructions dump.
        */
        public final void a(final boolean z6, final int i6, J source, final int i7) throws EOFException {
            boolean z7;
            long j4;
            boolean z8;
            o.h(source, "source");
            Http2Connection.this.getClass();
            long j6 = 0;
            if (!(i6 != 0 && (i6 & 1) == 0)) {
                Http2Stream http2StreamC = Http2Connection.this.c(i6);
                if (http2StreamC != null) {
                    byte[] bArr = Util.f22179a;
                    Http2Stream.FramingSource framingSource = http2StreamC.f22561i;
                    long j7 = i7;
                    framingSource.getClass();
                    long j8 = j7;
                    while (true) {
                        if (j8 <= j6) {
                            byte[] bArr2 = Util.f22179a;
                            Http2Stream.this.f22554b.g(j7);
                            break;
                        }
                        synchronized (Http2Stream.this) {
                            z7 = framingSource.f22571b;
                            j4 = j6;
                            z8 = framingSource.f22573d.f23100b + j8 > framingSource.f22570a;
                        }
                        if (z8) {
                            source.B(j8);
                            Http2Stream.this.e(ErrorCode.FLOW_CONTROL_ERROR);
                            break;
                        }
                        if (z7) {
                            source.B(j8);
                            break;
                        }
                        long j9 = source.read(framingSource.f22572c, j8);
                        if (j9 == -1) {
                            throw new EOFException();
                        }
                        j8 -= j9;
                        Http2Stream http2Stream = Http2Stream.this;
                        synchronized (http2Stream) {
                            try {
                                if (framingSource.f22574e) {
                                    framingSource.f22572c.a();
                                } else {
                                    C2740j c2740j = framingSource.f22573d;
                                    boolean z9 = c2740j.f23100b == j4;
                                    c2740j.q(framingSource.f22572c);
                                    if (z9) {
                                        http2Stream.notifyAll();
                                    }
                                }
                            } catch (Throwable th) {
                                throw th;
                            }
                        }
                        j6 = j4;
                    }
                } else {
                    Http2Connection.this.t(i6, ErrorCode.PROTOCOL_ERROR);
                    long j10 = i7;
                    Http2Connection.this.g(j10);
                    source.B(j10);
                }
            } else {
                final Http2Connection http2Connection = Http2Connection.this;
                http2Connection.getClass();
                final C2740j c2740j2 = new C2740j();
                long j11 = i7;
                source.A(j11);
                source.read(c2740j2, j11);
                final String str = http2Connection.f22475c + '[' + i6 + "] onData";
                http2Connection.l.c(new Task(str, http2Connection, i6, c2740j2, i7, z6) { // from class: okhttp3.internal.http2.Http2Connection$pushDataLater$$inlined$execute$default$1

                    /* JADX INFO: renamed from: e, reason: collision with root package name */
                    public final /* synthetic */ Http2Connection f22514e;

                    /* JADX INFO: renamed from: f, reason: collision with root package name */
                    public final /* synthetic */ int f22515f;

                    /* JADX INFO: renamed from: g, reason: collision with root package name */
                    public final /* synthetic */ C2740j f22516g;

                    /* JADX INFO: renamed from: h, reason: collision with root package name */
                    public final /* synthetic */ int f22517h;

                    @Override // okhttp3.internal.concurrent.Task
                    public final long a() {
                        try {
                            PushObserver pushObserver = this.f22514e.f22482n;
                            C2740j c2740j3 = this.f22516g;
                            int i8 = this.f22517h;
                            ((PushObserver.Companion.PushObserverCancel) pushObserver).getClass();
                            c2740j3.J(i8);
                            this.f22514e.f22470A.i(this.f22515f, ErrorCode.CANCEL);
                            synchronized (this.f22514e) {
                                this.f22514e.f22472C.remove(Integer.valueOf(this.f22515f));
                            }
                            return -1L;
                        } catch (IOException unused) {
                            return -1L;
                        }
                    }
                }, 0L);
            }
        }

        public final void b(final int i6, final List list, final boolean z6) {
            Http2Connection.this.getClass();
            if (i6 != 0 && (i6 & 1) == 0) {
                final Http2Connection http2Connection = Http2Connection.this;
                http2Connection.getClass();
                final String str = http2Connection.f22475c + '[' + i6 + "] onHeaders";
                http2Connection.l.c(new Task(str, http2Connection, i6, list, z6) { // from class: okhttp3.internal.http2.Http2Connection$pushHeadersLater$$inlined$execute$default$1

                    /* JADX INFO: renamed from: e, reason: collision with root package name */
                    public final /* synthetic */ Http2Connection f22518e;

                    /* JADX INFO: renamed from: f, reason: collision with root package name */
                    public final /* synthetic */ int f22519f;

                    /* JADX INFO: renamed from: g, reason: collision with root package name */
                    public final /* synthetic */ List f22520g;

                    @Override // okhttp3.internal.concurrent.Task
                    public final long a() {
                        ((PushObserver.Companion.PushObserverCancel) this.f22518e.f22482n).getClass();
                        try {
                            this.f22518e.f22470A.i(this.f22519f, ErrorCode.CANCEL);
                            synchronized (this.f22518e) {
                                this.f22518e.f22472C.remove(Integer.valueOf(this.f22519f));
                            }
                            return -1L;
                        } catch (IOException unused) {
                            return -1L;
                        }
                    }
                }, 0L);
                return;
            }
            final Http2Connection http2Connection2 = Http2Connection.this;
            synchronized (http2Connection2) {
                Http2Stream http2StreamC = http2Connection2.c(i6);
                if (http2StreamC != null) {
                    http2StreamC.i(Util.w(list), z6);
                    return;
                }
                if (http2Connection2.f22478f) {
                    return;
                }
                if (i6 <= http2Connection2.f22476d) {
                    return;
                }
                if (i6 % 2 == http2Connection2.f22477e % 2) {
                    return;
                }
                final Http2Stream http2Stream = new Http2Stream(i6, http2Connection2, false, z6, Util.w(list));
                http2Connection2.f22476d = i6;
                http2Connection2.f22474b.put(Integer.valueOf(i6), http2Stream);
                TaskQueue taskQueueE = http2Connection2.f22479j.e();
                final String str2 = http2Connection2.f22475c + '[' + i6 + "] onStream";
                taskQueueE.c(new Task(str2) { // from class: okhttp3.internal.http2.Http2Connection$ReaderRunnable$headers$lambda$2$$inlined$execute$default$1
                    @Override // okhttp3.internal.concurrent.Task
                    public final long a() {
                        Http2Stream http2Stream2 = http2Stream;
                        Http2Connection http2Connection3 = http2Connection2;
                        try {
                            http2Connection3.f22473a.b(http2Stream2);
                            return -1L;
                        } catch (IOException e6) {
                            Platform.Companion.get().log("Http2Connection.Listener failure for " + http2Connection3.f22475c, 4, e6);
                            try {
                                http2Stream2.c(ErrorCode.PROTOCOL_ERROR, e6);
                                return -1L;
                            } catch (IOException unused) {
                                return -1L;
                            }
                        }
                    }
                }, 0L);
            }
        }

        public final void c(final int i6, final List list) {
            final Http2Connection http2Connection = Http2Connection.this;
            http2Connection.getClass();
            synchronized (http2Connection) {
                if (http2Connection.f22472C.contains(Integer.valueOf(i6))) {
                    http2Connection.t(i6, ErrorCode.PROTOCOL_ERROR);
                    return;
                }
                http2Connection.f22472C.add(Integer.valueOf(i6));
                TaskQueue taskQueue = http2Connection.l;
                final String str = http2Connection.f22475c + '[' + i6 + "] onRequest";
                taskQueue.c(new Task(str) { // from class: okhttp3.internal.http2.Http2Connection$pushRequestLater$$inlined$execute$default$1
                    @Override // okhttp3.internal.concurrent.Task
                    public final long a() {
                        ((PushObserver.Companion.PushObserverCancel) http2Connection.f22482n).getClass();
                        try {
                            http2Connection.f22470A.i(i6, ErrorCode.CANCEL);
                            synchronized (http2Connection) {
                                http2Connection.f22472C.remove(Integer.valueOf(i6));
                            }
                            return -1L;
                        } catch (IOException unused) {
                            return -1L;
                        }
                    }
                }, 0L);
            }
        }

        /* JADX WARN: Multi-variable type inference failed */
        /* JADX WARN: Type inference failed for: r0v0, types: [okhttp3.internal.http2.Http2Connection] */
        /* JADX WARN: Type inference failed for: r3v0 */
        /* JADX WARN: Type inference failed for: r3v14 */
        /* JADX WARN: Type inference failed for: r3v15 */
        /* JADX WARN: Type inference failed for: r3v4 */
        /* JADX WARN: Type inference failed for: r3v5, types: [okhttp3.internal.http2.ErrorCode] */
        /* JADX WARN: Type inference failed for: r3v6 */
        /* JADX WARN: Type inference failed for: r3v8 */
        /* JADX WARN: Type inference fix 'apply assigned field type' failed
        java.lang.UnsupportedOperationException: ArgType.getObject(), call class: class jadx.core.dex.instructions.args.ArgType$UnknownArg
        	at jadx.core.dex.instructions.args.ArgType.getObject(ArgType.java:593)
        	at jadx.core.dex.attributes.nodes.ClassTypeVarsAttr.getTypeVarsMapFor(ClassTypeVarsAttr.java:35)
        	at jadx.core.dex.nodes.utils.TypeUtils.replaceClassGenerics(TypeUtils.java:177)
        	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.insertExplicitUseCast(FixTypesVisitor.java:397)
        	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.tryFieldTypeWithNewCasts(FixTypesVisitor.java:359)
        	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.applyFieldType(FixTypesVisitor.java:309)
        	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.visit(FixTypesVisitor.java:94)
         */
        @Override // J4.a
        public final Object invoke() throws Throwable {
            Throwable th;
            ErrorCode errorCode;
            ?? r02 = Http2Connection.this;
            Http2Reader http2Reader = this.f22503a;
            ErrorCode errorCode2 = ErrorCode.INTERNAL_ERROR;
            ?? r32 = 1;
            IOException e6 = null;
            try {
                try {
                    try {
                    } catch (Throwable th2) {
                        th = th2;
                    }
                } catch (IOException e7) {
                    e6 = e7;
                    errorCode = errorCode2;
                }
                if (!http2Reader.a(true, this)) {
                    throw new IOException("Required SETTINGS preface not received");
                }
                do {
                    try {
                    } catch (Throwable th3) {
                        th = th3;
                    }
                } while (http2Reader.a(false, this));
                errorCode = ErrorCode.NO_ERROR;
                try {
                    errorCode2 = ErrorCode.CANCEL;
                    r02.a(errorCode, errorCode2, null);
                    r32 = errorCode;
                } catch (IOException e8) {
                    e6 = e8;
                    errorCode2 = ErrorCode.PROTOCOL_ERROR;
                    r02.a(errorCode2, errorCode2, e6);
                    r32 = errorCode;
                }
                Util.c(http2Reader);
                return C3137A.f25453a;
            } catch (Throwable th4) {
                th = th4;
            }
            r32 = errorCode2;
            r02.a(r32, errorCode2, e6);
            Util.c(http2Reader);
            throw th;
        }
    }

    static {
        Settings settings = new Settings();
        settings.c(7, 65535);
        settings.c(5, 16384);
        f22469E = settings;
    }

    public Http2Connection(Builder builder) {
        this.f22473a = builder.f22500f;
        String str = builder.f22497c;
        if (str == null) {
            o.n("connectionName");
            throw null;
        }
        this.f22475c = str;
        this.f22477e = 3;
        TaskRunner taskRunner = builder.f22495a;
        this.f22479j = taskRunner;
        this.f22480k = taskRunner.e();
        this.l = taskRunner.e();
        this.f22481m = taskRunner.e();
        this.f22482n = builder.f22501g;
        Settings settings = new Settings();
        settings.c(7, 16777216);
        this.f22488t = settings;
        this.f22489u = f22469E;
        this.f22493y = r0.a();
        Socket socket = builder.f22496b;
        if (socket == null) {
            o.n("socket");
            throw null;
        }
        this.f22494z = socket;
        I i6 = builder.f22499e;
        if (i6 == null) {
            o.n("sink");
            throw null;
        }
        this.f22470A = new Http2Writer(i6);
        J j4 = builder.f22498d;
        if (j4 == null) {
            o.n("source");
            throw null;
        }
        this.f22471B = new ReaderRunnable(new Http2Reader(j4));
        this.f22472C = new LinkedHashSet();
    }

    public final void a(ErrorCode errorCode, ErrorCode errorCode2, IOException iOException) {
        int i6;
        Object[] array;
        byte[] bArr = Util.f22179a;
        try {
            f(errorCode);
        } catch (IOException unused) {
        }
        synchronized (this) {
            if (this.f22474b.isEmpty()) {
                array = null;
            } else {
                array = this.f22474b.values().toArray(new Http2Stream[0]);
                this.f22474b.clear();
            }
        }
        Http2Stream[] http2StreamArr = (Http2Stream[]) array;
        if (http2StreamArr != null) {
            for (Http2Stream http2Stream : http2StreamArr) {
                try {
                    http2Stream.c(errorCode2, iOException);
                } catch (IOException unused2) {
                }
            }
        }
        try {
            this.f22470A.close();
        } catch (IOException unused3) {
        }
        try {
            this.f22494z.close();
        } catch (IOException unused4) {
        }
        this.f22480k.f();
        this.l.f();
        this.f22481m.f();
    }

    public final void b(IOException iOException) {
        ErrorCode errorCode = ErrorCode.PROTOCOL_ERROR;
        a(errorCode, errorCode, iOException);
    }

    public final synchronized Http2Stream c(int i6) {
        return (Http2Stream) this.f22474b.get(Integer.valueOf(i6));
    }

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

    public final synchronized Http2Stream d(int i6) {
        Http2Stream http2Stream;
        http2Stream = (Http2Stream) this.f22474b.remove(Integer.valueOf(i6));
        notifyAll();
        return http2Stream;
    }

    public final void f(ErrorCode errorCode) {
        synchronized (this.f22470A) {
            synchronized (this) {
                if (this.f22478f) {
                    return;
                }
                this.f22478f = true;
                this.f22470A.d(this.f22476d, errorCode, Util.f22179a);
            }
        }
    }

    public final synchronized void g(long j4) {
        long j6 = this.f22490v + j4;
        this.f22490v = j6;
        long j7 = j6 - this.f22491w;
        if (j7 >= this.f22488t.a() / 2) {
            y(0, j7);
            this.f22491w += 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.f22470A.f22580c);
        r6 = r2;
        r8.f22492x += r6;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void i(int i6, boolean z6, C2740j c2740j, long j4) {
        int iMin;
        long j6;
        if (j4 == 0) {
            this.f22470A.b(z6, i6, c2740j, 0);
            return;
        }
        while (j4 > 0) {
            synchronized (this) {
                while (true) {
                    try {
                        try {
                            long j7 = this.f22492x;
                            long j8 = this.f22493y;
                            if (j7 < j8) {
                                break;
                            } else {
                                if (!this.f22474b.containsKey(Integer.valueOf(i6))) {
                                    throw new IOException("stream closed");
                                }
                                wait();
                            }
                        } catch (InterruptedException unused) {
                            Thread.currentThread().interrupt();
                            throw new InterruptedIOException();
                        }
                    } catch (Throwable th) {
                        throw th;
                    }
                }
            }
            j4 -= j6;
            this.f22470A.b(z6 && j4 == 0, i6, c2740j, iMin);
        }
    }

    public final void t(final int i6, final ErrorCode errorCode) {
        final String str = this.f22475c + '[' + i6 + "] writeSynReset";
        this.f22480k.c(new Task(str) { // from class: okhttp3.internal.http2.Http2Connection$writeSynResetLater$$inlined$execute$default$1
            @Override // okhttp3.internal.concurrent.Task
            public final long a() {
                Http2Connection http2Connection = this;
                try {
                    http2Connection.f22470A.i(i6, errorCode);
                    return -1L;
                } catch (IOException e6) {
                    Http2Connection.Companion companion = Http2Connection.f22468D;
                    http2Connection.b(e6);
                    return -1L;
                }
            }
        }, 0L);
    }

    public final void y(final int i6, final long j4) {
        final String str = this.f22475c + '[' + i6 + "] windowUpdate";
        this.f22480k.c(new Task(str) { // from class: okhttp3.internal.http2.Http2Connection$writeWindowUpdateLater$$inlined$execute$default$1
            @Override // okhttp3.internal.concurrent.Task
            public final long a() {
                Http2Connection http2Connection = this;
                try {
                    http2Connection.f22470A.t(i6, j4);
                    return -1L;
                } catch (IOException e6) {
                    Http2Connection.Companion companion = Http2Connection.f22468D;
                    http2Connection.b(e6);
                    return -1L;
                }
            }
        }, 0L);
    }
}
