From 5aa9439f2c90671fdd67a851802967c77251c5e9 Mon Sep 17 00:00:00 2001 From: milimoe <110188673+milimoe@users.noreply.github.com> Date: Mon, 24 Apr 2023 01:40:38 +0800 Subject: [PATCH] Update SocketHandler (#8) --- FunGame.Desktop/Controller/MainController.cs | 2 + .../Controller/RunTimeController.cs | 21 +- FunGame.Desktop/Library/Config/Config.cs | 7 - FunGame.Desktop/Library/Config/RunTime.cs | 6 +- FunGame.Desktop/Library/Config/Usercfg.cs | 15 -- FunGame.Desktop/Model/LoginModel.cs | 2 +- FunGame.Desktop/Model/MainModel.cs | 15 +- FunGame.Desktop/Model/RunTimeModel.cs | 180 ++++-------------- FunGame.Desktop/UI/Main/Main.cs | 26 +-- 9 files changed, 75 insertions(+), 199 deletions(-) delete mode 100644 FunGame.Desktop/Library/Config/Usercfg.cs diff --git a/FunGame.Desktop/Controller/MainController.cs b/FunGame.Desktop/Controller/MainController.cs index b20f32d..021d18d 100644 --- a/FunGame.Desktop/Controller/MainController.cs +++ b/FunGame.Desktop/Controller/MainController.cs @@ -14,6 +14,8 @@ namespace Milimoe.FunGame.Desktop.Controller private MainModel MainModel { get; } private Main Main { get; } + private readonly Core.Model.Session Usercfg = RunTime.Session; + public MainController(Main Main) { this.Main = Main; diff --git a/FunGame.Desktop/Controller/RunTimeController.cs b/FunGame.Desktop/Controller/RunTimeController.cs index 23cf64c..9dc8880 100644 --- a/FunGame.Desktop/Controller/RunTimeController.cs +++ b/FunGame.Desktop/Controller/RunTimeController.cs @@ -7,9 +7,9 @@ using Milimoe.FunGame.Desktop.UI; namespace Milimoe.FunGame.Desktop.Controller { - public class RunTimeController + public class RunTimeController : Core.Controller.RunTimeController { - public bool Connected => RunTimeModel.Connected; + public override bool Connected => RunTimeModel.Connected; private RunTimeModel RunTimeModel { get; } private Main Main { get; } @@ -20,7 +20,7 @@ namespace Milimoe.FunGame.Desktop.Controller RunTimeModel = new RunTimeModel(Main); } - public async Task GetServerConnection() + public override async Task GetServerConnection() { bool result = false; @@ -37,7 +37,7 @@ namespace Milimoe.FunGame.Desktop.Controller return result; } - public async Task Connect() + public override async Task Connect() { ConnectResult result = ConnectResult.ConnectFailed; @@ -60,7 +60,7 @@ namespace Milimoe.FunGame.Desktop.Controller return result; } - public bool Disconnect() + public override bool Disconnect() { bool result = false; @@ -82,7 +82,7 @@ namespace Milimoe.FunGame.Desktop.Controller return result; } - public bool Close(Exception? e = null) + public override bool Close(Exception? e = null) { bool result; @@ -98,12 +98,12 @@ namespace Milimoe.FunGame.Desktop.Controller return result; } - public bool Error(Exception e) + public override bool Error(Exception e) { return Close(e); } - public async Task AutoLogin(string Username, string Password, string AutoKey) + public override async Task AutoLogin(string Username, string Password, string AutoKey) { try { @@ -116,5 +116,10 @@ namespace Milimoe.FunGame.Desktop.Controller Main.GetMessage(e.GetErrorInfo(), TimeType.None); } } + + public override void WritelnSystemInfo(string msg) + { + Main?.GetMessage(msg); + } } } diff --git a/FunGame.Desktop/Library/Config/Config.cs b/FunGame.Desktop/Library/Config/Config.cs index 18899a5..bbdad6e 100644 --- a/FunGame.Desktop/Library/Config/Config.cs +++ b/FunGame.Desktop/Library/Config/Config.cs @@ -2,9 +2,6 @@ { public class Config { - /** - * FunGame Desktop Configs - */ public static bool FunGame_isAutoConnect { get; set; } = true; // 是否自动连接服务器 public static bool FunGame_isAutoLogin { get; set; } = false; // 是否自动登录 public static bool FunGame_isMatching { get; set; } = false; // 是否在匹配中 @@ -19,9 +16,5 @@ public static string FunGame_AutoLoginUser { get; set; } = ""; // 自动登录的账号 public static string FunGame_AutoLoginPassword { get; set; } = ""; // 自动登录的密码 public static string FunGame_AutoLoginKey { get; set; } = ""; // 自动登录的秘钥 - - /*** GUID For Socket ***/ - public static Guid Guid_Socket { get; set; } = Guid.Empty; - public static Guid Guid_LoginKey { get; set; } = Guid.Empty; } } diff --git a/FunGame.Desktop/Library/Config/RunTime.cs b/FunGame.Desktop/Library/Config/RunTime.cs index b3998f1..1bafa7e 100644 --- a/FunGame.Desktop/Library/Config/RunTime.cs +++ b/FunGame.Desktop/Library/Config/RunTime.cs @@ -6,8 +6,10 @@ /// public class RunTime { + public static Core.Model.RoomList RoomList { get; } = new(); + public static Core.Model.Session Session { get; } = new(); public static Core.Library.Common.Network.Socket? Socket { get; set; } = null; - public static Controller.RunTimeController? Connector { get; set; } = null; + public static Controller.RunTimeController? Controller { get; set; } = null; public static UI.Main? Main { get; set; } = null; public static UI.Login? Login { get; set; } = null; public static UI.Register? Register { get; set; } = null; @@ -18,7 +20,7 @@ public static void WritelnSystemInfo(string msg) { - Main?.GetMessage(msg); + Controller?.WritelnSystemInfo(msg); } } } diff --git a/FunGame.Desktop/Library/Config/Usercfg.cs b/FunGame.Desktop/Library/Config/Usercfg.cs deleted file mode 100644 index ad09bf7..0000000 --- a/FunGame.Desktop/Library/Config/Usercfg.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Milimoe.FunGame.Core.Entity; -using Milimoe.FunGame.Core.Library.Constant; - -namespace Milimoe.FunGame.Desktop.Library -{ - public class Usercfg - { - /** - * 玩家设定内容 - */ - public static User LoginUser { get; set; } = General.UnknownUserInstance; // 已登录的用户 - public static string LoginUserName { get; set; } = ""; // 已登录用户名 - public static Room InRoom { get; set; } = General.HallInstance; // 所处的房间 - } -} diff --git a/FunGame.Desktop/Model/LoginModel.cs b/FunGame.Desktop/Model/LoginModel.cs index 7c10681..1e35846 100644 --- a/FunGame.Desktop/Model/LoginModel.cs +++ b/FunGame.Desktop/Model/LoginModel.cs @@ -99,7 +99,7 @@ namespace Milimoe.FunGame.Desktop.Model if (Work.Length > 1) msg = Work.GetParam(1); if (key != Guid.Empty) { - Config.Guid_LoginKey = key; + RunTime.Session.LoginKey = key; } } catch (Exception e) diff --git a/FunGame.Desktop/Model/MainModel.cs b/FunGame.Desktop/Model/MainModel.cs index 08b0794..72026e9 100644 --- a/FunGame.Desktop/Model/MainModel.cs +++ b/FunGame.Desktop/Model/MainModel.cs @@ -14,6 +14,7 @@ namespace Milimoe.FunGame.Desktop.Model public class MainModel : BaseModel { private readonly Main Main; + private readonly Core.Model.Session Usercfg = RunTime.Session; public MainModel(Main main) : base(RunTime.Socket) { @@ -27,17 +28,17 @@ namespace Milimoe.FunGame.Desktop.Model try { // 需要当时登录给的Key发回去,确定是账号本人在操作才允许登出 - if (Config.Guid_LoginKey != Guid.Empty) + if (Usercfg.LoginKey != Guid.Empty) { SetWorking(); - if (RunTime.Socket?.Send(SocketMessageType.Logout, Config.Guid_LoginKey) == SocketResult.Success) + if (RunTime.Socket?.Send(SocketMessageType.Logout, Usercfg.LoginKey) == SocketResult.Success) { string msg = ""; Guid key = Guid.Empty; (msg, key) = await Task.Factory.StartNew(SocketHandler_LogOut); - if (key == Config.Guid_LoginKey) + if (key == Usercfg.LoginKey) { - Config.Guid_LoginKey = Guid.Empty; + Usercfg.LoginKey = Guid.Empty; Main.UpdateUI(MainInvokeType.LogOut, msg ?? ""); return true; } @@ -202,9 +203,9 @@ namespace Milimoe.FunGame.Desktop.Model if (SocketObject.Length > 0) key = SocketObject.GetParam(0); if (SocketObject.Length > 1) msg = SocketObject.GetParam(1); msg ??= ""; - if (key == Config.Guid_LoginKey) + if (key == Usercfg.LoginKey) { - Config.Guid_LoginKey = Guid.Empty; + Usercfg.LoginKey = Guid.Empty; Main.UpdateUI(MainInvokeType.LogOut, msg ?? ""); } } @@ -236,7 +237,7 @@ namespace Milimoe.FunGame.Desktop.Model } catch (Exception e) { - RunTime.Connector?.Error(e); + RunTime.Controller?.Error(e); } } diff --git a/FunGame.Desktop/Model/RunTimeModel.cs b/FunGame.Desktop/Model/RunTimeModel.cs index fffdb5c..1b6a552 100644 --- a/FunGame.Desktop/Model/RunTimeModel.cs +++ b/FunGame.Desktop/Model/RunTimeModel.cs @@ -12,72 +12,19 @@ namespace Milimoe.FunGame.Desktop.Model /// /// 与创建关闭Socket相关的方法,使用此类 /// - public class RunTimeModel + public class RunTimeModel : Core.Model.RunTime { - public bool Connected => Socket != null && Socket.Connected; + public override Socket? Socket => _Socket; private readonly Main Main; - private Task? ReceivingTask; - private Socket? Socket; - private bool IsReceiving = false; + private readonly Core.Model.Session Usercfg = RunTime.Session; public RunTimeModel(Main main) { Main = main; } - #region 公开方法 - - public bool Disconnect() - { - bool result = false; - - try - { - result = Socket?.Send(SocketMessageType.Disconnect, "") == SocketResult.Success; - } - catch (Exception e) - { - Main.GetMessage(e.GetErrorInfo()); - } - - return result; - } - - public void Disconnected() - { - Disconnect(); - } - - public void GetServerConnection() - { - try - { - // 获取服务器IP - string? ipaddress = (string?)Implement.GetFunGameImplValue(InterfaceType.IClient, InterfaceMethod.RemoteServerIP); - if (ipaddress != null) - { - string[] s = ipaddress.Split(':'); - if (s != null && s.Length > 1) - { - Constant.Server_IP = s[0]; - Constant.Server_Port = Convert.ToInt32(s[1]); - } - } - else - { - ShowMessage.ErrorMessage("查找可用的服务器失败!"); - Config.FunGame_isRetrying = false; - throw new FindServerFailedException(); - } - } - catch (Exception e) - { - Main.GetMessage(e.GetErrorInfo(), TimeType.None); - } - } - - public async Task Connect() + public override async Task Connect() { if (Constant.Server_IP == "" || Constant.Server_Port <= 0) { @@ -105,7 +52,7 @@ namespace Milimoe.FunGame.Desktop.Model // 与服务器建立连接 Socket?.Close(); Config.FunGame_isRetrying = true; - Socket = Socket.Connect(Constant.Server_IP, Constant.Server_Port); + _Socket = Socket.Connect(Constant.Server_IP, Constant.Server_Port); if (Socket != null && Socket.Connected) { // 设置可复用Socket @@ -123,7 +70,7 @@ namespace Milimoe.FunGame.Desktop.Model { while (true) { - if (IsReceiving) + if (_IsReceiving) { break; } @@ -155,31 +102,7 @@ namespace Milimoe.FunGame.Desktop.Model } } - public bool Close() - { - try - { - if (Socket != null) - { - Socket.Close(); - Socket = null; - } - if (ReceivingTask != null && !ReceivingTask.IsCompleted) - { - ReceivingTask.Wait(1); - ReceivingTask = null; - IsReceiving = false; - } - } - catch (Exception e) - { - Main.GetMessage(e.GetErrorInfo(), TimeType.None); - return false; - } - return true; - } - - public void Error(Exception e) + public override void Error(Exception e) { Main.GetMessage(e.GetErrorInfo(), TimeType.None); Main.UpdateUI(MainInvokeType.Disconnected); @@ -187,76 +110,35 @@ namespace Milimoe.FunGame.Desktop.Model Close(); } - #endregion - - #region 私有方法 - - private void StartReceiving() + public override void GetServerConnection() { - ReceivingTask = Task.Factory.StartNew(() => - { - Thread.Sleep(100); - IsReceiving = true; - while (Socket != null && Socket.Connected) - { - Receiving(); - } - }); - Socket?.StartReceiving(ReceivingTask); - } - - private SocketObject GetServerMessage() - { - if (Socket != null && Socket.Connected) - { - return Socket.Receive(); - } - return new SocketObject(); - } - - private SocketMessageType Receiving() - { - if (Socket is null) return SocketMessageType.Unknown; - SocketMessageType result = SocketMessageType.Unknown; try { - SocketObject ServerMessage = GetServerMessage(); - SocketMessageType type = ServerMessage.SocketType; - object[] objs = ServerMessage.Parameters; - result = type; - switch (type) + // 获取服务器IP + string? ipaddress = (string?)Implement.GetFunGameImplValue(InterfaceType.IClient, InterfaceMethod.RemoteServerIP); + if (ipaddress != null) { - case SocketMessageType.Connect: - if (!SocketHandler_Connect(ServerMessage)) return SocketMessageType.Unknown; - break; - - case SocketMessageType.Disconnect: - SocketHandler_Disconnect(ServerMessage); - break; - - case SocketMessageType.HeartBeat: - if (Socket.Connected && Usercfg.LoginUser.Id != 0) - Main.UpdateUI(MainInvokeType.SetGreenAndPing); - break; - - case SocketMessageType.Unknown: - default: - break; + string[] s = ipaddress.Split(':'); + if (s != null && s.Length > 1) + { + Constant.Server_IP = s[0]; + Constant.Server_Port = Convert.ToInt32(s[1]); + } + } + else + { + ShowMessage.ErrorMessage("查找可用的服务器失败!"); + Config.FunGame_isRetrying = false; + throw new FindServerFailedException(); } } catch (Exception e) { - // 报错中断服务器连接 - Error(e); + Main.GetMessage(e.GetErrorInfo(), TimeType.None); } - return result; } - #endregion - - #region SocketHandler - - private bool SocketHandler_Connect(SocketObject ServerMessage) + protected override bool SocketHandler_Connect(SocketObject ServerMessage) { string msg = ""; Guid token = Guid.Empty; @@ -276,14 +158,14 @@ namespace Milimoe.FunGame.Desktop.Model Config.FunGame_Notice = ServerNotice; if (ServerMessage.Parameters.Length > 1) token = ServerMessage.GetParam(1); Socket!.Token = token; - Config.Guid_Socket = token; + Usercfg.SocketToken = token; Main.GetMessage($"已连接服务器:{ServerName}。\n\n********** 服务器公告 **********\n\n{ServerNotice}\n\n"); // 设置等待登录的黄灯 Main.UpdateUI(MainInvokeType.WaitLoginAndSetYellow); return true; } - private void SocketHandler_Disconnect(SocketObject ServerMessage) + protected override void SocketHandler_Disconnect(SocketObject ServerMessage) { string msg = ""; if (ServerMessage.Parameters.Length > 0) msg = ServerMessage.GetParam(0)!; @@ -294,6 +176,12 @@ namespace Milimoe.FunGame.Desktop.Model Main.OnAfterDisconnectEvent(new GeneralEventArgs()); } - #endregion + protected override void SocketHandler_HeartBeat(SocketObject ServerMessage) + { + if (Socket != null && Socket.Connected && Usercfg.LoginUser.Id != 0) + { + Main.UpdateUI(MainInvokeType.SetGreenAndPing); + } + } } } \ No newline at end of file diff --git a/FunGame.Desktop/UI/Main/Main.cs b/FunGame.Desktop/UI/Main/Main.cs index 857b676..0a0757d 100644 --- a/FunGame.Desktop/UI/Main/Main.cs +++ b/FunGame.Desktop/UI/Main/Main.cs @@ -1,7 +1,6 @@ using System.Diagnostics; using Milimoe.FunGame.Core.Api.Utility; using Milimoe.FunGame.Core.Entity; -using Milimoe.FunGame.Core.Library.Common.Architecture; using Milimoe.FunGame.Core.Library.Common.Event; using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Library.Exception; @@ -29,7 +28,8 @@ namespace Milimoe.FunGame.Desktop.UI */ private Task? MatchFunGame = null; // 匹配线程 private MainController? MainController = null; - private readonly RoomList Rooms = new(); + private readonly Core.Model.RoomList Rooms = RunTime.RoomList; + private readonly Core.Model.Session Usercfg = RunTime.Session; /** * 委托【即将删除】 @@ -53,7 +53,7 @@ namespace Milimoe.FunGame.Desktop.UI ShowFunGameInfo(); // 显示FunGame信息 GetFunGameConfig(); // 获取FunGame配置 // 创建RunTime - RunTime.Connector = new RunTimeController(this); + RunTime.Controller = new RunTimeController(this); // 窗口句柄创建后,进行委托 await Task.Factory.StartNew(() => { @@ -64,7 +64,7 @@ namespace Milimoe.FunGame.Desktop.UI break; } } - if (Config.FunGame_isAutoConnect) RunTime.Connector?.GetServerConnection(); + if (Config.FunGame_isAutoConnect) RunTime.Controller?.GetServerConnection(); }); } @@ -130,7 +130,7 @@ namespace Milimoe.FunGame.Desktop.UI if (MainController != null && Config.FunGame_isAutoConnect) { // 自动连接服务器 - RunTime.Connector?.Connect(); + RunTime.Controller?.Connect(); } break; @@ -858,7 +858,7 @@ namespace Milimoe.FunGame.Desktop.UI if (ShowMessage.OKCancelMessage("你确定关闭游戏?", "退出") == (int)MessageResult.OK) { if (MainController != null) await MainController.LogOut(); - RunTime.Connector?.Close(); + RunTime.Controller?.Close(); Environment.Exit(0); } } @@ -1237,7 +1237,7 @@ namespace Milimoe.FunGame.Desktop.UI Task.Run(() => { Thread.Sleep(5000); - if (Config.FunGame_isConnected && Config.FunGame_isAutoRetry) RunTime.Connector?.Connect(); // 再次判断是否开启自动重连 + if (Config.FunGame_isConnected && Config.FunGame_isAutoRetry) RunTime.Controller?.Connect(); // 再次判断是否开启自动重连 }); GetMessage("连接服务器失败,5秒后自动尝试重连。"); } @@ -1258,7 +1258,7 @@ namespace Milimoe.FunGame.Desktop.UI if (MainController != null && Config.FunGame_isAutoLogin && Config.FunGame_AutoLoginUser != "" && Config.FunGame_AutoLoginPassword != "" && Config.FunGame_AutoLoginKey != "") { // 自动登录 - _ = RunTime.Connector?.AutoLogin(Config.FunGame_AutoLoginUser, Config.FunGame_AutoLoginPassword, Config.FunGame_AutoLoginKey); + _ = RunTime.Controller?.AutoLogin(Config.FunGame_AutoLoginUser, Config.FunGame_AutoLoginPassword, Config.FunGame_AutoLoginKey); } return EventResult.Success; } @@ -1337,7 +1337,7 @@ namespace Milimoe.FunGame.Desktop.UI { CurrentRetryTimes = -1; Config.FunGame_isAutoRetry = true; - RunTime.Connector?.Connect(); + RunTime.Controller?.Connect(); } else WritelnGameInfo(">> 你不能在连接服务器的同时重试连接!"); @@ -1347,20 +1347,20 @@ namespace Milimoe.FunGame.Desktop.UI { CurrentRetryTimes = -1; Config.FunGame_isAutoRetry = true; - RunTime.Connector?.GetServerConnection(); + RunTime.Controller?.GetServerConnection(); } break; case Constant.FunGame_Disconnect: if (Config.FunGame_isConnected && MainController != null) { // 先退出登录再断开连接 - if (MainController != null && await MainController.LogOut()) RunTime.Connector?.Disconnect(); + if (MainController != null && await MainController.LogOut()) RunTime.Controller?.Disconnect(); } break; case Constant.FunGame_DisconnectWhenNotLogin: if (Config.FunGame_isConnected && MainController != null) { - RunTime.Connector?.Disconnect(); + RunTime.Controller?.Disconnect(); } break; case Constant.FunGame_ConnectTo: @@ -1391,7 +1391,7 @@ namespace Milimoe.FunGame.Desktop.UI Constant.Server_Port = port; CurrentRetryTimes = -1; Config.FunGame_isAutoRetry = true; - RunTime.Connector?.Connect(); + RunTime.Controller?.Connect(); } else if (ErrorType == Core.Library.Constant.ErrorType.IsNotIP) ShowMessage.ErrorMessage("这不是一个IP地址!"); else if (ErrorType == Core.Library.Constant.ErrorType.IsNotPort) ShowMessage.ErrorMessage("这不是一个端口号!\n正确范围:1~65535");