diff --git a/Api/Transmittal/DataRequest.cs b/Api/Transmittal/DataRequest.cs index 7878b22..7decd8a 100644 --- a/Api/Transmittal/DataRequest.cs +++ b/Api/Transmittal/DataRequest.cs @@ -53,9 +53,10 @@ namespace Milimoe.FunGame.Core.Api.Transmittal /// /// /// - internal DataRequest(Socket Socket, DataRequestType RequestType) + /// + internal DataRequest(Socket Socket, DataRequestType RequestType, bool IsLongRunning = false) { - Worker = new(Socket, RequestType); + Worker = new(Socket, RequestType, IsLongRunning); } /// @@ -69,6 +70,14 @@ namespace Milimoe.FunGame.Core.Api.Transmittal else Worker.RequestData.Add(key, value); } + /// + /// 长时间运行的数据请求需要在使用完毕后自行关闭 + /// + public void Dispose() + { + Worker.Dispose(); + } + /// /// 向服务器发送数据请求 /// @@ -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(0); if (type == RequestType) { - Dispose(); + if (!_IsLongRunning) Dispose(); _ResultData = SocketObject.GetParam(1) ?? new(); _Result = RequestResult.Success; } diff --git a/Controller/RunTimeController.cs b/Controller/RunTimeController.cs index b7c5e1c..aced6e7 100644 --- a/Controller/RunTimeController.cs +++ b/Controller/RunTimeController.cs @@ -151,6 +151,22 @@ namespace Milimoe.FunGame.Core.Controller } throw new ConnectFailedException(); } + + /// + /// 基于本地已连接的Socket创建长时间运行的数据请求 + /// + /// + /// + /// + public DataRequest NewLongRunningDataRequest(DataRequestType RequestType) + { + if (_Socket != null) + { + DataRequest request = new(_Socket, RequestType, true); + return request; + } + throw new ConnectFailedException(); + } /// /// 开始接收服务器信息 diff --git a/Controller/SocketHandlerController.cs b/Controller/SocketHandlerController.cs index ff3d26d..d9b4de7 100644 --- a/Controller/SocketHandlerController.cs +++ b/Controller/SocketHandlerController.cs @@ -56,6 +56,7 @@ namespace Milimoe.FunGame.Core.Controller /// public void Dispose() { + OnDisposed(); Dispose(true); GC.SuppressFinalize(this); } @@ -75,5 +76,27 @@ namespace Milimoe.FunGame.Core.Controller } IsDisposed = true; } + + /// + /// 关闭事件 + /// + /// SocketObject + protected delegate void DisposedEvent(); + + /// + /// Controller关闭时事件 + /// 不建议new Dispose()方法,建议使用事件 + /// 事件会在base.Dispose()执行前触发 + /// + protected static event DisposedEvent? Disposed; + + /// + /// 触发关闭事件 + /// + /// SocketObject + protected static void OnDisposed() + { + Disposed?.Invoke(); + } } -} \ No newline at end of file +}