package io.netty.channel;

import g.a.a.a.a;
import java.util.ArrayDeque;
import java.util.Objects;
import java.util.Queue;

/* JADX INFO: loaded from: classes.dex */
public final class ChannelFlushPromiseNotifier {
    private final Queue<FlushCheckpoint> flushCheckpoints;
    private final boolean tryNotify;
    private long writeCounter;

    public static class DefaultFlushCheckpoint implements FlushCheckpoint {
        private long checkpoint;
        private final ChannelPromise future;

        public DefaultFlushCheckpoint(long j2, ChannelPromise channelPromise) {
            this.checkpoint = j2;
            this.future = channelPromise;
        }

        @Override // io.netty.channel.ChannelFlushPromiseNotifier.FlushCheckpoint
        public long flushCheckpoint() {
            return this.checkpoint;
        }

        @Override // io.netty.channel.ChannelFlushPromiseNotifier.FlushCheckpoint
        public void flushCheckpoint(long j2) {
            this.checkpoint = j2;
        }

        @Override // io.netty.channel.ChannelFlushPromiseNotifier.FlushCheckpoint
        public ChannelPromise promise() {
            return this.future;
        }
    }

    public interface FlushCheckpoint {
        long flushCheckpoint();

        void flushCheckpoint(long j2);

        ChannelPromise promise();
    }

    public ChannelFlushPromiseNotifier() {
        this(false);
    }

    public ChannelFlushPromiseNotifier(boolean z2) {
        this.flushCheckpoints = new ArrayDeque();
        this.tryNotify = z2;
    }

    private void notifyPromises0(Throwable th) {
        if (this.flushCheckpoints.isEmpty()) {
            this.writeCounter = 0L;
            return;
        }
        long j2 = this.writeCounter;
        while (true) {
            FlushCheckpoint flushCheckpointPeek = this.flushCheckpoints.peek();
            if (flushCheckpointPeek == null) {
                this.writeCounter = 0L;
                break;
            }
            if (flushCheckpointPeek.flushCheckpoint() <= j2) {
                this.flushCheckpoints.remove();
                ChannelPromise channelPromisePromise = flushCheckpointPeek.promise();
                boolean z2 = this.tryNotify;
                if (th == null) {
                    if (z2) {
                        channelPromisePromise.trySuccess();
                    } else {
                        channelPromisePromise.setSuccess();
                    }
                } else if (z2) {
                    channelPromisePromise.tryFailure(th);
                } else {
                    channelPromisePromise.setFailure(th);
                }
            } else if (j2 > 0 && this.flushCheckpoints.size() == 1) {
                this.writeCounter = 0L;
                flushCheckpointPeek.flushCheckpoint(flushCheckpointPeek.flushCheckpoint() - j2);
            }
        }
        long j3 = this.writeCounter;
        if (j3 >= 549755813888L) {
            this.writeCounter = 0L;
            for (FlushCheckpoint flushCheckpoint : this.flushCheckpoints) {
                flushCheckpoint.flushCheckpoint(flushCheckpoint.flushCheckpoint() - j3);
            }
        }
    }

    @Deprecated
    public ChannelFlushPromiseNotifier add(ChannelPromise channelPromise, int i2) {
        return add(channelPromise, i2);
    }

    public ChannelFlushPromiseNotifier add(ChannelPromise channelPromise, long j2) {
        Objects.requireNonNull(channelPromise, "promise");
        if (j2 < 0) {
            throw new IllegalArgumentException(a.q("pendingDataSize must be >= 0 but was ", j2));
        }
        long j3 = this.writeCounter + j2;
        if (channelPromise instanceof FlushCheckpoint) {
            FlushCheckpoint flushCheckpoint = (FlushCheckpoint) channelPromise;
            flushCheckpoint.flushCheckpoint(j3);
            this.flushCheckpoints.add(flushCheckpoint);
        } else {
            this.flushCheckpoints.add(new DefaultFlushCheckpoint(j3, channelPromise));
        }
        return this;
    }

    public ChannelFlushPromiseNotifier increaseWriteCounter(long j2) {
        if (j2 < 0) {
            throw new IllegalArgumentException(a.q("delta must be >= 0 but was ", j2));
        }
        this.writeCounter += j2;
        return this;
    }

    @Deprecated
    public ChannelFlushPromiseNotifier notifyFlushFutures() {
        return notifyPromises();
    }

    @Deprecated
    public ChannelFlushPromiseNotifier notifyFlushFutures(Throwable th) {
        return notifyPromises(th);
    }

    @Deprecated
    public ChannelFlushPromiseNotifier notifyFlushFutures(Throwable th, Throwable th2) {
        return notifyPromises(th, th2);
    }

    public ChannelFlushPromiseNotifier notifyPromises() {
        notifyPromises0(null);
        return this;
    }

    public ChannelFlushPromiseNotifier notifyPromises(Throwable th) {
        notifyPromises();
        while (true) {
            FlushCheckpoint flushCheckpointPoll = this.flushCheckpoints.poll();
            if (flushCheckpointPoll == null) {
                return this;
            }
            boolean z2 = this.tryNotify;
            ChannelPromise channelPromisePromise = flushCheckpointPoll.promise();
            if (z2) {
                channelPromisePromise.tryFailure(th);
            } else {
                channelPromisePromise.setFailure(th);
            }
        }
    }

    public ChannelFlushPromiseNotifier notifyPromises(Throwable th, Throwable th2) {
        notifyPromises0(th);
        while (true) {
            FlushCheckpoint flushCheckpointPoll = this.flushCheckpoints.poll();
            if (flushCheckpointPoll == null) {
                return this;
            }
            boolean z2 = this.tryNotify;
            ChannelPromise channelPromisePromise = flushCheckpointPoll.promise();
            if (z2) {
                channelPromisePromise.tryFailure(th2);
            } else {
                channelPromisePromise.setFailure(th2);
            }
        }
    }

    public long writeCounter() {
        return this.writeCounter;
    }
}
