private void Process() { while (ContinueProcess) { ClientHandler client = null; lock (ConnectionPool.SyncRoot) { if (ConnectionPool.Count > 0) { client = ConnectionPool.Dequeue(); } } if (client != null) { client.Process(); // Provoke client // if client still connect, schedufor later processingle it if (client.Alive) { ConnectionPool.Enqueue(client); } } Thread.Sleep(100); } }
public static void StartListening() { ManualResetEvent waitForConnect = new ManualResetEvent(false); ClientService ClientTask; IPAddress localAddr = IPAddress.Parse("127.0.0.1"); // Client Connections Pool ClientConnectionPool ConnectionPool = new ClientConnectionPool(); // Client Task to handle client requests ClientTask = new ClientService(ConnectionPool); ClientTask.Start(); TcpListener listener = new TcpListener(localAddr, portNum); try { listener.Start(); int TestingCycle = 3; // Number of testing cycles int ClientNbr = 0; // Start listening for connections. Console.WriteLine("Waiting for a connection..."); while (TestingCycle > 0) { TcpClient handler = listener.AcceptTcpClient(); if (handler != null) { Console.WriteLine("Ecvhange connection#{0} accepted!", ++ClientNbr); // An incoming connection needs to be processed. ConnectionPool.Enqueue(new ClientHandler(handler)); //--TestingCycle; } else { break; } waitForConnect.WaitOne(100); // } listener.Stop(); Console.WriteLine("Order Listener Stoped"); // Stop client requests handling ClientTask.Stop(); } catch (Exception e) { Console.WriteLine(e.ToString()); } Console.WriteLine("\nHit enter to continue..."); Console.Read(); }