void Send_Click(object sender, RoutedEventArgs e) { if (CommandLine.Text.Length > 0) { GlobalVars.AddNewMessage(CommandLine.Text); GlobalVars.serverInstance.HandleCommand(CommandLine.Text); } CommandLine.Text = ""; }
public CServer(int port) { Port = port; playerList = new List <CPlayer>(); ServerHandler = new UdpClient(port); Run(); GlobalVars.AddNewMessage("Connected on port [b]" + port + "[/b]"); GlobalVars.AddNewMessage("* Hostname: [b]" + GlobalVars.serverInfo.Properties.HostName + "[/b]"); GlobalVars.AddNewMessage("* Players: [b]0/" + GlobalVars.serverInfo.Properties.MaxPlayers + "[/b]"); GlobalVars.AddNewMessage("Server started! Waiting for players..."); }
private void Listen() { while (true) { IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, Port); string data = GetString(ServerHandler.Receive(ref remoteEP)); string AddressEP = remoteEP.ToString(); string[] info = data.Split('|'); CPlayer PlayerWriting = null; foreach (CPlayer pl in playerList) { if (AddressEP == pl.Address) { PlayerWriting = pl; } } if (PlayerWriting == null && info.Length > 3) { if (info[0] == "req" && info[1] == ConnectionKey && info[2] == Port.ToString()) { CPlayer newPlayer = new CPlayer(GetNewUniqueID(), info[3], remoteEP); playerList.Add(newPlayer); GlobalVars.AddNewMessage("New player (" + info[3] + ", ID: " + newPlayer.ID + ") successfuly connected!"); foreach (CPlayer pl in playerList) { if (pl != newPlayer) { SendMessage("JOIN|" + newPlayer.ID + "|" + info[3] + "|" + info[4], pl.endPoint); SendMessage("JOIN|" + pl.ID + "|" + pl.userName + "|0/0/0", newPlayer.endPoint); } } } else { GlobalVars.AddNewMessage("Incorrect connection request: [b]" + data + "[/b]"); } } else if (PlayerWriting != null) { PlayerWriting.lastPacket = DateTime.Now; if (info.Length > 1 && info[0] == "INFO") { foreach (CPlayer pl in playerList) { if (pl != PlayerWriting) { SendMessage("SETINFO|" + PlayerWriting.ID + "|" + info[1] + "|" + info[2] + "|" + info[3] + "|" + info[4] + "|" + info[5] + "|" + info[6] + "|" + info[7] + "|" + info[8] + "|" + info[9] + "|" + info[10], pl.endPoint); } } } else if (data == "QUIT") { GlobalVars.AddNewMessage(PlayerWriting.userName + " disconnected."); DisconnectPlayer(PlayerWriting); } else if (info.Length > 3 && info[0] == "HIT") { int id = (int)ExtractDataFromString(info[1], SentData.Int); foreach (CPlayer pl in playerList) { if (pl.ID != id) { SendMessage("PLGOTHIT|" + id + "|" + info[2] + "|" + info[3], pl.endPoint); return; } else { SendMessage("UGOTHIT|" + PlayerWriting.ID + "|" + info[2] + "|" + info[3], pl.endPoint); return; } } } } } }