DataRequest:添加长运行模式和关闭事件 (#41)

* DataRequest:添加长运行模式和关闭事件

* Update SocketHandlerController.cs

---------

Co-authored-by: Yezi <53083103+yeziuku@users.noreply.github.com>
This commit is contained in:
milimoe 2023-07-18 00:14:01 +08:00 committed by GitHub
parent 1cce3d12ab
commit 9c0f742f00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 8 deletions

View File

@ -53,9 +53,10 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
/// </summary> /// </summary>
/// <param name="Socket"></param> /// <param name="Socket"></param>
/// <param name="RequestType"></param> /// <param name="RequestType"></param>
internal DataRequest(Socket Socket, DataRequestType RequestType) /// <param name="IsLongRunning"></param>
internal DataRequest(Socket Socket, DataRequestType RequestType, bool IsLongRunning = false)
{ {
Worker = new(Socket, RequestType); Worker = new(Socket, RequestType, IsLongRunning);
} }
/// <summary> /// <summary>
@ -69,6 +70,14 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
else Worker.RequestData.Add(key, value); else Worker.RequestData.Add(key, value);
} }
/// <summary>
/// 长时间运行的数据请求需要在使用完毕后自行关闭
/// </summary>
public void Dispose()
{
Worker.Dispose();
}
/// <summary> /// <summary>
/// 向服务器发送数据请求 /// 向服务器发送数据请求
/// </summary> /// </summary>
@ -109,10 +118,11 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
private readonly Socket? Socket; private readonly Socket? Socket;
private readonly DataRequestType RequestType; private readonly DataRequestType RequestType;
private readonly bool _IsLongRunning;
private Hashtable _ResultData = new();
private RequestResult _Result = RequestResult.Missing; private RequestResult _Result = RequestResult.Missing;
private string _Error = ""; private string _Error = "";
private Hashtable _ResultData = new();
public void SendRequest() public void SendRequest()
{ {
@ -152,10 +162,11 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
} }
} }
public Request(Socket? socket, DataRequestType requestType) : base(socket) public Request(Socket? Socket, DataRequestType RequestType, bool IsLongRunning = false) : base(Socket)
{ {
Socket = socket; this.Socket = Socket;
RequestType = requestType; this.RequestType = RequestType;
_IsLongRunning = IsLongRunning;
} }
public override void SocketHandler(SocketObject SocketObject) public override void SocketHandler(SocketObject SocketObject)
@ -169,7 +180,7 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
DataRequestType type = SocketObject.GetParam<DataRequestType>(0); DataRequestType type = SocketObject.GetParam<DataRequestType>(0);
if (type == RequestType) if (type == RequestType)
{ {
Dispose(); if (!_IsLongRunning) Dispose();
_ResultData = SocketObject.GetParam<Hashtable>(1) ?? new(); _ResultData = SocketObject.GetParam<Hashtable>(1) ?? new();
_Result = RequestResult.Success; _Result = RequestResult.Success;
} }

View File

@ -152,6 +152,22 @@ namespace Milimoe.FunGame.Core.Controller
throw new ConnectFailedException(); throw new ConnectFailedException();
} }
/// <summary>
/// 基于本地已连接的Socket创建长时间运行的数据请求
/// </summary>
/// <param name="RequestType"></param>
/// <returns></returns>
/// <exception cref="ConnectFailedException"></exception>
public DataRequest NewLongRunningDataRequest(DataRequestType RequestType)
{
if (_Socket != null)
{
DataRequest request = new(_Socket, RequestType, true);
return request;
}
throw new ConnectFailedException();
}
/// <summary> /// <summary>
/// 开始接收服务器信息 /// 开始接收服务器信息
/// </summary> /// </summary>

View File

@ -56,6 +56,7 @@ namespace Milimoe.FunGame.Core.Controller
/// </summary> /// </summary>
public void Dispose() public void Dispose()
{ {
OnDisposed();
Dispose(true); Dispose(true);
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
} }
@ -75,5 +76,27 @@ namespace Milimoe.FunGame.Core.Controller
} }
IsDisposed = true; IsDisposed = true;
} }
/// <summary>
/// 关闭事件
/// </summary>
/// <param name="SocketObject">SocketObject</param>
protected delegate void DisposedEvent();
/// <summary>
/// <para>Controller关闭时事件</para>
/// <para>不建议new Dispose()方法,建议使用事件</para>
/// <para>事件会在base.Dispose()执行前触发</para>
/// </summary>
protected static event DisposedEvent? Disposed;
/// <summary>
/// 触发关闭事件
/// </summary>
/// <param name="SocketObject">SocketObject</param>
protected static void OnDisposed()
{
Disposed?.Invoke();
}
} }
} }