支持DataRequest.SendRequest异步 (#39)

This commit is contained in:
milimoe 2023-07-12 12:04:36 +08:00 committed by GitHub
parent 821b43f76f
commit 3614747433
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 76 additions and 37 deletions

View File

@ -100,7 +100,6 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
private readonly Socket? Socket; private readonly Socket? Socket;
private readonly DataRequestType RequestType; private readonly DataRequestType RequestType;
private bool _Finish = false;
private RequestResult _Result = RequestResult.Missing; private RequestResult _Result = RequestResult.Missing;
private string _Error = ""; private string _Error = "";
private Hashtable _ResultData = new(); private Hashtable _ResultData = new();
@ -109,19 +108,35 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
{ {
try try
{ {
SetWorking();
if (Socket?.Send(SocketMessageType.DataRequest, RequestType, RequestData) == SocketResult.Success) if (Socket?.Send(SocketMessageType.DataRequest, RequestType, RequestData) == SocketResult.Success)
{ {
while (true) WaitForWorkDone();
{
if (_Finish) break;
Thread.Sleep(100);
}
} }
else throw new ConnectFailedException(); else throw new ConnectFailedException();
} }
catch (Exception e) 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; _Result = RequestResult.Fail;
_Error = e.GetErrorInfo(); _Error = e.GetErrorInfo();
} }
@ -139,19 +154,20 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
{ {
if (SocketObject.SocketType == SocketMessageType.DataRequest) if (SocketObject.SocketType == SocketMessageType.DataRequest)
{ {
Dispose();
Work = SocketObject;
Working = false;
DataRequestType type = SocketObject.GetParam<DataRequestType>(0); DataRequestType type = SocketObject.GetParam<DataRequestType>(0);
if (type == RequestType) if (type == RequestType)
{ {
Dispose();
_ResultData = SocketObject.GetParam<Hashtable>(1) ?? new(); _ResultData = SocketObject.GetParam<Hashtable>(1) ?? new();
_Finish = true;
_Result = RequestResult.Success; _Result = RequestResult.Success;
} }
} }
} }
catch (Exception e) catch (Exception e)
{ {
_Finish = true; Working = false;
_Result = RequestResult.Fail; _Result = RequestResult.Fail;
_Error = e.GetErrorInfo(); _Error = e.GetErrorInfo();
} }

View File

@ -0,0 +1,42 @@
using Milimoe.FunGame.Core.Library.Common.Network;
namespace Milimoe.FunGame.Core.Library.Common.Architecture
{
/// <summary>
/// 继承这个类可以获得后台等待的功能
/// <para>参考实现 <see cref="BaseModel"/></para>
/// </summary>
public abstract class AsyncWorker<T>
{
/// <summary>
/// 接收到的实例
/// </summary>
protected abstract T? Work { get; set; }
/// <summary>
/// 是否处于等待服务器响应的状态
/// </summary>
protected bool Working { get; set; } = false;
/// <summary>
/// 异步操作前,请设置为等待状态
/// </summary>
protected virtual void SetWorking()
{
Working = true;
Work = default;
}
/// <summary>
/// 请等待任务完成
/// </summary>
protected virtual void WaitForWorkDone()
{
while (true)
{
if (!Working) break;
Thread.Sleep(100);
}
}
}
}

View File

@ -4,17 +4,18 @@ using Milimoe.FunGame.Core.Service;
namespace Milimoe.FunGame.Core.Library.Common.Architecture namespace Milimoe.FunGame.Core.Library.Common.Architecture
{ {
public class BaseModel : ISocketHandler, IDisposable /// <summary>
/// <para>继承 AsyncWorker 用法:</para>
/// <para>1、调用Socket.Send()前请设置为等待状态SetWorking();</para>
/// <para>2、调用Socket.Send() == Success后请等待任务完成WaitForWorkDone();</para>
/// <para>3、在其他任何地方修改Working状态均会使任务终止。</para>
/// </summary>
public class BaseModel : AsyncWorker<SocketObject>, ISocketHandler, IDisposable
{ {
/// <summary> /// <summary>
/// 接收到的SocketObject实例 /// 接收到的SocketObject实例
/// </summary> /// </summary>
protected virtual SocketObject Work { get; set; } protected override SocketObject Work { get; set; }
/// <summary>
/// 是否处于等待服务器响应的状态
/// </summary>
protected virtual bool Working { get; set; } = false;
/// <summary> /// <summary>
/// Socket /// Socket
@ -73,25 +74,5 @@ namespace Milimoe.FunGame.Core.Library.Common.Architecture
} }
IsDisposed = true; IsDisposed = true;
} }
/// <summary>
/// 调用Socket.Send()前,请设置为等待状态
/// </summary>
protected void SetWorking()
{
Working = true;
Work = default;
}
/// <summary>
/// 调用Socket.Send() == Success后请等待任务完成
/// </summary>
protected void WaitForWorkDone()
{
while (true)
{
if (!Working) break;
}
}
} }
} }