Inheritance: java.lang.Object
Esempio n. 1
0
        public void RegisterSelector(java.nio.channels.Selector selector, int mode, Socket source, out int error)
        {
            error = 0;
            if (jServerSocket != null)
            {
                // only accept operation, which included to the read list, is allowed for server sockets
                if (mode != 0)
                {
//					error = 10038; //WSAENOTSOCK (Socket operation on nonsocket)
#if DEBUG
                    Console.WriteLine("RegisterSelector, invalid mode {0} for the server socket", mode);
#endif
                    return;
                }

                try
                {
                    if (jServerSocketChannel.isBlocking())
                    {
                        /*
                         *      A channel must be placed into non-blocking mode before being registered
                         *      with a selector, and may not be returned to blocking mode until it has been
                         *      deregistered.
                         */
                        jServerSocketChannel.configureBlocking(false);
                    }

                    jServerSocketChannel.register(selector, java.nio.channels.SelectionKey.OP_ACCEPT, source);
                }
                catch (Exception e)
                {
                    error = 10022;                     //WSAEINVAL (Invalid argument)
#if DEBUG
                    Console.WriteLine("Caught exception during RegisterSelector, register server socket - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
#endif
                }
            }
            else
            {
                try
                {
                    int ops = java.nio.channels.SelectionKey.OP_READ;
                    if (mode > 0)
                    {
                        if (jSocketChannel.isConnectionPending())
                        {
                            ops = java.nio.channels.SelectionKey.OP_CONNECT;
                        }
                        else
                        {
                            ops = java.nio.channels.SelectionKey.OP_WRITE;
                        }
                    }

                    if (jSocketChannel.isBlocking())
                    {
                        /*
                         *      A channel must be placed into non-blocking mode before being registered
                         *      with a selector, and may not be returned to blocking mode until it has been
                         *      deregistered.
                         */
                        jSocketChannel.configureBlocking(false);
                    }

                    jSocketChannel.register(selector, ops, source);
                }
                catch (Exception e)
                {
                    error = 10022;                     //WSAEINVAL (Invalid argument)
#if DEBUG
                    Console.WriteLine("Caught exception during RegisterSelector, register client socket - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
#endif
                }
            }
        }
Esempio n. 2
0
        public bool Poll_internal(SelectMode mode, int timeout, Socket source, out int error)
        {
            error = 0;

            if (mode == SelectMode.SelectError && !jSocketChannel.isConnectionPending())
            {
                return(false);
            }

            java.nio.channels.Selector selector = java.nio.channels.Selector.open();
            RegisterSelector(selector, ((mode == SelectMode.SelectRead)?0:1), source, out error);

            if (error != 0)
            {
                error = 0;
                GHSocketFactory.CloseSelector(selector);
                return(mode == SelectMode.SelectError);
            }

            bool retVal = false;

            long timeOutMillis = 1;

            if (timeout < 0)
            {
                timeOutMillis = 0;
            }
            else if (timeout > 999)
            {
                timeOutMillis = (long)(timeout / 1000);
            }

            int readyCount = 0;

            try
            {
                readyCount = selector.select(timeOutMillis);
            }
            catch (Exception e)
            {
                error = 10022;                 //WSAEINVAL (Invalid argument)
#if DEBUG
                Console.WriteLine("Caught exception during Poll_internal selector.select - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
#endif
            }

            if (readyCount > 0)
            {
                if (jSocket != null && jSocketChannel.isConnectionPending())
                {
                    bool status = false;
                    try
                    {
                        status = jSocketChannel.finishConnect();
                    }
                    catch (Exception e)
                    {
#if DEBUG
                        Console.WriteLine("Caught exception during Poll_internal, finishConnect - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
#endif
                    }
                    if (status)
                    {
                        retVal = (mode != SelectMode.SelectError);
                    }
                    else
                    {
                        retVal = (mode == SelectMode.SelectError);
                    }
                }
                else
                {
                    retVal = true;
                }
            }

            GHSocketFactory.CloseSelector(selector);

            return(retVal);
        }