package io.netty.channel.nio;

import io.netty.channel.ChannelException;
import io.netty.channel.EventLoopException;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.SelectStrategy;
import io.netty.channel.SingleThreadEventLoop;
import io.netty.channel.nio.AbstractNioChannel;
import io.netty.util.IntSupplier;
import io.netty.util.concurrent.RejectedExecutionHandler;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.SystemPropertyUtil;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.channels.CancelledKeyException;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.spi.AbstractSelector;
import java.nio.channels.spi.SelectorProvider;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/* JADX INFO: loaded from: classes.dex */
public final class NioEventLoop extends SingleThreadEventLoop {
    private static final int CLEANUP_INTERVAL = 256;
    private static final int MIN_PREMATURE_SELECTOR_RETURNS = 3;
    private static final int SELECTOR_AUTO_REBUILD_THRESHOLD;
    private int cancelledKeys;
    private volatile int ioRatio;
    private boolean needsToSelectAgain;
    private final Callable<Integer> pendingTasksCallable;
    private final SelectorProvider provider;
    private final IntSupplier selectNowSupplier;
    private final SelectStrategy selectStrategy;
    private SelectedSelectionKeySet selectedKeys;
    Selector selector;
    private final AtomicBoolean wakenUp;
    private static final InternalLogger logger = InternalLoggerFactory.getInstance((Class<?>) NioEventLoop.class);
    private static final boolean DISABLE_KEYSET_OPTIMIZATION = SystemPropertyUtil.getBoolean("io.netty.noKeySetOptimization", false);

    static {
        try {
            if (SystemPropertyUtil.get("sun.nio.ch.bugLevel") == null) {
                System.setProperty("sun.nio.ch.bugLevel", "");
            }
        } catch (SecurityException e2) {
            if (logger.isDebugEnabled()) {
                logger.debug("Unable to get/set System Property: {}", "sun.nio.ch.bugLevel", e2);
            }
        }
        int i2 = SystemPropertyUtil.getInt("io.netty.selectorAutoRebuildThreshold", 512);
        SELECTOR_AUTO_REBUILD_THRESHOLD = i2 >= 3 ? i2 : 0;
        if (logger.isDebugEnabled()) {
            logger.debug("-Dio.netty.noKeySetOptimization: {}", Boolean.valueOf(DISABLE_KEYSET_OPTIMIZATION));
            logger.debug("-Dio.netty.selectorAutoRebuildThreshold: {}", Integer.valueOf(SELECTOR_AUTO_REBUILD_THRESHOLD));
        }
    }

    NioEventLoop(NioEventLoopGroup nioEventLoopGroup, Executor executor, SelectorProvider selectorProvider, SelectStrategy selectStrategy, RejectedExecutionHandler rejectedExecutionHandler) {
        super((EventLoopGroup) nioEventLoopGroup, executor, false, SingleThreadEventLoop.DEFAULT_MAX_PENDING_TASKS, rejectedExecutionHandler);
        this.selectNowSupplier = new IntSupplier() { // from class: io.netty.channel.nio.NioEventLoop.1
            @Override // io.netty.util.IntSupplier
            public int get() throws Exception {
                return NioEventLoop.this.selectNow();
            }
        };
        this.pendingTasksCallable = new Callable<Integer>() { // from class: io.netty.channel.nio.NioEventLoop.2
            /* JADX WARN: Can't rename method to resolve collision */
            @Override // java.util.concurrent.Callable
            public Integer call() throws Exception {
                return Integer.valueOf(NioEventLoop.super.pendingTasks());
            }
        };
        this.wakenUp = new AtomicBoolean();
        this.ioRatio = 50;
        if (selectorProvider == null) {
            throw new NullPointerException("selectorProvider");
        }
        if (selectStrategy == null) {
            throw new NullPointerException("selectStrategy");
        }
        this.provider = selectorProvider;
        this.selector = openSelector();
        this.selectStrategy = selectStrategy;
    }

    private void closeAll() {
        selectAgain();
        Set<SelectionKey> setKeys = this.selector.keys();
        ArrayList<AbstractNioChannel> arrayList = new ArrayList(setKeys.size());
        for (SelectionKey selectionKey : setKeys) {
            Object objAttachment = selectionKey.attachment();
            if (objAttachment instanceof AbstractNioChannel) {
                arrayList.add((AbstractNioChannel) objAttachment);
            } else {
                selectionKey.cancel();
                invokeChannelUnregistered((NioTask) objAttachment, selectionKey, null);
            }
        }
        for (AbstractNioChannel abstractNioChannel : arrayList) {
            abstractNioChannel.unsafe().close(abstractNioChannel.unsafe().voidPromise());
        }
    }

    private static void invokeChannelUnregistered(NioTask<SelectableChannel> nioTask, SelectionKey selectionKey, Throwable th) {
        try {
            nioTask.channelUnregistered(selectionKey.channel(), th);
        } catch (Exception e2) {
            logger.warn("Unexpected exception while running NioTask.channelUnregistered()", (Throwable) e2);
        }
    }

    private Selector openSelector() {
        try {
            AbstractSelector abstractSelectorOpenSelector = this.provider.openSelector();
            if (DISABLE_KEYSET_OPTIMIZATION) {
                return abstractSelectorOpenSelector;
            }
            try {
                SelectedSelectionKeySet selectedSelectionKeySet = new SelectedSelectionKeySet();
                Class<?> cls = Class.forName("sun.nio.ch.SelectorImpl", false, PlatformDependent.getSystemClassLoader());
                if (!cls.isAssignableFrom(abstractSelectorOpenSelector.getClass())) {
                    return abstractSelectorOpenSelector;
                }
                Field declaredField = cls.getDeclaredField("selectedKeys");
                Field declaredField2 = cls.getDeclaredField("publicSelectedKeys");
                declaredField.setAccessible(true);
                declaredField2.setAccessible(true);
                declaredField.set(abstractSelectorOpenSelector, selectedSelectionKeySet);
                declaredField2.set(abstractSelectorOpenSelector, selectedSelectionKeySet);
                this.selectedKeys = selectedSelectionKeySet;
                logger.trace("Instrumented an optimized java.util.Set into: {}", abstractSelectorOpenSelector);
            } catch (Throwable th) {
                this.selectedKeys = null;
                logger.trace("Failed to instrument an optimized java.util.Set into: {}", abstractSelectorOpenSelector, th);
            }
            return abstractSelectorOpenSelector;
        } catch (IOException e2) {
            throw new ChannelException("failed to open a new selector", e2);
        }
    }

    private void processSelectedKey(SelectionKey selectionKey, AbstractNioChannel abstractNioChannel) {
        AbstractNioChannel.NioUnsafe nioUnsafeUnsafe = abstractNioChannel.unsafe();
        if (!selectionKey.isValid()) {
            try {
                NioEventLoop nioEventLoopEventLoop = abstractNioChannel.eventLoop();
                if (nioEventLoopEventLoop != this || nioEventLoopEventLoop == null) {
                    return;
                }
                nioUnsafeUnsafe.close(nioUnsafeUnsafe.voidPromise());
                return;
            } catch (Throwable unused) {
                return;
            }
        }
        try {
            int i2 = selectionKey.readyOps();
            if ((i2 & 17) != 0 || i2 == 0) {
                nioUnsafeUnsafe.read();
                if (!abstractNioChannel.isOpen()) {
                    return;
                }
            }
            if ((i2 & 4) != 0) {
                abstractNioChannel.unsafe().forceFlush();
            }
            if ((i2 & 8) != 0) {
                selectionKey.interestOps(selectionKey.interestOps() & (-9));
                nioUnsafeUnsafe.finishConnect();
            }
        } catch (CancelledKeyException unused2) {
            nioUnsafeUnsafe.close(nioUnsafeUnsafe.voidPromise());
        }
    }

    private void processSelectedKeys() {
        SelectedSelectionKeySet selectedSelectionKeySet = this.selectedKeys;
        if (selectedSelectionKeySet != null) {
            processSelectedKeysOptimized(selectedSelectionKeySet.flip());
        } else {
            processSelectedKeysPlain(this.selector.selectedKeys());
        }
    }

    private void processSelectedKeysOptimized(SelectionKey[] selectionKeyArr) {
        int i2 = 0;
        while (true) {
            SelectionKey selectionKey = selectionKeyArr[i2];
            if (selectionKey == null) {
                return;
            }
            selectionKeyArr[i2] = null;
            Object objAttachment = selectionKey.attachment();
            if (objAttachment instanceof AbstractNioChannel) {
                processSelectedKey(selectionKey, (AbstractNioChannel) objAttachment);
            } else {
                processSelectedKey(selectionKey, (NioTask<SelectableChannel>) objAttachment);
            }
            if (this.needsToSelectAgain) {
                while (true) {
                    i2++;
                    if (selectionKeyArr[i2] == null) {
                        break;
                    } else {
                        selectionKeyArr[i2] = null;
                    }
                }
                selectAgain();
                selectionKeyArr = this.selectedKeys.flip();
                i2 = -1;
            }
            i2++;
        }
    }

    private void processSelectedKeysPlain(Set<SelectionKey> set) {
        if (set.isEmpty()) {
            return;
        }
        Iterator<SelectionKey> it = set.iterator();
        while (true) {
            SelectionKey next = it.next();
            Object objAttachment = next.attachment();
            it.remove();
            if (objAttachment instanceof AbstractNioChannel) {
                processSelectedKey(next, (AbstractNioChannel) objAttachment);
            } else {
                processSelectedKey(next, (NioTask<SelectableChannel>) objAttachment);
            }
            if (!it.hasNext()) {
                return;
            }
            if (this.needsToSelectAgain) {
                selectAgain();
                Set<SelectionKey> setSelectedKeys = this.selector.selectedKeys();
                if (setSelectedKeys.isEmpty()) {
                    return;
                } else {
                    it = setSelectedKeys.iterator();
                }
            }
        }
    }

    private void select(boolean z) throws IOException {
        Selector selector = this.selector;
        try {
            long jNanoTime = System.nanoTime();
            long jDelayNanos = delayNanos(jNanoTime) + jNanoTime;
            int i2 = 0;
            while (true) {
                long j2 = ((jDelayNanos - jNanoTime) + 500000) / 1000000;
                if (j2 > 0) {
                    if (!hasTasks() || !this.wakenUp.compareAndSet(false, true)) {
                        i2++;
                        if (selector.select(j2) != 0 || z || this.wakenUp.get() || hasTasks() || hasScheduledTasks()) {
                            break;
                        }
                        if (!Thread.interrupted()) {
                            long jNanoTime2 = System.nanoTime();
                            if (jNanoTime2 - TimeUnit.MILLISECONDS.toNanos(j2) < jNanoTime) {
                                if (SELECTOR_AUTO_REBUILD_THRESHOLD > 0 && i2 >= SELECTOR_AUTO_REBUILD_THRESHOLD) {
                                    logger.warn("Selector.select() returned prematurely {} times in a row; rebuilding Selector {}.", Integer.valueOf(i2), selector);
                                    rebuildSelector();
                                    selector = this.selector;
                                    selector.selectNow();
                                    break;
                                }
                            } else {
                                i2 = 1;
                            }
                            jNanoTime = jNanoTime2;
                        } else if (logger.isDebugEnabled()) {
                            logger.debug("Selector.select() returned prematurely because Thread.currentThread().interrupt() was called. Use NioEventLoop.shutdownGracefully() to shutdown the NioEventLoop.");
                        }
                    } else {
                        selector.selectNow();
                        break;
                    }
                } else if (i2 == 0) {
                    selector.selectNow();
                }
            }
            i2 = 1;
            if (i2 <= 3 || !logger.isDebugEnabled()) {
                return;
            }
            logger.debug("Selector.select() returned prematurely {} times in a row for Selector {}.", Integer.valueOf(i2 - 1), selector);
        } catch (CancelledKeyException e2) {
            if (logger.isDebugEnabled()) {
                logger.debug(CancelledKeyException.class.getSimpleName() + " raised by a Selector {} - JDK bug?", selector, e2);
            }
        }
    }

    private void selectAgain() {
        this.needsToSelectAgain = false;
        try {
            this.selector.selectNow();
        } catch (Throwable th) {
            logger.warn("Failed to update SelectionKeys.", th);
        }
    }

    void cancel(SelectionKey selectionKey) {
        selectionKey.cancel();
        int i2 = this.cancelledKeys + 1;
        this.cancelledKeys = i2;
        if (i2 >= CLEANUP_INTERVAL) {
            this.cancelledKeys = 0;
            this.needsToSelectAgain = true;
        }
    }

    @Override // io.netty.util.concurrent.SingleThreadEventExecutor
    protected void cleanup() {
        try {
            this.selector.close();
        } catch (IOException e2) {
            logger.warn("Failed to close a selector.", (Throwable) e2);
        }
    }

    public int getIoRatio() {
        return this.ioRatio;
    }

    @Override // io.netty.util.concurrent.SingleThreadEventExecutor
    protected Queue<Runnable> newTaskQueue(int i2) {
        return PlatformDependent.newMpscQueue(i2);
    }

    @Override // io.netty.util.concurrent.SingleThreadEventExecutor
    public int pendingTasks() {
        return inEventLoop() ? super.pendingTasks() : ((Integer) submit((Callable) this.pendingTasksCallable).syncUninterruptibly().getNow()).intValue();
    }

    @Override // io.netty.util.concurrent.SingleThreadEventExecutor
    protected Runnable pollTask() {
        Runnable runnablePollTask = super.pollTask();
        if (this.needsToSelectAgain) {
            selectAgain();
        }
        return runnablePollTask;
    }

    /* JADX WARN: Code restructure failed: missing block: B:46:?, code lost:
    
        continue;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public void rebuildSelector() {
        /*
            r9 = this;
            boolean r0 = r9.inEventLoop()
            if (r0 != 0) goto Lf
            io.netty.channel.nio.NioEventLoop$3 r0 = new io.netty.channel.nio.NioEventLoop$3
            r0.<init>()
            r9.execute(r0)
            return
        Lf:
            java.nio.channels.Selector r0 = r9.selector
            if (r0 != 0) goto L14
            return
        L14:
            java.nio.channels.Selector r1 = r9.openSelector()     // Catch: java.lang.Exception -> Lb3
            r2 = 0
        L19:
            java.util.Set r3 = r0.keys()     // Catch: java.util.ConcurrentModificationException -> L19
            java.util.Iterator r3 = r3.iterator()     // Catch: java.util.ConcurrentModificationException -> L19
        L21:
            boolean r4 = r3.hasNext()     // Catch: java.util.ConcurrentModificationException -> L19
            if (r4 == 0) goto L81
            java.lang.Object r4 = r3.next()     // Catch: java.util.ConcurrentModificationException -> L19
            java.nio.channels.SelectionKey r4 = (java.nio.channels.SelectionKey) r4     // Catch: java.util.ConcurrentModificationException -> L19
            java.lang.Object r5 = r4.attachment()     // Catch: java.util.ConcurrentModificationException -> L19
            boolean r6 = r4.isValid()     // Catch: java.lang.Exception -> L5d
            if (r6 == 0) goto L21
            java.nio.channels.SelectableChannel r6 = r4.channel()     // Catch: java.lang.Exception -> L5d
            java.nio.channels.SelectionKey r6 = r6.keyFor(r1)     // Catch: java.lang.Exception -> L5d
            if (r6 == 0) goto L42
            goto L21
        L42:
            int r6 = r4.interestOps()     // Catch: java.lang.Exception -> L5d
            r4.cancel()     // Catch: java.lang.Exception -> L5d
            java.nio.channels.SelectableChannel r7 = r4.channel()     // Catch: java.lang.Exception -> L5d
            java.nio.channels.SelectionKey r6 = r7.register(r1, r6, r5)     // Catch: java.lang.Exception -> L5d
            boolean r7 = r5 instanceof io.netty.channel.nio.AbstractNioChannel     // Catch: java.lang.Exception -> L5d
            if (r7 == 0) goto L5a
            r7 = r5
            io.netty.channel.nio.AbstractNioChannel r7 = (io.netty.channel.nio.AbstractNioChannel) r7     // Catch: java.lang.Exception -> L5d
            r7.selectionKey = r6     // Catch: java.lang.Exception -> L5d
        L5a:
            int r2 = r2 + 1
            goto L21
        L5d:
            r6 = move-exception
            io.netty.util.internal.logging.InternalLogger r7 = io.netty.channel.nio.NioEventLoop.logger     // Catch: java.util.ConcurrentModificationException -> L19
            java.lang.String r8 = "Failed to re-register a Channel to the new Selector."
            r7.warn(r8, r6)     // Catch: java.util.ConcurrentModificationException -> L19
            boolean r7 = r5 instanceof io.netty.channel.nio.AbstractNioChannel     // Catch: java.util.ConcurrentModificationException -> L19
            if (r7 == 0) goto L7b
            io.netty.channel.nio.AbstractNioChannel r5 = (io.netty.channel.nio.AbstractNioChannel) r5     // Catch: java.util.ConcurrentModificationException -> L19
            io.netty.channel.nio.AbstractNioChannel$NioUnsafe r4 = r5.unsafe()     // Catch: java.util.ConcurrentModificationException -> L19
            io.netty.channel.nio.AbstractNioChannel$NioUnsafe r5 = r5.unsafe()     // Catch: java.util.ConcurrentModificationException -> L19
            io.netty.channel.ChannelPromise r5 = r5.voidPromise()     // Catch: java.util.ConcurrentModificationException -> L19
            r4.close(r5)     // Catch: java.util.ConcurrentModificationException -> L19
            goto L21
        L7b:
            io.netty.channel.nio.NioTask r5 = (io.netty.channel.nio.NioTask) r5     // Catch: java.util.ConcurrentModificationException -> L19
            invokeChannelUnregistered(r5, r4, r6)     // Catch: java.util.ConcurrentModificationException -> L19
            goto L21
        L81:
            r9.selector = r1
            r0.close()     // Catch: java.lang.Throwable -> L87
            goto L97
        L87:
            r0 = move-exception
            io.netty.util.internal.logging.InternalLogger r1 = io.netty.channel.nio.NioEventLoop.logger
            boolean r1 = r1.isWarnEnabled()
            if (r1 == 0) goto L97
            io.netty.util.internal.logging.InternalLogger r1 = io.netty.channel.nio.NioEventLoop.logger
            java.lang.String r3 = "Failed to close the old Selector."
            r1.warn(r3, r0)
        L97:
            io.netty.util.internal.logging.InternalLogger r0 = io.netty.channel.nio.NioEventLoop.logger
            java.lang.StringBuilder r1 = new java.lang.StringBuilder
            r1.<init>()
            java.lang.String r3 = "Migrated "
            r1.append(r3)
            r1.append(r2)
            java.lang.String r2 = " channel(s) to the new Selector."
            r1.append(r2)
            java.lang.String r1 = r1.toString()
            r0.info(r1)
            return
        Lb3:
            r0 = move-exception
            io.netty.util.internal.logging.InternalLogger r1 = io.netty.channel.nio.NioEventLoop.logger
            java.lang.String r2 = "Failed to create a new Selector."
            r1.warn(r2, r0)
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: io.netty.channel.nio.NioEventLoop.rebuildSelector():void");
    }

    public void register(SelectableChannel selectableChannel, int i2, NioTask<?> nioTask) {
        if (selectableChannel == null) {
            throw new NullPointerException("ch");
        }
        if (i2 == 0) {
            throw new IllegalArgumentException("interestOps must be non-zero.");
        }
        if (((~selectableChannel.validOps()) & i2) != 0) {
            throw new IllegalArgumentException("invalid interestOps: " + i2 + "(validOps: " + selectableChannel.validOps() + ')');
        }
        if (nioTask == null) {
            throw new NullPointerException("task");
        }
        if (isShutdown()) {
            throw new IllegalStateException("event loop shut down");
        }
        try {
            selectableChannel.register(this.selector, i2, nioTask);
        } catch (Exception e2) {
            throw new EventLoopException("failed to register a channel", e2);
        }
    }

    @Override // io.netty.util.concurrent.SingleThreadEventExecutor
    protected void run() {
        while (true) {
            try {
                int iCalculateStrategy = this.selectStrategy.calculateStrategy(this.selectNowSupplier, hasTasks());
                if (iCalculateStrategy == -2) {
                    continue;
                } else {
                    if (iCalculateStrategy == -1) {
                        select(this.wakenUp.getAndSet(false));
                        if (this.wakenUp.get()) {
                            this.selector.wakeup();
                        }
                    }
                    this.cancelledKeys = 0;
                    this.needsToSelectAgain = false;
                    int i2 = this.ioRatio;
                    if (i2 == 100) {
                        processSelectedKeys();
                        runAllTasks();
                    } else {
                        long jNanoTime = System.nanoTime();
                        processSelectedKeys();
                        runAllTasks(((System.nanoTime() - jNanoTime) * ((long) (100 - i2))) / ((long) i2));
                    }
                    if (isShuttingDown()) {
                        closeAll();
                        if (confirmShutdown()) {
                            return;
                        }
                    } else {
                        continue;
                    }
                }
            } catch (Throwable th) {
                logger.warn("Unexpected exception in the selector loop.", th);
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException unused) {
                }
            }
        }
    }

    int selectNow() throws IOException {
        try {
            return this.selector.selectNow();
        } finally {
            if (this.wakenUp.get()) {
                this.selector.wakeup();
            }
        }
    }

    public SelectorProvider selectorProvider() {
        return this.provider;
    }

    public void setIoRatio(int i2) {
        if (i2 > 0 && i2 <= 100) {
            this.ioRatio = i2;
            return;
        }
        throw new IllegalArgumentException("ioRatio: " + i2 + " (expected: 0 < ioRatio <= 100)");
    }

    @Override // io.netty.util.concurrent.SingleThreadEventExecutor
    protected void wakeup(boolean z) {
        if (z || !this.wakenUp.compareAndSet(false, true)) {
            return;
        }
        this.selector.wakeup();
    }

    private static void processSelectedKey(SelectionKey selectionKey, NioTask<SelectableChannel> nioTask) {
        try {
            try {
                nioTask.channelReady(selectionKey.channel(), selectionKey);
                if (!selectionKey.isValid()) {
                    invokeChannelUnregistered(nioTask, selectionKey, null);
                }
            } catch (Exception e2) {
                selectionKey.cancel();
                invokeChannelUnregistered(nioTask, selectionKey, e2);
            }
        } catch (Throwable th) {
            selectionKey.cancel();
            invokeChannelUnregistered(nioTask, selectionKey, null);
            throw th;
        }
    }
}
