package io.netty.handler.codec.http2;

import g.a.a.a.a;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http2.Http2Connection;
import io.netty.handler.codec.http2.Http2RemoteFlowController;
import io.netty.handler.codec.http2.Http2Stream;
import io.netty.handler.codec.http2.StreamByteDistributor;
import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.util.ArrayDeque;
import java.util.Deque;

/* JADX INFO: loaded from: classes.dex */
public class DefaultHttp2RemoteFlowController implements Http2RemoteFlowController {
    public static final /* synthetic */ boolean $assertionsDisabled = false;
    private static final int MIN_WRITABLE_CHUNK = 32768;
    private static final InternalLogger logger = InternalLoggerFactory.getInstance((Class<?>) DefaultHttp2RemoteFlowController.class);
    private final Http2Connection connection;
    private final FlowState connectionState;
    private ChannelHandlerContext ctx;
    private int initialWindowSize;
    private WritabilityMonitor monitor;
    private final Http2Connection.PropertyKey stateKey;
    private final StreamByteDistributor streamByteDistributor;

    public final class FlowState implements StreamByteDistributor.StreamState {
        public static final /* synthetic */ boolean $assertionsDisabled = false;
        private boolean cancelled;
        private boolean markedWritable;
        private long pendingBytes;
        private final Deque<Http2RemoteFlowController.FlowControlled> pendingWriteQueue = new ArrayDeque(2);
        private final Http2Stream stream;
        private int window;
        private boolean writing;

        public FlowState(Http2Stream http2Stream) {
            this.stream = http2Stream;
        }

        private void decrementFlowControlWindow(int i2) {
            int i3 = -i2;
            try {
                DefaultHttp2RemoteFlowController.this.connectionState.incrementStreamWindow(i3);
                incrementStreamWindow(i3);
            } catch (Http2Exception e2) {
                StringBuilder sbF = a.F("Invalid window state when writing frame: ");
                sbF.append(e2.getMessage());
                throw new IllegalStateException(sbF.toString(), e2);
            }
        }

        private void decrementPendingBytes(int i2, boolean z2) {
            incrementPendingBytes(-i2, z2);
        }

        private void enqueueFrameWithoutMerge(Http2RemoteFlowController.FlowControlled flowControlled) {
            this.pendingWriteQueue.offer(flowControlled);
            incrementPendingBytes(flowControlled.size(), true);
        }

        private void incrementPendingBytes(int i2, boolean z2) {
            this.pendingBytes += (long) i2;
            DefaultHttp2RemoteFlowController.this.monitor.incrementPendingBytes(i2);
            if (z2) {
                DefaultHttp2RemoteFlowController.this.streamByteDistributor.updateStreamableBytes(this);
            }
        }

        private Http2RemoteFlowController.FlowControlled peek() {
            return this.pendingWriteQueue.peek();
        }

        private int writableWindow() {
            return Math.min(this.window, DefaultHttp2RemoteFlowController.this.connectionWindowSize());
        }

        private void writeError(Http2RemoteFlowController.FlowControlled flowControlled, Http2Exception http2Exception) {
            decrementPendingBytes(flowControlled.size(), true);
            flowControlled.error(DefaultHttp2RemoteFlowController.this.ctx, http2Exception);
        }

        public void cancel(Http2Error http2Error, Throwable th) {
            this.cancelled = true;
            if (this.writing) {
                return;
            }
            Http2RemoteFlowController.FlowControlled flowControlledPoll = this.pendingWriteQueue.poll();
            if (flowControlledPoll != null) {
                Http2Exception http2ExceptionStreamError = Http2Exception.streamError(this.stream.id(), http2Error, th, "Stream closed before write could take place", new Object[0]);
                do {
                    writeError(flowControlledPoll, http2ExceptionStreamError);
                    flowControlledPoll = this.pendingWriteQueue.poll();
                } while (flowControlledPoll != null);
            }
            DefaultHttp2RemoteFlowController.this.streamByteDistributor.updateStreamableBytes(this);
            DefaultHttp2RemoteFlowController.this.monitor.stateCancelled(this);
        }

        public void enqueueFrame(Http2RemoteFlowController.FlowControlled flowControlled) {
            Http2RemoteFlowController.FlowControlled flowControlledPeekLast = this.pendingWriteQueue.peekLast();
            if (flowControlledPeekLast == null) {
                enqueueFrameWithoutMerge(flowControlled);
                return;
            }
            int size = flowControlledPeekLast.size();
            if (flowControlledPeekLast.merge(DefaultHttp2RemoteFlowController.this.ctx, flowControlled)) {
                incrementPendingBytes(flowControlledPeekLast.size() - size, true);
            } else {
                enqueueFrameWithoutMerge(flowControlled);
            }
        }

        @Override // io.netty.handler.codec.http2.StreamByteDistributor.StreamState
        public boolean hasFrame() {
            return !this.pendingWriteQueue.isEmpty();
        }

        public int incrementStreamWindow(int i2) throws Http2Exception {
            if (i2 > 0 && Integer.MAX_VALUE - i2 < this.window) {
                throw Http2Exception.streamError(this.stream.id(), Http2Error.FLOW_CONTROL_ERROR, "Window size overflow for stream: %d", Integer.valueOf(this.stream.id()));
            }
            this.window += i2;
            DefaultHttp2RemoteFlowController.this.streamByteDistributor.updateStreamableBytes(this);
            return this.window;
        }

        public boolean isWritable() {
            return ((long) windowSize()) > pendingBytes() && !this.cancelled;
        }

        public void markedWritability(boolean z2) {
            this.markedWritable = z2;
        }

        public boolean markedWritability() {
            return this.markedWritable;
        }

        @Override // io.netty.handler.codec.http2.StreamByteDistributor.StreamState
        public long pendingBytes() {
            return this.pendingBytes;
        }

        @Override // io.netty.handler.codec.http2.StreamByteDistributor.StreamState
        public Http2Stream stream() {
            return this.stream;
        }

        @Override // io.netty.handler.codec.http2.StreamByteDistributor.StreamState
        public int windowSize() {
            return this.window;
        }

        public void windowSize(int i2) {
            this.window = i2;
        }

        public int writeAllocatedBytes(int i2) {
            int size;
            int i3;
            int i4;
            boolean z2;
            Http2RemoteFlowController.FlowControlled flowControlledPeek;
            int iMin;
            try {
                this.writing = true;
                size = i2;
                z2 = false;
                while (!this.cancelled && (flowControlledPeek = peek()) != null && ((iMin = Math.min(size, writableWindow())) > 0 || flowControlledPeek.size() <= 0)) {
                    try {
                        int size2 = flowControlledPeek.size();
                        try {
                            flowControlledPeek.write(DefaultHttp2RemoteFlowController.this.ctx, Math.max(0, iMin));
                            if (flowControlledPeek.size() == 0) {
                                this.pendingWriteQueue.remove();
                                flowControlledPeek.writeComplete();
                            }
                            size -= size2 - flowControlledPeek.size();
                            z2 = true;
                        } finally {
                        }
                    } catch (Throwable th) {
                        th = th;
                        try {
                            this.cancelled = true;
                            this.writing = false;
                            i4 = i2 - size;
                            decrementPendingBytes(i4, false);
                            decrementFlowControlWindow(i4);
                            if (this.cancelled) {
                                cancel(Http2Error.INTERNAL_ERROR, th);
                            }
                            return i4;
                        } finally {
                            this.writing = false;
                            i3 = i2 - size;
                            decrementPendingBytes(i3, false);
                            decrementFlowControlWindow(i3);
                            if (this.cancelled) {
                                cancel(Http2Error.INTERNAL_ERROR, null);
                            }
                        }
                    }
                }
            } catch (Throwable th2) {
                th = th2;
                size = i2;
            }
            if (!z2) {
                return -1;
            }
            this.writing = false;
            i4 = i2 - size;
            decrementPendingBytes(i4, false);
            decrementFlowControlWindow(i4);
            if (this.cancelled) {
                cancel(Http2Error.INTERNAL_ERROR, null);
            }
            return i4;
        }
    }

    public final class ListenerWritabilityMonitor extends WritabilityMonitor implements Http2StreamVisitor {
        private final Http2RemoteFlowController.Listener listener;

        public ListenerWritabilityMonitor(Http2RemoteFlowController.Listener listener) {
            super();
            this.listener = listener;
        }

        private void checkAllWritabilityChanged() {
            DefaultHttp2RemoteFlowController.this.connectionState.markedWritability(isWritableConnection());
            DefaultHttp2RemoteFlowController.this.connection.forEachActiveStream(this);
        }

        private void checkConnectionThenStreamWritabilityChanged(FlowState flowState) {
            if (isWritableConnection() != DefaultHttp2RemoteFlowController.this.connectionState.markedWritability()) {
                checkAllWritabilityChanged();
            } else if (isWritable(flowState) != flowState.markedWritability()) {
                notifyWritabilityChanged(flowState);
            }
        }

        private void checkStateWritability(FlowState flowState) {
            if (isWritable(flowState) != flowState.markedWritability()) {
                if (flowState == DefaultHttp2RemoteFlowController.this.connectionState) {
                    checkAllWritabilityChanged();
                } else {
                    notifyWritabilityChanged(flowState);
                }
            }
        }

        private void notifyWritabilityChanged(FlowState flowState) {
            flowState.markedWritability(!flowState.markedWritability());
            try {
                this.listener.writabilityChanged(flowState.stream);
            } catch (Throwable th) {
                DefaultHttp2RemoteFlowController.logger.error("Caught Throwable from listener.writabilityChanged", th);
            }
        }

        @Override // io.netty.handler.codec.http2.DefaultHttp2RemoteFlowController.WritabilityMonitor
        public void channelWritabilityChange() {
            if (DefaultHttp2RemoteFlowController.this.connectionState.markedWritability() != DefaultHttp2RemoteFlowController.this.isChannelWritable()) {
                checkAllWritabilityChanged();
            }
        }

        @Override // io.netty.handler.codec.http2.DefaultHttp2RemoteFlowController.WritabilityMonitor
        public void enqueueFrame(FlowState flowState, Http2RemoteFlowController.FlowControlled flowControlled) {
            super.enqueueFrame(flowState, flowControlled);
            checkConnectionThenStreamWritabilityChanged(flowState);
        }

        @Override // io.netty.handler.codec.http2.DefaultHttp2RemoteFlowController.WritabilityMonitor
        public void incrementWindowSize(FlowState flowState, int i2) throws Http2Exception {
            super.incrementWindowSize(flowState, i2);
            checkStateWritability(flowState);
        }

        @Override // io.netty.handler.codec.http2.DefaultHttp2RemoteFlowController.WritabilityMonitor
        public void initialWindowSize(int i2) {
            super.initialWindowSize(i2);
            if (isWritableConnection()) {
                checkAllWritabilityChanged();
            }
        }

        @Override // io.netty.handler.codec.http2.DefaultHttp2RemoteFlowController.WritabilityMonitor
        public void stateCancelled(FlowState flowState) {
            try {
                checkConnectionThenStreamWritabilityChanged(flowState);
            } catch (Http2Exception e2) {
                throw new RuntimeException("Caught unexpected exception from checkAllWritabilityChanged", e2);
            }
        }

        @Override // io.netty.handler.codec.http2.Http2StreamVisitor
        public boolean visit(Http2Stream http2Stream) {
            FlowState flowStateState = DefaultHttp2RemoteFlowController.this.state(http2Stream);
            if (isWritable(flowStateState) == flowStateState.markedWritability()) {
                return true;
            }
            notifyWritabilityChanged(flowStateState);
            return true;
        }

        @Override // io.netty.handler.codec.http2.DefaultHttp2RemoteFlowController.WritabilityMonitor
        public void windowSize(FlowState flowState, int i2) {
            super.windowSize(flowState, i2);
            try {
                checkStateWritability(flowState);
            } catch (Http2Exception e2) {
                throw new RuntimeException("Caught unexpected exception from window", e2);
            }
        }
    }

    public class WritabilityMonitor implements StreamByteDistributor.Writer {
        private boolean inWritePendingBytes;
        private long totalPendingBytes;

        private WritabilityMonitor() {
        }

        public void channelWritabilityChange() {
        }

        public void enqueueFrame(FlowState flowState, Http2RemoteFlowController.FlowControlled flowControlled) {
            flowState.enqueueFrame(flowControlled);
        }

        public final void incrementPendingBytes(int i2) {
            this.totalPendingBytes += (long) i2;
        }

        public void incrementWindowSize(FlowState flowState, int i2) throws Http2Exception {
            flowState.incrementStreamWindow(i2);
        }

        public void initialWindowSize(int i2) {
            if (i2 < 0) {
                throw new IllegalArgumentException(a.l("Invalid initial window size: ", i2));
            }
            final int i3 = i2 - DefaultHttp2RemoteFlowController.this.initialWindowSize;
            DefaultHttp2RemoteFlowController.this.initialWindowSize = i2;
            DefaultHttp2RemoteFlowController.this.connection.forEachActiveStream(new Http2StreamVisitor() { // from class: io.netty.handler.codec.http2.DefaultHttp2RemoteFlowController.WritabilityMonitor.1
                @Override // io.netty.handler.codec.http2.Http2StreamVisitor
                public boolean visit(Http2Stream http2Stream) throws Http2Exception {
                    DefaultHttp2RemoteFlowController.this.state(http2Stream).incrementStreamWindow(i3);
                    return true;
                }
            });
            if (i3 <= 0 || !DefaultHttp2RemoteFlowController.this.isChannelWritable()) {
                return;
            }
            writePendingBytes();
        }

        public final boolean isWritable(FlowState flowState) {
            return isWritableConnection() && flowState.isWritable();
        }

        public final boolean isWritableConnection() {
            return ((long) DefaultHttp2RemoteFlowController.this.connectionState.windowSize()) - this.totalPendingBytes > 0 && DefaultHttp2RemoteFlowController.this.isChannelWritable();
        }

        public void stateCancelled(FlowState flowState) {
        }

        public void windowSize(FlowState flowState, int i2) {
            flowState.windowSize(i2);
        }

        @Override // io.netty.handler.codec.http2.StreamByteDistributor.Writer
        public final void write(Http2Stream http2Stream, int i2) {
            DefaultHttp2RemoteFlowController.this.state(http2Stream).writeAllocatedBytes(i2);
        }

        public final void writePendingBytes() {
            if (this.inWritePendingBytes) {
                return;
            }
            this.inWritePendingBytes = true;
            try {
                int iWritableBytes = DefaultHttp2RemoteFlowController.this.writableBytes();
                while (DefaultHttp2RemoteFlowController.this.streamByteDistributor.distribute(iWritableBytes, this) && (iWritableBytes = DefaultHttp2RemoteFlowController.this.writableBytes()) > 0 && DefaultHttp2RemoteFlowController.this.isChannelWritable0()) {
                }
            } finally {
                this.inWritePendingBytes = false;
            }
        }
    }

    public DefaultHttp2RemoteFlowController(Http2Connection http2Connection) {
        this(http2Connection, (Http2RemoteFlowController.Listener) null);
    }

    public DefaultHttp2RemoteFlowController(Http2Connection http2Connection, Http2RemoteFlowController.Listener listener) {
        this(http2Connection, new WeightedFairQueueByteDistributor(http2Connection), listener);
    }

    public DefaultHttp2RemoteFlowController(Http2Connection http2Connection, StreamByteDistributor streamByteDistributor) {
        this(http2Connection, streamByteDistributor, null);
    }

    public DefaultHttp2RemoteFlowController(Http2Connection http2Connection, StreamByteDistributor streamByteDistributor, Http2RemoteFlowController.Listener listener) {
        this.initialWindowSize = 65535;
        this.connection = (Http2Connection) ObjectUtil.checkNotNull(http2Connection, "connection");
        this.streamByteDistributor = (StreamByteDistributor) ObjectUtil.checkNotNull(streamByteDistributor, "streamWriteDistributor");
        Http2Connection.PropertyKey propertyKeyNewKey = http2Connection.newKey();
        this.stateKey = propertyKeyNewKey;
        FlowState flowState = new FlowState(http2Connection.connectionStream());
        this.connectionState = flowState;
        http2Connection.connectionStream().setProperty(propertyKeyNewKey, flowState);
        listener(listener);
        this.monitor.windowSize(flowState, this.initialWindowSize);
        http2Connection.addListener(new Http2ConnectionAdapter() { // from class: io.netty.handler.codec.http2.DefaultHttp2RemoteFlowController.1
            @Override // io.netty.handler.codec.http2.Http2ConnectionAdapter, io.netty.handler.codec.http2.Http2Connection.Listener
            public void onStreamActive(Http2Stream http2Stream) {
                DefaultHttp2RemoteFlowController.this.monitor.windowSize(DefaultHttp2RemoteFlowController.this.state(http2Stream), DefaultHttp2RemoteFlowController.this.initialWindowSize);
            }

            @Override // io.netty.handler.codec.http2.Http2ConnectionAdapter, io.netty.handler.codec.http2.Http2Connection.Listener
            public void onStreamAdded(Http2Stream http2Stream) {
                http2Stream.setProperty(DefaultHttp2RemoteFlowController.this.stateKey, DefaultHttp2RemoteFlowController.this.new FlowState(http2Stream));
            }

            @Override // io.netty.handler.codec.http2.Http2ConnectionAdapter, io.netty.handler.codec.http2.Http2Connection.Listener
            public void onStreamClosed(Http2Stream http2Stream) {
                DefaultHttp2RemoteFlowController.this.state(http2Stream).cancel(Http2Error.STREAM_CLOSED, null);
            }

            @Override // io.netty.handler.codec.http2.Http2ConnectionAdapter, io.netty.handler.codec.http2.Http2Connection.Listener
            public void onStreamHalfClosed(Http2Stream http2Stream) {
                if (Http2Stream.State.HALF_CLOSED_LOCAL == http2Stream.state()) {
                    DefaultHttp2RemoteFlowController.this.state(http2Stream).cancel(Http2Error.STREAM_CLOSED, null);
                }
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public int connectionWindowSize() {
        return this.connectionState.windowSize();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public boolean isChannelWritable() {
        return this.ctx != null && isChannelWritable0();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public boolean isChannelWritable0() {
        return this.ctx.channel().isWritable();
    }

    private int maxUsableChannelBytes() {
        int iMin = (int) Math.min(2147483647L, this.ctx.channel().bytesBeforeUnwritable());
        return Math.min(this.connectionState.windowSize(), iMin > 0 ? Math.max(iMin, minUsableChannelBytes()) : 0);
    }

    private int minUsableChannelBytes() {
        return Math.max(this.ctx.channel().config().getWriteBufferLowWaterMark(), 32768);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public FlowState state(Http2Stream http2Stream) {
        return (FlowState) http2Stream.getProperty(this.stateKey);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public int writableBytes() {
        return Math.min(connectionWindowSize(), maxUsableChannelBytes());
    }

    @Override // io.netty.handler.codec.http2.Http2RemoteFlowController
    public void addFlowControlled(Http2Stream http2Stream, Http2RemoteFlowController.FlowControlled flowControlled) {
        ObjectUtil.checkNotNull(flowControlled, "frame");
        try {
            this.monitor.enqueueFrame(state(http2Stream), flowControlled);
        } catch (Throwable th) {
            flowControlled.error(this.ctx, th);
        }
    }

    @Override // io.netty.handler.codec.http2.Http2RemoteFlowController
    public ChannelHandlerContext channelHandlerContext() {
        return this.ctx;
    }

    @Override // io.netty.handler.codec.http2.Http2FlowController
    public void channelHandlerContext(ChannelHandlerContext channelHandlerContext) {
        this.ctx = (ChannelHandlerContext) ObjectUtil.checkNotNull(channelHandlerContext, "ctx");
        channelWritabilityChanged();
        if (isChannelWritable()) {
            writePendingBytes();
        }
    }

    @Override // io.netty.handler.codec.http2.Http2RemoteFlowController
    public void channelWritabilityChanged() {
        this.monitor.channelWritabilityChange();
    }

    @Override // io.netty.handler.codec.http2.Http2RemoteFlowController
    public boolean hasFlowControlled(Http2Stream http2Stream) {
        return state(http2Stream).hasFrame();
    }

    @Override // io.netty.handler.codec.http2.Http2FlowController
    public void incrementWindowSize(Http2Stream http2Stream, int i2) throws Http2Exception {
        this.monitor.incrementWindowSize(state(http2Stream), i2);
    }

    @Override // io.netty.handler.codec.http2.Http2FlowController
    public int initialWindowSize() {
        return this.initialWindowSize;
    }

    @Override // io.netty.handler.codec.http2.Http2FlowController
    public void initialWindowSize(int i2) {
        this.monitor.initialWindowSize(i2);
    }

    @Override // io.netty.handler.codec.http2.Http2RemoteFlowController
    public boolean isWritable(Http2Stream http2Stream) {
        return this.monitor.isWritable(state(http2Stream));
    }

    @Override // io.netty.handler.codec.http2.Http2RemoteFlowController
    public void listener(Http2RemoteFlowController.Listener listener) {
        this.monitor = listener == null ? new WritabilityMonitor() : new ListenerWritabilityMonitor(listener);
    }

    @Override // io.netty.handler.codec.http2.Http2RemoteFlowController
    public void updateDependencyTree(int i2, int i3, short s2, boolean z2) {
        this.streamByteDistributor.updateDependencyTree(i2, i3, s2, z2);
    }

    @Override // io.netty.handler.codec.http2.Http2FlowController
    public int windowSize(Http2Stream http2Stream) {
        return state(http2Stream).windowSize();
    }

    @Override // io.netty.handler.codec.http2.Http2RemoteFlowController
    public void writePendingBytes() {
        this.monitor.writePendingBytes();
    }
}
