private void deregister(bool fireChannelInactive)
            {
                if (!channel.registered)
                {
                    return;
                }

                WindowsSelectorImpl windowsSelector = ((WindowsSelectorImpl)((NioEventLoop)(channel.eventLoop())).unwrappedSelector());

                windowsSelector.deRegister((SelectionKeyImpl)channel.selectionKey());

                // As a user may call deregister() from within any method while doing processing in the ChannelPipeline,
                // we need to ensure we do the actual deregister operation later. This is needed as for example,
                // we may be in the ByteToMessageDecoder.callDecode(...) method and so still try to do processing in
                // the old EventLoop while the user already registered the Channel to a new EventLoop. Without delay,
                // the deregister operation this could lead to have a handler invoked by different EventLoop and so
                // threads.
                //
                // See:
                // https://github.com/netty/netty/issues/4435

                invokeLater(() =>
                {
                    try
                    {
                        doDeregister();
                    }
                    catch (Exception t)
                    {
                        logger.Warn(t, "Unexpected exception occurred while deregistering a channel.");
                    }
                    finally
                    {
                        if (fireChannelInactive)
                        {
                            channel.pipeline().fireChannelInactive();
                        }
                        // Some transports like local and AIO does not allow the deregistration of
                        // an open channel.  Their doDeregister() calls close(). Consequently,
                        // close() calls deregister() again - no need to fire channelUnregistered, so check
                        // if it was registered.
                        if (channel.registered)
                        {
                            channel.registered = false;
                            channel.pipeline().fireChannelUnregistered();
                        }
                    }
                });
            }
Beispiel #2
0
        protected override void run()
        {
            while (true)
            {
                if (isTask == true)
                {
                    runAllTasks();
                    Thread.Sleep(1);
                }
                else
                {
                    WindowsSelectorImpl windowsSelector = (WindowsSelectorImpl)_unwrappedSelector;
                    SelectionKeyImpl[]  selectionKeys   = windowsSelector.getChannels();

                    foreach (SelectionKeyImpl selectionKey in selectionKeys)
                    {
                        if (selectionKey == null)
                        {
                            continue;
                        }

                        Object a = selectionKey.attachment();

                        if (!(a is AbstractChannel))
                        {
                            continue;
                        }

                        AbstractChannel ch = (AbstractChannel)a;
                        ch.getUnsafe().close();
                        runAllTasks();
                    }

                    break;
                }
            }
        }