diff --git a/Api/Transmittal/DataRequest.cs b/Api/Transmittal/DataRequest.cs
index 083eb2b..df65b64 100644
--- a/Api/Transmittal/DataRequest.cs
+++ b/Api/Transmittal/DataRequest.cs
@@ -10,17 +10,22 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
/// 如果是 的数据请求,则配合 使用
/// 确保已添加对应的枚举
///
- public class DataRequest
+ public class DataRequest : IDisposable
{
///
/// 数据请求结果
///
- 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);
///
/// 详细错误信息
///
- public string Error => Worker != null ? Worker.Error : (GamingWorker != null ? GamingWorker.Error : "");
+ public string Error => _worker != null ? _worker.Error : (_gamingWorker != null ? _gamingWorker.Error : "");
+
+ ///
+ /// 是否已经关闭
+ ///
+ public bool IsDisposed => _isDisposed;
// 获取ResultData中key值对应的Json字符串
// -- 此索引器仅返回Json字符串,对象类型请使用反序列化方法GetResult() --
@@ -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
///
/// 私有的实现类
///
- private readonly SocketRequest? Worker;
+ private readonly SocketRequest? _worker;
///
/// 私有的实现类(这是局内请求的)
///
- private readonly GamingRequest? GamingWorker;
+ private readonly GamingRequest? _gamingWorker;
+
+ ///
+ /// 指示关闭的变量
+ ///
+ private bool _isDisposed = false;
///
/// 基于本地已连接的 创建新的数据请求
/// 使用 中的 创建一个新的请求
/// 插件则使用 中的 创建一个新的请求
///
- ///
- ///
- ///
- ///
- 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);
}
///
@@ -70,13 +80,13 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
/// 插件则使用 中的 创建一个新的请求
/// 此数据请求只能调用异步方法 请求数据
///
- ///
- ///
- ///
- ///
- 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);
}
///
@@ -84,13 +94,13 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
/// 使用 中的 创建一个新的请求
/// 此构造方法是给 提供的
///
- ///
- ///
- ///
- ///
- 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);
}
///
@@ -99,13 +109,13 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
/// 此构造方法是给 提供的
/// 此数据请求只能调用异步方法 请求数据
///
- ///
- ///
- ///
- ///
- 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);
}
///
@@ -115,13 +125,13 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
///
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
///
public void Dispose()
{
- Worker?.Dispose();
- GamingWorker?.Dispose();
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
+
+ ///
+ /// 关闭时
+ ///
+ ///
+ protected void Dispose(bool disposing)
+ {
+ if (!_isDisposed)
+ {
+ if (disposing)
+ {
+ _worker?.Dispose();
+ _gamingWorker?.Dispose();
+ }
+ }
+ _isDisposed = true;
}
///
@@ -142,8 +169,8 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
///
public RequestResult SendRequest()
{
- Worker?.SendRequest();
- GamingWorker?.SendRequest();
+ _worker?.SendRequest();
+ _gamingWorker?.SendRequest();
return Result;
}
@@ -153,13 +180,13 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
///
public async Task 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
///
public T? GetResult(string key)
{
- if (Worker != null)
+ if (_worker != null)
{
- return GetDictionaryJsonObject(Worker.ResultData, key);
+ return GetDictionaryJsonObject(_worker.ResultData, key);
}
- else if (GamingWorker != null)
+ else if (_gamingWorker != null)
{
- return GetDictionaryJsonObject(GamingWorker.ResultData, key);
+ return GetDictionaryJsonObject(_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(0);
- Guid id = SocketObject.GetParam(1);
+ DataRequestType type = obj.GetParam(0);
+ Guid id = obj.GetParam(1);
if (type == RequestType && id == RequestID)
{
if (!IsLongRunning) Dispose();
- Work = SocketObject;
+ ReceivedObject = obj;
Working = false;
- _ResultData = SocketObject.GetParam>(2) ?? [];
+ _ResultData = obj.GetParam>(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(0);
- Guid id = SocketObject.GetParam(1);
+ GamingType type = obj.GetParam(0);
+ Guid id = obj.GetParam(1);
if (type == GamingType && id == RequestID)
{
if (!IsLongRunning) Dispose();
- Work = SocketObject;
+ ReceivedObject = obj;
Working = false;
- _ResultData = SocketObject.GetParam>(2) ?? [];
+ _ResultData = obj.GetParam>(2) ?? [];
_Result = RequestResult.Success;
}
}
diff --git a/Api/Utility/General.cs b/Api/Utility/General.cs
index bd54cc8..5e0edb4 100644
--- a/Api/Utility/General.cs
+++ b/Api/Utility/General.cs
@@ -671,13 +671,13 @@ namespace Milimoe.FunGame.Core.Api.Utility
public class TaskUtility
{
///
- /// 开启一个任务:调用返回对象的OnCompleted()方法可以执行后续操作,支持异步
+ /// 开启一个异步任务。调用返回对象的 OnCompleted() 方法可以执行后续操作
///
///
public static TaskAwaiter NewTask(Action action) => new(Service.TaskManager.NewTask(action));
///
- /// 开启一个任务:调用返回对象的OnCompleted()方法可以执行后续操作,支持异步
+ /// 开启一个异步任务。调用返回对象的 OnCompleted() 方法可以执行后续操作
///
///
public static TaskAwaiter NewTask(Func task) => new(Service.TaskManager.NewTask(task));
diff --git a/Controller/SocketHandlerController.cs b/Controller/SocketHandlerController.cs
index 4407a3e..eb82ea8 100644
--- a/Controller/SocketHandlerController.cs
+++ b/Controller/SocketHandlerController.cs
@@ -15,17 +15,17 @@ namespace Milimoe.FunGame.Core.Controller
///
/// 接收到的SocketObject实例
///
- protected override SocketObject Work { get; set; }
+ protected override SocketObject ReceivedObject { get; set; }
///
/// Socket
///
- private readonly Socket? _Socket;
+ private readonly Socket? _socket;
///
/// WebSocket
///
- private readonly HTTPClient? _WebSocket;
+ private readonly HTTPClient? _webSocket;
///
/// 继承请调用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
///
/// 判断是否已经Disposed
///
- private bool IsDisposed = false;
+ private bool _isDisposed = false;
///
/// 公开的Dispose方法
@@ -85,15 +85,15 @@ namespace Milimoe.FunGame.Core.Controller
///
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;
}
///
diff --git a/Library/Common/Architecture/AsyncAwaiter.cs b/Library/Common/Architecture/AsyncAwaiter.cs
index b5987ff..8ca1f2a 100644
--- a/Library/Common/Architecture/AsyncAwaiter.cs
+++ b/Library/Common/Architecture/AsyncAwaiter.cs
@@ -9,7 +9,7 @@
///
/// 接收到的实例
///
- protected abstract T? Work { get; set; }
+ protected abstract T? ReceivedObject { get; set; }
///
/// 是否处于等待的状态
@@ -22,7 +22,7 @@
protected virtual void SetWorking()
{
Working = true;
- Work = default;
+ ReceivedObject = default;
}
///
diff --git a/Library/Common/Architecture/TaskAwaiter.cs b/Library/Common/Architecture/TaskAwaiter.cs
index fada6e9..254f44c 100644
--- a/Library/Common/Architecture/TaskAwaiter.cs
+++ b/Library/Common/Architecture/TaskAwaiter.cs
@@ -33,9 +33,25 @@ namespace Milimoe.FunGame.Core.Library.Common.Architecture
}
///
- /// 返回TaskAwaiter可以连续的调用方法
- /// 但是意义不大,前一个OnCompleted方法并不会等待下一个方法
- /// 可以理解为并行广播
+ /// 返回 TaskAwaiter 可以进一步等待并执行方法
+ /// 注意事项:async () 委托的后续 OnCompleted 方法将不会进一步等待,而是直接执行,因为它是异步的
+ ///
+ /// 这意味着你不能这样操作:
+ ///
+ /// TaskUtility.NewTask(() =>
+ /// {
+ /// Console.WriteLine(0);
+ /// }).OnCompleted(async () =>
+ /// {
+ /// await Task.Delay(3000);
+ /// Console.WriteLine(1);
+ /// }).OnCompleted(() =>
+ /// {
+ /// Console.WriteLine(2);
+ /// });
+ ///
+ /// 上述代码将导致:任务输出 0 之后,2 不会等待 1 的完成,因此会直接输出 2,再输出 1.
+ ///
///
///
///
diff --git a/Library/Common/Network/SocketObject.cs b/Library/Common/Network/SocketObject.cs
index e5bd0ba..d44e757 100644
--- a/Library/Common/Network/SocketObject.cs
+++ b/Library/Common/Network/SocketObject.cs
@@ -4,20 +4,41 @@ using Milimoe.FunGame.Core.Service;
namespace Milimoe.FunGame.Core.Library.Common.Network
{
- [Serializable]
+ ///
+ /// 唯一指定的通信数据结构
+ ///
public readonly struct SocketObject
{
+ ///
+ /// 通信类型
+ ///
public SocketMessageType SocketType { get; } = SocketMessageType.Unknown;
+
+ ///
+ /// 通信令牌
+ ///
public Guid Token { get; } = Guid.Empty;
+
+ ///
+ /// 参数列表
+ ///
public object[] Parameters { get; } = [];
+ ///
+ /// 参数数量
+ ///
[JsonIgnore]
public int Length => Parameters.Length;
- // 从参数列表中获取指定索引的参数的Json字符串
- // -- 此索引器仅返回Json字符串,对象类型请使用反序列化方法GetParam() --
- // -- 当然也可以自己反序列化 --
- // -- 基本类型可能有效,但仍建议使用反序列化方法 --
+ ///
+ /// 从参数列表中获取指定索引的参数的Json对象
+ /// -- 此索引器仅返回Json对象,获取实例请使用反序列化方法GetParam[T]() --
+ /// -- 当然也可以自己反序列化 --
+ /// -- 基本类型可能有效,但仍建议使用反序列化方法 --
+ ///
+ ///
+ ///
+ /// 索引超过数组上限
public object? this[int index]
{
get
@@ -28,6 +49,12 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
}
}
+ ///
+ /// 构建通信数据对象
+ ///
+ ///
+ ///
+ ///
[JsonConstructor]
public SocketObject(SocketMessageType socketType, Guid token, params object[] parameters)
{
diff --git a/Service/TaskManager.cs b/Service/TaskManager.cs
index 96d7572..021ed94 100644
--- a/Service/TaskManager.cs
+++ b/Service/TaskManager.cs
@@ -5,14 +5,14 @@ namespace Milimoe.FunGame.Core.Service
internal class TaskManager
{
///
- /// 开启一个任务:调用返回对象的OnCompleted()方法可以执行后续操作,支持异步
+ /// 开启一个任务:调用返回对象的 OnCompleted() 方法可以执行后续操作,支持异步
///
///
///
internal static ITaskAwaiter NewTask(Action action) => new TaskAwaiter(action);
///
- /// 开启一个任务:调用返回对象的OnCompleted()方法可以执行后续操作,支持异步
+ /// 开启一个任务:调用返回对象的 OnCompleted() 方法可以执行后续操作,支持异步
///
///
///
@@ -39,21 +39,22 @@ namespace Milimoe.FunGame.Core.Service
internal TaskAwaiter(Func function) => Worker(function);
///
- /// 返回ITaskAwaiter可以进一步调用方法
- /// 但是意义不大,前一个OnCompleted方法并不会等待下一个方法
- /// 可以理解为并行广播
+ /// 返回 ITaskAwaiter 可以进一步等待并执行方法
+ /// 注意事项:async () 委托的后续 OnCompleted 方法将不会进一步等待,而是直接执行,因为它是异步的
///
///
///
public ITaskAwaiter OnCompleted(Action action)
{
- if (IsCompleted) action();
- else Completed += new CompletedEvent(action);
+ Completed += () =>
+ {
+ action();
+ };
return this;
}
///
- /// 在捕获到异常时,将触发Error事件
+ /// 在捕获到异常时,将触发 Error 事件
///
///
///