/// <summary>
        /// Starts listening for incoming connections
        /// </summary>
        public void Listen()
        {
            _listenSocket.Listen(_backlog);
            while (true)
            {
                try
                {
                    var socket = _listenSocket.Accept();
                    var ea     = new IncomingConnectionEventArgs {
                        Socket = socket
                    };

                    using (new PerfLimit("Tcp connection listener connection handle"))
                        OnIncomingConnection(ea);

                    if (!ea.Handled)
                    {
                        Logger.Warn("Closing non-handled connection {0}", socket.RemoteEndPoint);
                        using (new PerfLimit("Tcp connection listener close"))
                            socket.Close();
                    }
                }
                catch (ObjectDisposedException)
                {
                    return;
                }
                catch (SocketException e)
                {
                    OnException(new ExceptionEventArgs {
                        Exception = e
                    });
                }
            }
        }
        public void OnIncomingConnection(IncomingConnectionEventArgs e)
        {
            var handler = IncomingConnection;

            if (handler != null)
            {
                handler(this, e);
            }
        }
Example #3
0
        void _listener_IncomingConnection(object sender, IncomingConnectionEventArgs e)
        {
            Logger.Info("Incoming connection");

            var connection = new HyperServerConnection(e.Socket);

            connection.Handshake += connection_Handshake;
            connection.ConnectionStatusChanged += connection_ConnectionStatusChanged;
            lock (_unknownConnections)
                _unknownConnections.Add(connection);

            connection.StartAsync();
            e.Handled = true;
        }
        protected void OnIncomingConnection(IAsyncResult ar)
        {
            try
            {
                var s = (Socket)ar.AsyncState;
                var incomingSocket = s.EndAccept(ar);
                var ea             = new IncomingConnectionEventArgs {
                    Socket = incomingSocket
                };

                OnIncomingConnection(ea);

                if (!ea.Handled)
                {
                    incomingSocket.BeginDisconnect(false, null, null);
                }

                // continue accepting
                AcceptNext(s);
            }
            catch (ObjectDisposedException)
            {
            }
            catch (SocketException e)
            {
                OnException(new ExceptionEventArgs {
                    Exception = e
                });

                // try to continue listening
                try
                {
                    var s = (Socket)ar.AsyncState;
                    AcceptNext(s);
                }
                catch (Exception x)
                {
                    OnException(new ExceptionEventArgs {
                        Exception = x
                    });
                }
            }
        }