Ignore:
Timestamp:
Jan 21, 2011, 6:32:48 PM (11 years ago)
Author:
schwittmann
Message:

Shutdown external client server gracefully

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrypPlugins/KeySearcher/Server/Server.cs

    r2214 r2411  
    2121
    2222    private Dictionary<EndPoint, TcpClient> connectedClients = new Dictionary<EndPoint, TcpClient>();
     23    private TcpListener tcpListener;
     24    private bool running = false;
    2325
    2426    ///<summary>
    25     /// Starts the server. Will block forever, you might want to start this in an additional thread.
     27    /// Starts the server. Will block as long as the server runs, you might want to start this in an additional thread.
    2628    ///</summary>
    2729    public void Run()
    2830    {
    29         TcpListener tcpListener = null;
     31        lock (this)
     32        {
     33            if (running)
     34            {
     35                throw new Exception("Invalid state: Already running");
     36            }
     37            running = true;
     38        }
     39
    3040        try
    3141        {
     
    3343            tcpListener.Start();
    3444            Console.WriteLine("Listening for client on port " + Port);
    35             while (true)
     45            while (running)
    3646            {
    3747                TcpClient client = tcpListener.AcceptTcpClient();
     
    4858        {
    4959        }
     60        catch (SocketException e)
     61        {
     62            if (running)
     63            {
     64                Console.WriteLine("CryptoolServer: Got SocketException while running");
     65            }
     66        }
     67
    5068
    5169        tcpListener.Stop();
     
    137155    }
    138156
     157    /// <summary>
     158    /// Closes this server. Any concurrent call to Run() in any other thread will return.
     159    /// </summary>
     160    public void Shutdown()
     161    {
     162        lock (this)
     163        {
     164            running = false;
     165            tcpListener.Stop();
     166        }
     167    }
    139168}
Note: See TracChangeset for help on using the changeset viewer.