Search
Duplicate
🌉

dotnet Core Game Server

설치한 패키지

Windows Service 설정
Windows Service를 사용하기 위해서는 System.ServiceProcess.ServiceController 설치 필요.
추가 → 새항목 → Windows Service
아래 코드 추가
static void Main(string[] args) { //If this application run in Mono/Linux, change the control script to be executable if ( Path.DirectorySeparatorChar == '/') ChangeScriptExecutable(); var servicesToRun = new ServiceBase[] { new ServiceGame() }; if (Environment.UserInteractive) { RunInteractiveServices(servicesToRun); } else { ServiceBase.Run(servicesToRun); } }
C#
복사
static void RunInteractiveServices(ServiceBase[] servicesToRun) { //LogSystem.Instance.RegistLog($"Start the services in interactive mode.", SYSTEM_LOG_LEVEL.MONITORING); // Get the method to invoke on each service to start it var onStartMethod = typeof(ServiceBase).GetMethod("OnStart", BindingFlags.Instance | BindingFlags.NonPublic); // Start services loop foreach (var service in servicesToRun) { //LogSystem.Instance.RegistLog($"Starting {service.ServiceName} ... ", SYSTEM_LOG_LEVEL.MONITORING); if (onStartMethod != null) onStartMethod.Invoke(service, new object[] {new string[] { }}); //LogSystem.Instance.RegistLog($"Started", SYSTEM_LOG_LEVEL.MONITORING); } // Waiting the end Console.WriteLine("Press 'q' key to stop services et finish process..."); var keyInfo = Console.ReadKey(); while (keyInfo.Key != ConsoleKey.Q) keyInfo = Console.ReadKey(); Console.WriteLine(); // Get the method to invoke on each service to stop it var onStopMethod = typeof(ServiceBase).GetMethod("OnStop", BindingFlags.Instance | BindingFlags.NonPublic); // Stop loop foreach (var service in servicesToRun) { //LogSystem.Instance.RegistLog("Stopping {service.ServiceName} ... "); if (onStopMethod != null) onStopMethod.Invoke(service, null); //LogSystem.Instance.RegistLog("Stopped"); } //LogSystem.Instance.RegistLog("All services are stopped."); // Waiting a key press to not return to VS directly if (!System.Diagnostics.Debugger.IsAttached) return; Console.WriteLine("=== Press a key to quit ==="); Console.ReadKey(); }
C#
복사
private static void ChangeScriptExecutable() { var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "supersocket.sh"); try { if (!File.Exists(filePath)) return; File.SetAttributes(filePath, (FileAttributes)((uint)File.GetAttributes(filePath) | 0x80000000)); } catch { } }
C#
복사