public void StopGameServer(bool status) { if (_serverProcess == null) { return; } serverCrashed = status; _rconObj.SendCommand("disconnect"); // Drop everyone from the server. Thread.Sleep(100); if (_rconObj.SendCommand("quit") == null) // Run a proper clean quit. { TerminateProgram(_serverProcess); // Couldn't quit safely, kill process. } _serverProcess = null; Thread.Sleep(100); }
private static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.Red; // Locate the 'startup' script: if (!LoadStartupScript()) { Console.WriteLine("Unable to load server_config.txt, file does not exist or is invalid!\nPress any key to exit..."); Console.ReadKey(); return; } _address = Utils.GetLocalIPAddress(); if (string.IsNullOrEmpty(_address)) { Console.WriteLine("Press any key to exit..."); Console.ReadKey(); return; } if (_data.ContainsKey("port")) { _port = _data["port"]; } _rconPassword = Utils.GetRandomString(12); _shouldShutdown = false; SimpleRCON rconBase = new SimpleRCON(_address, _port, _rconPassword, _data.ContainsKey("hlds")); Console.WriteLine(string.Format("SRCDS monitor has been initialized for {0}, using RCON password {1}:\n", _data["game"], _rconPassword)); Console.WriteLine("Commands:\nRestart\nReloadscript\nShowdata\nQuit\n\nAnything else will be written directly to the server via RCON.\n"); Console.ForegroundColor = ConsoleColor.White; Thread.Sleep(300); _gameServerThread = new GameServer(_address, _port, _rconPassword, _data); Thread gameServerMonitorThread = new Thread(new ThreadStart(_gameServerThread.MonitorGameServer)); gameServerMonitorThread.Start(); _exitHandler = new EventHandler(HandleExit); SetConsoleCtrlHandler(_exitHandler, true); while (true) { string line = Console.ReadLine(); string cmd = line.ToLower(); if (cmd.Equals("quit")) { _shouldShutdown = true; Console.WriteLine("Quitting, server shutting down!"); Thread.Sleep(250); break; } else if (cmd.Equals("restart")) { _gameServerThread.StopGameServer(false); Thread.Sleep(250); } else if (cmd.Equals("showdata")) { foreach (KeyValuePair <string, string> pair in _data.ToArray()) { Console.WriteLine(string.Format("{0} : {1}", pair.Key, pair.Value)); } Console.WriteLine("Command Line:"); foreach (string s in _commandLineOptions) { Console.WriteLine(s); } Console.WriteLine("Passed to command line when restarting from a crash:"); foreach (string s in _crashedCommandLineOptions) { Console.WriteLine(s); } } else if (cmd.Equals("reloadscript")) { _data.Clear(); _commandLineOptions.Clear(); _crashedCommandLineOptions.Clear(); if (!LoadStartupScript()) { _shouldShutdown = true; Console.WriteLine("Unable to load server_config.txt, file does not exist or is invalid!\nPress any key to exit..."); Thread.Sleep(250); Console.ReadKey(); break; } Console.WriteLine("Reloaded server_config.txt successfully, you have to restart the server in order for the changes to take effect!"); _gameServerThread.SetData(_data); } else // Send via source RCON. { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(rconBase.SendCommand(line)); Console.ForegroundColor = ConsoleColor.White; } } rconBase = null; Shutdown(); }