// 10 seconds.
            /// <summary>
            /// Waits on the channel with the given timeout using one of the
            /// cached selectors.
            /// </summary>
            /// <remarks>
            /// Waits on the channel with the given timeout using one of the
            /// cached selectors. It also removes any cached selectors that are
            /// idle for a few seconds.
            /// </remarks>
            /// <param name="channel"/>
            /// <param name="ops"/>
            /// <param name="timeout"/>
            /// <returns/>
            /// <exception cref="System.IO.IOException"/>
            internal virtual int Select(SelectableChannel channel, int ops, long timeout)
            {
                SocketIOWithTimeout.SelectorPool.SelectorInfo info = Get(channel);
                SelectionKey key = null;
                int          ret = 0;

                try
                {
                    while (true)
                    {
                        long start = (timeout == 0) ? 0 : Time.Now();
                        key = channel.Register(info.selector, ops);
                        ret = info.selector.Select(timeout);
                        if (ret != 0)
                        {
                            return(ret);
                        }
                        if (Thread.CurrentThread().IsInterrupted())
                        {
                            throw new ThreadInterruptedException("Interrupted while waiting for " + "IO on channel "
                                                                 + channel + ". " + timeout + " millis timeout left.");
                        }

                        /* Sometimes select() returns 0 much before timeout for
                         * unknown reasons. So select again if required.
                         */
                        if (timeout > 0)
                        {
                            timeout -= Time.Now() - start;
                            if (timeout <= 0)
                            {
                                return(0);
                            }
                        }
                    }
                }
                finally
                {
                    if (key != null)
                    {
                        key.Cancel();
                    }
                    //clear the canceled key.
                    try
                    {
                        info.selector.SelectNow();
                    }
                    catch (IOException e)
                    {
                        Log.Info("Unexpected Exception while clearing selector : ", e);
                        // don't put the selector back.
                        info.Close();
                        return(ret);
                    }
                    Release(info);
                }
            }
Exemple #2
0
        // -- Closing --

        /// <summary>
        /// Closes this channel.
        ///
        /// <para> This method, which is specified in the {@link
        /// AbstractInterruptibleChannel} class and is invoked by the {@link
        /// java.nio.channels.Channel#close close} method, in turn invokes the
        /// <seealso cref="#implCloseSelectableChannel implCloseSelectableChannel"/> method in
        /// order to perform the actual work of closing this channel.  It then
        /// cancels all of this channel's keys.  </para>
        /// </summary>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: protected final void implCloseChannel() throws java.io.IOException
        protected internal sealed override void ImplCloseChannel()
        {
            ImplCloseSelectableChannel();
            lock (KeyLock)
            {
                int count = (Keys == null) ? 0 : Keys.Length;
                for (int i = 0; i < count; i++)
                {
                    SelectionKey k = Keys[i];
                    if (k != null)
                    {
                        k.Cancel();
                    }
                }
            }
        }