public static void CloseServer() { if (UDPBroadCastSocket != null) { UDPBroadCastSocket.Close(); UDPBroadCastSocket = null; } if (TCPBroadCastTcpListener != null) { TCPBroadCastTcpListener.Stop(); TCPBroadCastTcpListener = null; } }
public static void CloseServer(object sender, EventArgs e) { Output.WriteLine("Closing MainServer..."); if (TCPBroadCastTcpListener != null) { TCPBroadCastTcpListener.Stop(); TCPBroadCastTcpListener = null; } foreach (GameServerProcess gameServer in GameServerDict.Values) { gameServer.Kill(); } }
private static void TCPConnectAsyncCallback(IAsyncResult asyncResult) { TcpClient client = TCPBroadCastTcpListener.EndAcceptTcpClient(asyncResult); Output.WriteLine($"\n\tUser {client.Client.RemoteEndPoint} is trying to connect to the discovery server..."); TCPBeginReceiveDiscoveryClients(); byte discoveryClientCount = 255; while (true) { discoveryClientCount++; if (ClientDictionary.ContainsKey(discoveryClientCount)) { if (ClientDictionary[discoveryClientCount].TCPClient != null) { continue; } } else { ClientDictionary.Add(discoveryClientCount, new DiscoveryTCPClientServer(discoveryClientCount)); } break; } ClientDictionary[discoveryClientCount].Connect(client); //TODO: 9001 Make a seperate packet for this, that a server must hear for - like the InternetDiscoveryClientPackets listened for string serverName = Server.ServerName; int currentPlayerCount = Server.GetCurrentNumPlayers(); int maxPlayerCount = Server.MaxNumPlayers; string mapName = Server.MapName; //TODO: Actually get ping value for int ping = 10; ClientDictionary[discoveryClientCount].SendServerData(serverName, currentPlayerCount, maxPlayerCount, mapName, ping); Output.WriteLine($"\tSent Server Data packet to: {client.Client.RemoteEndPoint}"); }
private static void TCPConnectAsyncCallback(IAsyncResult asyncResult) { try { TcpClient client = TCPBroadCastTcpListener.EndAcceptTcpClient(asyncResult); Output.WriteLine($"\nUser {client.Client.RemoteEndPoint} is trying to connect to the discovery server..."); TCPBeginReceiveDiscoveryClients(); byte discoveryClientCount = SearchForDictSpace(ref ClientDictionary); ClientDictionary[discoveryClientCount].Connect(client); InternetDiscoveryTCPServerSend.SendWelcome(discoveryClientCount); string ip = client.Client.RemoteEndPoint.ToString().Split(':')[0].ToString(); Output.WriteLine($"Connected and sent welcome to new DiscoveryTCPClient: {client.Client.RemoteEndPoint}"); try { if (IPsConnected.NumNodes == 0) { IPsConnected.Add(ip); } if (!IPsConnected.Contains(ip)) { IPsConnected.Add(ip); } using (StreamWriter sw = new StreamWriter("IPs.txt", false)) { sw.WriteLine(IPsConnected.NumNodes); } } catch (Exception e) { Output.WriteLine($"Error saving num nodes...\n{e}"); } } catch (Exception e) { Output.WriteLine($"Error in TCPConnectAsyncCallback of InternetDiscoveryTCPServer...\n{e}"); } }
private static void TCPBeginReceiveDiscoveryClients() { TCPBroadCastTcpListener.BeginAcceptTcpClient(new AsyncCallback(TCPConnectAsyncCallback), null); }