From 3f27123c03647e4ab55bcbd998953be736001301 Mon Sep 17 00:00:00 2001 From: milimoe Date: Tue, 12 Nov 2024 20:26:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20Ctrl+C=20=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FunGame.Server/Main.cs | 15 ++++++++- FunGame.Server/Others/FunGameSystem.cs | 46 ++++++++++++++++++++++++-- FunGame.WebAPI/Program.cs | 11 +++++- 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/FunGame.Server/Main.cs b/FunGame.Server/Main.cs index 1c7582c..91e6305 100644 --- a/FunGame.Server/Main.cs +++ b/FunGame.Server/Main.cs @@ -16,6 +16,13 @@ HTTPListener? WebSocketListener = null; StartServer(); +Console.CancelKeyPress += (sender, e) => +{ + e.Cancel = true; // 防止程序立即退出 + CloseServer(); + Environment.Exit(0); // 退出程序 +}; + while (Running) { string order = Console.ReadLine() ?? ""; @@ -29,6 +36,7 @@ while (Running) case OrderDictionary.Exit: case OrderDictionary.Close: Running = false; + CloseServer(); break; case OrderDictionary.Restart: if (SocketListener is null || WebSocketListener is null) @@ -93,7 +101,7 @@ void StartServer() ServerHelper.GetServerSettings(); Console.Title = Config.ServerName + " - FunGame Server Port: " + Config.ServerPort; } - ServerHelper.WriteLine("请输入 help 来获取帮助,输入 quit 关闭服务器。"); + ServerHelper.WriteLine("请输入 help 来获取帮助,按下 Ctrl+C 关闭服务器。"); ServerHelper.PrintFunGameTitle(); @@ -261,3 +269,8 @@ void StartServer() } }); } + +void CloseServer() +{ + FunGameSystem.CloseServer(); +} diff --git a/FunGame.Server/Others/FunGameSystem.cs b/FunGame.Server/Others/FunGameSystem.cs index ed4040e..6c2dd77 100644 --- a/FunGame.Server/Others/FunGameSystem.cs +++ b/FunGame.Server/Others/FunGameSystem.cs @@ -64,10 +64,10 @@ namespace Milimoe.FunGame.Server.Others /// public static void InitMailSender() { - MailSender? sender = null; try { - sender = SmtpHelper.GetMailSender(); + Factory.OpenFactory.RegisterFactory(SmtpHelper.GetMailSender); + MailSender? sender = SmtpHelper.GetMailSender(); if (sender != null) { ServerHelper.WriteLine("SMTP 服务已启动!"); @@ -84,6 +84,10 @@ namespace Milimoe.FunGame.Server.Others } } + /// + /// 加载游戏模组 + /// + /// public static bool GetGameModuleList() { List supported = []; @@ -131,6 +135,9 @@ namespace Milimoe.FunGame.Server.Others return Config.GameModuleSupported.Length > 0; } + /// + /// 加载服务器插件 + /// public static void GetServerPlugins() { Dictionary delegates = []; @@ -151,6 +158,9 @@ namespace Milimoe.FunGame.Server.Others } } + /// + /// 加载 Web API 插件 + /// public static void GetWebAPIPlugins() { Dictionary delegates = []; @@ -187,6 +197,10 @@ namespace Milimoe.FunGame.Server.Others sqlHelper.Execute(RoomQuery.Delete_Rooms()); } + /// + /// 创建 SQL 服务后需要做的事 + /// + /// public static void AfterCreateSQLService(SQLHelper sqlHelper) { Config.SQLMode = sqlHelper.Mode; @@ -194,5 +208,33 @@ namespace Milimoe.FunGame.Server.Others ClearRoomList(sqlHelper); sqlHelper.Dispose(); } + + /// + /// 关闭服务器要做的事 + /// + public static void CloseServer() + { + if (Config.GameModuleLoader != null) + { + foreach (GameModuleServer server in Config.GameModuleLoader.ModuleServers.Values) + { + server.Controller.Close(); + } + } + if (Config.ServerPluginLoader != null) + { + foreach (ServerPlugin plugin in Config.ServerPluginLoader.Plugins.Values) + { + plugin.Controller.Close(); + } + } + if (Config.WebAPIPluginLoader != null) + { + foreach (WebAPIPlugin plugin in Config.WebAPIPluginLoader.Plugins.Values) + { + plugin.Controller.Close(); + } + } + } } } diff --git a/FunGame.WebAPI/Program.cs b/FunGame.WebAPI/Program.cs index 9ae6c34..428f38d 100644 --- a/FunGame.WebAPI/Program.cs +++ b/FunGame.WebAPI/Program.cs @@ -68,7 +68,7 @@ try RESTfulAPIListener apiListener = new(); RESTfulAPIListener.Instance = apiListener; - ServerHelper.WriteLine(" help ȡ quit رշ"); + ServerHelper.WriteLine(" help ȡ Ctrl+C رշ"); ServerHelper.PrintFunGameTitle(); @@ -210,6 +210,10 @@ try }); }); + // ׽رճ¼ + IHostApplicationLifetime lifetime = app.Services.GetRequiredService(); + lifetime.ApplicationStopping.Register(CloseServer); + // WebSockets м WebSocketOptions webSocketOptions = new() { @@ -299,3 +303,8 @@ async Task WebSocketConnectionHandler(HttpContext context) ServerHelper.Error(e); } } + +void CloseServer() +{ + FunGameSystem.CloseServer(); +}