/// <summary> /// Handles the client by answering a single request and closing the connection. /// </summary> /// <param name="client">The client.</param> public void HandleClient(TcpClient client) { new Task(() => { NetworkStream stream = client.GetStream(); MessageReader reader = new MessageReader(new StreamReader(stream)); MessageWriter writer = new MessageWriter(new StreamWriter(stream)); { string commandLine = reader.ReadMessage(); Console.WriteLine(commandLine); string result = _controller.ExecuteCommand(commandLine, client); writer.WriteMessage(result); /*if the requset was for a new game, * than the client handeling responsability goes to the game, * therefore the connection isb't closed. */ if (!(commandLine.Contains("start") || commandLine.Contains("join"))) { client.Close(); stream.Close(); reader.Close(); writer.Close(); } } }).Start(); }
/// <summary> /// Handles the player. /// </summary> /// <param name="client">The client.</param> public void HandleClient(TcpClient client) { NetworkStream stream = client.GetStream(); MessageReader reader = new MessageReader(new StreamReader(stream)); MessageWriter writer = new MessageWriter(new StreamWriter(stream)); // read requsets from the client and procces them Task read = new Task(() => { string execute = ""; do { try { // try get input from client string input = reader.ReadMessage(); execute = _gameController.ExecuteCommand(input, client); } catch (Exception e) { // close the connection execute = "close"; } // while the requst isn't closing request } while (!execute.Equals("close")); }); // send the client the game state until a closing state is reached Task write = new Task(() => { string output; { do { output = _gameController.GetState(client); writer.WriteMessage(output); } while (!output.Equals("close")); stream.Close(); reader.Close(); writer.Close(); client.Close(); } }); read.Start(); write.Start(); }
/// <summary> /// Starts this session /// </summary> public void Start() { // initilaize the tcp end point IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ConfigurationManager.AppSettings[0]), int.Parse(ConfigurationManager.AppSettings[1])); string answer = ""; string msg; string commandLine = ""; while (true) { // connect to the server TcpClient client = new TcpClient(); client.Connect(ep); Console.WriteLine("You are connected"); _stream = client.GetStream(); _reader = new MessageReader(new StreamReader(_stream)); _writer = new MessageWriter(new StreamWriter(_stream)); Task write = null; // task for read answers from the server Task read = new Task(() => { do { answer = _reader.ReadMessage(); try { // check if it's a move Move move = Move.FromJson(answer); if (move.ClientId != clientId) { Console.WriteLine(move.ToString()); } } catch { if (!answer.Contains("close")) { Console.WriteLine(answer); } // if game end else { Console.WriteLine("Game ended!"); } // if it's not multiple game break if (!CheckIfMultiple(commandLine, answer)) { break; } } } while (!answer.Contains("close")); }); // task for write a command to the server write = new Task(() => { while (true) { commandLine = Console.ReadLine(); _writer.WriteMessage(commandLine); } }); write.Start(); read.Start(); // wait to answer from server read.Wait(); _stream.Close(); _reader.Close(); _writer.Close(); client.Close(); } }