diff --git a/FunGame.Desktop/Controller/RunTimeController.cs b/FunGame.Desktop/Controller/RunTimeController.cs index a732ac8..136578c 100644 --- a/FunGame.Desktop/Controller/RunTimeController.cs +++ b/FunGame.Desktop/Controller/RunTimeController.cs @@ -9,7 +9,7 @@ namespace Milimoe.FunGame.Desktop.Controller { public class RunTimeController { - public bool Connected => Do(RunTimeInvokeType.Connected); + public bool Connected => RunTimeModel.Connected; private RunTimeModel RunTimeModel { get; } private Main Main { get; } @@ -20,100 +20,100 @@ namespace Milimoe.FunGame.Desktop.Controller RunTimeModel = new RunTimeModel(Main); } - /** - * 从内部去调用Model的方法,并记录日志。 - */ - private T Do(RunTimeInvokeType DoType, params object[] args) + public async Task GetServerConnection() { - object result = new(); - switch (DoType) + bool result = false; + + try { - case RunTimeInvokeType.GetServerConnection: - result = RunTimeModel.GetServerConnection(); - break; - - case RunTimeInvokeType.Connect: - result = RunTimeModel.Connect(); - if ((ConnectResult)result != ConnectResult.Success) - { - Main.OnFailedConnectEvent(new GeneralEventArgs()); - Main.OnAfterConnectEvent(new GeneralEventArgs()); - } - break; - - case RunTimeInvokeType.Connected: - result = RunTimeModel.Connected; - break; - - case RunTimeInvokeType.Disconnect: - if (Main.OnBeforeDisconnectEvent(new GeneralEventArgs()) == EventResult.Fail) return (T)result; - RunTimeModel.Disconnect(); - break; - - case RunTimeInvokeType.Disconnected: - break; - - case RunTimeInvokeType.AutoLogin: - break; - - case RunTimeInvokeType.Close: - if (args != null && args.Length > 0) - { - RunTimeModel.Error((Exception)args[0]); - result = true; - } - else - result = RunTimeModel.Close(); - break; - - default: - break; + RunTimeModel.GetServerConnection(); + result = await Connect() == ConnectResult.Success; } - return (T)result; + catch (Exception e) + { + Main.GetMessage(e.GetErrorInfo(), TimeType.None); + } + + return result; } - public bool GetServerConnection() + public async Task Connect() { - return Do(RunTimeInvokeType.GetServerConnection); + ConnectResult result = ConnectResult.ConnectFailed; + + try + { + ConnectEventArgs EventArgs = new(Constant.Server_IP, Constant.Server_Port); + if (Main.OnBeforeConnectEvent(EventArgs) == EventResult.Fail) return ConnectResult.ConnectFailed; + + result = await RunTimeModel.Connect(); + + if (result == ConnectResult.Success) Main.OnSucceedConnectEvent(EventArgs); + else Main.OnFailedConnectEvent(EventArgs); + Main.OnAfterConnectEvent(EventArgs); + } + catch (Exception e) + { + Main.GetMessage(e.GetErrorInfo(), TimeType.None); + } + + return result; } - public ConnectResult Connect() + public bool Disconnect() { - return Do(RunTimeInvokeType.Connect); + bool result = false; + + try + { + if (Main.OnBeforeDisconnectEvent(new GeneralEventArgs()) == EventResult.Fail) return result; + + result = RunTimeModel.Disconnect(); + + if (result) Main.OnSucceedDisconnectEvent(new GeneralEventArgs()); + else Main.OnFailedDisconnectEvent(new GeneralEventArgs()); + Main.OnAfterDisconnectEvent(new GeneralEventArgs()); + } + catch (Exception e) + { + Main.GetMessage(e.GetErrorInfo(), TimeType.None); + } + + return result; } - public void Disconnect() + public bool Close(params object[] args) { - Do(RunTimeInvokeType.Disconnect); - } + bool result; - public void Disconnected() - { - Do(RunTimeInvokeType.Disconnected); - } + if (Connected) Disconnect(); - public bool Close() - { - return Do(RunTimeInvokeType.Close); + if (args != null && args.Length > 0) + { + RunTimeModel.Error((Exception)args[0]); + result = true; + } + else result = RunTimeModel.Close(); + + return result; } public bool Error(Exception e) { - return Do(RunTimeInvokeType.Close, e); + return Close(e); } public async Task AutoLogin(params object[] objs) { try { - Do(RunTimeInvokeType.AutoLogin); LoginController LoginController = new(); await LoginController.LoginAccount(objs); LoginController.Dispose(); } catch (Exception e) { - Main.GetMessage(e.GetErrorInfo()); + Main.GetMessage(e.GetErrorInfo(), TimeType.None); } } } diff --git a/FunGame.Desktop/Library/Base/BaseMain.cs b/FunGame.Desktop/Library/Base/BaseMain.cs index 5089a9b..82fd39c 100644 --- a/FunGame.Desktop/Library/Base/BaseMain.cs +++ b/FunGame.Desktop/Library/Base/BaseMain.cs @@ -8,12 +8,12 @@ namespace Milimoe.FunGame.Desktop.Library.Base public class BaseMain : GeneralForm, IConnectEventHandler, IDisconnectEventHandler, ILoginEventHandler, ILogoutEventHandler, IIntoRoomEventHandler, ISendTalkEventHandler, ICreateRoomEventHandler, IQuitRoomEventHandler, IStartMatchEventHandler, IStartGameEventHandler, IOpenInventoryEventHandler, IOpenStoreEventHandler { - public event IEventHandler.BeforeEventHandler? BeforeConnect; - public event IEventHandler.AfterEventHandler? AfterConnect; - public event IEventHandler.SucceedEventHandler? SucceedConnect; - public event IEventHandler.FailedEventHandler? FailedConnect; + public event IConnectEventHandler.BeforeEventHandler? BeforeConnect; + public event IConnectEventHandler.AfterEventHandler? AfterConnect; + public event IConnectEventHandler.SucceedEventHandler? SucceedConnect; + public event IConnectEventHandler.FailedEventHandler? FailedConnect; - public EventResult OnAfterConnectEvent(GeneralEventArgs e) + public EventResult OnAfterConnectEvent(ConnectEventArgs e) { if (AfterConnect != null) { @@ -22,7 +22,7 @@ namespace Milimoe.FunGame.Desktop.Library.Base else return EventResult.NoEventImplement; } - public EventResult OnBeforeConnectEvent(GeneralEventArgs e) + public EventResult OnBeforeConnectEvent(ConnectEventArgs e) { if (BeforeConnect != null) { @@ -31,7 +31,7 @@ namespace Milimoe.FunGame.Desktop.Library.Base else return EventResult.NoEventImplement; } - public EventResult OnSucceedConnectEvent(GeneralEventArgs e) + public EventResult OnSucceedConnectEvent(ConnectEventArgs e) { if (SucceedConnect != null) { @@ -40,7 +40,7 @@ namespace Milimoe.FunGame.Desktop.Library.Base else return EventResult.NoEventImplement; } - public EventResult OnFailedConnectEvent(GeneralEventArgs e) + public EventResult OnFailedConnectEvent(ConnectEventArgs e) { if (FailedConnect != null) { diff --git a/FunGame.Desktop/Library/Config/Constant.cs b/FunGame.Desktop/Library/Config/Constant.cs index 5256c31..cd84eac 100644 --- a/FunGame.Desktop/Library/Config/Constant.cs +++ b/FunGame.Desktop/Library/Config/Constant.cs @@ -13,7 +13,7 @@ namespace Milimoe.FunGame.Desktop.Library /** * Socket Configs */ - public static string Server_Address { get; set; } = ""; // 服务器IP地址 + public static string Server_IP { get; set; } = ""; // 服务器IP地址 public static int Server_Port { get; set; } = 0; // 服务器端口号 public static Encoding Default_Encoding { get; } = General.DefaultEncoding; diff --git a/FunGame.Desktop/Model/LoginModel.cs b/FunGame.Desktop/Model/LoginModel.cs index 0457a77..6f2329a 100644 --- a/FunGame.Desktop/Model/LoginModel.cs +++ b/FunGame.Desktop/Model/LoginModel.cs @@ -16,8 +16,8 @@ namespace Milimoe.FunGame.Desktop.Model /// public class LoginModel : BaseModel { - private static SocketObject Work; - private static bool Working = false; + private static new SocketObject Work; + private static new bool Working = false; public LoginModel() : base(RunTime.Socket) { @@ -133,7 +133,7 @@ namespace Milimoe.FunGame.Desktop.Model return ds; } - private static void SetWorking() + private static new void SetWorking() { Working = true; Work = default; diff --git a/FunGame.Desktop/Model/RegisterModel.cs b/FunGame.Desktop/Model/RegisterModel.cs index b616e3e..5c4a427 100644 --- a/FunGame.Desktop/Model/RegisterModel.cs +++ b/FunGame.Desktop/Model/RegisterModel.cs @@ -12,8 +12,6 @@ namespace Milimoe.FunGame.Desktop.Model public class RegisterModel : BaseModel { private readonly Register Register; - private SocketObject Work; - private bool Working = false; public RegisterModel(Register reg) : base(RunTime.Socket) { @@ -129,11 +127,5 @@ namespace Milimoe.FunGame.Desktop.Model } return (success, msg); } - - private void SetWorking() - { - Working = true; - Work = default; - } } } diff --git a/FunGame.Desktop/Model/RunTimeModel.cs b/FunGame.Desktop/Model/RunTimeModel.cs index e57e6f4..ed061a4 100644 --- a/FunGame.Desktop/Model/RunTimeModel.cs +++ b/FunGame.Desktop/Model/RunTimeModel.cs @@ -28,18 +28,20 @@ namespace Milimoe.FunGame.Desktop.Model #region 公开方法 - public void Disconnect() + public bool Disconnect() { + bool result = false; + try { - Socket?.Send(SocketMessageType.Disconnect, ""); + result = Socket?.Send(SocketMessageType.Disconnect, "") == SocketResult.Success; } catch (Exception e) { Main.GetMessage(e.GetErrorInfo()); - Main.OnFailedDisconnectEvent(new GeneralEventArgs()); - Main.OnAfterDisconnectEvent(new GeneralEventArgs()); } + + return result; } public void Disconnected() @@ -47,7 +49,7 @@ namespace Milimoe.FunGame.Desktop.Model Disconnect(); } - public bool GetServerConnection() + public void GetServerConnection() { try { @@ -58,9 +60,8 @@ namespace Milimoe.FunGame.Desktop.Model string[] s = ipaddress.Split(':'); if (s != null && s.Length > 1) { - Constant.Server_Address = s[0]; + Constant.Server_IP = s[0]; Constant.Server_Port = Convert.ToInt32(s[1]); - if (Connect() == ConnectResult.Success) return true; // 连接服务器 } } else @@ -74,18 +75,13 @@ namespace Milimoe.FunGame.Desktop.Model { Main.GetMessage(e.GetErrorInfo(), TimeType.None); } - - return false; } - public ConnectResult Connect() + public async Task Connect() { - if (Main.OnBeforeConnectEvent(new GeneralEventArgs()) == EventResult.Fail) return ConnectResult.ConnectFailed; - if (Constant.Server_Address == "" || Constant.Server_Port <= 0) + if (Constant.Server_IP == "" || Constant.Server_Port <= 0) { ShowMessage.ErrorMessage("查找可用的服务器失败!"); - Main.OnFailedConnectEvent(new GeneralEventArgs()); - Main.OnAfterConnectEvent(new GeneralEventArgs()); return ConnectResult.FindServerFailed; } try @@ -94,8 +90,6 @@ namespace Milimoe.FunGame.Desktop.Model { Main.GetMessage("正在连接服务器,请耐心等待。"); Config.FunGame_isRetrying = false; - Main.OnFailedConnectEvent(new GeneralEventArgs()); - Main.OnAfterConnectEvent(new GeneralEventArgs()); return ConnectResult.CanNotConnect; } if (!Config.FunGame_isConnected) @@ -111,7 +105,7 @@ namespace Milimoe.FunGame.Desktop.Model // 与服务器建立连接 Socket?.Close(); Config.FunGame_isRetrying = true; - Socket = Socket.Connect(Constant.Server_Address, Constant.Server_Port); + Socket = Socket.Connect(Constant.Server_IP, Constant.Server_Port); if (Socket != null && Socket.Connected) { // 设置可复用Socket @@ -119,35 +113,32 @@ namespace Milimoe.FunGame.Desktop.Model // 发送连接请求 if (Socket.Send(SocketMessageType.Connect) == SocketResult.Success) { - Task t = Task.Factory.StartNew(() => + SocketMessageType Result = Receiving(); + if (Result == SocketMessageType.Connect) { - if (Receiving() == SocketMessageType.Connect) + Main.GetMessage("连接服务器成功,请登录账号以体验FunGame。"); + Main.UpdateUI(MainInvokeType.Connected); + StartReceiving(); + await Task.Factory.StartNew(() => { - Main.GetMessage("连接服务器成功,请登录账号以体验FunGame。"); - Main.UpdateUI(MainInvokeType.Connected); - StartReceiving(); while (true) { if (IsReceiving) { - Main.OnSucceedConnectEvent(new GeneralEventArgs()); - Main.OnAfterConnectEvent(new GeneralEventArgs()); break; } } - } - else - { - Config.FunGame_isRetrying = false; - Socket.Close(); - } - }); - return ConnectResult.Success; + }); + return ConnectResult.Success; + } } - Socket?.Close(); Config.FunGame_isRetrying = false; - throw new CanNotConnectException(); + Socket.Close(); + return ConnectResult.ConnectFailed; } + Socket?.Close(); + Config.FunGame_isRetrying = false; + throw new CanNotConnectException(); } else { @@ -160,14 +151,8 @@ namespace Milimoe.FunGame.Desktop.Model Main.GetMessage(e.GetErrorInfo(), TimeType.None); Main.UpdateUI(MainInvokeType.SetRed); Config.FunGame_isRetrying = false; - Task.Factory.StartNew(() => - { - Main.OnFailedConnectEvent(new GeneralEventArgs()); - Main.OnAfterConnectEvent(new GeneralEventArgs()); - }); return ConnectResult.ConnectFailed; } - return ConnectResult.CanNotConnect; } public bool Close() @@ -198,7 +183,7 @@ namespace Milimoe.FunGame.Desktop.Model { Main.GetMessage(e.GetErrorInfo(), TimeType.None); Main.UpdateUI(MainInvokeType.Disconnected); - Main.OnFailedConnectEvent(new GeneralEventArgs()); + Main.OnFailedConnectEvent(new ConnectEventArgs(Constant.Server_IP, Constant.Server_Port)); Close(); } diff --git a/FunGame.Desktop/UI/Main/Main.cs b/FunGame.Desktop/UI/Main/Main.cs index 98effba..66ac95e 100644 --- a/FunGame.Desktop/UI/Main/Main.cs +++ b/FunGame.Desktop/UI/Main/Main.cs @@ -107,7 +107,7 @@ namespace Milimoe.FunGame.Desktop.UI case MainInvokeType.SetGreenAndPing: Config.FunGame_isRetrying = false; - SetServerStatusLight((int)LightType.Green, ping: NetworkUtility.GetServerPing(Constant.Server_Address)); + SetServerStatusLight((int)LightType.Green, ping: NetworkUtility.GetServerPing(Constant.Server_IP)); SetButtonEnableIfLogon(true, ClientState.Online); Config.FunGame_isConnected = true; CurrentRetryTimes = 0; @@ -1354,7 +1354,7 @@ namespace Milimoe.FunGame.Desktop.UI ErrorType ErrorType = NetworkUtility.IsServerAddress(ip, port); if (ErrorType == Core.Library.Constant.ErrorType.None) { - Constant.Server_Address = ip; + Constant.Server_IP = ip; Constant.Server_Port = port; CurrentRetryTimes = -1; Config.FunGame_isAutoRetry = true;