package io.netty.channel;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufHolder;
import io.netty.buffer.Unpooled;
import io.netty.util.Recycler;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.concurrent.FastThreadLocal;
import io.netty.util.internal.InternalThreadLocalMap;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.ThrowableUtil;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import org.snmp4j.mp.MPv3;

/* JADX INFO: loaded from: classes.dex */
public final class ChannelOutboundBuffer {
    static final /* synthetic */ boolean $assertionsDisabled = false;
    private static final AtomicLongFieldUpdater<ChannelOutboundBuffer> TOTAL_PENDING_SIZE_UPDATER;
    private static final AtomicIntegerFieldUpdater<ChannelOutboundBuffer> UNWRITABLE_UPDATER;
    private final Channel channel;
    private volatile Runnable fireChannelWritabilityChangedTask;
    private int flushed;
    private Entry flushedEntry;
    private boolean inFail;
    private int nioBufferCount;
    private long nioBufferSize;
    private Entry tailEntry;
    private volatile long totalPendingSize;
    private Entry unflushedEntry;
    private volatile int unwritable;
    private static final InternalLogger logger = InternalLoggerFactory.getInstance((Class<?>) ChannelOutboundBuffer.class);
    private static final FastThreadLocal<ByteBuffer[]> NIO_BUFFERS = new FastThreadLocal<ByteBuffer[]>() { // from class: io.netty.channel.ChannelOutboundBuffer.1
        /* JADX INFO: Access modifiers changed from: protected */
        @Override // io.netty.util.concurrent.FastThreadLocal
        public ByteBuffer[] initialValue() throws Exception {
            return new ByteBuffer[1024];
        }
    };

    static final class Entry {
        private static final Recycler<Entry> RECYCLER = new Recycler<Entry>() { // from class: io.netty.channel.ChannelOutboundBuffer.Entry.1
            /* JADX INFO: Access modifiers changed from: protected */
            @Override // io.netty.util.Recycler
            /* JADX INFO: renamed from: newObject, reason: merged with bridge method [inline-methods] */
            public Entry newObject2(Recycler.Handle<Entry> handle) {
                return new Entry(handle);
            }
        };
        ByteBuffer buf;
        ByteBuffer[] bufs;
        boolean cancelled;
        int count;
        private final Recycler.Handle<Entry> handle;
        Object msg;
        Entry next;
        int pendingSize;
        long progress;
        ChannelPromise promise;
        long total;

        static Entry newInstance(Object obj, int i2, long j2, ChannelPromise channelPromise) {
            Entry entry = RECYCLER.get();
            entry.msg = obj;
            entry.pendingSize = i2;
            entry.total = j2;
            entry.promise = channelPromise;
            return entry;
        }

        int cancel() {
            if (this.cancelled) {
                return 0;
            }
            this.cancelled = true;
            int i2 = this.pendingSize;
            ReferenceCountUtil.safeRelease(this.msg);
            this.msg = Unpooled.EMPTY_BUFFER;
            this.pendingSize = 0;
            this.total = 0L;
            this.progress = 0L;
            this.bufs = null;
            this.buf = null;
            return i2;
        }

        void recycle() {
            this.next = null;
            this.bufs = null;
            this.buf = null;
            this.msg = null;
            this.promise = null;
            this.progress = 0L;
            this.total = 0L;
            this.pendingSize = 0;
            this.count = -1;
            this.cancelled = false;
            this.handle.recycle(this);
        }

        Entry recycleAndGetNext() {
            Entry entry = this.next;
            recycle();
            return entry;
        }

        private Entry(Recycler.Handle<Entry> handle) {
            this.count = -1;
            this.handle = handle;
        }
    }

    public interface MessageProcessor {
        boolean processMessage(Object obj) throws Exception;
    }

    static {
        AtomicIntegerFieldUpdater<ChannelOutboundBuffer> atomicIntegerFieldUpdaterNewAtomicIntegerFieldUpdater = PlatformDependent.newAtomicIntegerFieldUpdater(ChannelOutboundBuffer.class, "unwritable");
        if (atomicIntegerFieldUpdaterNewAtomicIntegerFieldUpdater == null) {
            atomicIntegerFieldUpdaterNewAtomicIntegerFieldUpdater = AtomicIntegerFieldUpdater.newUpdater(ChannelOutboundBuffer.class, "unwritable");
        }
        UNWRITABLE_UPDATER = atomicIntegerFieldUpdaterNewAtomicIntegerFieldUpdater;
        AtomicLongFieldUpdater<ChannelOutboundBuffer> atomicLongFieldUpdaterNewAtomicLongFieldUpdater = PlatformDependent.newAtomicLongFieldUpdater(ChannelOutboundBuffer.class, "totalPendingSize");
        if (atomicLongFieldUpdaterNewAtomicLongFieldUpdater == null) {
            atomicLongFieldUpdaterNewAtomicLongFieldUpdater = AtomicLongFieldUpdater.newUpdater(ChannelOutboundBuffer.class, "totalPendingSize");
        }
        TOTAL_PENDING_SIZE_UPDATER = atomicLongFieldUpdaterNewAtomicLongFieldUpdater;
    }

    ChannelOutboundBuffer(AbstractChannel abstractChannel) {
        this.channel = abstractChannel;
    }

    private void clearNioBuffers() {
        int i2 = this.nioBufferCount;
        if (i2 > 0) {
            this.nioBufferCount = 0;
            Arrays.fill(NIO_BUFFERS.get(), 0, i2, (Object) null);
        }
    }

    private void clearUserDefinedWritability(int i2) {
        int i3;
        int i4;
        int iWritabilityMask = writabilityMask(i2);
        do {
            i3 = this.unwritable;
            i4 = i3 | iWritabilityMask;
        } while (!UNWRITABLE_UPDATER.compareAndSet(this, i3, i4));
        if (i3 != 0 || i4 == 0) {
            return;
        }
        fireChannelWritabilityChanged(true);
    }

    private static ByteBuffer[] expandNioBufferArray(ByteBuffer[] byteBufferArr, int i2, int i3) {
        int length = byteBufferArr.length;
        do {
            length <<= 1;
            if (length < 0) {
                throw new IllegalStateException();
            }
        } while (i2 > length);
        ByteBuffer[] byteBufferArr2 = new ByteBuffer[length];
        System.arraycopy(byteBufferArr, 0, byteBufferArr2, 0, i3);
        return byteBufferArr2;
    }

    private static int fillBufferArray(ByteBuffer[] byteBufferArr, ByteBuffer[] byteBufferArr2, int i2) {
        int length = byteBufferArr.length;
        int i3 = 0;
        while (i3 < length) {
            ByteBuffer byteBuffer = byteBufferArr[i3];
            if (byteBuffer == null) {
                break;
            }
            byteBufferArr2[i2] = byteBuffer;
            i3++;
            i2++;
        }
        return i2;
    }

    private void fireChannelWritabilityChanged(boolean z) {
        final ChannelPipeline channelPipelinePipeline = this.channel.pipeline();
        if (!z) {
            channelPipelinePipeline.fireChannelWritabilityChanged();
            return;
        }
        Runnable runnable = this.fireChannelWritabilityChangedTask;
        if (runnable == null) {
            runnable = new Runnable() { // from class: io.netty.channel.ChannelOutboundBuffer.2
                @Override // java.lang.Runnable
                public void run() {
                    channelPipelinePipeline.fireChannelWritabilityChanged();
                }
            };
            this.fireChannelWritabilityChangedTask = runnable;
        }
        this.channel.eventLoop().execute(runnable);
    }

    private boolean isFlushedEntry(Entry entry) {
        return (entry == null || entry == this.unflushedEntry) ? false : true;
    }

    private boolean remove0(Throwable th, boolean z) {
        Entry entry = this.flushedEntry;
        if (entry == null) {
            clearNioBuffers();
            return false;
        }
        Object obj = entry.msg;
        ChannelPromise channelPromise = entry.promise;
        int i2 = entry.pendingSize;
        removeEntry(entry);
        if (!entry.cancelled) {
            ReferenceCountUtil.safeRelease(obj);
            safeFail(channelPromise, th);
            decrementPendingOutboundBytes(i2, false, z);
        }
        entry.recycle();
        return true;
    }

    private void removeEntry(Entry entry) {
        int i2 = this.flushed - 1;
        this.flushed = i2;
        if (i2 != 0) {
            this.flushedEntry = entry.next;
            return;
        }
        this.flushedEntry = null;
        if (entry == this.tailEntry) {
            this.tailEntry = null;
            this.unflushedEntry = null;
        }
    }

    private static void safeFail(ChannelPromise channelPromise, Throwable th) {
        if ((channelPromise instanceof VoidChannelPromise) || channelPromise.tryFailure(th)) {
            return;
        }
        Throwable thCause = channelPromise.cause();
        if (thCause == null) {
            logger.warn("Failed to mark a promise as failure because it has succeeded already: {}", channelPromise, th);
        } else {
            logger.warn("Failed to mark a promise as failure because it has failed already: {}, unnotified cause {}", channelPromise, ThrowableUtil.stackTraceToString(thCause), th);
        }
    }

    private static void safeSuccess(ChannelPromise channelPromise) {
        if ((channelPromise instanceof VoidChannelPromise) || channelPromise.trySuccess()) {
            return;
        }
        Throwable thCause = channelPromise.cause();
        if (thCause == null) {
            logger.warn("Failed to mark a promise as success because it has succeeded already: {}", channelPromise);
        } else {
            logger.warn("Failed to mark a promise as success because it has failed already: {}, unnotified cause {}", channelPromise, ThrowableUtil.stackTraceToString(thCause));
        }
    }

    private void setUnwritable(boolean z) {
        int i2;
        int i3;
        do {
            i2 = this.unwritable;
            i3 = i2 | 1;
        } while (!UNWRITABLE_UPDATER.compareAndSet(this, i2, i3));
        if (i2 != 0 || i3 == 0) {
            return;
        }
        fireChannelWritabilityChanged(z);
    }

    private void setWritable(boolean z) {
        int i2;
        int i3;
        do {
            i2 = this.unwritable;
            i3 = i2 & (-2);
        } while (!UNWRITABLE_UPDATER.compareAndSet(this, i2, i3));
        if (i2 == 0 || i3 != 0) {
            return;
        }
        fireChannelWritabilityChanged(z);
    }

    private static long total(Object obj) {
        if (obj instanceof ByteBuf) {
            return ((ByteBuf) obj).readableBytes();
        }
        if (obj instanceof FileRegion) {
            return ((FileRegion) obj).count();
        }
        if (obj instanceof ByteBufHolder) {
            return ((ByteBufHolder) obj).content().readableBytes();
        }
        return -1L;
    }

    private static int writabilityMask(int i2) {
        if (i2 >= 1 && i2 <= 31) {
            return 1 << i2;
        }
        throw new IllegalArgumentException("index: " + i2 + " (expected: 1~31)");
    }

    public void addFlush() {
        Entry entry = this.unflushedEntry;
        if (entry != null) {
            if (this.flushedEntry == null) {
                this.flushedEntry = entry;
            }
            do {
                this.flushed++;
                if (!entry.promise.setUncancellable()) {
                    decrementPendingOutboundBytes(entry.cancel(), false, true);
                }
                entry = entry.next;
            } while (entry != null);
            this.unflushedEntry = null;
        }
    }

    public void addMessage(Object obj, int i2, ChannelPromise channelPromise) {
        Entry entryNewInstance = Entry.newInstance(obj, i2, total(obj), channelPromise);
        Entry entry = this.tailEntry;
        if (entry == null) {
            this.flushedEntry = null;
            this.tailEntry = entryNewInstance;
        } else {
            entry.next = entryNewInstance;
            this.tailEntry = entryNewInstance;
        }
        if (this.unflushedEntry == null) {
            this.unflushedEntry = entryNewInstance;
        }
        incrementPendingOutboundBytes(i2, false);
    }

    public long bytesBeforeUnwritable() {
        long writeBufferHighWaterMark = ((long) this.channel.config().getWriteBufferHighWaterMark()) - this.totalPendingSize;
        if (writeBufferHighWaterMark <= 0 || !isWritable()) {
            return 0L;
        }
        return writeBufferHighWaterMark;
    }

    public long bytesBeforeWritable() {
        long writeBufferLowWaterMark = this.totalPendingSize - ((long) this.channel.config().getWriteBufferLowWaterMark());
        if (writeBufferLowWaterMark <= 0 || isWritable()) {
            return 0L;
        }
        return writeBufferLowWaterMark;
    }

    void close(final ClosedChannelException closedChannelException) {
        if (this.inFail) {
            this.channel.eventLoop().execute(new Runnable() { // from class: io.netty.channel.ChannelOutboundBuffer.3
                @Override // java.lang.Runnable
                public void run() {
                    ChannelOutboundBuffer.this.close(closedChannelException);
                }
            });
            return;
        }
        this.inFail = true;
        if (this.channel.isOpen()) {
            throw new IllegalStateException("close() must be invoked after the channel is closed.");
        }
        if (!isEmpty()) {
            throw new IllegalStateException("close() must be invoked after all flushed writes are handled.");
        }
        try {
            for (Entry entryRecycleAndGetNext = this.unflushedEntry; entryRecycleAndGetNext != null; entryRecycleAndGetNext = entryRecycleAndGetNext.recycleAndGetNext()) {
                TOTAL_PENDING_SIZE_UPDATER.addAndGet(this, -entryRecycleAndGetNext.pendingSize);
                if (!entryRecycleAndGetNext.cancelled) {
                    ReferenceCountUtil.safeRelease(entryRecycleAndGetNext.msg);
                    safeFail(entryRecycleAndGetNext.promise, closedChannelException);
                }
            }
            this.inFail = false;
            clearNioBuffers();
        } catch (Throwable th) {
            this.inFail = false;
            throw th;
        }
    }

    public Object current() {
        Entry entry = this.flushedEntry;
        if (entry == null) {
            return null;
        }
        return entry.msg;
    }

    void decrementPendingOutboundBytes(long j2) {
        decrementPendingOutboundBytes(j2, true, true);
    }

    void failFlushed(Throwable th, boolean z) {
        if (this.inFail) {
            return;
        }
        try {
            this.inFail = true;
            do {
            } while (remove0(th, z));
        } finally {
            this.inFail = false;
        }
    }

    public void forEachFlushedMessage(MessageProcessor messageProcessor) throws Exception {
        if (messageProcessor == null) {
            throw new NullPointerException("processor");
        }
        Entry entry = this.flushedEntry;
        if (entry == null) {
            return;
        }
        do {
            if (!entry.cancelled && !messageProcessor.processMessage(entry.msg)) {
                return;
            } else {
                entry = entry.next;
            }
        } while (isFlushedEntry(entry));
    }

    public boolean getUserDefinedWritability(int i2) {
        return (writabilityMask(i2) & this.unwritable) == 0;
    }

    void incrementPendingOutboundBytes(long j2) {
        incrementPendingOutboundBytes(j2, true);
    }

    public boolean isEmpty() {
        return this.flushed == 0;
    }

    public boolean isWritable() {
        return this.unwritable == 0;
    }

    public int nioBufferCount() {
        return this.nioBufferCount;
    }

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

    public ByteBuffer[] nioBuffers() throws Throwable {
        ByteBuf byteBuf;
        int i2;
        int iWriterIndex;
        InternalThreadLocalMap internalThreadLocalMap = InternalThreadLocalMap.get();
        ByteBuffer[] byteBufferArrExpandNioBufferArray = NIO_BUFFERS.get(internalThreadLocalMap);
        long j2 = 0;
        int iFillBufferArray = 0;
        for (Entry entry = this.flushedEntry; isFlushedEntry(entry); entry = entry.next) {
            Object obj = entry.msg;
            if (!(obj instanceof ByteBuf)) {
                break;
            }
            if (!entry.cancelled && (iWriterIndex = byteBuf.writerIndex() - (i2 = (byteBuf = (ByteBuf) obj).readerIndex())) > 0) {
                if (MPv3.MAX_MESSAGE_ID - iWriterIndex < j2) {
                    break;
                }
                j2 += (long) iWriterIndex;
                int iNioBufferCount = entry.count;
                if (iNioBufferCount == -1) {
                    iNioBufferCount = byteBuf.nioBufferCount();
                    entry.count = iNioBufferCount;
                }
                int i3 = iFillBufferArray + iNioBufferCount;
                if (i3 > byteBufferArrExpandNioBufferArray.length) {
                    byteBufferArrExpandNioBufferArray = expandNioBufferArray(byteBufferArrExpandNioBufferArray, i3, iFillBufferArray);
                    NIO_BUFFERS.set(internalThreadLocalMap, byteBufferArrExpandNioBufferArray);
                }
                if (iNioBufferCount == 1) {
                    ByteBuffer byteBufferInternalNioBuffer = entry.buf;
                    if (byteBufferInternalNioBuffer == null) {
                        byteBufferInternalNioBuffer = byteBuf.internalNioBuffer(i2, iWriterIndex);
                        entry.buf = byteBufferInternalNioBuffer;
                    }
                    byteBufferArrExpandNioBufferArray[iFillBufferArray] = byteBufferInternalNioBuffer;
                    iFillBufferArray++;
                } else {
                    ByteBuffer[] byteBufferArrNioBuffers = entry.bufs;
                    if (byteBufferArrNioBuffers == null) {
                        byteBufferArrNioBuffers = byteBuf.nioBuffers();
                        entry.bufs = byteBufferArrNioBuffers;
                    }
                    iFillBufferArray = fillBufferArray(byteBufferArrNioBuffers, byteBufferArrExpandNioBufferArray, iFillBufferArray);
                }
            }
        }
        this.nioBufferCount = iFillBufferArray;
        this.nioBufferSize = j2;
        return byteBufferArrExpandNioBufferArray;
    }

    public void progress(long j2) {
        Entry entry = this.flushedEntry;
        ChannelPromise channelPromise = entry.promise;
        if (channelPromise instanceof ChannelProgressivePromise) {
            long j3 = entry.progress + j2;
            entry.progress = j3;
            ((ChannelProgressivePromise) channelPromise).tryProgress(j3, entry.total);
        }
    }

    @Deprecated
    public void recycle() {
    }

    public boolean remove() {
        Entry entry = this.flushedEntry;
        if (entry == null) {
            clearNioBuffers();
            return false;
        }
        Object obj = entry.msg;
        ChannelPromise channelPromise = entry.promise;
        int i2 = entry.pendingSize;
        removeEntry(entry);
        if (!entry.cancelled) {
            ReferenceCountUtil.safeRelease(obj);
            safeSuccess(channelPromise);
            decrementPendingOutboundBytes(i2, false, true);
        }
        entry.recycle();
        return true;
    }

    public void removeBytes(long j2) {
        while (true) {
            Object objCurrent = current();
            if (!(objCurrent instanceof ByteBuf)) {
                break;
            }
            ByteBuf byteBuf = (ByteBuf) objCurrent;
            int i2 = byteBuf.readerIndex();
            long jWriterIndex = byteBuf.writerIndex() - i2;
            if (jWriterIndex <= j2) {
                if (j2 != 0) {
                    progress(jWriterIndex);
                    j2 -= jWriterIndex;
                }
                remove();
            } else if (j2 != 0) {
                byteBuf.readerIndex(i2 + ((int) j2));
                progress(j2);
            }
        }
        clearNioBuffers();
    }

    public void setUserDefinedWritability(int i2, boolean z) {
        if (z) {
            setUserDefinedWritability(i2);
        } else {
            clearUserDefinedWritability(i2);
        }
    }

    public int size() {
        return this.flushed;
    }

    public long totalPendingWriteBytes() {
        return this.totalPendingSize;
    }

    private void decrementPendingOutboundBytes(long j2, boolean z, boolean z2) {
        if (j2 == 0) {
            return;
        }
        long jAddAndGet = TOTAL_PENDING_SIZE_UPDATER.addAndGet(this, -j2);
        if (z2) {
            if (jAddAndGet == 0 || jAddAndGet <= this.channel.config().getWriteBufferLowWaterMark()) {
                setWritable(z);
            }
        }
    }

    private void incrementPendingOutboundBytes(long j2, boolean z) {
        if (j2 != 0 && TOTAL_PENDING_SIZE_UPDATER.addAndGet(this, j2) >= this.channel.config().getWriteBufferHighWaterMark()) {
            setUnwritable(z);
        }
    }

    private void setUserDefinedWritability(int i2) {
        int i3;
        int i4;
        int iWritabilityMask = writabilityMask(i2) ^ (-1);
        do {
            i3 = this.unwritable;
            i4 = i3 & iWritabilityMask;
        } while (!UNWRITABLE_UPDATER.compareAndSet(this, i3, i4));
        if (i3 == 0 || i4 != 0) {
            return;
        }
        fireChannelWritabilityChanged(true);
    }

    public boolean remove(Throwable th) {
        return remove0(th, true);
    }
}
