public void AbortClose(SocketChannel channel)
        {
            // TODO: Do we need to handle expection like JVM?

            channel.Socket.LingerState = new LingerOption(true, 0);
            channel.Close();
        }
Beispiel #2
0
        void disposeInternal(bool disposeBrother)
        {
            if (!mDisposed)
            {
                try
                {
                    mInnerChannel.Close();
                }
                catch (Exception ex)
                {
                    Debug.Fail($"InnerChannel close catch an exception: {ex.ToString()}");
                }

                if (mBrotherTunnel != null && disposeBrother)
                {
                    mBrotherTunnel.disposeInternal(false);
                }

                mInnerChannel  = null;
                mSelector      = null;
                mBrotherTunnel = null;
                mDisposed      = true;
                sessionCount--;

                onDispose();
                NatSessionManager.removeSession(portKey);
            }
        }
        // does not reach here.
        /// <summary>
        /// The contract is similar to
        /// <see cref="SocketChannel.Connect(System.Net.EndPoint)"/>
        ///
        /// with a timeout.
        /// </summary>
        /// <seealso cref="SocketChannel.Connect(System.Net.EndPoint)"/>
        /// <param name="channel">
        /// - this should be a
        /// <see cref="SelectableChannel"/>
        /// </param>
        /// <param name="endpoint"/>
        /// <exception cref="System.IO.IOException"/>
        internal static void Connect(SocketChannel channel, EndPoint endpoint, int timeout
                                     )
        {
            bool blockingOn = channel.IsBlocking();

            if (blockingOn)
            {
                channel.ConfigureBlocking(false);
            }
            try
            {
                if (channel.Connect(endpoint))
                {
                    return;
                }
                long timeoutLeft = timeout;
                long endTime     = (timeout > 0) ? (Time.Now() + timeout) : 0;
                while (true)
                {
                    // we might have to call finishConnect() more than once
                    // for some channels (with user level protocols)
                    int ret = selector.Select((SelectableChannel)channel, SelectionKey.OpConnect, timeoutLeft
                                              );
                    if (ret > 0 && channel.FinishConnect())
                    {
                        return;
                    }
                    if (ret == 0 || (timeout > 0 && (timeoutLeft = (endTime - Time.Now())) <= 0))
                    {
                        throw new SocketTimeoutException(TimeoutExceptionString(channel, timeout, SelectionKey
                                                                                .OpConnect));
                    }
                }
            }
            catch (IOException e)
            {
                // javadoc for SocketChannel.connect() says channel should be closed.
                try
                {
                    channel.Close();
                }
                catch (IOException)
                {
                }
                throw;
            }
            finally
            {
                if (blockingOn && channel.IsOpen())
                {
                    channel.ConfigureBlocking(true);
                }
            }
        }
 public void Run(Action <LocalServerTest> body)
 {
     try
     {
         SetServerSocketOptions();
         LocalServerChannel.Socket.Bind(ServerAddress);
         LocalServerChannel.Socket.Blocking = false;
         LocalServerChannel.Socket.Listen(100);
         body(this);
     }
     finally
     {
         LocalServerChannel.Close();
     }
 }
        private void SendFailure(SocketChannel channel, Exception ex)
        {
            //Logger.WriteDebug("this, Pipeline => MessageFailure");

            var pos   = ex.Message.IndexOfAny(new[] { '\r', '\n' });
            var descr = pos == -1 ? ex.Message : ex.Message.Substring(0, pos);

            byte[] response = Encoding.UTF8.GetBytes("HTTP/1.1 400 OK\r\n" +
                                                     "Content-Type: text/html; charset=UTF-8\r\n\r\n" +
                                                     "<doctype !html><html><head><title></title></head>" +
                                                     "<body>" + descr + "</body></html>\r\n");

            channel.Send(response);
            channel.Close();
        }