// /// <summary> /// recive command from client and send to controller that translate the command and send to /// model to perfrom /// </summary> /// <param name="client"> the client that </param> /// <param name="controller">the controller that communicate betwen client and servrer</param> public void HandleClient(TcpClient client, Controller.Controller controller) { new Task(() => { while (true) { try { //try accept message from client string commandLine = reader.ReadString(); //if requestto close ,get out from loop if (commandLine.Equals("close me")) { break; } //sent the command to controller to send to model to execute the command string result = controller.ExecuteCommand(commandLine, cclient); //response back to client writer.Write(result); } catch { break; } } client.Close(); }).Start(); }
/// <summary> /// Handles the client. /// </summary> /// <param name="client">The client.</param> public void HandleClient(Client client) { Task t = new Task(() => { try { bool keepCom; do { //Console.WriteLine("Waiting for message from {0}", client.ToString()); string command = client.ReadMessage(); CommandResult res = controller.ExecuteCommand(command, client); keepCom = res.KeepConnection; client.SendCommandResult(res); } while (keepCom && client.Connected); Console.WriteLine("Closing connection with {0}.", client.ToString()); client.Close(); } catch (Exception e) { Console.WriteLine("Could not reach {0}, closing the connection.", client.ToString()); } }); t.Start(); }