From 5a8a34a68ef237df1ab88f149aa955f4dcc7cea9 Mon Sep 17 00:00:00 2001 From: Mili Date: Tue, 28 Mar 2023 23:34:15 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AD=89=E5=BE=85=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FunGame.Core/Api/Utility/ConcurrentQueue.cs | 31 +++---- .../Common/Architecture/DataRequest.cs | 16 ++-- .../Library/Common/Event/LoginEventArgs.cs | 15 ++-- FunGame.Desktop/Controller/LoginController.cs | 26 ++---- FunGame.Desktop/Model/LoginModel.cs | 83 ++++++++++++------- FunGame.Desktop/UI/Login/Login.cs | 3 - 6 files changed, 92 insertions(+), 82 deletions(-) diff --git a/FunGame.Core/Api/Utility/ConcurrentQueue.cs b/FunGame.Core/Api/Utility/ConcurrentQueue.cs index 5ffe49c..883ed74 100644 --- a/FunGame.Core/Api/Utility/ConcurrentQueue.cs +++ b/FunGame.Core/Api/Utility/ConcurrentQueue.cs @@ -2,35 +2,30 @@ { public class ConcurrentQueue { - public bool Lock { get; set; } + private bool Lock { get; set; } - private System.Collections.Concurrent.ConcurrentQueue Instance { get; } + private System.Collections.Concurrent.ConcurrentQueue Instance { get; } = new(); - public ConcurrentQueue() - { - Instance = new System.Collections.Concurrent.ConcurrentQueue(); - } - - public void Add(T obj) + public async void AddAsync(T obj) { if (Lock) { - return; + await Task.Run(() => + { + while (true) + { + if (!Lock) break; + Thread.Sleep(100); + } + }); } Lock = true; - lock (Instance) - { - Instance.Enqueue(obj); - } + Instance.Enqueue(obj); } public bool Delete() { - bool result = false; - lock (Instance) - { - result = Instance.TryDequeue(out _); - } + bool result = Instance.TryDequeue(out _); Lock = false; return result; } diff --git a/FunGame.Core/Library/Common/Architecture/DataRequest.cs b/FunGame.Core/Library/Common/Architecture/DataRequest.cs index b347dd6..e12be58 100644 --- a/FunGame.Core/Library/Common/Architecture/DataRequest.cs +++ b/FunGame.Core/Library/Common/Architecture/DataRequest.cs @@ -22,7 +22,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Architecture public async Task SendRequest() { - Queue.Add(Worker); + Queue.AddAsync(Worker); if (await Worker.SendRequest() == RequestResult.Success) { Queue.Delete(); @@ -83,13 +83,17 @@ namespace Milimoe.FunGame.Core.Library.Common.Architecture { if (SocketObject.SocketType == SocketMessageType.DataRequest) { - Dispose(); - switch (RequestType) + DataRequestType type = SocketObject.GetParam(0); + if (type == RequestType) { - default: - break; + Dispose(); + switch (RequestType) + { + default: + break; + } + JobFinish = true; } - JobFinish = true; } } } diff --git a/FunGame.Core/Library/Common/Event/LoginEventArgs.cs b/FunGame.Core/Library/Common/Event/LoginEventArgs.cs index 25b12c6..f903dcc 100644 --- a/FunGame.Core/Library/Common/Event/LoginEventArgs.cs +++ b/FunGame.Core/Library/Common/Event/LoginEventArgs.cs @@ -2,13 +2,18 @@ { public class LoginEventArgs : GeneralEventArgs { - public string Username; - public string Password; + public string Username { get; set; } = ""; + public string Password { get; set; } = ""; + public string AutoKey { get; set; } = ""; - public LoginEventArgs(string username = "", string password = "") + public LoginEventArgs(params object[]? objs) { - Username = username; - Password = password; + if (objs != null) + { + if (objs.Length > 0) Username = (string)objs[0]; + if (objs.Length > 1) Password = (string)objs[1]; + if (objs.Length > 2) AutoKey = (string)objs[2]; + } } } } diff --git a/FunGame.Desktop/Controller/LoginController.cs b/FunGame.Desktop/Controller/LoginController.cs index 2a6b035..9b7f331 100644 --- a/FunGame.Desktop/Controller/LoginController.cs +++ b/FunGame.Desktop/Controller/LoginController.cs @@ -1,9 +1,7 @@ using Milimoe.FunGame.Core.Library.Common.Event; using Milimoe.FunGame.Core.Library.Common.Architecture; using Milimoe.FunGame.Desktop.Library; -using Milimoe.FunGame.Desktop.Library.Component; using Milimoe.FunGame.Desktop.Model; -using Milimoe.FunGame.Desktop.UI; namespace Milimoe.FunGame.Desktop.Controller { @@ -23,24 +21,12 @@ namespace Milimoe.FunGame.Desktop.Controller public static bool LoginAccount(params object[]? objs) { - if (RunTime.Login?.OnBeforeLoginEvent(Login.EventArgs) == Core.Library.Constant.EventResult.Fail) return false; - bool result = LoginModel.LoginAccount(objs); - if (!result) - { - ShowMessage.ErrorMessage("登录失败!!", "登录失败", 5); - RunTime.Login?.OnFailedLoginEvent(Login.EventArgs); - } - return result; - } - - public static bool CheckLogin(params object[]? objs) - { - bool result = LoginModel.CheckLogin(objs); - if (!result) - { - ShowMessage.ErrorMessage("登录失败!!", "登录失败", 5); - RunTime.Login?.OnFailedLoginEvent(Login.EventArgs); - } + LoginEventArgs LoginEventArgs = new(objs); + if (RunTime.Login?.OnBeforeLoginEvent(LoginEventArgs) == Core.Library.Constant.EventResult.Fail) return false; + bool result = LoginModel.LoginAccountAsync(objs).Result; + if (result) RunTime.Login?.OnSucceedLoginEvent(LoginEventArgs); + else RunTime.Login?.OnFailedLoginEvent(LoginEventArgs); + RunTime.Login?.OnAfterLoginEvent(LoginEventArgs); return result; } } diff --git a/FunGame.Desktop/Model/LoginModel.cs b/FunGame.Desktop/Model/LoginModel.cs index 14ba8de..1a42cf5 100644 --- a/FunGame.Desktop/Model/LoginModel.cs +++ b/FunGame.Desktop/Model/LoginModel.cs @@ -5,10 +5,8 @@ using Milimoe.FunGame.Core.Library.Common.Architecture; using Milimoe.FunGame.Core.Library.Common.Network; using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Library.Exception; -using Milimoe.FunGame.Desktop.Controller; using Milimoe.FunGame.Desktop.Library; using Milimoe.FunGame.Desktop.Library.Component; -using Milimoe.FunGame.Desktop.UI; namespace Milimoe.FunGame.Desktop.Model { @@ -17,6 +15,10 @@ namespace Milimoe.FunGame.Desktop.Model /// public class LoginModel : BaseModel { + private static SocketObject Work; + private static bool Working = false; + private static bool Success = false; + public LoginModel() : base(RunTime.Socket) { @@ -26,18 +28,9 @@ namespace Milimoe.FunGame.Desktop.Model { try { - SocketMessageType type = SocketObject.SocketType; - object[] objs = SocketObject.Parameters; - switch (SocketObject.SocketType) - { - case SocketMessageType.Login: - SocketHandler_Login(SocketObject); - break; - - case SocketMessageType.CheckLogin: - SocketHandler_CheckLogin(SocketObject); - break; - } + Work = SocketObject; + Success = true; + Working = false; } catch (Exception e) { @@ -45,7 +38,7 @@ namespace Milimoe.FunGame.Desktop.Model } } - public static bool LoginAccount(params object[]? objs) + public static async Task LoginAccountAsync(params object[]? objs) { try { @@ -61,14 +54,26 @@ namespace Milimoe.FunGame.Desktop.Model password = password.Encrypt(username); if (Socket.Send(SocketMessageType.Login, username, password, autokey) == SocketResult.Success) { - return true; + SetWorking(); + await Task.Factory.StartNew(async() => + { + while (true) + { + if (!Working) break; + } + if (Success) + { + await CheckLoginAsync(Work); + } + }); } } } catch (Exception e) { - e.GetErrorInfo(); + RunTime.WritelnSystemInfo(e.GetErrorInfo()); } + return false; } @@ -89,12 +94,12 @@ namespace Milimoe.FunGame.Desktop.Model } catch (Exception e) { - e.GetErrorInfo(); + RunTime.WritelnSystemInfo(e.GetErrorInfo()); } return false; } - private static void SocketHandler_Login(SocketObject SocketObject) + private static async Task CheckLoginAsync(SocketObject SocketObject) { Guid key = Guid.Empty; string? msg = ""; @@ -105,26 +110,41 @@ namespace Milimoe.FunGame.Desktop.Model if (msg != null && msg.Trim() != "") { ShowMessage.ErrorMessage(msg, "登录失败"); - RunTime.Login?.OnFailedLoginEvent(Login.EventArgs); - RunTime.Login?.OnAfterLoginEvent(Login.EventArgs); + return false; } else { if (key != Guid.Empty) { Config.Guid_LoginKey = key; - LoginController.CheckLogin(key); + Socket Socket = RunTime.Socket!; + if (Socket.Send(SocketMessageType.CheckLogin, key) == SocketResult.Success) + { + SetWorking(); + await Task.Factory.StartNew(() => + { + while (true) + { + if (!Working) break; + } + if (Success) + { + SocketHandler_CheckLogin(Work); + } + }); + return true; + } } else { ShowMessage.ErrorMessage("登录失败!!", "登录失败", 5); - RunTime.Login?.OnFailedLoginEvent(Login.EventArgs); - RunTime.Login?.OnAfterLoginEvent(Login.EventArgs); + return false; } } + return true; } - private static void SocketHandler_CheckLogin(SocketObject SocketObject) + private static bool SocketHandler_CheckLogin(SocketObject SocketObject) { // 返回构造User对象的DataTable object[] objs = SocketObject.Parameters; @@ -133,14 +153,17 @@ namespace Milimoe.FunGame.Desktop.Model DataSet? DataSet = SocketObject.GetParam(0); // 创建User对象并返回到Main RunTime.Main?.UpdateUI(MainInvokeType.SetUser, new object[] { Factory.GetInstance(DataSet) }); - RunTime.Login?.OnSucceedLoginEvent(Login.EventArgs); - RunTime.Login?.OnAfterLoginEvent(Login.EventArgs); - return; + return true; } ShowMessage.ErrorMessage("登录失败!!", "登录失败", 5); - RunTime.Login?.OnFailedLoginEvent(Login.EventArgs); - RunTime.Login?.OnAfterLoginEvent(Login.EventArgs); + return false; } + private static void SetWorking() + { + Working = true; + Success = false; + Work = default; + } } } diff --git a/FunGame.Desktop/UI/Login/Login.cs b/FunGame.Desktop/UI/Login/Login.cs index db809a9..f496d74 100644 --- a/FunGame.Desktop/UI/Login/Login.cs +++ b/FunGame.Desktop/UI/Login/Login.cs @@ -11,8 +11,6 @@ namespace Milimoe.FunGame.Desktop.UI { public partial class Login : BaseLogin { - public static LoginEventArgs EventArgs { get; set; } = new LoginEventArgs(); - private readonly LoginController LoginController; public Login() @@ -48,7 +46,6 @@ namespace Milimoe.FunGame.Desktop.UI UsernameText.Focus(); return false; } - EventArgs = new LoginEventArgs(username, password); if (!LoginController.LoginAccount(username, password)) { ShowMessage.Message("登录失败!!", "登录失败");