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