mirror of
https://github.com/project-redbud/FunGame-Core.git
synced 2025-04-23 12:39:35 +08:00
优化 TaskUtility;DataRequest 实现 IDisposable 接口 (#110)
This commit is contained in:
parent
58b00d5b96
commit
9e55587ea0
@ -10,17 +10,22 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
||||
/// 如果是 <see cref="Model.Gaming"/> 的数据请求,则配合 <see cref="GamingType"/> 使用<para/>
|
||||
/// 确保已添加对应的枚举
|
||||
/// </summary>
|
||||
public class DataRequest
|
||||
public class DataRequest : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据请求结果
|
||||
/// </summary>
|
||||
public RequestResult Result => Worker != null ? Worker.Result : (GamingWorker != null ? GamingWorker.Result : RequestResult.Missing);
|
||||
public RequestResult Result => _worker != null ? _worker.Result : (_gamingWorker != null ? _gamingWorker.Result : RequestResult.Missing);
|
||||
|
||||
/// <summary>
|
||||
/// 详细错误信息
|
||||
/// </summary>
|
||||
public string Error => Worker != null ? Worker.Error : (GamingWorker != null ? GamingWorker.Error : "");
|
||||
public string Error => _worker != null ? _worker.Error : (_gamingWorker != null ? _gamingWorker.Error : "");
|
||||
|
||||
/// <summary>
|
||||
/// 是否已经关闭
|
||||
/// </summary>
|
||||
public bool IsDisposed => _isDisposed;
|
||||
|
||||
// 获取ResultData中key值对应的Json字符串
|
||||
// -- 此索引器仅返回Json字符串,对象类型请使用反序列化方法GetResult<T>() --
|
||||
@ -30,8 +35,8 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Worker != null) return Worker.ResultData[key];
|
||||
else if (GamingWorker != null) return GamingWorker.ResultData[key];
|
||||
if (_worker != null) return _worker.ResultData[key];
|
||||
else if (_gamingWorker != null) return _gamingWorker.ResultData[key];
|
||||
return null;
|
||||
}
|
||||
set
|
||||
@ -43,25 +48,30 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
||||
/// <summary>
|
||||
/// 私有的实现类
|
||||
/// </summary>
|
||||
private readonly SocketRequest? Worker;
|
||||
private readonly SocketRequest? _worker;
|
||||
|
||||
/// <summary>
|
||||
/// 私有的实现类(这是局内请求的)
|
||||
/// </summary>
|
||||
private readonly GamingRequest? GamingWorker;
|
||||
private readonly GamingRequest? _gamingWorker;
|
||||
|
||||
/// <summary>
|
||||
/// 指示关闭的变量
|
||||
/// </summary>
|
||||
private bool _isDisposed = false;
|
||||
|
||||
/// <summary>
|
||||
/// 基于本地已连接的 <see cref="Socket"/> 创建新的数据请求<para/>
|
||||
/// 使用 <see cref="RunTimeController"/> 中的 <see cref="RunTimeController.NewDataRequest(DataRequestType)"/> 创建一个新的请求
|
||||
/// 插件则使用 <see cref="RunTimeController"/> 中的 <see cref="RunTimeController.NewDataRequestForAddon(DataRequestType)"/> 创建一个新的请求<para/>
|
||||
/// </summary>
|
||||
/// <param name="Socket"></param>
|
||||
/// <param name="RequestType"></param>
|
||||
/// <param name="IsLongRunning"></param>
|
||||
/// <param name="RuntimeType"></param>
|
||||
internal DataRequest(Socket Socket, DataRequestType RequestType, bool IsLongRunning = false, SocketRuntimeType RuntimeType = SocketRuntimeType.Client)
|
||||
/// <param name="socket"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="longRunning"></param>
|
||||
/// <param name="runtime"></param>
|
||||
internal DataRequest(Socket socket, DataRequestType type, bool longRunning = false, SocketRuntimeType runtime = SocketRuntimeType.Client)
|
||||
{
|
||||
Worker = new(Socket, RequestType, Guid.NewGuid(), IsLongRunning, RuntimeType);
|
||||
_worker = new(socket, type, Guid.NewGuid(), longRunning, runtime);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -70,13 +80,13 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
||||
/// 插件则使用 <see cref="RunTimeController"/> 中的 <see cref="RunTimeController.NewDataRequestForAddon(DataRequestType)"/> 创建一个新的请求<para/>
|
||||
/// 此数据请求只能调用异步方法 <see cref="SendRequestAsync"/> 请求数据
|
||||
/// </summary>
|
||||
/// <param name="HTTPClient"></param>
|
||||
/// <param name="RequestType"></param>
|
||||
/// <param name="IsLongRunning"></param>
|
||||
/// <param name="RuntimeType"></param>
|
||||
internal DataRequest(HTTPClient HTTPClient, DataRequestType RequestType, bool IsLongRunning = false, SocketRuntimeType RuntimeType = SocketRuntimeType.Client)
|
||||
/// <param name="client"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="longRunning"></param>
|
||||
/// <param name="runtime"></param>
|
||||
internal DataRequest(HTTPClient client, DataRequestType type, bool longRunning = false, SocketRuntimeType runtime = SocketRuntimeType.Client)
|
||||
{
|
||||
Worker = new(HTTPClient, RequestType, Guid.NewGuid(), IsLongRunning, RuntimeType);
|
||||
_worker = new(client, type, Guid.NewGuid(), longRunning, runtime);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -84,13 +94,13 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
||||
/// 使用 <see cref="RunTimeController"/> 中的 <see cref="RunTimeController.NewDataRequestForAddon(GamingType)"/> 创建一个新的请求<para/>
|
||||
/// 此构造方法是给 <see cref="Library.Common.Addon.GameModule"/> 提供的
|
||||
/// </summary>
|
||||
/// <param name="Socket"></param>
|
||||
/// <param name="GamingType"></param>
|
||||
/// <param name="IsLongRunning"></param>
|
||||
/// <param name="RuntimeType"></param>
|
||||
internal DataRequest(Socket Socket, GamingType GamingType, bool IsLongRunning = false, SocketRuntimeType RuntimeType = SocketRuntimeType.Client)
|
||||
/// <param name="socket"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="longRunning"></param>
|
||||
/// <param name="runtime"></param>
|
||||
internal DataRequest(Socket socket, GamingType type, bool longRunning = false, SocketRuntimeType runtime = SocketRuntimeType.Client)
|
||||
{
|
||||
GamingWorker = new(Socket, GamingType, Guid.NewGuid(), IsLongRunning, RuntimeType);
|
||||
_gamingWorker = new(socket, type, Guid.NewGuid(), longRunning, runtime);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -99,13 +109,13 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
||||
/// 此构造方法是给 <see cref="Library.Common.Addon.GameModule"/> 提供的<para/>
|
||||
/// 此数据请求只能调用异步方法 <see cref="SendRequestAsync"/> 请求数据
|
||||
/// </summary>
|
||||
/// <param name="Client"></param>
|
||||
/// <param name="GamingType"></param>
|
||||
/// <param name="IsLongRunning"></param>
|
||||
/// <param name="RuntimeType"></param>
|
||||
internal DataRequest(HTTPClient Client, GamingType GamingType, bool IsLongRunning = false, SocketRuntimeType RuntimeType = SocketRuntimeType.Client)
|
||||
/// <param name="client"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="longRunning"></param>
|
||||
/// <param name="runtime"></param>
|
||||
internal DataRequest(HTTPClient client, GamingType type, bool longRunning = false, SocketRuntimeType runtime = SocketRuntimeType.Client)
|
||||
{
|
||||
GamingWorker = new(Client, GamingType, Guid.NewGuid(), IsLongRunning, RuntimeType);
|
||||
_gamingWorker = new(client, type, Guid.NewGuid(), longRunning, runtime);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -115,13 +125,13 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
||||
/// <param name="value"></param>
|
||||
public void AddRequestData(string key, object value)
|
||||
{
|
||||
if (Worker != null)
|
||||
if (_worker != null)
|
||||
{
|
||||
if (!Worker.RequestData.TryAdd(key, value)) Worker.RequestData[key] = value;
|
||||
if (!_worker.RequestData.TryAdd(key, value)) _worker.RequestData[key] = value;
|
||||
}
|
||||
else if (GamingWorker != null)
|
||||
else if (_gamingWorker != null)
|
||||
{
|
||||
if (!GamingWorker.RequestData.TryAdd(key, value)) GamingWorker.RequestData[key] = value;
|
||||
if (!_gamingWorker.RequestData.TryAdd(key, value)) _gamingWorker.RequestData[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
@ -130,8 +140,25 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
Worker?.Dispose();
|
||||
GamingWorker?.Dispose();
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 关闭时
|
||||
/// </summary>
|
||||
/// <param name="disposing"></param>
|
||||
protected void Dispose(bool disposing)
|
||||
{
|
||||
if (!_isDisposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_worker?.Dispose();
|
||||
_gamingWorker?.Dispose();
|
||||
}
|
||||
}
|
||||
_isDisposed = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -142,8 +169,8 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
||||
/// <exception cref="AsyncSendException"></exception>
|
||||
public RequestResult SendRequest()
|
||||
{
|
||||
Worker?.SendRequest();
|
||||
GamingWorker?.SendRequest();
|
||||
_worker?.SendRequest();
|
||||
_gamingWorker?.SendRequest();
|
||||
return Result;
|
||||
}
|
||||
|
||||
@ -153,13 +180,13 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
||||
/// <returns></returns>
|
||||
public async Task<RequestResult> SendRequestAsync()
|
||||
{
|
||||
if (Worker != null)
|
||||
if (_worker != null)
|
||||
{
|
||||
await Worker.SendRequestAsync();
|
||||
await _worker.SendRequestAsync();
|
||||
}
|
||||
else if (GamingWorker != null)
|
||||
else if (_gamingWorker != null)
|
||||
{
|
||||
await GamingWorker.SendRequestAsync();
|
||||
await _gamingWorker.SendRequestAsync();
|
||||
}
|
||||
return Result;
|
||||
}
|
||||
@ -172,13 +199,13 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
||||
/// <returns></returns>
|
||||
public T? GetResult<T>(string key)
|
||||
{
|
||||
if (Worker != null)
|
||||
if (_worker != null)
|
||||
{
|
||||
return GetDictionaryJsonObject<T>(Worker.ResultData, key);
|
||||
return GetDictionaryJsonObject<T>(_worker.ResultData, key);
|
||||
}
|
||||
else if (GamingWorker != null)
|
||||
else if (_gamingWorker != null)
|
||||
{
|
||||
return GetDictionaryJsonObject<T>(GamingWorker.ResultData, key);
|
||||
return GetDictionaryJsonObject<T>(_gamingWorker.ResultData, key);
|
||||
}
|
||||
return default;
|
||||
}
|
||||
@ -203,22 +230,22 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
||||
private RequestResult _Result = RequestResult.Missing;
|
||||
private string _Error = "";
|
||||
|
||||
public SocketRequest(Socket? Socket, DataRequestType RequestType, Guid RequestID, bool IsLongRunning = false, SocketRuntimeType RuntimeType = SocketRuntimeType.Client) : base(Socket)
|
||||
public SocketRequest(Socket? socket, DataRequestType type, Guid requestId, bool longRunning = false, SocketRuntimeType runtime = SocketRuntimeType.Client) : base(socket)
|
||||
{
|
||||
this.Socket = Socket;
|
||||
this.RequestType = RequestType;
|
||||
this.RequestID = RequestID;
|
||||
this.IsLongRunning = IsLongRunning;
|
||||
this.RuntimeType = RuntimeType;
|
||||
Socket = socket;
|
||||
RequestType = type;
|
||||
RequestID = requestId;
|
||||
IsLongRunning = longRunning;
|
||||
RuntimeType = runtime;
|
||||
}
|
||||
|
||||
public SocketRequest(HTTPClient? HTTPClient, DataRequestType RequestType, Guid RequestID, bool IsLongRunning = false, SocketRuntimeType RuntimeType = SocketRuntimeType.Client) : base(HTTPClient)
|
||||
public SocketRequest(HTTPClient? client, DataRequestType type, Guid requestId, bool longRunning = false, SocketRuntimeType runtime = SocketRuntimeType.Client) : base(client)
|
||||
{
|
||||
this.HTTPClient = HTTPClient;
|
||||
this.RequestType = RequestType;
|
||||
this.RequestID = RequestID;
|
||||
this.IsLongRunning = IsLongRunning;
|
||||
this.RuntimeType = RuntimeType;
|
||||
HTTPClient = client;
|
||||
RequestType = type;
|
||||
RequestID = requestId;
|
||||
IsLongRunning = longRunning;
|
||||
RuntimeType = runtime;
|
||||
}
|
||||
|
||||
public void SendRequest()
|
||||
@ -279,20 +306,20 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
||||
}
|
||||
}
|
||||
|
||||
public override void SocketHandler(SocketObject SocketObject)
|
||||
public override void SocketHandler(SocketObject obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (SocketObject.SocketType == SocketMessageType.DataRequest)
|
||||
if (obj.SocketType == SocketMessageType.DataRequest)
|
||||
{
|
||||
DataRequestType type = SocketObject.GetParam<DataRequestType>(0);
|
||||
Guid id = SocketObject.GetParam<Guid>(1);
|
||||
DataRequestType type = obj.GetParam<DataRequestType>(0);
|
||||
Guid id = obj.GetParam<Guid>(1);
|
||||
if (type == RequestType && id == RequestID)
|
||||
{
|
||||
if (!IsLongRunning) Dispose();
|
||||
Work = SocketObject;
|
||||
ReceivedObject = obj;
|
||||
Working = false;
|
||||
_ResultData = SocketObject.GetParam<Dictionary<string, object>>(2) ?? [];
|
||||
_ResultData = obj.GetParam<Dictionary<string, object>>(2) ?? [];
|
||||
_Result = RequestResult.Success;
|
||||
}
|
||||
}
|
||||
@ -326,22 +353,22 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
||||
private RequestResult _Result = RequestResult.Missing;
|
||||
private string _Error = "";
|
||||
|
||||
public GamingRequest(Socket? Socket, GamingType GamingType, Guid RequestID, bool IsLongRunning = false, SocketRuntimeType RuntimeType = SocketRuntimeType.Client) : base(Socket)
|
||||
public GamingRequest(Socket? socket, GamingType type, Guid requestId, bool longRunning = false, SocketRuntimeType runtime = SocketRuntimeType.Client) : base(socket)
|
||||
{
|
||||
this.Socket = Socket;
|
||||
this.GamingType = GamingType;
|
||||
this.RequestID = RequestID;
|
||||
this.IsLongRunning = IsLongRunning;
|
||||
this.RuntimeType = RuntimeType;
|
||||
Socket = socket;
|
||||
GamingType = type;
|
||||
RequestID = requestId;
|
||||
IsLongRunning = longRunning;
|
||||
RuntimeType = runtime;
|
||||
}
|
||||
|
||||
public GamingRequest(HTTPClient? HTTPClient, GamingType GamingType, Guid RequestID, bool IsLongRunning = false, SocketRuntimeType RuntimeType = SocketRuntimeType.Client) : base(HTTPClient)
|
||||
public GamingRequest(HTTPClient? client, GamingType type, Guid requestId, bool longRunning = false, SocketRuntimeType runtime = SocketRuntimeType.Client) : base(client)
|
||||
{
|
||||
this.HTTPClient = HTTPClient;
|
||||
this.GamingType = GamingType;
|
||||
this.RequestID = RequestID;
|
||||
this.IsLongRunning = IsLongRunning;
|
||||
this.RuntimeType = RuntimeType;
|
||||
HTTPClient = client;
|
||||
GamingType = type;
|
||||
RequestID = requestId;
|
||||
IsLongRunning = longRunning;
|
||||
RuntimeType = runtime;
|
||||
}
|
||||
|
||||
public void SendRequest()
|
||||
@ -402,20 +429,20 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
||||
}
|
||||
}
|
||||
|
||||
public override void SocketHandler(SocketObject SocketObject)
|
||||
public override void SocketHandler(SocketObject obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (SocketObject.SocketType == SocketMessageType.GamingRequest)
|
||||
if (obj.SocketType == SocketMessageType.GamingRequest)
|
||||
{
|
||||
GamingType type = SocketObject.GetParam<GamingType>(0);
|
||||
Guid id = SocketObject.GetParam<Guid>(1);
|
||||
GamingType type = obj.GetParam<GamingType>(0);
|
||||
Guid id = obj.GetParam<Guid>(1);
|
||||
if (type == GamingType && id == RequestID)
|
||||
{
|
||||
if (!IsLongRunning) Dispose();
|
||||
Work = SocketObject;
|
||||
ReceivedObject = obj;
|
||||
Working = false;
|
||||
_ResultData = SocketObject.GetParam<Dictionary<string, object>>(2) ?? [];
|
||||
_ResultData = obj.GetParam<Dictionary<string, object>>(2) ?? [];
|
||||
_Result = RequestResult.Success;
|
||||
}
|
||||
}
|
||||
|
@ -671,13 +671,13 @@ namespace Milimoe.FunGame.Core.Api.Utility
|
||||
public class TaskUtility
|
||||
{
|
||||
/// <summary>
|
||||
/// 开启一个任务:调用返回对象的OnCompleted()方法可以执行后续操作,支持异步
|
||||
/// 开启一个异步任务。调用返回对象的 OnCompleted() 方法可以执行后续操作
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
public static TaskAwaiter NewTask(Action action) => new(Service.TaskManager.NewTask(action));
|
||||
|
||||
/// <summary>
|
||||
/// 开启一个任务:调用返回对象的OnCompleted()方法可以执行后续操作,支持异步
|
||||
/// 开启一个异步任务。调用返回对象的 OnCompleted() 方法可以执行后续操作
|
||||
/// </summary>
|
||||
/// <param name="task"></param>
|
||||
public static TaskAwaiter NewTask(Func<Task> task) => new(Service.TaskManager.NewTask(task));
|
||||
|
@ -15,17 +15,17 @@ namespace Milimoe.FunGame.Core.Controller
|
||||
/// <summary>
|
||||
/// 接收到的SocketObject实例
|
||||
/// </summary>
|
||||
protected override SocketObject Work { get; set; }
|
||||
protected override SocketObject ReceivedObject { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Socket
|
||||
/// </summary>
|
||||
private readonly Socket? _Socket;
|
||||
private readonly Socket? _socket;
|
||||
|
||||
/// <summary>
|
||||
/// WebSocket
|
||||
/// </summary>
|
||||
private readonly HTTPClient? _WebSocket;
|
||||
private readonly HTTPClient? _webSocket;
|
||||
|
||||
/// <summary>
|
||||
/// 继承请调用base构造
|
||||
@ -35,7 +35,7 @@ namespace Milimoe.FunGame.Core.Controller
|
||||
{
|
||||
if (socket != null)
|
||||
{
|
||||
_Socket = socket;
|
||||
_socket = socket;
|
||||
socket.AddSocketObjectHandler(SocketHandler);
|
||||
}
|
||||
else throw new SocketCreateReceivingException();
|
||||
@ -49,7 +49,7 @@ namespace Milimoe.FunGame.Core.Controller
|
||||
{
|
||||
if (websocket != null)
|
||||
{
|
||||
_WebSocket = websocket;
|
||||
_webSocket = websocket;
|
||||
websocket.AddSocketObjectHandler(SocketHandler);
|
||||
}
|
||||
else throw new SocketCreateReceivingException();
|
||||
@ -67,7 +67,7 @@ namespace Milimoe.FunGame.Core.Controller
|
||||
/// <summary>
|
||||
/// 判断是否已经Disposed
|
||||
/// </summary>
|
||||
private bool IsDisposed = false;
|
||||
private bool _isDisposed = false;
|
||||
|
||||
/// <summary>
|
||||
/// 公开的Dispose方法
|
||||
@ -85,15 +85,15 @@ namespace Milimoe.FunGame.Core.Controller
|
||||
/// <param name="Disposing"></param>
|
||||
protected void Dispose(bool Disposing)
|
||||
{
|
||||
if (!IsDisposed)
|
||||
if (!_isDisposed)
|
||||
{
|
||||
if (Disposing)
|
||||
{
|
||||
_Socket?.RemoveSocketObjectHandler(SocketHandler);
|
||||
_WebSocket?.RemoveSocketObjectHandler(SocketHandler);
|
||||
_socket?.RemoveSocketObjectHandler(SocketHandler);
|
||||
_webSocket?.RemoveSocketObjectHandler(SocketHandler);
|
||||
}
|
||||
}
|
||||
IsDisposed = true;
|
||||
_isDisposed = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -9,7 +9,7 @@
|
||||
/// <summary>
|
||||
/// 接收到的实例
|
||||
/// </summary>
|
||||
protected abstract T? Work { get; set; }
|
||||
protected abstract T? ReceivedObject { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否处于等待的状态
|
||||
@ -22,7 +22,7 @@
|
||||
protected virtual void SetWorking()
|
||||
{
|
||||
Working = true;
|
||||
Work = default;
|
||||
ReceivedObject = default;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -33,9 +33,25 @@ namespace Milimoe.FunGame.Core.Library.Common.Architecture
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回TaskAwaiter可以连续的调用方法<para/>
|
||||
/// 但是意义不大,前一个OnCompleted方法并不会等待下一个方法<para/>
|
||||
/// 可以理解为并行广播<para/>
|
||||
/// 返回 TaskAwaiter 可以进一步等待并执行方法<para/>
|
||||
/// 注意事项:async () 委托的后续 OnCompleted 方法将不会进一步等待,而是直接执行,因为它是异步的<para/>
|
||||
/// <example>
|
||||
/// 这意味着你不能这样操作:
|
||||
/// <code>
|
||||
/// TaskUtility.NewTask(() =>
|
||||
/// {
|
||||
/// Console.WriteLine(0);
|
||||
/// }).OnCompleted(async () =>
|
||||
/// {
|
||||
/// await Task.Delay(3000);
|
||||
/// Console.WriteLine(1);
|
||||
/// }).OnCompleted(() =>
|
||||
/// {
|
||||
/// Console.WriteLine(2);
|
||||
/// });
|
||||
/// </code>
|
||||
/// 上述代码将导致:任务输出 0 之后,2 不会等待 1 的完成,因此会直接输出 2,再输出 1.
|
||||
/// </example>
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
/// <returns></returns>
|
||||
|
@ -4,20 +4,41 @@ using Milimoe.FunGame.Core.Service;
|
||||
|
||||
namespace Milimoe.FunGame.Core.Library.Common.Network
|
||||
{
|
||||
[Serializable]
|
||||
/// <summary>
|
||||
/// 唯一指定的通信数据结构
|
||||
/// </summary>
|
||||
public readonly struct SocketObject
|
||||
{
|
||||
/// <summary>
|
||||
/// 通信类型
|
||||
/// </summary>
|
||||
public SocketMessageType SocketType { get; } = SocketMessageType.Unknown;
|
||||
|
||||
/// <summary>
|
||||
/// 通信令牌
|
||||
/// </summary>
|
||||
public Guid Token { get; } = Guid.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 参数列表
|
||||
/// </summary>
|
||||
public object[] Parameters { get; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// 参数数量
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
public int Length => Parameters.Length;
|
||||
|
||||
// 从参数列表中获取指定索引的参数的Json字符串
|
||||
// -- 此索引器仅返回Json字符串,对象类型请使用反序列化方法GetParam<T>() --
|
||||
// -- 当然也可以自己反序列化 --
|
||||
// -- 基本类型可能有效,但仍建议使用反序列化方法 --
|
||||
/// <summary>
|
||||
/// 从参数列表中获取指定索引的参数的Json对象<para/>
|
||||
/// -- 此索引器仅返回Json对象,获取实例请使用反序列化方法GetParam[T]() --<para/>
|
||||
/// -- 当然也可以自己反序列化 --<para/>
|
||||
/// -- 基本类型可能有效,但仍建议使用反序列化方法 --
|
||||
/// </summary>
|
||||
/// <param name="index"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="IndexOutOfArrayLengthException">索引超过数组上限</exception>
|
||||
public object? this[int index]
|
||||
{
|
||||
get
|
||||
@ -28,6 +49,12 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构建通信数据对象
|
||||
/// </summary>
|
||||
/// <param name="socketType"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <param name="parameters"></param>
|
||||
[JsonConstructor]
|
||||
public SocketObject(SocketMessageType socketType, Guid token, params object[] parameters)
|
||||
{
|
||||
|
@ -5,14 +5,14 @@ namespace Milimoe.FunGame.Core.Service
|
||||
internal class TaskManager
|
||||
{
|
||||
/// <summary>
|
||||
/// 开启一个任务:调用返回对象的OnCompleted()方法可以执行后续操作,支持异步
|
||||
/// 开启一个任务:调用返回对象的 OnCompleted() 方法可以执行后续操作,支持异步
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
/// <returns></returns>
|
||||
internal static ITaskAwaiter NewTask(Action action) => new TaskAwaiter(action);
|
||||
|
||||
/// <summary>
|
||||
/// 开启一个任务:调用返回对象的OnCompleted()方法可以执行后续操作,支持异步
|
||||
/// 开启一个任务:调用返回对象的 OnCompleted() 方法可以执行后续操作,支持异步
|
||||
/// </summary>
|
||||
/// <param name="function"></param>
|
||||
/// <returns></returns>
|
||||
@ -39,21 +39,22 @@ namespace Milimoe.FunGame.Core.Service
|
||||
internal TaskAwaiter(Func<Task> function) => Worker(function);
|
||||
|
||||
/// <summary>
|
||||
/// 返回ITaskAwaiter可以进一步调用方法<para/>
|
||||
/// 但是意义不大,前一个OnCompleted方法并不会等待下一个方法<para/>
|
||||
/// 可以理解为并行广播<para/>
|
||||
/// 返回 ITaskAwaiter 可以进一步等待并执行方法<para/>
|
||||
/// 注意事项:async () 委托的后续 OnCompleted 方法将不会进一步等待,而是直接执行,因为它是异步的
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
/// <returns></returns>
|
||||
public ITaskAwaiter OnCompleted(Action action)
|
||||
{
|
||||
if (IsCompleted) action();
|
||||
else Completed += new CompletedEvent(action);
|
||||
Completed += () =>
|
||||
{
|
||||
action();
|
||||
};
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在捕获到异常时,将触发Error事件
|
||||
/// 在捕获到异常时,将触发 Error 事件
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
/// <returns></returns>
|
||||
|
Loading…
x
Reference in New Issue
Block a user