Пример #1
0
 /// <summary>
 /// Close a connection.
 /// </summary>
 /// <param name="cnxHandle">Connection to close</param>
 public void KillClient(TcpConnectionHandle cnxHandle)
 {
     try
     {
         if (cnxHandle != null)
         {
             OnCloseMessageReceived(new MessageData(null, 0, cnxHandle.ConnectionClient.Client.RemoteEndPoint, cnxHandle));
             // Stop tcpclient.
             cnxHandle.ConnectionClient.Client.Shutdown(SocketShutdown.Both);
             cnxHandle.ConnectionClient.Client.Close();
             cnxHandle.ConnectionClient.Close();
             // Kill the thread.
             cnxHandle.ConnectionThread.Join(50);
             lock (TcpConnectionHandler)
             {
                 TcpConnectionHandler.Remove(cnxHandle);
             }
             cnxHandle = null;
         }
     }
     catch (Exception ex)
     {
         Logger.Error("Exception raised killing tcpconnectionhandle.", ex);
     }
 }
Пример #2
0
 /// <summary>
 /// Event caller.
 /// </summary>
 /// <param name="message">Raw message received from network</param>
 protected void OnCloseMessageReceived(MessageData message)
 {
     try
     {
         if (CloseConnection != null)
         {
             CloseConnection(this, message);
             lock (TcpConnectionHandler)
             {
                 TcpConnectionHandler.Remove(message.ConnectionHandle);
             }
         }
     }
     catch (Exception)
     {
         Logger.Error("Exception raised closing connection handle and removing from connection list.");
     }
 }
Пример #3
0
 /// <summary>
 /// Event caller.
 /// </summary>
 /// <param name="message">Raw message received from network</param>
 protected void OnNewConnection(MessageData message)
 {
     try
     {
         if (NewConnection != null)
         {
             NewConnection(this, message);
             lock (TcpConnectionHandler)
             {
                 TcpConnectionHandler.Add(message.ConnectionHandle);
             }
         }
     }
     catch (Exception ex)
     {
         Logger.Error("Exception raised creating new connection.", ex);
     }
 }
Пример #4
0
        /// <summary>
        /// Initializes this instance.
        /// </summary>
        protected void Initialize()
        {
            // Create the listener for selected endPoint interface (any by default).
            _tcpListener = new TcpListener(_localEndPoint);

            // Create the main thread of the listener.
            _listenerThread      = new Thread(new ThreadStart(ClientsListener));
            _listenerThread.Name = "TcpListener";

            // Create the whitelist.
            _whitelist = new List <IPRange>();

            // Create the blacklist.
            _blacklist = new List <IPRange>();

            // Create the connection list.
            TcpConnectionHandler = new TcpConnectionHandler();

            _usePermanentConnection = false;
        }
Пример #5
0
        /// <summary>
        /// Stop the Tcp Server.
        /// </summary>
        public void Stop()
        {
            if (_isRunning)
            {
                _isRunning = false;

                if (TcpConnectionHandler.Count > 0)
                {
                    for (int i = 0; i < TcpConnectionHandler.Count; i++)
                    {
                        try
                        {
                            // Stop tcpclient.
                            TcpConnectionHandler[i].ConnectionClient.Client.Shutdown(SocketShutdown.Both);
                            TcpConnectionHandler[i].ConnectionClient.Client.Close();
                            TcpConnectionHandler[i].ConnectionClient.Close();
                            // Kill the thread.
                            TcpConnectionHandler[i].ConnectionThread.Join();
                        }
                        catch
                        {
                        }
                    }
                }

                // Stop the listener.
                _tcpListener.Stop();

                if (_listenerThread.IsAlive)
                {
                    // Kill the listener thread.
                    _listenerThread.Join();
                }

                TcpConnectionHandler.Clear();
            }
        }