/// <summary> /// Reports corrupted / invalid message /// </summary> /// <param name="sender">Sender of the message</param> /// <param name="data">Message content</param> private void InvalidData(ClientHandler sender, String data) { Console.WriteLine("Invalid data received from {0}:", sender.Name); Console.WriteLine(data); }
/// <summary> /// Handles remote message (client message) /// </summary> /// <param name="sender">Sender of the message</param> /// <param name="msg">Message to be handled</param> private void ParseClient(ClientHandler sender, string msg) { String[] parsed = msg.Split(' '); if (parsed.Length == 0) { return; } if (parsed[0] == "MSG" && parsed.Length >= 2) { // Reconstruct the message StringBuilder nmsg = new StringBuilder(); for (int i = 1; i < parsed.Length; i++) { nmsg.Append(parsed[i]); nmsg.Append(" "); } // Prints the message Console.WriteLine("[{0}] {1}", sender.Name, nmsg.ToString()); // Resends the message parentServer.SendAll("MSG " + sender.Name + " " + nmsg); } else if (parsed[0] == "PING" && parsed.Length == 1) { } else if (parsed[0] == "READY" && parsed.Length == 1) { // We are already in game, READY message is invalid if (parentServer.State == ServerState.INGAME) { InvalidData(sender, msg); return; } sender.IsReady = true; // Informs all clients about the new ready player parentServer.SendAll("READY " + sender.Id); parentServer.RefreshStates(); } else if (parsed[0] == "SETSTART" && parsed.Length == 3) { // We have to be in game to receive this message if (parentServer.State != ServerState.INGAME) { InvalidData(sender, msg); return; } try { int x = Int32.Parse(parsed[1]); int y = Int32.Parse(parsed[2]); parentServer.SetStart(sender.Id, x, y); } catch (FormatException e) { InvalidData(sender, msg); return; } } else if (parsed[0] == "SETSTART" && parsed.Length == 3) { // We have to be in game to receive this message if (parentServer.State != ServerState.INGAME) { InvalidData(sender, msg); return; } try { int x = Int32.Parse(parsed[1]); int y = Int32.Parse(parsed[2]); parentServer.SetStart(sender.Id, x, y); } catch (FormatException e) { InvalidData(sender, msg); return; } } else if (parsed[0] == "PLACERAIL" && parsed.Length == 4) { // We have to be in game to receive this message if (parentServer.State != ServerState.INGAME) { InvalidData(sender, msg); return; } try { int x = Int32.Parse(parsed[1]); int y = Int32.Parse(parsed[2]); Direction d = GetDirection(parsed[3]); parentServer.PlaceRail(sender.Id, x, y, d); } catch (FormatException e) { InvalidData(sender, msg); return; } } else { InvalidData(sender, msg); } }
/// <summary> /// Creates message with specified sender client /// </summary> /// <param name="sender">Sender client</param> /// <param name="content">Content of the message</param> public Message(ClientHandler sender, string content) { this.Sender = sender; this.Content = content; }