/// <summary> /// Sends the provided message to the provided TCP client /// </summary> /// <param name="client">The client to send the message over</param> /// <param name="msg">The message to serialize and send</param> static public void SendMessage(ClientStruct client, MsgBase msg) { // Serialize the message string s = JsonSerializer.Serialize(msg, msg.GetType()); if (print_output) { Console.WriteLine("Sending " + s); } // Convert the message to bytes, write, and flush the stream byte[] bytes = Encoding.ASCII.GetBytes(s); client.stream.Write(bytes, 0, bytes.Length); client.stream.Flush(); }
private void tmrServerTick_Tick(object sender, EventArgs e) { // Read input messages int read_msg = 0; while (read_msg < 10) { MsgBase msg = Network.GameComms.ReceiveMessage(); if (msg == null) { break; } Console.WriteLine("Received " + msg.GetType().ToString()); if (msg is MsgGameStatus) { MsgGameStatus status = (MsgGameStatus)msg; if (game_window != null) { game_window.UpdateGame(status); } } else if (msg is MsgGameList) { MsgGameList game_list = (MsgGameList)msg; ListGames.Items.Clear(); foreach (MsgGameList.ListItem i in game_list.Games) { CommonEntry gi = new CommonEntry(id: i.GameIDValue, game_type: (GameTypes)i.GameType); ListViewItem lvi = new ListViewItem(gi.ToString()) { Tag = gi }; ListGames.Items.Add(lvi); } ListLobbies.Items.Clear(); foreach (MsgGameList.ListItem i in game_list.Lobbies) { CommonEntry li = new CommonEntry(id: i.GameIDValue, game_type: (GameTypes)i.GameType); ListViewItem lvi = new ListViewItem(li.ToString()) { Tag = li }; ListLobbies.Items.Add(lvi); } } else if (msg is MsgLobbyStatus) { MsgLobbyStatus status = (MsgLobbyStatus)msg; if (lobby_window != null) { lobby_window.UpdateStatus(status); } } read_msg += 1; } // Check for server failure if (Network.GameComms.Failed()) { #if DEBUG Application.Exit(); #else Application.Restart(); #endif } }