From 36147474338a50104a02b5a178c5d76207db3fa2 Mon Sep 17 00:00:00 2001 From: milimoe <110188673+milimoe@users.noreply.github.com> Date: Wed, 12 Jul 2023 12:04:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81DataRequest.SendRequest?= =?UTF-8?q?=E5=BC=82=E6=AD=A5=20(#39)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Api/Transmittal/DataRequest.cs | 36 +++++++++++++------ Library/Common/Architecture/AsyncWorker.cs | 42 ++++++++++++++++++++++ Library/Common/Architecture/BaseModel.cs | 35 +++++------------- 3 files changed, 76 insertions(+), 37 deletions(-) create mode 100644 Library/Common/Architecture/AsyncWorker.cs diff --git a/Api/Transmittal/DataRequest.cs b/Api/Transmittal/DataRequest.cs index fcaf462..eb376ee 100644 --- a/Api/Transmittal/DataRequest.cs +++ b/Api/Transmittal/DataRequest.cs @@ -100,7 +100,6 @@ namespace Milimoe.FunGame.Core.Api.Transmittal private readonly Socket? Socket; private readonly DataRequestType RequestType; - private bool _Finish = false; private RequestResult _Result = RequestResult.Missing; private string _Error = ""; private Hashtable _ResultData = new(); @@ -109,19 +108,35 @@ namespace Milimoe.FunGame.Core.Api.Transmittal { try { + SetWorking(); if (Socket?.Send(SocketMessageType.DataRequest, RequestType, RequestData) == SocketResult.Success) { - while (true) - { - if (_Finish) break; - Thread.Sleep(100); - } + WaitForWorkDone(); } else throw new ConnectFailedException(); } catch (Exception e) { - _Finish = true; + Working = false; + _Result = RequestResult.Fail; + _Error = e.GetErrorInfo(); + } + } + + public async Task SendRequestAsync() + { + try + { + SetWorking(); + if (Socket?.Send(SocketMessageType.DataRequest, RequestType, RequestData) == SocketResult.Success) + { + await Task.Factory.StartNew(WaitForWorkDone); + } + else throw new ConnectFailedException(); + } + catch (Exception e) + { + Working = false; _Result = RequestResult.Fail; _Error = e.GetErrorInfo(); } @@ -139,19 +154,20 @@ namespace Milimoe.FunGame.Core.Api.Transmittal { if (SocketObject.SocketType == SocketMessageType.DataRequest) { + Dispose(); + Work = SocketObject; + Working = false; DataRequestType type = SocketObject.GetParam(0); if (type == RequestType) { - Dispose(); _ResultData = SocketObject.GetParam(1) ?? new(); - _Finish = true; _Result = RequestResult.Success; } } } catch (Exception e) { - _Finish = true; + Working = false; _Result = RequestResult.Fail; _Error = e.GetErrorInfo(); } diff --git a/Library/Common/Architecture/AsyncWorker.cs b/Library/Common/Architecture/AsyncWorker.cs new file mode 100644 index 0000000..f82d816 --- /dev/null +++ b/Library/Common/Architecture/AsyncWorker.cs @@ -0,0 +1,42 @@ +using Milimoe.FunGame.Core.Library.Common.Network; + +namespace Milimoe.FunGame.Core.Library.Common.Architecture +{ + /// + /// 继承这个类可以获得后台等待的功能 + /// 参考实现 + /// + public abstract class AsyncWorker + { + /// + /// 接收到的实例 + /// + protected abstract T? Work { get; set; } + + /// + /// 是否处于等待服务器响应的状态 + /// + protected bool Working { get; set; } = false; + + /// + /// 异步操作前,请设置为等待状态 + /// + protected virtual void SetWorking() + { + Working = true; + Work = default; + } + + /// + /// 请等待任务完成 + /// + protected virtual void WaitForWorkDone() + { + while (true) + { + if (!Working) break; + Thread.Sleep(100); + } + } + } +} diff --git a/Library/Common/Architecture/BaseModel.cs b/Library/Common/Architecture/BaseModel.cs index f791fdb..e1b303d 100644 --- a/Library/Common/Architecture/BaseModel.cs +++ b/Library/Common/Architecture/BaseModel.cs @@ -4,17 +4,18 @@ using Milimoe.FunGame.Core.Service; namespace Milimoe.FunGame.Core.Library.Common.Architecture { - public class BaseModel : ISocketHandler, IDisposable + /// + /// 继承 AsyncWorker 用法: + /// 1、调用Socket.Send()前,请设置为等待状态:SetWorking(); + /// 2、调用Socket.Send() == Success后,请等待任务完成:WaitForWorkDone(); + /// 3、在其他任何地方修改Working状态,均会使任务终止。 + /// + public class BaseModel : AsyncWorker, ISocketHandler, IDisposable { /// /// 接收到的SocketObject实例 /// - protected virtual SocketObject Work { get; set; } - - /// - /// 是否处于等待服务器响应的状态 - /// - protected virtual bool Working { get; set; } = false; + protected override SocketObject Work { get; set; } /// /// Socket @@ -73,25 +74,5 @@ namespace Milimoe.FunGame.Core.Library.Common.Architecture } IsDisposed = true; } - - /// - /// 调用Socket.Send()前,请设置为等待状态 - /// - protected void SetWorking() - { - Working = true; - Work = default; - } - - /// - /// 调用Socket.Send() == Success后,请等待任务完成 - /// - protected void WaitForWorkDone() - { - while (true) - { - if (!Working) break; - } - } } }