diff --git a/FunGame.Core/Library/Common/Network/ServerSocket.cs b/FunGame.Core/Library/Common/Network/ServerSocket.cs index 7df6131..e0df93e 100644 --- a/FunGame.Core/Library/Common/Network/ServerSocket.cs +++ b/FunGame.Core/Library/Common/Network/ServerSocket.cs @@ -29,14 +29,21 @@ namespace Milimoe.FunGame.Core.Library.Common.Network public bool Receiving { get; private set; } = false; public bool SendingHeartBeat { get; private set; } = false; - private ServerSocket(System.Net.Sockets.Socket Instance, int ServerPort) + private readonly ThreadManager PlayerThreads; + + private ServerSocket(System.Net.Sockets.Socket Instance, int ServerPort, int MaxConnection = 0) { this.Instance = Instance; this.ServerPort = ServerPort; + if (MaxConnection <= 0) + PlayerThreads = new ThreadManager(); + else + PlayerThreads = new ThreadManager(MaxConnection); } - public ServerSocket StartListening(int Port = 22222, int MaxConnection = 20) + public ServerSocket StartListening(int Port = 22222, int MaxConnection = 0) { + if (MaxConnection <= 0) MaxConnection = SocketSet.MaxConnection_General; System.Net.Sockets.Socket? socket = SocketManager.StartListening(Port, MaxConnection); if (socket != null) return new ServerSocket(socket, Port); else throw new System.Exception("无法创建监听,请重新启动服务器再试。"); @@ -54,6 +61,16 @@ namespace Milimoe.FunGame.Core.Library.Common.Network throw new System.Exception("无法获取客户端信息。"); } + public bool AddClient(string ClientName, Task t) + { + return PlayerThreads.Add(ClientName, t); + } + + public bool RemoveClient(string ClientName) + { + return PlayerThreads.Remove(ClientName); + } + public SocketResult Send(SocketMessageType type, params object[] objs) { throw new System.Exception("监听Socket不能用于发送信息。"); diff --git a/FunGame.Core/Library/Constant/ConstantSet.cs b/FunGame.Core/Library/Constant/ConstantSet.cs index 13c7d28..a29c289 100644 --- a/FunGame.Core/Library/Constant/ConstantSet.cs +++ b/FunGame.Core/Library/Constant/ConstantSet.cs @@ -25,6 +25,9 @@ namespace Milimoe.FunGame.Core.Library.Constant public class SocketSet { public static int MaxRetryTimes { get; } = 20; + public static int MaxConnection_1C2G { get; } = 10; + public static int MaxConnection_General { get; } = 20; + public static int MaxConnection_4C4G { get; } = 40; public const string Unknown = "Unknown"; public const string Connect = "Connect"; diff --git a/FunGame.Core/Library/Constant/General.cs b/FunGame.Core/Library/Constant/General.cs index b6bd46c..e1c5df6 100644 --- a/FunGame.Core/Library/Constant/General.cs +++ b/FunGame.Core/Library/Constant/General.cs @@ -9,7 +9,14 @@ namespace Milimoe.FunGame.Core.Library.Constant { public class General { + // Static Variable public static Empty EntityInstance { get; } = new(); public static Encoding DEFAULT_ENCODING { get; } = Encoding.UTF8; + + // Const + public const int MaxRetryTimes = 20; + public const int MaxTask_1C2G = 10; + public const int MaxTask_General = 20; + public const int MaxTask_4C4G = 40; } } diff --git a/FunGame.Core/Service/SocketManager.cs b/FunGame.Core/Service/SocketManager.cs index 01b9726..c53a798 100644 --- a/FunGame.Core/Service/SocketManager.cs +++ b/FunGame.Core/Service/SocketManager.cs @@ -30,8 +30,9 @@ namespace Milimoe.FunGame.Core.Service /// 监听端口号 /// 最大连接数量 /// 服务器端专用Socket - internal static Socket? StartListening(int Port = 22222, int MaxConnection = 20) + internal static Socket? StartListening(int Port = 22222, int MaxConnection = 0) { + if (MaxConnection <= 0) MaxConnection = SocketSet.MaxConnection_General; try { ServerSocket = new(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); diff --git a/FunGame.Core/Service/ThreadManager.cs b/FunGame.Core/Service/ThreadManager.cs index 781a2a1..aafdfa6 100644 --- a/FunGame.Core/Service/ThreadManager.cs +++ b/FunGame.Core/Service/ThreadManager.cs @@ -9,10 +9,35 @@ namespace Milimoe.FunGame.Core.Service { internal class ThreadManager { - internal static int MAX_THREAD { get; } = 20; + /// + /// 最大接受的线程数量 + /// + private int MaxConnection { get; } + /// + /// 可参与高并发的字典,但添加效率较低 + /// private ConcurrentDictionary Threads { get; } = new(); + /// + /// Init ThreadManager + /// + /// MaxConnection + internal ThreadManager(int MaxConnection = 0) + { + if (MaxConnection <= 0) + this.MaxConnection = Library.Constant.General.MaxTask_General; + else + { + this.MaxConnection = MaxConnection; + } + } + + /// + /// 获取Task对象 + /// + /// Task的Key + /// Task对象 internal Task this[string name] { get @@ -21,16 +46,53 @@ namespace Milimoe.FunGame.Core.Service } } + /// + /// 向线程管理器中添加Task + /// + /// Task的Key + /// Task对象 + /// True:操作成功 internal bool Add(string name, Task t) { + if (Threads.Count + 1 > MaxConnection) return false; return Threads.TryAdd(name, t); } + /// + /// 从线程管理器中移除Task + /// + /// Task的Key + /// True:操作成功 internal bool Remove(string name) { return Threads.TryRemove(name, out _); } + /// + /// 将Task移除,并取得这个Task + /// + /// Task的Key + /// Task对象 + /// 被移除的Task + internal bool Remove(string name, ref Task? t) + { + return Threads.TryRemove(name, out t); + } + + /// + /// 将Task移除,并取得这个Task + /// + /// Task的Key + /// 被移除的Task + internal Task? RemoveAndGet(string name) + { + Threads.TryRemove(name, out Task? result); + return result; + } + + /// + /// 清空线程管理器 + /// internal void Clear() { Threads.Clear();