package okhttp3.internal.http2;

import c.k;
import c.q.d.j;
import c.q.d.o;
import c.q.d.q;
import d.f;
import d.g;
import d.h;
import d.i;
import d.p;
import java.io.Closeable;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.Socket;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import okhttp3.internal.Util;
import okhttp3.internal.http2.Http2Reader;
import okhttp3.internal.platform.Platform;

/* JADX INFO: compiled from: Http2Connection.kt */
/* JADX INFO: loaded from: classes.dex */
public final class Http2Connection implements Closeable {
    public static final int OKHTTP_CLIENT_WINDOW_SIZE = 16777216;
    private boolean awaitingPong;
    private long bytesLeftInWriteWindow;
    private final boolean client;
    private final String connectionName;
    private final Set<Integer> currentPushRequests;
    private boolean isShutdown;
    private int lastGoodStreamId;
    private final Listener listener;
    private int nextStreamId;
    private final Settings okHttpSettings;
    private final Settings peerSettings;
    private final ThreadPoolExecutor pushExecutor;
    private final PushObserver pushObserver;
    private final ReaderRunnable readerRunnable;
    private boolean receivedInitialPeerSettings;
    private final Socket socket;
    private final Map<Integer, Http2Stream> streams;
    private long unacknowledgedBytesRead;
    private final Http2Writer writer;
    private final ScheduledThreadPoolExecutor writerExecutor;
    public static final Companion Companion = new Companion(null);
    private static final ThreadPoolExecutor listenerExecutor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60, TimeUnit.SECONDS, new SynchronousQueue(), Util.threadFactory("OkHttp Http2Connection", true));

    /* JADX INFO: compiled from: Http2Connection.kt */
    public static final class Builder {
        private boolean client;
        public String connectionName;
        private int pingIntervalMillis;
        public g sink;
        public Socket socket;
        public h source;
        private Listener listener = Listener.REFUSE_INCOMING_STREAMS;
        private PushObserver pushObserver = PushObserver.CANCEL;

        public Builder(boolean z) {
            this.client = z;
        }

        public static /* synthetic */ Builder socket$default(Builder builder, Socket socket, String str, h hVar, g gVar, int i, Object obj) throws IOException {
            if ((i & 2) != 0) {
                str = Util.connectionName(socket);
            }
            if ((i & 4) != 0) {
                hVar = p.d(p.l(socket));
            }
            if ((i & 8) != 0) {
                gVar = p.c(p.h(socket));
            }
            return builder.socket(socket, str, hVar, gVar);
        }

        public final Http2Connection build() {
            return new Http2Connection(this);
        }

        public final boolean getClient$okhttp() {
            return this.client;
        }

        public final String getConnectionName$okhttp() {
            String str = this.connectionName;
            if (str != null) {
                return str;
            }
            j.j("connectionName");
            throw null;
        }

        public final Listener getListener$okhttp() {
            return this.listener;
        }

        public final int getPingIntervalMillis$okhttp() {
            return this.pingIntervalMillis;
        }

        public final PushObserver getPushObserver$okhttp() {
            return this.pushObserver;
        }

        public final g getSink$okhttp() {
            g gVar = this.sink;
            if (gVar != null) {
                return gVar;
            }
            j.j("sink");
            throw null;
        }

        public final Socket getSocket$okhttp() {
            Socket socket = this.socket;
            if (socket != null) {
                return socket;
            }
            j.j("socket");
            throw null;
        }

        public final h getSource$okhttp() {
            h hVar = this.source;
            if (hVar != null) {
                return hVar;
            }
            j.j("source");
            throw null;
        }

        public final Builder listener(Listener listener) {
            j.c(listener, "listener");
            this.listener = listener;
            return this;
        }

        public final Builder pingIntervalMillis(int i) {
            this.pingIntervalMillis = i;
            return this;
        }

        public final Builder pushObserver(PushObserver pushObserver) {
            j.c(pushObserver, "pushObserver");
            this.pushObserver = pushObserver;
            return this;
        }

        public final void setClient$okhttp(boolean z) {
            this.client = z;
        }

        public final void setConnectionName$okhttp(String str) {
            j.c(str, "<set-?>");
            this.connectionName = str;
        }

        public final void setListener$okhttp(Listener listener) {
            j.c(listener, "<set-?>");
            this.listener = listener;
        }

        public final void setPingIntervalMillis$okhttp(int i) {
            this.pingIntervalMillis = i;
        }

        public final void setPushObserver$okhttp(PushObserver pushObserver) {
            j.c(pushObserver, "<set-?>");
            this.pushObserver = pushObserver;
        }

        public final void setSink$okhttp(g gVar) {
            j.c(gVar, "<set-?>");
            this.sink = gVar;
        }

        public final void setSocket$okhttp(Socket socket) {
            j.c(socket, "<set-?>");
            this.socket = socket;
        }

        public final void setSource$okhttp(h hVar) {
            j.c(hVar, "<set-?>");
            this.source = hVar;
        }

        public final Builder socket(Socket socket) throws IOException {
            return socket$default(this, socket, null, null, null, 14, null);
        }

        public final Builder socket(Socket socket, String str) throws IOException {
            return socket$default(this, socket, str, null, null, 12, null);
        }

        public final Builder socket(Socket socket, String str, h hVar) throws IOException {
            return socket$default(this, socket, str, hVar, null, 8, null);
        }

        public final Builder socket(Socket socket, String str, h hVar, g gVar) throws IOException {
            j.c(socket, "socket");
            j.c(str, "connectionName");
            j.c(hVar, "source");
            j.c(gVar, "sink");
            this.socket = socket;
            this.connectionName = str;
            this.source = hVar;
            this.sink = gVar;
            return this;
        }
    }

    /* JADX INFO: compiled from: Http2Connection.kt */
    public static final class Companion {
        private Companion() {
        }

        public /* synthetic */ Companion(c.q.d.g gVar) {
            this();
        }
    }

    /* JADX INFO: compiled from: Http2Connection.kt */
    public static abstract class Listener {
        public static final Companion Companion = new Companion(null);
        public static final Listener REFUSE_INCOMING_STREAMS = new Listener() { // from class: okhttp3.internal.http2.Http2Connection$Listener$Companion$REFUSE_INCOMING_STREAMS$1
            @Override // okhttp3.internal.http2.Http2Connection.Listener
            public void onStream(Http2Stream http2Stream) throws IOException {
                j.c(http2Stream, "stream");
                http2Stream.close(ErrorCode.REFUSED_STREAM, null);
            }
        };

        /* JADX INFO: compiled from: Http2Connection.kt */
        public static final class Companion {
            private Companion() {
            }

            public /* synthetic */ Companion(c.q.d.g gVar) {
                this();
            }
        }

        public void onSettings(Http2Connection http2Connection) {
            j.c(http2Connection, "connection");
        }

        public abstract void onStream(Http2Stream http2Stream) throws IOException;
    }

    /* JADX INFO: compiled from: Http2Connection.kt */
    public final class ReaderRunnable implements Runnable, Http2Reader.Handler {
        private final Http2Reader reader;
        final /* synthetic */ Http2Connection this$0;

        public ReaderRunnable(Http2Connection http2Connection, Http2Reader http2Reader) {
            j.c(http2Reader, "reader");
            this.this$0 = http2Connection;
            this.reader = http2Reader;
        }

        private final void applyAndAckSettings(final Settings settings) {
            ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = this.this$0.writerExecutor;
            final String str = "OkHttp " + this.this$0.getConnectionName$okhttp() + " ACK Settings";
            try {
                scheduledThreadPoolExecutor.execute(new Runnable() { // from class: okhttp3.internal.http2.Http2Connection$ReaderRunnable$applyAndAckSettings$$inlined$tryExecute$1
                    @Override // java.lang.Runnable
                    public final void run() {
                        String str2 = str;
                        Thread threadCurrentThread = Thread.currentThread();
                        j.b(threadCurrentThread, "currentThread");
                        String name = threadCurrentThread.getName();
                        threadCurrentThread.setName(str2);
                        try {
                            try {
                                this.this$0.getWriter().applyAndAckSettings(settings);
                            } catch (IOException e2) {
                                this.this$0.failConnection(e2);
                            }
                        } finally {
                            threadCurrentThread.setName(name);
                        }
                    }
                });
            } catch (RejectedExecutionException unused) {
            }
        }

        @Override // okhttp3.internal.http2.Http2Reader.Handler
        public void ackSettings() {
        }

        @Override // okhttp3.internal.http2.Http2Reader.Handler
        public void alternateService(int i, String str, i iVar, String str2, int i2, long j) {
            j.c(str, "origin");
            j.c(iVar, "protocol");
            j.c(str2, "host");
        }

        @Override // okhttp3.internal.http2.Http2Reader.Handler
        public void data(boolean z, int i, h hVar, int i2) throws IOException {
            j.c(hVar, "source");
            if (this.this$0.pushedStream$okhttp(i)) {
                this.this$0.pushDataLater$okhttp(i, hVar, i2, z);
                return;
            }
            Http2Stream stream = this.this$0.getStream(i);
            if (stream == null) {
                this.this$0.writeSynResetLater$okhttp(i, ErrorCode.PROTOCOL_ERROR);
                long j = i2;
                this.this$0.updateConnectionFlowControl$okhttp(j);
                hVar.l(j);
                return;
            }
            stream.receiveData(hVar, i2);
            if (z) {
                stream.receiveHeaders(Util.EMPTY_HEADERS, true);
            }
        }

        public final Http2Reader getReader$okhttp() {
            return this.reader;
        }

        @Override // okhttp3.internal.http2.Http2Reader.Handler
        public void goAway(int i, ErrorCode errorCode, i iVar) {
            int i2;
            Http2Stream[] http2StreamArr;
            j.c(errorCode, "errorCode");
            j.c(iVar, "debugData");
            iVar.s();
            synchronized (this.this$0) {
                Collection<Http2Stream> collectionValues = this.this$0.getStreams$okhttp().values();
                if (collectionValues == null) {
                    throw new c.h("null cannot be cast to non-null type java.util.Collection<T>");
                }
                Object[] array = collectionValues.toArray(new Http2Stream[0]);
                if (array == null) {
                    throw new c.h("null cannot be cast to non-null type kotlin.Array<T>");
                }
                http2StreamArr = (Http2Stream[]) array;
                this.this$0.setShutdown$okhttp(true);
                c.j jVar = c.j.f1417a;
            }
            for (Http2Stream http2Stream : http2StreamArr) {
                if (http2Stream.getId() > i && http2Stream.isLocallyInitiated()) {
                    http2Stream.receiveRstStream(ErrorCode.REFUSED_STREAM);
                    this.this$0.removeStream$okhttp(http2Stream.getId());
                }
            }
        }

        @Override // okhttp3.internal.http2.Http2Reader.Handler
        public void headers(final boolean z, final int i, int i2, final List<Header> list) {
            j.c(list, "headerBlock");
            if (this.this$0.pushedStream$okhttp(i)) {
                this.this$0.pushHeadersLater$okhttp(i, list, z);
                return;
            }
            synchronized (this.this$0) {
                final Http2Stream stream = this.this$0.getStream(i);
                if (stream != null) {
                    c.j jVar = c.j.f1417a;
                    stream.receiveHeaders(Util.toHeaders(list), z);
                    return;
                }
                if (this.this$0.isShutdown()) {
                    return;
                }
                if (i <= this.this$0.getLastGoodStreamId$okhttp()) {
                    return;
                }
                if (i % 2 == this.this$0.getNextStreamId$okhttp() % 2) {
                    return;
                }
                final Http2Stream http2Stream = new Http2Stream(i, this.this$0, false, z, Util.toHeaders(list));
                this.this$0.setLastGoodStreamId$okhttp(i);
                this.this$0.getStreams$okhttp().put(Integer.valueOf(i), http2Stream);
                ThreadPoolExecutor threadPoolExecutor = Http2Connection.listenerExecutor;
                final String str = "OkHttp " + this.this$0.getConnectionName$okhttp() + " stream " + i;
                threadPoolExecutor.execute(new Runnable() { // from class: okhttp3.internal.http2.Http2Connection$ReaderRunnable$headers$$inlined$synchronized$lambda$1
                    @Override // java.lang.Runnable
                    public final void run() {
                        String str2 = str;
                        Thread threadCurrentThread = Thread.currentThread();
                        j.b(threadCurrentThread, "currentThread");
                        String name = threadCurrentThread.getName();
                        threadCurrentThread.setName(str2);
                        try {
                            try {
                                this.this$0.getListener$okhttp().onStream(http2Stream);
                            } catch (IOException e2) {
                                Platform.Companion.get().log(4, "Http2Connection.Listener failure for " + this.this$0.getConnectionName$okhttp(), e2);
                                try {
                                    http2Stream.close(ErrorCode.PROTOCOL_ERROR, e2);
                                } catch (IOException unused) {
                                }
                            }
                        } finally {
                            threadCurrentThread.setName(name);
                        }
                    }
                });
            }
        }

        @Override // okhttp3.internal.http2.Http2Reader.Handler
        public void ping(boolean z, final int i, final int i2) {
            if (z) {
                synchronized (this.this$0) {
                    this.this$0.awaitingPong = false;
                    Http2Connection http2Connection = this.this$0;
                    if (http2Connection == null) {
                        throw new c.h("null cannot be cast to non-null type java.lang.Object");
                    }
                    http2Connection.notifyAll();
                    c.j jVar = c.j.f1417a;
                }
                return;
            }
            ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = this.this$0.writerExecutor;
            final String str = "OkHttp " + this.this$0.getConnectionName$okhttp() + " ping";
            try {
                scheduledThreadPoolExecutor.execute(new Runnable() { // from class: okhttp3.internal.http2.Http2Connection$ReaderRunnable$ping$$inlined$tryExecute$1
                    @Override // java.lang.Runnable
                    public final void run() {
                        String str2 = str;
                        Thread threadCurrentThread = Thread.currentThread();
                        j.b(threadCurrentThread, "currentThread");
                        String name = threadCurrentThread.getName();
                        threadCurrentThread.setName(str2);
                        try {
                            this.this$0.writePing(true, i, i2);
                        } finally {
                            threadCurrentThread.setName(name);
                        }
                    }
                });
            } catch (RejectedExecutionException unused) {
            }
        }

        @Override // okhttp3.internal.http2.Http2Reader.Handler
        public void priority(int i, int i2, int i3, boolean z) {
        }

        @Override // okhttp3.internal.http2.Http2Reader.Handler
        public void pushPromise(int i, int i2, List<Header> list) {
            j.c(list, "requestHeaders");
            this.this$0.pushRequestLater$okhttp(i2, list);
        }

        @Override // okhttp3.internal.http2.Http2Reader.Handler
        public void rstStream(int i, ErrorCode errorCode) {
            j.c(errorCode, "errorCode");
            if (this.this$0.pushedStream$okhttp(i)) {
                this.this$0.pushResetLater$okhttp(i, errorCode);
                return;
            }
            Http2Stream http2StreamRemoveStream$okhttp = this.this$0.removeStream$okhttp(i);
            if (http2StreamRemoveStream$okhttp != null) {
                http2StreamRemoveStream$okhttp.receiveRstStream(errorCode);
            }
        }

        @Override // java.lang.Runnable
        public void run() throws Throwable {
            ErrorCode errorCode;
            ErrorCode errorCode2;
            ErrorCode errorCode3 = ErrorCode.INTERNAL_ERROR;
            IOException e2 = null;
            try {
                this.reader.readConnectionPreface(this);
                while (this.reader.nextFrame(false, this)) {
                }
                errorCode = ErrorCode.NO_ERROR;
                try {
                    try {
                        errorCode2 = ErrorCode.CANCEL;
                    } catch (IOException e3) {
                        e2 = e3;
                        errorCode = ErrorCode.PROTOCOL_ERROR;
                        errorCode2 = ErrorCode.PROTOCOL_ERROR;
                    }
                } catch (Throwable th) {
                    th = th;
                    this.this$0.close$okhttp(errorCode, errorCode3, e2);
                    Util.closeQuietly(this.reader);
                    throw th;
                }
            } catch (IOException e4) {
                e2 = e4;
            } catch (Throwable th2) {
                th = th2;
                errorCode = errorCode3;
                this.this$0.close$okhttp(errorCode, errorCode3, e2);
                Util.closeQuietly(this.reader);
                throw th;
            }
            this.this$0.close$okhttp(errorCode, errorCode2, e2);
            Util.closeQuietly(this.reader);
        }

        /* JADX WARN: Multi-variable type inference failed */
        /* JADX WARN: Type inference failed for: r1v24, types: [T, okhttp3.internal.http2.Http2Stream[]] */
        @Override // okhttp3.internal.http2.Http2Reader.Handler
        public void settings(final boolean z, final Settings settings) {
            j.c(settings, "settings");
            final c.q.d.p pVar = new c.q.d.p();
            pVar.f1448b = 0L;
            final q qVar = new q();
            qVar.f1449b = null;
            synchronized (this.this$0) {
                int initialWindowSize = this.this$0.getPeerSettings().getInitialWindowSize();
                if (z) {
                    this.this$0.getPeerSettings().clear();
                }
                this.this$0.getPeerSettings().merge(settings);
                applyAndAckSettings(settings);
                int initialWindowSize2 = this.this$0.getPeerSettings().getInitialWindowSize();
                if (initialWindowSize2 != -1 && initialWindowSize2 != initialWindowSize) {
                    pVar.f1448b = initialWindowSize2 - initialWindowSize;
                    if (!this.this$0.getReceivedInitialPeerSettings$okhttp()) {
                        this.this$0.setReceivedInitialPeerSettings$okhttp(true);
                    }
                    if (!this.this$0.getStreams$okhttp().isEmpty()) {
                        Collection<Http2Stream> collectionValues = this.this$0.getStreams$okhttp().values();
                        if (collectionValues == null) {
                            throw new c.h("null cannot be cast to non-null type java.util.Collection<T>");
                        }
                        Object[] array = collectionValues.toArray(new Http2Stream[0]);
                        if (array == null) {
                            throw new c.h("null cannot be cast to non-null type kotlin.Array<T>");
                        }
                        qVar.f1449b = (Http2Stream[]) array;
                    }
                }
                ThreadPoolExecutor threadPoolExecutor = Http2Connection.listenerExecutor;
                final String str = "OkHttp " + this.this$0.getConnectionName$okhttp() + " settings";
                threadPoolExecutor.execute(new Runnable() { // from class: okhttp3.internal.http2.Http2Connection$ReaderRunnable$settings$$inlined$synchronized$lambda$1
                    @Override // java.lang.Runnable
                    public final void run() {
                        String str2 = str;
                        Thread threadCurrentThread = Thread.currentThread();
                        j.b(threadCurrentThread, "currentThread");
                        String name = threadCurrentThread.getName();
                        threadCurrentThread.setName(str2);
                        try {
                            this.this$0.getListener$okhttp().onSettings(this.this$0);
                        } finally {
                            threadCurrentThread.setName(name);
                        }
                    }
                });
                c.j jVar = c.j.f1417a;
            }
            T t = qVar.f1449b;
            if (((Http2Stream[]) t) == null || pVar.f1448b == 0) {
                return;
            }
            Http2Stream[] http2StreamArr = (Http2Stream[]) t;
            if (http2StreamArr == null) {
                j.g();
                throw null;
            }
            for (Http2Stream http2Stream : http2StreamArr) {
                synchronized (http2Stream) {
                    http2Stream.addBytesToWriteWindow(pVar.f1448b);
                    c.j jVar2 = c.j.f1417a;
                }
            }
        }

        @Override // okhttp3.internal.http2.Http2Reader.Handler
        public void windowUpdate(int i, long j) {
            if (i != 0) {
                Http2Stream stream = this.this$0.getStream(i);
                if (stream != null) {
                    synchronized (stream) {
                        stream.addBytesToWriteWindow(j);
                        c.j jVar = c.j.f1417a;
                    }
                    return;
                }
                return;
            }
            synchronized (this.this$0) {
                Http2Connection http2Connection = this.this$0;
                http2Connection.setBytesLeftInWriteWindow$okhttp(http2Connection.getBytesLeftInWriteWindow() + j);
                Http2Connection http2Connection2 = this.this$0;
                if (http2Connection2 == null) {
                    throw new c.h("null cannot be cast to non-null type java.lang.Object");
                }
                http2Connection2.notifyAll();
                c.j jVar2 = c.j.f1417a;
            }
        }
    }

    public Http2Connection(Builder builder) {
        j.c(builder, "builder");
        this.client = builder.getClient$okhttp();
        this.listener = builder.getListener$okhttp();
        this.streams = new LinkedHashMap();
        this.connectionName = builder.getConnectionName$okhttp();
        this.nextStreamId = builder.getClient$okhttp() ? 3 : 2;
        this.writerExecutor = new ScheduledThreadPoolExecutor(1, Util.threadFactory(Util.format("OkHttp %s Writer", this.connectionName), false));
        this.pushExecutor = new ThreadPoolExecutor(0, 1, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue(), Util.threadFactory(Util.format("OkHttp %s Push Observer", this.connectionName), true));
        this.pushObserver = builder.getPushObserver$okhttp();
        Settings settings = new Settings();
        if (builder.getClient$okhttp()) {
            settings.set(7, OKHTTP_CLIENT_WINDOW_SIZE);
        }
        this.okHttpSettings = settings;
        Settings settings2 = new Settings();
        settings2.set(7, Settings.DEFAULT_INITIAL_WINDOW_SIZE);
        settings2.set(5, Http2.INITIAL_MAX_FRAME_SIZE);
        this.peerSettings = settings2;
        this.bytesLeftInWriteWindow = settings2.getInitialWindowSize();
        this.socket = builder.getSocket$okhttp();
        this.writer = new Http2Writer(builder.getSink$okhttp(), this.client);
        this.readerRunnable = new ReaderRunnable(this, new Http2Reader(builder.getSource$okhttp(), this.client));
        this.currentPushRequests = new LinkedHashSet();
        if (builder.getPingIntervalMillis$okhttp() != 0) {
            this.writerExecutor.scheduleAtFixedRate(new Runnable() { // from class: okhttp3.internal.http2.Http2Connection.1
                @Override // java.lang.Runnable
                public final void run() {
                    String str = "OkHttp " + Http2Connection.this.getConnectionName$okhttp() + " ping";
                    Thread threadCurrentThread = Thread.currentThread();
                    j.b(threadCurrentThread, "currentThread");
                    String name = threadCurrentThread.getName();
                    threadCurrentThread.setName(str);
                    try {
                        Http2Connection.this.writePing(false, 0, 0);
                    } finally {
                        threadCurrentThread.setName(name);
                    }
                }
            }, builder.getPingIntervalMillis$okhttp(), builder.getPingIntervalMillis$okhttp(), TimeUnit.MILLISECONDS);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void failConnection(IOException iOException) {
        ErrorCode errorCode = ErrorCode.PROTOCOL_ERROR;
        close$okhttp(errorCode, errorCode, iOException);
    }

    public static /* synthetic */ void start$default(Http2Connection http2Connection, boolean z, int i, Object obj) throws IOException {
        if ((i & 1) != 0) {
            z = true;
        }
        http2Connection.start(z);
    }

    public final synchronized void awaitPong() throws InterruptedException {
        while (this.awaitingPong) {
            wait();
        }
    }

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

    public final void close$okhttp(ErrorCode errorCode, ErrorCode errorCode2, IOException iOException) {
        int i;
        j.c(errorCode, "connectionCode");
        j.c(errorCode2, "streamCode");
        boolean z = !Thread.holdsLock(this);
        if (k.f1418a && !z) {
            throw new AssertionError("Assertion failed");
        }
        try {
            shutdown(errorCode);
        } catch (IOException unused) {
        }
        Http2Stream[] http2StreamArr = null;
        synchronized (this) {
            if (!this.streams.isEmpty()) {
                Collection<Http2Stream> collectionValues = this.streams.values();
                if (collectionValues == null) {
                    throw new c.h("null cannot be cast to non-null type java.util.Collection<T>");
                }
                Object[] array = collectionValues.toArray(new Http2Stream[0]);
                if (array == null) {
                    throw new c.h("null cannot be cast to non-null type kotlin.Array<T>");
                }
                http2StreamArr = (Http2Stream[]) array;
                this.streams.clear();
            }
            c.j jVar = c.j.f1417a;
        }
        if (http2StreamArr != null) {
            for (Http2Stream http2Stream : http2StreamArr) {
                try {
                    http2Stream.close(errorCode2, iOException);
                } catch (IOException unused2) {
                }
            }
        }
        try {
            this.writer.close();
        } catch (IOException unused3) {
        }
        try {
            this.socket.close();
        } catch (IOException unused4) {
        }
        this.writerExecutor.shutdown();
        this.pushExecutor.shutdown();
    }

    public final void flush() throws IOException {
        this.writer.flush();
    }

    public final long getBytesLeftInWriteWindow() {
        return this.bytesLeftInWriteWindow;
    }

    public final boolean getClient$okhttp() {
        return this.client;
    }

    public final String getConnectionName$okhttp() {
        return this.connectionName;
    }

    public final int getLastGoodStreamId$okhttp() {
        return this.lastGoodStreamId;
    }

    public final Listener getListener$okhttp() {
        return this.listener;
    }

    public final int getNextStreamId$okhttp() {
        return this.nextStreamId;
    }

    public final Settings getOkHttpSettings() {
        return this.okHttpSettings;
    }

    public final Settings getPeerSettings() {
        return this.peerSettings;
    }

    public final ReaderRunnable getReaderRunnable() {
        return this.readerRunnable;
    }

    public final boolean getReceivedInitialPeerSettings$okhttp() {
        return this.receivedInitialPeerSettings;
    }

    public final Socket getSocket$okhttp() {
        return this.socket;
    }

    public final synchronized Http2Stream getStream(int i) {
        return this.streams.get(Integer.valueOf(i));
    }

    public final Map<Integer, Http2Stream> getStreams$okhttp() {
        return this.streams;
    }

    public final long getUnacknowledgedBytesRead() {
        return this.unacknowledgedBytesRead;
    }

    public final Http2Writer getWriter() {
        return this.writer;
    }

    public final synchronized boolean isShutdown() {
        return this.isShutdown;
    }

    public final synchronized int maxConcurrentStreams() {
        return this.peerSettings.getMaxConcurrentStreams(Integer.MAX_VALUE);
    }

    public final Http2Stream newStream(List<Header> list, boolean z) throws IOException {
        j.c(list, "requestHeaders");
        return newStream(0, list, z);
    }

    public final synchronized int openStreamCount() {
        return this.streams.size();
    }

    public final void pushDataLater$okhttp(final int i, h hVar, final int i2, final boolean z) throws IOException {
        j.c(hVar, "source");
        final f fVar = new f();
        long j = i2;
        hVar.u(j);
        hVar.read(fVar, j);
        if (this.isShutdown) {
            return;
        }
        ThreadPoolExecutor threadPoolExecutor = this.pushExecutor;
        final String str = "OkHttp " + this.connectionName + " Push Data[" + i + ']';
        threadPoolExecutor.execute(new Runnable() { // from class: okhttp3.internal.http2.Http2Connection$pushDataLater$$inlined$execute$1
            @Override // java.lang.Runnable
            public final void run() {
                String str2 = str;
                Thread threadCurrentThread = Thread.currentThread();
                j.b(threadCurrentThread, "currentThread");
                String name = threadCurrentThread.getName();
                threadCurrentThread.setName(str2);
                try {
                    boolean zOnData = this.pushObserver.onData(i, fVar, i2, z);
                    if (zOnData) {
                        this.getWriter().rstStream(i, ErrorCode.CANCEL);
                    }
                    if (zOnData || z) {
                        synchronized (this) {
                            this.currentPushRequests.remove(Integer.valueOf(i));
                        }
                    }
                } catch (IOException unused) {
                } catch (Throwable th) {
                    threadCurrentThread.setName(name);
                    throw th;
                }
                threadCurrentThread.setName(name);
            }
        });
    }

    public final void pushHeadersLater$okhttp(final int i, final List<Header> list, final boolean z) {
        j.c(list, "requestHeaders");
        if (this.isShutdown) {
            return;
        }
        ThreadPoolExecutor threadPoolExecutor = this.pushExecutor;
        final String str = "OkHttp " + this.connectionName + " Push Headers[" + i + ']';
        try {
            threadPoolExecutor.execute(new Runnable() { // from class: okhttp3.internal.http2.Http2Connection$pushHeadersLater$$inlined$tryExecute$1
                /* JADX WARN: Removed duplicated region for block: B:24:0x003a A[EXC_TOP_SPLITTER, SYNTHETIC] */
                /* JADX WARN: Removed duplicated region for block: B:9:0x0037 A[Catch: IOException -> 0x004e, all -> 0x0052, TryCatch #0 {all -> 0x0052, blocks: (B:3:0x0012, B:5:0x0024, B:7:0x0033, B:9:0x0037, B:10:0x0039, B:12:0x0049, B:15:0x004c, B:16:0x004d), top: B:23:0x0012 }] */
                @Override // java.lang.Runnable
                /*
                    Code decompiled incorrectly, please refer to instructions dump.
                    To view partially-correct add '--show-bad-code' argument
                */
                public final void run() {
                    /*
                        r6 = this;
                        java.lang.String r0 = r1
                        java.lang.Thread r1 = java.lang.Thread.currentThread()
                        java.lang.String r2 = "currentThread"
                        c.q.d.j.b(r1, r2)
                        java.lang.String r2 = r1.getName()
                        r1.setName(r0)
                        okhttp3.internal.http2.Http2Connection r0 = r2     // Catch: java.lang.Throwable -> L52
                        okhttp3.internal.http2.PushObserver r0 = okhttp3.internal.http2.Http2Connection.access$getPushObserver$p(r0)     // Catch: java.lang.Throwable -> L52
                        int r3 = r3     // Catch: java.lang.Throwable -> L52
                        java.util.List r4 = r4     // Catch: java.lang.Throwable -> L52
                        boolean r5 = r5     // Catch: java.lang.Throwable -> L52
                        boolean r0 = r0.onHeaders(r3, r4, r5)     // Catch: java.lang.Throwable -> L52
                        if (r0 == 0) goto L31
                        okhttp3.internal.http2.Http2Connection r3 = r2     // Catch: java.io.IOException -> L4e java.lang.Throwable -> L52
                        okhttp3.internal.http2.Http2Writer r3 = r3.getWriter()     // Catch: java.io.IOException -> L4e java.lang.Throwable -> L52
                        int r4 = r3     // Catch: java.io.IOException -> L4e java.lang.Throwable -> L52
                        okhttp3.internal.http2.ErrorCode r5 = okhttp3.internal.http2.ErrorCode.CANCEL     // Catch: java.io.IOException -> L4e java.lang.Throwable -> L52
                        r3.rstStream(r4, r5)     // Catch: java.io.IOException -> L4e java.lang.Throwable -> L52
                    L31:
                        if (r0 != 0) goto L37
                        boolean r0 = r5     // Catch: java.io.IOException -> L4e java.lang.Throwable -> L52
                        if (r0 == 0) goto L4e
                    L37:
                        okhttp3.internal.http2.Http2Connection r0 = r2     // Catch: java.io.IOException -> L4e java.lang.Throwable -> L52
                        monitor-enter(r0)     // Catch: java.io.IOException -> L4e java.lang.Throwable -> L52
                        okhttp3.internal.http2.Http2Connection r3 = r2     // Catch: java.lang.Throwable -> L4b
                        java.util.Set r3 = okhttp3.internal.http2.Http2Connection.access$getCurrentPushRequests$p(r3)     // Catch: java.lang.Throwable -> L4b
                        int r4 = r3     // Catch: java.lang.Throwable -> L4b
                        java.lang.Integer r4 = java.lang.Integer.valueOf(r4)     // Catch: java.lang.Throwable -> L4b
                        r3.remove(r4)     // Catch: java.lang.Throwable -> L4b
                        monitor-exit(r0)     // Catch: java.io.IOException -> L4e java.lang.Throwable -> L52
                        goto L4e
                    L4b:
                        r3 = move-exception
                        monitor-exit(r0)     // Catch: java.io.IOException -> L4e java.lang.Throwable -> L52
                        throw r3     // Catch: java.io.IOException -> L4e java.lang.Throwable -> L52
                    L4e:
                        r1.setName(r2)
                        return
                    L52:
                        r0 = move-exception
                        r1.setName(r2)
                        throw r0
                    */
                    throw new UnsupportedOperationException("Method not decompiled: okhttp3.internal.http2.Http2Connection$pushHeadersLater$$inlined$tryExecute$1.run():void");
                }
            });
        } catch (RejectedExecutionException unused) {
        }
    }

    public final void pushRequestLater$okhttp(final int i, final List<Header> list) {
        j.c(list, "requestHeaders");
        synchronized (this) {
            if (this.currentPushRequests.contains(Integer.valueOf(i))) {
                writeSynResetLater$okhttp(i, ErrorCode.PROTOCOL_ERROR);
                return;
            }
            this.currentPushRequests.add(Integer.valueOf(i));
            if (this.isShutdown) {
                return;
            }
            ThreadPoolExecutor threadPoolExecutor = this.pushExecutor;
            final String str = "OkHttp " + this.connectionName + " Push Request[" + i + ']';
            try {
                threadPoolExecutor.execute(new Runnable() { // from class: okhttp3.internal.http2.Http2Connection$pushRequestLater$$inlined$tryExecute$1
                    @Override // java.lang.Runnable
                    public final void run() {
                        String str2 = str;
                        Thread threadCurrentThread = Thread.currentThread();
                        j.b(threadCurrentThread, "currentThread");
                        String name = threadCurrentThread.getName();
                        threadCurrentThread.setName(str2);
                        try {
                            if (this.pushObserver.onRequest(i, list)) {
                                try {
                                    this.getWriter().rstStream(i, ErrorCode.CANCEL);
                                    synchronized (this) {
                                        this.currentPushRequests.remove(Integer.valueOf(i));
                                    }
                                } catch (IOException unused) {
                                }
                            }
                        } finally {
                            threadCurrentThread.setName(name);
                        }
                    }
                });
            } catch (RejectedExecutionException unused) {
            }
        }
    }

    public final void pushResetLater$okhttp(final int i, final ErrorCode errorCode) {
        j.c(errorCode, "errorCode");
        if (this.isShutdown) {
            return;
        }
        ThreadPoolExecutor threadPoolExecutor = this.pushExecutor;
        final String str = "OkHttp " + this.connectionName + " Push Reset[" + i + ']';
        threadPoolExecutor.execute(new Runnable() { // from class: okhttp3.internal.http2.Http2Connection$pushResetLater$$inlined$execute$1
            @Override // java.lang.Runnable
            public final void run() {
                String str2 = str;
                Thread threadCurrentThread = Thread.currentThread();
                j.b(threadCurrentThread, "currentThread");
                String name = threadCurrentThread.getName();
                threadCurrentThread.setName(str2);
                try {
                    this.pushObserver.onReset(i, errorCode);
                    synchronized (this) {
                        this.currentPushRequests.remove(Integer.valueOf(i));
                    }
                } finally {
                    threadCurrentThread.setName(name);
                }
            }
        });
    }

    public final Http2Stream pushStream(int i, List<Header> list, boolean z) throws IOException {
        j.c(list, "requestHeaders");
        if (!this.client) {
            return newStream(i, list, z);
        }
        throw new IllegalStateException("Client cannot push requests.".toString());
    }

    public final boolean pushedStream$okhttp(int i) {
        return i != 0 && (i & 1) == 0;
    }

    public final synchronized Http2Stream removeStream$okhttp(int i) {
        Http2Stream http2StreamRemove;
        http2StreamRemove = this.streams.remove(Integer.valueOf(i));
        notifyAll();
        return http2StreamRemove;
    }

    public final void setBytesLeftInWriteWindow$okhttp(long j) {
        this.bytesLeftInWriteWindow = j;
    }

    public final void setLastGoodStreamId$okhttp(int i) {
        this.lastGoodStreamId = i;
    }

    public final void setNextStreamId$okhttp(int i) {
        this.nextStreamId = i;
    }

    public final void setReceivedInitialPeerSettings$okhttp(boolean z) {
        this.receivedInitialPeerSettings = z;
    }

    public final void setSettings(Settings settings) throws IOException {
        j.c(settings, "settings");
        synchronized (this.writer) {
            synchronized (this) {
                if (this.isShutdown) {
                    throw new ConnectionShutdownException();
                }
                this.okHttpSettings.merge(settings);
                c.j jVar = c.j.f1417a;
            }
            this.writer.settings(settings);
            c.j jVar2 = c.j.f1417a;
        }
    }

    public final void setShutdown$okhttp(boolean z) {
        this.isShutdown = z;
    }

    public final void shutdown(ErrorCode errorCode) throws IOException {
        j.c(errorCode, "statusCode");
        synchronized (this.writer) {
            synchronized (this) {
                if (this.isShutdown) {
                    return;
                }
                this.isShutdown = true;
                int i = this.lastGoodStreamId;
                c.j jVar = c.j.f1417a;
                this.writer.goAway(i, errorCode, Util.EMPTY_BYTE_ARRAY);
                c.j jVar2 = c.j.f1417a;
            }
        }
    }

    public final void start() throws IOException {
        start$default(this, false, 1, null);
    }

    public final void start(boolean z) throws IOException {
        if (z) {
            this.writer.connectionPreface();
            this.writer.settings(this.okHttpSettings);
            if (this.okHttpSettings.getInitialWindowSize() != 65535) {
                this.writer.windowUpdate(0, r6 - Settings.DEFAULT_INITIAL_WINDOW_SIZE);
            }
        }
        new Thread(this.readerRunnable, "OkHttp " + this.connectionName).start();
    }

    public final synchronized void updateConnectionFlowControl$okhttp(long j) {
        long j2 = this.unacknowledgedBytesRead + j;
        this.unacknowledgedBytesRead = j2;
        if (j2 >= this.okHttpSettings.getInitialWindowSize() / 2) {
            writeWindowUpdateLater$okhttp(0, this.unacknowledgedBytesRead);
            this.unacknowledgedBytesRead = 0L;
        }
    }

    public final void writeData(int i, boolean z, f fVar, long j) throws IOException {
        int iMin;
        if (j == 0) {
            this.writer.data(z, i, fVar, 0);
            return;
        }
        while (j > 0) {
            o oVar = new o();
            synchronized (this) {
                while (this.bytesLeftInWriteWindow <= 0) {
                    try {
                        if (!this.streams.containsKey(Integer.valueOf(i))) {
                            throw new IOException("stream closed");
                        }
                        wait();
                    } catch (InterruptedException unused) {
                        Thread.currentThread().interrupt();
                        throw new InterruptedIOException();
                    }
                }
                int iMin2 = (int) Math.min(j, this.bytesLeftInWriteWindow);
                oVar.f1447b = iMin2;
                iMin = Math.min(iMin2, this.writer.maxDataLength());
                oVar.f1447b = iMin;
                this.bytesLeftInWriteWindow -= (long) iMin;
                c.j jVar = c.j.f1417a;
            }
            j -= (long) iMin;
            this.writer.data(z && j == 0, i, fVar, oVar.f1447b);
        }
    }

    public final void writeHeaders$okhttp(int i, boolean z, List<Header> list) throws IOException {
        j.c(list, "alternating");
        this.writer.headers(z, i, list);
    }

    public final void writePing(boolean z, int i, int i2) {
        boolean z2;
        if (!z) {
            synchronized (this) {
                z2 = this.awaitingPong;
                this.awaitingPong = true;
                c.j jVar = c.j.f1417a;
            }
            if (z2) {
                failConnection(null);
                return;
            }
        }
        try {
            this.writer.ping(z, i, i2);
        } catch (IOException e2) {
            failConnection(e2);
        }
    }

    public final void writePingAndAwaitPong() throws InterruptedException {
        writePing(false, 1330343787, -257978967);
        awaitPong();
    }

    public final void writeSynReset$okhttp(int i, ErrorCode errorCode) throws IOException {
        j.c(errorCode, "statusCode");
        this.writer.rstStream(i, errorCode);
    }

    public final void writeSynResetLater$okhttp(final int i, final ErrorCode errorCode) {
        j.c(errorCode, "errorCode");
        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = this.writerExecutor;
        final String str = "OkHttp " + this.connectionName + " stream " + i;
        try {
            scheduledThreadPoolExecutor.execute(new Runnable() { // from class: okhttp3.internal.http2.Http2Connection$writeSynResetLater$$inlined$tryExecute$1
                @Override // java.lang.Runnable
                public final void run() {
                    String str2 = str;
                    Thread threadCurrentThread = Thread.currentThread();
                    j.b(threadCurrentThread, "currentThread");
                    String name = threadCurrentThread.getName();
                    threadCurrentThread.setName(str2);
                    try {
                        try {
                            this.writeSynReset$okhttp(i, errorCode);
                        } catch (IOException e2) {
                            this.failConnection(e2);
                        }
                    } finally {
                        threadCurrentThread.setName(name);
                    }
                }
            });
        } catch (RejectedExecutionException unused) {
        }
    }

    public final void writeWindowUpdateLater$okhttp(final int i, final long j) {
        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = this.writerExecutor;
        final String str = "OkHttp Window Update " + this.connectionName + " stream " + i;
        try {
            scheduledThreadPoolExecutor.execute(new Runnable() { // from class: okhttp3.internal.http2.Http2Connection$writeWindowUpdateLater$$inlined$tryExecute$1
                @Override // java.lang.Runnable
                public final void run() {
                    String str2 = str;
                    Thread threadCurrentThread = Thread.currentThread();
                    j.b(threadCurrentThread, "currentThread");
                    String name = threadCurrentThread.getName();
                    threadCurrentThread.setName(str2);
                    try {
                        try {
                            this.getWriter().windowUpdate(i, j);
                        } catch (IOException e2) {
                            this.failConnection(e2);
                        }
                    } finally {
                        threadCurrentThread.setName(name);
                    }
                }
            });
        } catch (RejectedExecutionException unused) {
        }
    }

    private final Http2Stream newStream(int i, List<Header> list, boolean z) throws IOException {
        int i2;
        Http2Stream http2Stream;
        boolean z2;
        boolean z3 = !z;
        synchronized (this.writer) {
            synchronized (this) {
                if (this.nextStreamId > 1073741823) {
                    shutdown(ErrorCode.REFUSED_STREAM);
                }
                if (this.isShutdown) {
                    throw new ConnectionShutdownException();
                }
                i2 = this.nextStreamId;
                this.nextStreamId += 2;
                http2Stream = new Http2Stream(i2, this, z3, false, null);
                z2 = !z || this.bytesLeftInWriteWindow == 0 || http2Stream.getBytesLeftInWriteWindow() == 0;
                if (http2Stream.isOpen()) {
                    this.streams.put(Integer.valueOf(i2), http2Stream);
                }
                c.j jVar = c.j.f1417a;
            }
            if (i == 0) {
                this.writer.headers(z3, i2, list);
            } else {
                if (!(true ^ this.client)) {
                    throw new IllegalArgumentException("client streams shouldn't have associated stream IDs".toString());
                }
                this.writer.pushPromise(i, i2, list);
            }
            c.j jVar2 = c.j.f1417a;
        }
        if (z2) {
            this.writer.flush();
        }
        return http2Stream;
    }
}
