Package io.netty.channel.nio
Class NioIoHandler
- java.lang.Object
-
- io.netty.channel.nio.NioIoHandler
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) classNioIoHandler.DefaultNioRegistrationprivate static classNioIoHandler.SelectorTuple
-
Field Summary
Fields Modifier and Type Field Description private intcancelledKeysprivate static intCLEANUP_INTERVALprivate static booleanDISABLE_KEY_SET_OPTIMIZATIONprivate ThreadAwareExecutorexecutorprivate static InternalLoggerloggerprivate static intMIN_PREMATURE_SELECTOR_RETURNSprivate booleanneedsToSelectAgainprivate java.nio.channels.spi.SelectorProviderproviderprivate SelectedSelectionKeySetselectedKeysprivate IntSupplierselectNowSupplierprivate java.nio.channels.SelectorselectorThe NIOSelector.private static intSELECTOR_AUTO_REBUILD_THRESHOLDprivate SelectStrategyselectStrategyprivate java.nio.channels.SelectorunwrappedSelectorprivate java.util.concurrent.atomic.AtomicBooleanwakenUpBoolean that controls determines if a blocked Selector.select should break out of its selection process.
-
Constructor Summary
Constructors Modifier Constructor Description privateNioIoHandler(ThreadAwareExecutor executor, java.nio.channels.spi.SelectorProvider selectorProvider, SelectStrategy strategy)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static NioIoOpscast(IoOps ops)voiddestroy()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.private static longmillisBeforeDeadline(long selectDeadLineNanos, long currentTimeNanos)static IoHandlerFactorynewFactory()Returns 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 NioIoHandlenioHandle(IoHandle handle)(package private) intnumRegistered()private NioIoHandler.SelectorTupleopenSelector()voidprepareToDestroy()Prepare to destroy thisIoHandler.private voidprocessSelectedKey(java.nio.channels.SelectionKey k)private intprocessSelectedKeys()private intprocessSelectedKeysOptimized()private intprocessSelectedKeysPlain(java.util.Set<java.nio.channels.SelectionKey> selectedKeys)(package private) voidrebuildSelector0()IoRegistrationregister(IoHandle handle)Register aIoHandlefor IO.(package private) java.util.Set<java.nio.channels.SelectionKey>registeredSet()intrun(IoHandlerContext context)Run the IO handled by thisIoHandler.private voidselect(IoHandlerContext runner, boolean oldWakenUp)private voidselectAgain()(package private) intselectNow()(package private) java.nio.channels.Selectorselector()java.nio.channels.spi.SelectorProviderselectorProvider()private java.nio.channels.SelectorselectRebuildSelector(int selectCnt)(package private) java.nio.channels.SelectorunwrappedSelector()voidwakeup()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, wait
-
Methods inherited from interface io.netty.channel.IoHandler
initialize
-
-
-
-
Field Detail
-
logger
private static final InternalLogger logger
-
CLEANUP_INTERVAL
private static final int CLEANUP_INTERVAL
- See Also:
- Constant Field Values
-
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 NIOSelector.
-
unwrappedSelector
private java.nio.channels.Selector unwrappedSelector
-
selectedKeys
private SelectedSelectionKeySet selectedKeys
-
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.
-
selectStrategy
private final SelectStrategy selectStrategy
-
executor
private final ThreadAwareExecutor executor
-
cancelledKeys
private int cancelledKeys
-
needsToSelectAgain
private boolean needsToSelectAgain
-
-
Constructor Detail
-
NioIoHandler
private NioIoHandler(ThreadAwareExecutor executor, java.nio.channels.spi.SelectorProvider selectorProvider, SelectStrategy strategy)
-
-
Method Detail
-
openSelector
private NioIoHandler.SelectorTuple 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
private static NioIoHandle nioHandle(IoHandle handle)
-
register
public IoRegistration register(IoHandle handle) throws java.lang.Exception
Description copied from interface:IoHandlerRegister aIoHandlefor IO.
-
run
public int run(IoHandlerContext context)
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
public void destroy()
Description copied from 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)
-
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
public void wakeup()
Description copied from interface:IoHandlerWakeup theIoHandler, which means if any operation blocks it should be unblocked and return as soon as possible.
-
isCompatible
public boolean isCompatible(java.lang.Class<? extends IoHandle> handleType)
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
private void select(IoHandlerContext runner, boolean oldWakenUp) throws java.io.IOException
- Throws:
java.io.IOException
-
millisBeforeDeadline
private static long millisBeforeDeadline(long selectDeadLineNanos, long currentTimeNanos)
-
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
public static IoHandlerFactory newFactory()
Returns a newIoHandlerFactorythat createsNioIoHandlerinstances- Returns:
- factory the
IoHandlerFactory.
-
newFactory
public static IoHandlerFactory newFactory(java.nio.channels.spi.SelectorProvider selectorProvider)
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.
-
-