/// <summary> /// Server's main loop implementation. /// </summary> /// <param name="log"> The Logger instance to be used.</param> public void Run(Logger log) { TcpListener srv = null; try { srv = new TcpListener(IPAddress.Loopback, portNumber); srv.Start(); while (true) { log.LogMessage("Listener - Waiting for connection requests."); using (TcpClient socket = srv.AcceptTcpClient()) { socket.LingerState = new LingerOption(true, 10); log.LogMessage(String.Format("Listener - Connection established with {0}.", socket.Client.RemoteEndPoint)); // Instantiating protocol handler and associate it to the current TCP connection Handler protocolHandler = new Handler(socket.GetStream(), log); // Synchronously process requests made through de current TCP connection protocolHandler.Run(); } Program.ShowInfo(Store.Instance); } } finally { log.LogMessage("Listener - Ending."); srv.Stop(); } }
/// <summary> /// Server's main loop implementation. /// </summary> /// <param name="log"> The Logger instance to be used.</param> public Task<Boolean> Run(Logger log) { TcpListener srv = null; try { srv = new TcpListener(IPAddress.Loopback, portNumber); srv.Start(); ///////////////////////////////////////////////////////////////// /////////////////////////////APM///////////////////////////////// ///////////////////////////////////////////////////////////////// AsyncCallback onAccepted = null; onAccepted = delegate(IAsyncResult ar) { TcpClient socket = null; try { int timeout = (int)ar.AsyncState; if (timeout == 0) return; int time = Environment.TickCount; using (socket = srv.EndAcceptTcpClient(ar)) { if(Environment.TickCount - time > timeout) return; socket.LingerState = new LingerOption(true, 10); log.LogMessage(String.Format("Listener - Connection established with {0}.", socket.Client.RemoteEndPoint)); log.IncrementRequests(); // Instantiating protocol handler and associate it to the current TCP connection Handler protocolHandler = new Handler(socket.GetStream(), log); // Synchronously process requests made through de current TCP connection protocolHandler.Run(); srv.BeginAcceptTcpClient(onAccepted, null); } Program.ShowInfo(Store.Instance); } catch (SocketException) { Console.WriteLine("Socket error"); } catch (ObjectDisposedException) { } }; srv.BeginAcceptTcpClient(onAccepted, 20); Console.ReadLine(); return null; ////////////////////////////////////////////////////// ////////////////////////////////////////////////////// //while (true) //{ // log.LogMessage("Listener - Waiting for connection requests."); // using (TcpClient socket = srv.AcceptTcpClient()) // { // socket.LingerState = new LingerOption(true, 10); // log.LogMessage(String.Format("Listener - Connection established with {0}.", // socket.Client.RemoteEndPoint)); // // Instantiating protocol handler and associate it to the current TCP connection // Handler protocolHandler = new Handler(socket.GetStream(), log); // // Synchronously process requests made through de current TCP connection // protocolHandler.Run(); // } // Program.ShowInfo(Store.Instance); //} } finally { log.LogMessage("Listener - Ending."); srv.Stop(); } }