private static void ListenForClients() { try { while (true) { TcpClient tcpClient = clientListener.AcceptTcpClient(); GameserverClient client = new GameserverClient(tcpClient); ConnectedClients.Add(client); if (ConnectedClients.Count == 1) { stopwatch.Start(); } Console.WriteLine($"Client connected! from {tcpClient.Client.RemoteEndPoint.ToString()}"); } } catch (Exception e) { Console.WriteLine(e.Message); } }
public static void SendRegisterRequest() { TcpClient tcpClient = new TcpClient(_serverManagerIP, _serverManagerPort); ConnectedServerManager = new GameserverClient(tcpClient); Console.WriteLine("Getting game ID.."); Process p = new Process(); p.StartInfo = new ProcessStartInfo() { FileName = "printenv", Arguments = "SESSION_NAME", UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true }; string gameIdentifier = ""; // Read result from process p.Start(); while (!p.StandardOutput.EndOfStream) { gameIdentifier = p.StandardOutput.ReadLine(); } p.WaitForExit(); Console.WriteLine("GameID: " + gameIdentifier); Console.WriteLine("\n"); Console.WriteLine("Creating game instance.."); Instance = new GameserverInstance() { GameserverID = "", // Read from kubernetes pod Port = ((IPEndPoint)clientListener.LocalEndpoint).Port, IP = "212.10.51.254" }; if (gameIdentifier.Contains("game-")) { Instance.GameserverID = gameIdentifier; } else { Instance.GameserverID = "Default"; } Console.WriteLine("Instance created"); Console.WriteLine($"ServerID {Instance.GameserverID}"); Console.WriteLine($"Server IP {Instance.IP}"); Console.WriteLine($"Server Port {Instance.Port}"); Console.WriteLine("\n"); string JSON = JsonConvert.SerializeObject(Instance); Packet packet = new Packet((int)MessageType.Register); packet.Write(JSON); packet.WriteLength(); Console.WriteLine($"Sending game instance to server manager, to be configured.."); tcpClient.GetStream().BeginWrite(packet.ToArray(), 0, packet.Length(), null, null); }