From 924a45413a14715e178c97f7c3227fca18b43ece Mon Sep 17 00:00:00 2001 From: milimoe <110188673+milimoe@users.noreply.github.com> Date: Thu, 8 Aug 2024 12:18:20 +0800 Subject: [PATCH] =?UTF-8?q?#80=EF=BC=9A=E4=B8=BAAddon=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=E4=B8=93=E7=94=A8=E7=9A=84DataRequest=E6=96=B9?= =?UTF-8?q?=E6=B3=95=20(#84)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Api/Transmittal/DataRequest.cs | 64 ++++++++++++++++++++------ Controller/RunTimeController.cs | 46 +++++++++++++++--- Docs/FunGame.Core.xml | 44 ++++++++++++++---- Library/Common/Network/ServerSocket.cs | 4 +- Library/Constant/ConstantSet.cs | 1 + Library/Constant/TypeEnum.cs | 3 +- Model/RoomList.cs | 13 ++---- Service/ModelManager.cs | 23 +++++++-- 8 files changed, 152 insertions(+), 46 deletions(-) diff --git a/Api/Transmittal/DataRequest.cs b/Api/Transmittal/DataRequest.cs index 8ce30e8..1377317 100644 --- a/Api/Transmittal/DataRequest.cs +++ b/Api/Transmittal/DataRequest.cs @@ -54,53 +54,59 @@ namespace Milimoe.FunGame.Core.Api.Transmittal /// /// 基于本地已连接的 创建新的数据请求 /// 使用 中的 创建一个新的请求 + /// 插件则使用 中的 创建一个新的请求 /// /// /// /// - internal DataRequest(Socket Socket, DataRequestType RequestType, bool IsLongRunning = false) + /// + internal DataRequest(Socket Socket, DataRequestType RequestType, bool IsLongRunning = false, SocketRuntimeType RuntimeType = SocketRuntimeType.Client) { - Worker = new(Socket, RequestType, Guid.NewGuid(), IsLongRunning); + Worker = new(Socket, RequestType, Guid.NewGuid(), IsLongRunning, RuntimeType); } /// /// 基于本地已连接的 创建新的数据请求 /// 使用 中的 创建一个新的请求 + /// 插件则使用 中的 创建一个新的请求 /// 此数据请求只能调用异步方法 请求数据 /// /// /// /// - internal DataRequest(HTTPClient WebSocket, DataRequestType RequestType, bool IsLongRunning = false) + /// + internal DataRequest(HTTPClient WebSocket, DataRequestType RequestType, bool IsLongRunning = false, SocketRuntimeType RuntimeType = SocketRuntimeType.Client) { - Worker = new(WebSocket, RequestType, Guid.NewGuid(), IsLongRunning); + Worker = new(WebSocket, RequestType, Guid.NewGuid(), IsLongRunning, RuntimeType); } /// /// 基于本地已连接的 创建新的局内()数据请求 - /// 使用 中的 创建一个新的请求 + /// 使用 中的 创建一个新的请求 /// 此构造方法是给 提供的 /// /// /// /// - internal DataRequest(Socket Socket, GamingType GamingType, bool IsLongRunning = false) + /// + internal DataRequest(Socket Socket, GamingType GamingType, bool IsLongRunning = false, SocketRuntimeType RuntimeType = SocketRuntimeType.Client) { - GamingWorker = new(Socket, GamingType, Guid.NewGuid(), IsLongRunning); + GamingWorker = new(Socket, GamingType, Guid.NewGuid(), IsLongRunning, RuntimeType); } /// /// 基于本地已连接的 创建新的局内()数据请求 - /// 使用 中的 创建一个新的请求 + /// 使用 中的 创建一个新的请求 /// 此构造方法是给 提供的 /// 此数据请求只能调用异步方法 请求数据 /// /// /// /// - internal DataRequest(HTTPClient WebSocket, GamingType GamingType, bool IsLongRunning = false) + /// + internal DataRequest(HTTPClient WebSocket, GamingType GamingType, bool IsLongRunning = false, SocketRuntimeType RuntimeType = SocketRuntimeType.Client) { - GamingWorker = new(WebSocket, GamingType, Guid.NewGuid(), IsLongRunning); + GamingWorker = new(WebSocket, GamingType, Guid.NewGuid(), IsLongRunning, RuntimeType); } /// @@ -195,24 +201,27 @@ namespace Milimoe.FunGame.Core.Api.Transmittal private readonly DataRequestType RequestType = DataRequestType.UnKnown; private readonly Guid RequestID = Guid.Empty; private readonly bool IsLongRunning = false; + private readonly SocketRuntimeType RuntimeType = SocketRuntimeType.Client; private Hashtable _ResultData = []; private RequestResult _Result = RequestResult.Missing; private string _Error = ""; - public SocketRequest(Socket? Socket, DataRequestType RequestType, Guid RequestID, bool IsLongRunning = false) : base(Socket) + public SocketRequest(Socket? Socket, DataRequestType RequestType, Guid RequestID, bool IsLongRunning = false, SocketRuntimeType RuntimeType = SocketRuntimeType.Client) : base(Socket) { this.Socket = Socket; this.RequestType = RequestType; this.RequestID = RequestID; this.IsLongRunning = IsLongRunning; + this.RuntimeType = RuntimeType; } - public SocketRequest(HTTPClient? WebSocket, DataRequestType RequestType, Guid RequestID, bool IsLongRunning = false) : base(WebSocket) + public SocketRequest(HTTPClient? WebSocket, DataRequestType RequestType, Guid RequestID, bool IsLongRunning = false, SocketRuntimeType RuntimeType = SocketRuntimeType.Client) : base(WebSocket) { this.WebSocket = WebSocket; this.RequestType = RequestType; this.RequestID = RequestID; this.IsLongRunning = IsLongRunning; + this.RuntimeType = RuntimeType; } public void SendRequest() @@ -220,6 +229,12 @@ namespace Milimoe.FunGame.Core.Api.Transmittal try { SetWorking(); + if (RuntimeType == SocketRuntimeType.Addon || RuntimeType == SocketRuntimeType.Addon) + { + if (RequestData.ContainsKey(SocketSet.Plugins_Mark)) RequestData[SocketSet.Plugins_Mark] = "true"; + else RequestData.Add(SocketSet.Plugins_Mark, true); + } + else RequestData.Remove(SocketSet.Plugins_Mark); if (Socket != null && Socket.Send(SocketMessageType.DataRequest, RequestType, RequestID, RequestData) == SocketResult.Success) { WaitForWorkDone(); @@ -243,6 +258,12 @@ namespace Milimoe.FunGame.Core.Api.Transmittal try { SetWorking(); + if (RuntimeType == SocketRuntimeType.Addon || RuntimeType == SocketRuntimeType.Addon) + { + if (RequestData.ContainsKey(SocketSet.Plugins_Mark)) RequestData[SocketSet.Plugins_Mark] = "true"; + else RequestData.Add(SocketSet.Plugins_Mark, true); + } + else RequestData.Remove(SocketSet.Plugins_Mark); if (Socket != null && Socket.Send(SocketMessageType.DataRequest, RequestType, RequestID, RequestData) == SocketResult.Success) { await WaitForWorkDoneAsync(); @@ -303,24 +324,27 @@ namespace Milimoe.FunGame.Core.Api.Transmittal private readonly GamingType GamingType = GamingType.None; private readonly Guid RequestID = Guid.Empty; private readonly bool IsLongRunning = false; + private readonly SocketRuntimeType RuntimeType = SocketRuntimeType.Client; private Hashtable _ResultData = []; private RequestResult _Result = RequestResult.Missing; private string _Error = ""; - public GamingRequest(Socket? Socket, GamingType GamingType, Guid RequestID, bool IsLongRunning = false) : base(Socket) + public GamingRequest(Socket? Socket, GamingType GamingType, Guid RequestID, bool IsLongRunning = false, SocketRuntimeType RuntimeType = SocketRuntimeType.Client) : base(Socket) { this.Socket = Socket; this.GamingType = GamingType; this.RequestID = RequestID; this.IsLongRunning = IsLongRunning; + this.RuntimeType = RuntimeType; } - public GamingRequest(HTTPClient? WebSocket, GamingType GamingType, Guid RequestID, bool IsLongRunning = false) : base(WebSocket) + public GamingRequest(HTTPClient? WebSocket, GamingType GamingType, Guid RequestID, bool IsLongRunning = false, SocketRuntimeType RuntimeType = SocketRuntimeType.Client) : base(WebSocket) { this.WebSocket = WebSocket; this.GamingType = GamingType; this.RequestID = RequestID; this.IsLongRunning = IsLongRunning; + this.RuntimeType = RuntimeType; } public void SendRequest() @@ -328,6 +352,12 @@ namespace Milimoe.FunGame.Core.Api.Transmittal try { SetWorking(); + if (RuntimeType == SocketRuntimeType.Addon || RuntimeType == SocketRuntimeType.Addon) + { + if (RequestData.ContainsKey(SocketSet.Plugins_Mark)) RequestData[SocketSet.Plugins_Mark] = "true"; + else RequestData.Add(SocketSet.Plugins_Mark, true); + } + else RequestData.Remove(SocketSet.Plugins_Mark); if (Socket != null && Socket.Send(SocketMessageType.DataRequest, GamingType, RequestID, RequestData) == SocketResult.Success) { WaitForWorkDone(); @@ -351,6 +381,12 @@ namespace Milimoe.FunGame.Core.Api.Transmittal try { SetWorking(); + if (RuntimeType == SocketRuntimeType.Addon || RuntimeType == SocketRuntimeType.Addon) + { + if (RequestData.ContainsKey(SocketSet.Plugins_Mark)) RequestData[SocketSet.Plugins_Mark] = "true"; + else RequestData.Add(SocketSet.Plugins_Mark, true); + } + else RequestData.Remove(SocketSet.Plugins_Mark); if (Socket != null && Socket.Send(SocketMessageType.DataRequest, GamingType, RequestID, RequestData) == SocketResult.Success) { await WaitForWorkDoneAsync(); diff --git a/Controller/RunTimeController.cs b/Controller/RunTimeController.cs index ed17982..e51d998 100644 --- a/Controller/RunTimeController.cs +++ b/Controller/RunTimeController.cs @@ -285,19 +285,53 @@ namespace Milimoe.FunGame.Core.Controller } throw new ConnectFailedException(); } + + /// + /// 基于本地已连接的Socket创建新的数据请求 + /// 加载项专用( / ) + /// + /// + /// + /// + public DataRequest NewDataRequestForAddon(DataRequestType RequestType) + { + if (_Socket != null) + { + DataRequest request = new(_Socket, RequestType, false, SocketRuntimeType.Addon); + return request; + } + throw new ConnectFailedException(); + } + + /// + /// 基于本地已连接的Socket创建长时间运行的数据请求 + /// 加载项专用( / ) + /// + /// + /// + /// + public DataRequest NewLongRunningDataRequestForAddon(DataRequestType RequestType) + { + if (_Socket != null) + { + DataRequest request = new(_Socket, RequestType, true, SocketRuntimeType.Addon); + return request; + } + throw new ConnectFailedException(); + } /// /// 基于本地已连接的Socket创建新的局内()数据请求 - /// 此方法是给 提供的 + /// 加载项专用:此方法是给 提供的 /// /// /// /// - public DataRequest NewDataRequest(GamingType GamingType) + public DataRequest NewDataRequestForAddon(GamingType GamingType) { if (_Socket != null) { - DataRequest request = new(_Socket, GamingType); + DataRequest request = new(_Socket, GamingType, false, SocketRuntimeType.Addon); return request; } throw new ConnectFailedException(); @@ -305,16 +339,16 @@ namespace Milimoe.FunGame.Core.Controller /// /// 基于本地已连接的Socket创建长时间运行的局内()数据请求 - /// 此方法是给 提供的 + /// 加载项专用:此方法是给 提供的 /// /// /// /// - public DataRequest NewLongRunningDataRequest(GamingType GamingType) + public DataRequest NewLongRunningDataRequestForAddon(GamingType GamingType) { if (_Socket != null) { - DataRequest request = new(_Socket, GamingType, true); + DataRequest request = new(_Socket, GamingType, true, SocketRuntimeType.Addon); return request; } throw new ConnectFailedException(); diff --git a/Docs/FunGame.Core.xml b/Docs/FunGame.Core.xml index 2877a40..ac911b1 100644 --- a/Docs/FunGame.Core.xml +++ b/Docs/FunGame.Core.xml @@ -31,45 +31,51 @@ 私有的实现类(这是局内请求的) - + 基于本地已连接的 创建新的数据请求 使用 中的 创建一个新的请求 + 插件则使用 中的 创建一个新的请求 + - + 基于本地已连接的 创建新的数据请求 使用 中的 创建一个新的请求 + 插件则使用 中的 创建一个新的请求 此数据请求只能调用异步方法 请求数据 + - + 基于本地已连接的 创建新的局内()数据请求 - 使用 中的 创建一个新的请求 + 使用 中的 创建一个新的请求 此构造方法是给 提供的 + - + 基于本地已连接的 创建新的局内()数据请求 - 使用 中的 创建一个新的请求 + 使用 中的 创建一个新的请求 此构造方法是给 提供的 此数据请求只能调用异步方法 请求数据 + @@ -1378,19 +1384,37 @@ - + + + 基于本地已连接的Socket创建新的数据请求 + 加载项专用( / ) + + + + + + + + 基于本地已连接的Socket创建长时间运行的数据请求 + 加载项专用( / ) + + + + + + 基于本地已连接的Socket创建新的局内()数据请求 - 此方法是给 提供的 + 加载项专用:此方法是给 提供的 - + 基于本地已连接的Socket创建长时间运行的局内()数据请求 - 此方法是给 提供的 + 加载项专用:此方法是给 提供的 diff --git a/Library/Common/Network/ServerSocket.cs b/Library/Common/Network/ServerSocket.cs index ffe5319..7c604b2 100644 --- a/Library/Common/Network/ServerSocket.cs +++ b/Library/Common/Network/ServerSocket.cs @@ -31,8 +31,8 @@ namespace Milimoe.FunGame.Core.Library.Common.Network this.ServerPort = ServerPort; if (MaxConnection <= 0) { - OnlineClients = new(); - OnlineUsers = new(); + OnlineClients = []; + OnlineUsers = []; } else { diff --git a/Library/Constant/ConstantSet.cs b/Library/Constant/ConstantSet.cs index 0a909ef..d4ea0df 100644 --- a/Library/Constant/ConstantSet.cs +++ b/Library/Constant/ConstantSet.cs @@ -33,6 +33,7 @@ namespace Milimoe.FunGame.Core.Library.Constant public const int MaxConnection_1C2G = 10; public const int MaxConnection_2C2G = 20; public const int MaxConnection_4C4G = 40; + public const string Plugins_Mark = "plugins_mark"; public const string Socket = "Socket"; public const string Unknown = "Unknown"; diff --git a/Library/Constant/TypeEnum.cs b/Library/Constant/TypeEnum.cs index e1a0dc2..1506cbf 100644 --- a/Library/Constant/TypeEnum.cs +++ b/Library/Constant/TypeEnum.cs @@ -144,8 +144,7 @@ namespace Milimoe.FunGame.Core.Library.Constant { Client, Server, - Client_Plugin, - Server_Plugin + Addon } public enum ErrorIPAddressType diff --git a/Model/RoomList.cs b/Model/RoomList.cs index 45e4a64..591e5ef 100644 --- a/Model/RoomList.cs +++ b/Model/RoomList.cs @@ -18,9 +18,9 @@ namespace Milimoe.FunGame.Core.Model public int GetReadyPlayerCount(string roomid) => GetReadyPlayerList(roomid).Count; - public List ListRoom => _List.Values.ToList(); + public List ListRoom => [.. _List.Values]; - public List ListRoomID => _List.Keys.ToList(); + public List ListRoomID => [.. _List.Keys]; public List GetPlayerList(string roomid) => _PlayerList.TryGetValue(roomid, out List? user) ? user : []; @@ -112,7 +112,7 @@ namespace Milimoe.FunGame.Core.Model } } - public IEnumerator GetEnumerator() + public IEnumerator GetEnumerator() { foreach (Room room in ListRoom) { @@ -120,12 +120,9 @@ namespace Milimoe.FunGame.Core.Model } } - IEnumerator IEnumerable.GetEnumerator() + IEnumerator IEnumerable.GetEnumerator() { - foreach (Room room in ListRoom) - { - yield return room; - } + return GetEnumerator(); } } } diff --git a/Service/ModelManager.cs b/Service/ModelManager.cs index 86b3fbb..ef69cc5 100644 --- a/Service/ModelManager.cs +++ b/Service/ModelManager.cs @@ -1,8 +1,10 @@ -using System.Collections.Concurrent; +using System.Collections; +using System.Collections.Concurrent; +using Milimoe.FunGame.Core.Entity; namespace Milimoe.FunGame.Core.Service { - internal class ModelManager + internal class ModelManager : IEnumerable { /// /// 目前的Model数量 @@ -17,7 +19,7 @@ namespace Milimoe.FunGame.Core.Service /// /// 可参与高并发的字典,但添加效率较低 /// - private ConcurrentDictionary Models { get; } = new(); + private ConcurrentDictionary Models { get; } = []; /// /// Init ModelManager @@ -103,7 +105,20 @@ namespace Milimoe.FunGame.Core.Service /// internal List GetList() { - return Models.Values.ToList(); + return [.. Models.Values]; + } + + public IEnumerator GetEnumerator() + { + foreach (T instance in Models.Values) + { + yield return instance; + } + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); } } }