private void CloseSocket()
        {
            if (sock == null)
            {
                return;
            }

            try
            {
                sock.Close();
            }
            catch
            {
            }
            finally
            {
                sock = null;
            }
            RemoveConnection();
        }
        public HttpConnection(Socket sock, EndPointListener epl, bool secure, X509Certificate2 cert, AsymmetricAlgorithm key)
        {
            this.sock   = sock;
            this.epl    = epl;
            this.secure = secure;
            this.key    = key;
            if (secure == false)
            {
                stream = new NetworkStream(sock, false);
            }
            else
            {
#if SSL
                SslServerStream ssl_stream = new SslServerStream(new NetworkStream(sock, false), cert, false, true, false);
                ssl_stream.PrivateKeyCertSelectionDelegate += OnPVKSelection;
                ssl_stream.ClientCertValidationDelegate    += OnClientCertificateValidation;
                stream = ssl_stream;
#else
                stream = new NetworkStream(sock, false);
#endif
            }
            timer = new Timer(OnTimeout, null, Timeout.Infinite, Timeout.Infinite);
            Init();
        }
        internal void Close(bool force_close)
        {
            if (sock != null)
            {
                Stream st = GetResponseStream();
                if (st != null)
                {
                    if (force_close)
                    {
                        try
                        {
                            st.Close();
                        }
                        catch
                        {
                        }
                    }
                    else
                    {
                        st.Close();
                    }
                }

                o_stream = null;
            }

            if (sock != null)
            {
                force_close |= !context.Request.KeepAlive;
                if (!force_close)
                {
                    force_close = (context.Response.Headers["connection"] == "close");
                }

                /*
                 * if (!force_close) {
                 * //					bool conn_close = (status_code == 400 || status_code == 408 || status_code == 411 ||
                 * //							status_code == 413 || status_code == 414 || status_code == 500 ||
                 * //							status_code == 503);
                 *
                 *      force_close |= (context.Request.ProtocolVersion <= HttpVersion.Version10);
                 * }
                 */

                if (!force_close && context.Request.FlushInput())
                {
                    if (chunked && context.Response.ForceCloseChunked == false)
                    {
                        // Don't close. Keep working.
                        reuses++;
                        Unbind();
                        Init();
                        BeginReadRequest();
                        return;
                    }

                    reuses++;
                    Unbind();
                    Init();
                    BeginReadRequest();
                    return;
                }

                Socket s = sock;
                sock = null;
                try
                {
                    if (s != null)
                    {
                        s.Shutdown(SocketShutdown.Both);
                    }
                }
                catch
                {
                }
                finally
                {
                    if (s != null)
                    {
                        s.Close();
                    }
                }
                Unbind();
                RemoveConnection();
                return;
            }
        }
Ejemplo n.º 4
0
        private void receiveRequest()
        {
            TcpClient cl = null;

            while (true)
            {
                try
                {
#if SSHARP
                    cl = _listener.Accept();
#else
                    cl = _listener.AcceptTcpClient();
#endif
                    ThreadPool.QueueUserWorkItem(state =>
                    {
                        try
                        {
                            var ctx = cl.GetWebSocketContext(null, _secure,
#if !NETCF || BCC || SSL
                                                             _sslConfigInUse,
#endif
                                                             _log);
                            if (!ctx.Authenticate(_authSchemes, _realmInUse, _userCredFinder))
                            {
                                return;
                            }

                            processRequest(ctx);
                        }
                        catch (Exception ex)
                        {
                            _log.Fatal(ex.ToString());
                            cl.Close();
                        }
                    });
                }
                catch (SocketException ex)
                {
                    if (_state == ServerState.ShuttingDown)
                    {
                        _log.Info("The receiving is stopped.");
                        break;
                    }

                    _log.Fatal(ex.ToString());
                    break;
                }
                catch (Exception ex)
                {
                    _log.Fatal(ex.ToString());
                    if (cl != null)
                    {
                        cl.Close();
                    }

                    break;
                }
            }

            if (_state != ServerState.ShuttingDown)
            {
                abort();
            }
        }