Class NioIoHandler
java.lang.Object
io.netty.channel.nio.NioIoHandler
- All Implemented Interfaces:
IoHandler
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) final classprivate static final class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate intprivate static final intprivate static final booleanprivate final ThreadAwareExecutorprivate static final InternalLoggerprivate static final intprivate booleanprivate final java.nio.channels.spi.SelectorProviderprivate SelectedSelectionKeySetprivate final IntSupplierprivate java.nio.channels.SelectorThe NIOSelector.private static final intprivate final SelectStrategyprivate java.nio.channels.Selectorprivate final java.util.concurrent.atomic.AtomicBooleanBoolean that controls determines if a blocked Selector.select should break out of its selection process. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateNioIoHandler(ThreadAwareExecutor executor, java.nio.channels.spi.SelectorProvider selectorProvider, SelectStrategy strategy) -
Method Summary
Modifier and TypeMethodDescriptionprivate static NioIoOpsvoiddestroy()Destroy theIoHandlerand free all its resources.private static voidhandleLoopException(java.lang.Throwable t) booleanisCompatible(java.lang.Class<? extends IoHandle> handleType) Returnstrueif the given type is compatible with thisIoHandlerand so can be registered,falseotherwise.(package private) static longmillisBeforeDeadline(long selectDeadLineNanos, long currentTimeNanos) Returns the timeout in milliseconds forSelector.select(long)when the next scheduled task is due after the given deadline.static IoHandlerFactoryReturns a newIoHandlerFactorythat createsNioIoHandlerinstancesstatic IoHandlerFactorynewFactory(java.nio.channels.spi.SelectorProvider selectorProvider) Returns a newIoHandlerFactorythat createsNioIoHandlerinstances.static IoHandlerFactorynewFactory(java.nio.channels.spi.SelectorProvider selectorProvider, SelectStrategyFactory selectStrategyFactory) Returns a newIoHandlerFactorythat createsNioIoHandlerinstances.private static NioIoHandle(package private) intprivate NioIoHandler.SelectorTuplevoidPrepare to destroy thisIoHandler.private voidprocessSelectedKey(java.nio.channels.SelectionKey k) private intprivate intprivate intprocessSelectedKeysPlain(java.util.Set<java.nio.channels.SelectionKey> selectedKeys) (package private) voidRegister aIoHandlefor IO.(package private) java.util.Set<java.nio.channels.SelectionKey> intrun(IoHandlerContext context) Run the IO handled by thisIoHandler.private voidselect(IoHandlerContext runner, boolean oldWakenUp) private void(package private) int(package private) java.nio.channels.Selectorselector()java.nio.channels.spi.SelectorProviderprivate java.nio.channels.SelectorselectRebuildSelector(int selectCnt) (package private) java.nio.channels.Selectorvoidwakeup()Wakeup theIoHandler, which means if any operation blocks it should be unblocked and return as soon as possible.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface IoHandler
initialize
-
Field Details
-
logger
-
CLEANUP_INTERVAL
private static final int CLEANUP_INTERVAL- See Also:
-
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:
-
SELECTOR_AUTO_REBUILD_THRESHOLD
private static final int SELECTOR_AUTO_REBUILD_THRESHOLD -
selectNowSupplier
-
selector
private java.nio.channels.Selector selectorThe NIOSelector. -
unwrappedSelector
private java.nio.channels.Selector unwrappedSelector -
selectedKeys
-
provider
private final java.nio.channels.spi.SelectorProvider provider -
wakenUp
private final java.util.concurrent.atomic.AtomicBoolean wakenUpBoolean 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. -
selectStrategy
-
executor
-
cancelledKeys
private int cancelledKeys -
needsToSelectAgain
private boolean needsToSelectAgain
-
-
Constructor Details
-
NioIoHandler
private NioIoHandler(ThreadAwareExecutor executor, java.nio.channels.spi.SelectorProvider selectorProvider, SelectStrategy strategy)
-
-
Method Details
-
openSelector
-
selectorProvider
public java.nio.channels.spi.SelectorProvider selectorProvider() -
selector
java.nio.channels.Selector selector() -
numRegistered
int numRegistered() -
registeredSet
java.util.Set<java.nio.channels.SelectionKey> registeredSet() -
rebuildSelector0
void rebuildSelector0() -
nioHandle
-
cast
-
register
-
run
Description copied from interface:IoHandlerRun the IO handled by thisIoHandler. TheIoHandlerContextshould be used to ensure we not execute too long and so block the processing of other task that are scheduled on theThreadAwareExecutor. This is done by takingIoHandlerContext.delayNanos(long)orIoHandlerContext.deadlineNanos()into account.- Specified by:
runin interfaceIoHandler- Parameters:
context- theIoHandlerContext.- Returns:
- the number of
IoHandlefor which I/O was handled. Internal events such as wakeups and timer expirations must not be included in this count.
-
handleLoopException
private static void handleLoopException(java.lang.Throwable t) -
processSelectedKeys
private int processSelectedKeys() -
destroy
-
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) -
prepareToDestroy
public void prepareToDestroy()Description copied from interface:IoHandlerPrepare to destroy thisIoHandler. This method will be called beforeIoHandler.destroy()and may be called multiple times.- Specified by:
prepareToDestroyin interfaceIoHandler
-
wakeup
-
isCompatible
Description copied from interface:IoHandlerReturnstrueif the given type is compatible with thisIoHandlerand so can be registered,falseotherwise.- Specified by:
isCompatiblein interfaceIoHandler- Parameters:
handleType- the type of theIoHandle.- Returns:
- if compatible of not.
-
unwrappedSelector
java.nio.channels.Selector unwrappedSelector() -
select
- Throws:
java.io.IOException
-
millisBeforeDeadline
static long millisBeforeDeadline(long selectDeadLineNanos, long currentTimeNanos) Returns the timeout in milliseconds forSelector.select(long)when the next scheduled task is due after the given deadline. The value is rounded up, so it is only0if the deadline is less than 5 microseconds away. Rounding to the nearest millisecond would return0for a deadline less than 0.5 ms away, and the event loop would then callSelector.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() -
newFactory
Returns a newIoHandlerFactorythat createsNioIoHandlerinstances- Returns:
- factory the
IoHandlerFactory.
-
newFactory
Returns a newIoHandlerFactorythat createsNioIoHandlerinstances.- Parameters:
selectorProvider- theSelectorProviderto use.- Returns:
- factory the
IoHandlerFactory.
-
newFactory
public static IoHandlerFactory newFactory(java.nio.channels.spi.SelectorProvider selectorProvider, SelectStrategyFactory selectStrategyFactory) Returns a newIoHandlerFactorythat createsNioIoHandlerinstances.- Parameters:
selectorProvider- theSelectorProviderto use.selectStrategyFactory- theSelectStrategyFactoryto use.- Returns:
- factory the
IoHandlerFactory.
-