添加 Ctrl+C 事件

This commit is contained in:
milimoe 2024-11-12 20:26:18 +08:00
parent d19d8f5e95
commit 3f27123c03
Signed by: milimoe
GPG Key ID: 05D280912DA6C69E
3 changed files with 68 additions and 4 deletions

View File

@ -16,6 +16,13 @@ HTTPListener? WebSocketListener = null;
StartServer(); StartServer();
Console.CancelKeyPress += (sender, e) =>
{
e.Cancel = true; // 防止程序立即退出
CloseServer();
Environment.Exit(0); // 退出程序
};
while (Running) while (Running)
{ {
string order = Console.ReadLine() ?? ""; string order = Console.ReadLine() ?? "";
@ -29,6 +36,7 @@ while (Running)
case OrderDictionary.Exit: case OrderDictionary.Exit:
case OrderDictionary.Close: case OrderDictionary.Close:
Running = false; Running = false;
CloseServer();
break; break;
case OrderDictionary.Restart: case OrderDictionary.Restart:
if (SocketListener is null || WebSocketListener is null) if (SocketListener is null || WebSocketListener is null)
@ -93,7 +101,7 @@ void StartServer()
ServerHelper.GetServerSettings(); ServerHelper.GetServerSettings();
Console.Title = Config.ServerName + " - FunGame Server Port: " + Config.ServerPort; Console.Title = Config.ServerName + " - FunGame Server Port: " + Config.ServerPort;
} }
ServerHelper.WriteLine("请输入 help 来获取帮助,输入 quit 关闭服务器。"); ServerHelper.WriteLine("请输入 help 来获取帮助,按下 Ctrl+C 关闭服务器。");
ServerHelper.PrintFunGameTitle(); ServerHelper.PrintFunGameTitle();
@ -261,3 +269,8 @@ void StartServer()
} }
}); });
} }
void CloseServer()
{
FunGameSystem.CloseServer();
}

View File

@ -64,10 +64,10 @@ namespace Milimoe.FunGame.Server.Others
/// </summary> /// </summary>
public static void InitMailSender() public static void InitMailSender()
{ {
MailSender? sender = null;
try try
{ {
sender = SmtpHelper.GetMailSender(); Factory.OpenFactory.RegisterFactory(SmtpHelper.GetMailSender);
MailSender? sender = SmtpHelper.GetMailSender();
if (sender != null) if (sender != null)
{ {
ServerHelper.WriteLine("SMTP 服务已启动!"); ServerHelper.WriteLine("SMTP 服务已启动!");
@ -84,6 +84,10 @@ namespace Milimoe.FunGame.Server.Others
} }
} }
/// <summary>
/// 加载游戏模组
/// </summary>
/// <returns></returns>
public static bool GetGameModuleList() public static bool GetGameModuleList()
{ {
List<string> supported = []; List<string> supported = [];
@ -131,6 +135,9 @@ namespace Milimoe.FunGame.Server.Others
return Config.GameModuleSupported.Length > 0; return Config.GameModuleSupported.Length > 0;
} }
/// <summary>
/// 加载服务器插件
/// </summary>
public static void GetServerPlugins() public static void GetServerPlugins()
{ {
Dictionary<string, object> delegates = []; Dictionary<string, object> delegates = [];
@ -151,6 +158,9 @@ namespace Milimoe.FunGame.Server.Others
} }
} }
/// <summary>
/// 加载 Web API 插件
/// </summary>
public static void GetWebAPIPlugins() public static void GetWebAPIPlugins()
{ {
Dictionary<string, object> delegates = []; Dictionary<string, object> delegates = [];
@ -187,6 +197,10 @@ namespace Milimoe.FunGame.Server.Others
sqlHelper.Execute(RoomQuery.Delete_Rooms()); sqlHelper.Execute(RoomQuery.Delete_Rooms());
} }
/// <summary>
/// 创建 SQL 服务后需要做的事
/// </summary>
/// <param name="sqlHelper"></param>
public static void AfterCreateSQLService(SQLHelper sqlHelper) public static void AfterCreateSQLService(SQLHelper sqlHelper)
{ {
Config.SQLMode = sqlHelper.Mode; Config.SQLMode = sqlHelper.Mode;
@ -194,5 +208,33 @@ namespace Milimoe.FunGame.Server.Others
ClearRoomList(sqlHelper); ClearRoomList(sqlHelper);
sqlHelper.Dispose(); sqlHelper.Dispose();
} }
/// <summary>
/// 关闭服务器要做的事
/// </summary>
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();
}
}
}
} }
} }

View File

@ -68,7 +68,7 @@ try
RESTfulAPIListener apiListener = new(); RESTfulAPIListener apiListener = new();
RESTfulAPIListener.Instance = apiListener; RESTfulAPIListener.Instance = apiListener;
ServerHelper.WriteLine("请输入 help 来获取帮助,输入 quit 关闭服务器。"); ServerHelper.WriteLine("请输入 help 来获取帮助,按下 Ctrl+C 关闭服务器。");
ServerHelper.PrintFunGameTitle(); ServerHelper.PrintFunGameTitle();
@ -210,6 +210,10 @@ try
}); });
}); });
// 捕捉关闭程序事件
IHostApplicationLifetime lifetime = app.Services.GetRequiredService<IHostApplicationLifetime>();
lifetime.ApplicationStopping.Register(CloseServer);
// 启用 WebSockets 中间件 // 启用 WebSockets 中间件
WebSocketOptions webSocketOptions = new() WebSocketOptions webSocketOptions = new()
{ {
@ -299,3 +303,8 @@ async Task WebSocketConnectionHandler(HttpContext context)
ServerHelper.Error(e); ServerHelper.Error(e);
} }
} }
void CloseServer()
{
FunGameSystem.CloseServer();
}