mirror of
https://github.com/project-redbud/FunGame-Core.git
synced 2025-04-21 11:39:35 +08:00
DataRequest:添加长运行模式和关闭事件 (#41)
* DataRequest:添加长运行模式和关闭事件 * Update SocketHandlerController.cs --------- Co-authored-by: Yezi <53083103+yeziuku@users.noreply.github.com>
This commit is contained in:
parent
1cce3d12ab
commit
9c0f742f00
@ -53,9 +53,10 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
||||
/// </summary>
|
||||
/// <param name="Socket"></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>
|
||||
@ -69,6 +70,14 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
||||
else Worker.RequestData.Add(key, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 长时间运行的数据请求需要在使用完毕后自行关闭
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
Worker.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向服务器发送数据请求
|
||||
/// </summary>
|
||||
@ -109,10 +118,11 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
||||
|
||||
private readonly Socket? Socket;
|
||||
private readonly DataRequestType RequestType;
|
||||
private readonly bool _IsLongRunning;
|
||||
|
||||
private Hashtable _ResultData = new();
|
||||
private RequestResult _Result = RequestResult.Missing;
|
||||
private string _Error = "";
|
||||
private Hashtable _ResultData = new();
|
||||
|
||||
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;
|
||||
RequestType = requestType;
|
||||
this.Socket = Socket;
|
||||
this.RequestType = RequestType;
|
||||
_IsLongRunning = IsLongRunning;
|
||||
}
|
||||
|
||||
public override void SocketHandler(SocketObject SocketObject)
|
||||
@ -169,7 +180,7 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
||||
DataRequestType type = SocketObject.GetParam<DataRequestType>(0);
|
||||
if (type == RequestType)
|
||||
{
|
||||
Dispose();
|
||||
if (!_IsLongRunning) Dispose();
|
||||
_ResultData = SocketObject.GetParam<Hashtable>(1) ?? new();
|
||||
_Result = RequestResult.Success;
|
||||
}
|
||||
|
@ -152,6 +152,22 @@ namespace Milimoe.FunGame.Core.Controller
|
||||
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>
|
||||
|
@ -56,6 +56,7 @@ namespace Milimoe.FunGame.Core.Controller
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
OnDisposed();
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
@ -75,5 +76,27 @@ namespace Milimoe.FunGame.Core.Controller
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user