/// <summary> /// Buffer: /// int PacketNum; /// string message; /// </summary> private static void HandleGlChatMsg(ClientTCP client, byte[] data) { PacketBuffer buffer = new PacketBuffer(); buffer.WriteBytes(data); buffer.ReadInteger(); SendDataTCP.SendGlChatMsg(client.nickname, buffer.ReadString() /* message */); buffer.Dispose(); }
private void txtChat_KeyDown(object sender, KeyEventArgs e) { if (e.KeyValue.Equals(13)) { if (!ServerTCP.Closed) { if (!String.IsNullOrEmpty(txtChat.Text)) { if (txtChat.Text.StartsWith("/")) { string[] commands = txtChat.Text.Split(' '); switch (commands[0]) { case "/AddAccount": if (commands.Length == 4) { Global.data.AddAccount(commands[1], commands[2], commands[3]); } else { Global.serverForm.Debug("Invalid arguments in command: " + commands[0]); } break; case "/AddMap": if (commands.Length == 16) { Global.data.AddMap(int.Parse(commands[1]), int.Parse(commands[2]), int.Parse(commands[3]), new float[] { float.Parse(commands[4]), float.Parse(commands[5]) }, new float[] { float.Parse(commands[6]), float.Parse(commands[7]) }, new float[] { float.Parse(commands[8]), float.Parse(commands[9]), float.Parse(commands[10]), float.Parse(commands[11]) }, new float[] { float.Parse(commands[12]), float.Parse(commands[13]), float.Parse(commands[14]), float.Parse(commands[15]) }); } else { Global.serverForm.Debug("Invalid arguments in command: " + commands[0]); } break; default: Global.serverForm.Debug("Unknown command"); break; } } else { SendDataTCP.SendGlChatMsg("Server", txtChat.Text); txtChat.Text = ""; } } } } }
public void Close() { if (clientState != ClientTCPState.Sleep) { if (clientState == ClientTCPState.Entrance) { clientState = ClientTCPState.Sleep; try { socket.Dispose(); } catch (Exception ex) { Global.serverForm.Debug(ex + ""); } foreach (ClientTCP friend in friends) { SendDataTCP.SendFriendLeave(this, friend); friend.friends.Remove(this); } Global.clientList.Remove(this); } else if (clientState == ClientTCPState.MainLobby) { clientState = ClientTCPState.Sleep; try { socket.Dispose(); } catch (Exception ex) { Global.serverForm.Debug(ex + ""); } foreach (ClientTCP friend in friends) { SendDataTCP.SendFriendLeave(this, friend); friend.friends.Remove(this); } Global.clientList.Remove(this); SendDataTCP.SendGlChatMsg("Server", $"Player { nickname } disconnected."); } else if (clientState == ClientTCPState.GameRoom) { clientState = ClientTCPState.Sleep; if (playerState == NetPlayerState.SearchingForMatch) { room.DeletePlayer(this); } else if (playerState == NetPlayerState.Playing) { room.AbortGameSession(this); } else if (playerState == NetPlayerState.EndPlaying) { room.DeletePlayer(this); } foreach (ClientTCP friend in friends) { SendDataTCP.SendFriendLeave(this, friend); friend.friends.Remove(this); } Global.serverForm.Debug($"GamePlayer {nickname} lost connection"); Global.clientList.Remove(this); } Global.serverForm.RemoveClient(this); } }