package io.netty.handler.codec.spdy;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.spdy.SpdySession;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import io.netty.util.internal.ThrowableUtil;
import java.util.Iterator;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

/* JADX INFO: loaded from: classes.dex */
public class SpdySessionHandler extends ChannelDuplexHandler {
    private static final int DEFAULT_MAX_CONCURRENT_STREAMS = Integer.MAX_VALUE;
    private static final int DEFAULT_WINDOW_SIZE = 65536;
    private static final SpdyProtocolException PROTOCOL_EXCEPTION = (SpdyProtocolException) ThrowableUtil.unknownStackTrace(new SpdyProtocolException(), SpdySessionHandler.class, "handleOutboundMessage(...)");
    private static final SpdyProtocolException STREAM_CLOSED = (SpdyProtocolException) ThrowableUtil.unknownStackTrace(new SpdyProtocolException("Stream closed"), SpdySessionHandler.class, "removeStream(...)");
    private ChannelFutureListener closeSessionFutureListener;
    private int lastGoodStreamId;
    private final int minorVersion;
    private boolean receivedGoAwayFrame;
    private boolean sentGoAwayFrame;
    private final boolean server;
    private int initialSendWindowSize = 65536;
    private int initialReceiveWindowSize = 65536;
    private volatile int initialSessionReceiveWindowSize = 65536;
    private final SpdySession spdySession = new SpdySession(this.initialSendWindowSize, this.initialReceiveWindowSize);
    private int remoteConcurrentStreams = Integer.MAX_VALUE;
    private int localConcurrentStreams = Integer.MAX_VALUE;
    private final AtomicInteger pings = new AtomicInteger();

    public static final class ClosingChannelFutureListener implements ChannelFutureListener {
        private final ChannelHandlerContext ctx;
        private final ChannelPromise promise;

        public ClosingChannelFutureListener(ChannelHandlerContext channelHandlerContext, ChannelPromise channelPromise) {
            this.ctx = channelHandlerContext;
            this.promise = channelPromise;
        }

        @Override // io.netty.util.concurrent.GenericFutureListener
        public void operationComplete(ChannelFuture channelFuture) {
            this.ctx.close(this.promise);
        }
    }

    public SpdySessionHandler(SpdyVersion spdyVersion, boolean z2) {
        Objects.requireNonNull(spdyVersion, "version");
        this.server = z2;
        this.minorVersion = spdyVersion.getMinorVersion();
    }

    private boolean acceptStream(int i2, byte b2, boolean z2, boolean z3) {
        if (this.receivedGoAwayFrame || this.sentGoAwayFrame) {
            return false;
        }
        boolean zIsRemoteInitiatedId = isRemoteInitiatedId(i2);
        if (this.spdySession.numActiveStreams(zIsRemoteInitiatedId) >= (zIsRemoteInitiatedId ? this.localConcurrentStreams : this.remoteConcurrentStreams)) {
            return false;
        }
        this.spdySession.acceptStream(i2, b2, z2, z3, this.initialSendWindowSize, this.initialReceiveWindowSize, zIsRemoteInitiatedId);
        if (!zIsRemoteInitiatedId) {
            return true;
        }
        this.lastGoodStreamId = i2;
        return true;
    }

    private void halfCloseStream(int i2, boolean z2, ChannelFuture channelFuture) {
        if (z2) {
            this.spdySession.closeRemoteSide(i2, isRemoteInitiatedId(i2));
        } else {
            this.spdySession.closeLocalSide(i2, isRemoteInitiatedId(i2));
        }
        if (this.closeSessionFutureListener == null || !this.spdySession.noActiveStreams()) {
            return;
        }
        channelFuture.addListener((GenericFutureListener<? extends Future<? super Void>>) this.closeSessionFutureListener);
    }

    /* JADX WARN: Removed duplicated region for block: B:79:0x0188 A[PHI: r2
      0x0188: PHI (r2v8 int) = (r2v0 int), (r2v5 int), (r2v9 int) binds: [B:78:0x0186, B:36:0x00dd, B:16:0x008c] A[DONT_GENERATE, DONT_INLINE]] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    private void handleOutboundMessage(final io.netty.channel.ChannelHandlerContext r7, java.lang.Object r8, io.netty.channel.ChannelPromise r9) {
        /*
            Method dump skipped, instruction units count: 410
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: io.netty.handler.codec.spdy.SpdySessionHandler.handleOutboundMessage(io.netty.channel.ChannelHandlerContext, java.lang.Object, io.netty.channel.ChannelPromise):void");
    }

    private boolean isRemoteInitiatedId(int i2) {
        boolean zIsServerId = SpdyCodecUtil.isServerId(i2);
        boolean z2 = this.server;
        return (z2 && !zIsServerId) || (!z2 && zIsServerId);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void issueSessionError(ChannelHandlerContext channelHandlerContext, SpdySessionStatus spdySessionStatus) {
        sendGoAwayFrame(channelHandlerContext, spdySessionStatus).addListener((GenericFutureListener<? extends Future<? super Void>>) new ClosingChannelFutureListener(channelHandlerContext, channelHandlerContext.newPromise()));
    }

    private void issueStreamError(ChannelHandlerContext channelHandlerContext, int i2, SpdyStreamStatus spdyStreamStatus) {
        boolean z2 = !this.spdySession.isRemoteSideClosed(i2);
        ChannelPromise channelPromiseNewPromise = channelHandlerContext.newPromise();
        removeStream(i2, channelPromiseNewPromise);
        DefaultSpdyRstStreamFrame defaultSpdyRstStreamFrame = new DefaultSpdyRstStreamFrame(i2, spdyStreamStatus);
        channelHandlerContext.writeAndFlush(defaultSpdyRstStreamFrame, channelPromiseNewPromise);
        if (z2) {
            channelHandlerContext.fireChannelRead((Object) defaultSpdyRstStreamFrame);
        }
    }

    private void removeStream(int i2, ChannelFuture channelFuture) {
        this.spdySession.removeStream(i2, STREAM_CLOSED, isRemoteInitiatedId(i2));
        if (this.closeSessionFutureListener == null || !this.spdySession.noActiveStreams()) {
            return;
        }
        channelFuture.addListener((GenericFutureListener<? extends Future<? super Void>>) this.closeSessionFutureListener);
    }

    private ChannelFuture sendGoAwayFrame(ChannelHandlerContext channelHandlerContext, SpdySessionStatus spdySessionStatus) {
        if (this.sentGoAwayFrame) {
            return channelHandlerContext.newSucceededFuture();
        }
        this.sentGoAwayFrame = true;
        return channelHandlerContext.writeAndFlush(new DefaultSpdyGoAwayFrame(this.lastGoodStreamId, spdySessionStatus));
    }

    private void sendGoAwayFrame(ChannelHandlerContext channelHandlerContext, ChannelPromise channelPromise) {
        if (!channelHandlerContext.channel().isActive()) {
            channelHandlerContext.close(channelPromise);
            return;
        }
        ChannelFuture channelFutureSendGoAwayFrame = sendGoAwayFrame(channelHandlerContext, SpdySessionStatus.OK);
        if (this.spdySession.noActiveStreams()) {
            channelFutureSendGoAwayFrame.addListener((GenericFutureListener<? extends Future<? super Void>>) new ClosingChannelFutureListener(channelHandlerContext, channelPromise));
        } else {
            this.closeSessionFutureListener = new ClosingChannelFutureListener(channelHandlerContext, channelPromise);
        }
    }

    private void updateInitialReceiveWindowSize(int i2) {
        int i3 = i2 - this.initialReceiveWindowSize;
        this.initialReceiveWindowSize = i2;
        this.spdySession.updateAllReceiveWindowSizes(i3);
    }

    private void updateInitialSendWindowSize(int i2) {
        int i3 = i2 - this.initialSendWindowSize;
        this.initialSendWindowSize = i2;
        this.spdySession.updateAllSendWindowSizes(i3);
    }

    private void updateSendWindowSize(final ChannelHandlerContext channelHandlerContext, int i2, int i3) {
        ChannelFuture channelFutureWriteAndFlush;
        GenericFutureListener<? extends Future<? super Void>> genericFutureListener;
        this.spdySession.updateSendWindowSize(i2, i3);
        while (true) {
            SpdySession.PendingWrite pendingWrite = this.spdySession.getPendingWrite(i2);
            if (pendingWrite == null) {
                return;
            }
            SpdyDataFrame spdyDataFrame = pendingWrite.spdyDataFrame;
            int i4 = spdyDataFrame.content().readableBytes();
            int iStreamId = spdyDataFrame.streamId();
            int iMin = Math.min(this.spdySession.getSendWindowSize(iStreamId), this.spdySession.getSendWindowSize(0));
            if (iMin <= 0) {
                return;
            }
            if (iMin < i4) {
                int i5 = iMin * (-1);
                this.spdySession.updateSendWindowSize(iStreamId, i5);
                this.spdySession.updateSendWindowSize(0, i5);
                channelFutureWriteAndFlush = channelHandlerContext.writeAndFlush(new DefaultSpdyDataFrame(iStreamId, spdyDataFrame.content().readRetainedSlice(iMin)));
                genericFutureListener = new ChannelFutureListener() { // from class: io.netty.handler.codec.spdy.SpdySessionHandler.3
                    @Override // io.netty.util.concurrent.GenericFutureListener
                    public void operationComplete(ChannelFuture channelFuture) {
                        if (channelFuture.isSuccess()) {
                            return;
                        }
                        SpdySessionHandler.this.issueSessionError(channelHandlerContext, SpdySessionStatus.INTERNAL_ERROR);
                    }
                };
            } else {
                this.spdySession.removePendingWrite(iStreamId);
                int i6 = i4 * (-1);
                this.spdySession.updateSendWindowSize(iStreamId, i6);
                this.spdySession.updateSendWindowSize(0, i6);
                if (spdyDataFrame.isLast()) {
                    halfCloseStream(iStreamId, false, pendingWrite.promise);
                }
                channelFutureWriteAndFlush = channelHandlerContext.writeAndFlush(spdyDataFrame, pendingWrite.promise);
                genericFutureListener = new ChannelFutureListener() { // from class: io.netty.handler.codec.spdy.SpdySessionHandler.4
                    @Override // io.netty.util.concurrent.GenericFutureListener
                    public void operationComplete(ChannelFuture channelFuture) {
                        if (channelFuture.isSuccess()) {
                            return;
                        }
                        SpdySessionHandler.this.issueSessionError(channelHandlerContext, SpdySessionStatus.INTERNAL_ERROR);
                    }
                };
            }
            channelFutureWriteAndFlush.addListener(genericFutureListener);
        }
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelInactive(ChannelHandlerContext channelHandlerContext) {
        Iterator<Integer> it = this.spdySession.activeStreams().keySet().iterator();
        while (it.hasNext()) {
            removeStream(it.next().intValue(), channelHandlerContext.newSucceededFuture());
        }
        channelHandlerContext.fireChannelInactive();
    }

    /* JADX WARN: Removed duplicated region for block: B:81:0x0164 A[PHI: r2
      0x0164: PHI (r2v6 int) = (r2v5 int), (r2v7 int) binds: [B:128:0x0213, B:80:0x0162] A[DONT_GENERATE, DONT_INLINE]] */
    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public void channelRead(io.netty.channel.ChannelHandlerContext r8, java.lang.Object r9) {
        /*
            Method dump skipped, instruction units count: 594
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: io.netty.handler.codec.spdy.SpdySessionHandler.channelRead(io.netty.channel.ChannelHandlerContext, java.lang.Object):void");
    }

    @Override // io.netty.channel.ChannelDuplexHandler, io.netty.channel.ChannelOutboundHandler
    public void close(ChannelHandlerContext channelHandlerContext, ChannelPromise channelPromise) {
        sendGoAwayFrame(channelHandlerContext, channelPromise);
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelHandlerAdapter, io.netty.channel.ChannelHandler, io.netty.channel.ChannelInboundHandler
    public void exceptionCaught(ChannelHandlerContext channelHandlerContext, Throwable th) {
        if (th instanceof SpdyProtocolException) {
            issueSessionError(channelHandlerContext, SpdySessionStatus.PROTOCOL_ERROR);
        }
        channelHandlerContext.fireExceptionCaught(th);
    }

    public void setSessionReceiveWindowSize(int i2) {
        if (i2 < 0) {
            throw new IllegalArgumentException("sessionReceiveWindowSize");
        }
        this.initialSessionReceiveWindowSize = i2;
    }

    @Override // io.netty.channel.ChannelDuplexHandler, io.netty.channel.ChannelOutboundHandler
    public void write(ChannelHandlerContext channelHandlerContext, Object obj, ChannelPromise channelPromise) {
        if ((obj instanceof SpdyDataFrame) || (obj instanceof SpdySynStreamFrame) || (obj instanceof SpdySynReplyFrame) || (obj instanceof SpdyRstStreamFrame) || (obj instanceof SpdySettingsFrame) || (obj instanceof SpdyPingFrame) || (obj instanceof SpdyGoAwayFrame) || (obj instanceof SpdyHeadersFrame) || (obj instanceof SpdyWindowUpdateFrame)) {
            handleOutboundMessage(channelHandlerContext, obj, channelPromise);
        } else {
            channelHandlerContext.write(obj, channelPromise);
        }
    }
}
