Class NioIoHandler

  • All Implemented Interfaces:
    IoHandler

    public final class NioIoHandler
    extends java.lang.Object
    implements IoHandler
    IoHandler implementation which register the IoHandle's to a Selector.
    • Field Detail

      • DISABLE_KEY_SET_OPTIMIZATION

        private static final boolean DISABLE_KEY_SET_OPTIMIZATION
      • MIN_PREMATURE_SELECTOR_RETURNS

        private static final int MIN_PREMATURE_SELECTOR_RETURNS
        See Also:
        Constant Field Values
      • SELECTOR_AUTO_REBUILD_THRESHOLD

        private static final int SELECTOR_AUTO_REBUILD_THRESHOLD
      • selectNowSupplier

        private final IntSupplier selectNowSupplier
      • selector

        private java.nio.channels.Selector selector
        The NIO Selector.
      • unwrappedSelector

        private java.nio.channels.Selector unwrappedSelector
      • provider

        private final java.nio.channels.spi.SelectorProvider provider
      • wakenUp

        private final java.util.concurrent.atomic.AtomicBoolean wakenUp
        Boolean that controls determines if a blocked Selector.select should break out of its selection process. In our case we use a timeout for the select method and the select method will block for that time unless waken up.
      • cancelledKeys

        private int cancelledKeys
      • needsToSelectAgain

        private boolean needsToSelectAgain
    • Method Detail

      • selectorProvider

        public java.nio.channels.spi.SelectorProvider selectorProvider()
        Returns the SelectorProvider used by this NioEventLoop to obtain the Selector.
      • selector

        java.nio.channels.Selector selector()
      • numRegistered

        int numRegistered()
      • registeredSet

        java.util.Set<java.nio.channels.SelectionKey> registeredSet()
      • rebuildSelector0

        void rebuildSelector0()
      • register

        public IoRegistration register​(IoHandle handle)
                                throws java.lang.Exception
        Description copied from interface: IoHandler
        Register a IoHandle for IO.
        Specified by:
        register in interface IoHandler
        Parameters:
        handle - the IoHandle to register.
        Throws:
        java.lang.Exception - thrown if an error happens during registration.
      • handleLoopException

        private static void handleLoopException​(java.lang.Throwable t)
      • processSelectedKeys

        private int processSelectedKeys()
      • destroy

        public void destroy()
        Description copied from interface: IoHandler
        Destroy the IoHandler and free all its resources. Once destroyed using the IoHandler will cause undefined behaviour.
        Specified by:
        destroy in interface IoHandler
      • processSelectedKeysPlain

        private int processSelectedKeysPlain​(java.util.Set<java.nio.channels.SelectionKey> selectedKeys)
      • processSelectedKeysOptimized

        private int processSelectedKeysOptimized()
      • processSelectedKey

        private void processSelectedKey​(java.nio.channels.SelectionKey k)
      • wakeup

        public void wakeup()
        Description copied from interface: IoHandler
        Wakeup the IoHandler, which means if any operation blocks it should be unblocked and return as soon as possible.
        Specified by:
        wakeup in interface IoHandler
      • isCompatible

        public boolean isCompatible​(java.lang.Class<? extends IoHandle> handleType)
        Description copied from interface: IoHandler
        Returns true if the given type is compatible with this IoHandler and so can be registered, false otherwise.
        Specified by:
        isCompatible in interface IoHandler
        Parameters:
        handleType - the type of the IoHandle.
        Returns:
        if compatible of not.
      • unwrappedSelector

        java.nio.channels.Selector unwrappedSelector()
      • select

        private void select​(IoHandlerContext runner,
                            boolean oldWakenUp)
                     throws java.io.IOException
        Throws:
        java.io.IOException
      • millisBeforeDeadline

        static long millisBeforeDeadline​(long selectDeadLineNanos,
                                         long currentTimeNanos)
        Returns the timeout in milliseconds for Selector.select(long) when the next scheduled task is due after the given deadline. The value is rounded up, so it is only 0 if the deadline is less than 5 microseconds away. Rounding to the nearest millisecond would return 0 for a deadline less than 0.5 ms away, and the event loop would then call Selector.selectNow() in a loop, burning CPU, until the deadline is reached.
      • selectNow

        int selectNow()
               throws java.io.IOException
        Throws:
        java.io.IOException
      • selectRebuildSelector

        private java.nio.channels.Selector selectRebuildSelector​(int selectCnt)
                                                          throws java.io.IOException
        Throws:
        java.io.IOException
      • selectAgain

        private void selectAgain()