使ModelManager成为支持泛型的工具类

This commit is contained in:
milimoe 2023-09-09 21:24:45 +08:00
parent 9a59798543
commit a74ff63d8e
Signed by: milimoe
GPG Key ID: 05D280912DA6C69E
2 changed files with 11 additions and 12 deletions

View File

@ -19,16 +19,16 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
public int ClientCount => OnlineClients.Count; public int ClientCount => OnlineClients.Count;
public int BannedCount => BannedList.Count; public int BannedCount => BannedList.Count;
private readonly ModelManager OnlineClients; private readonly ModelManager<IServerModel> OnlineClients;
private ServerSocket(System.Net.Sockets.Socket Instance, int ServerPort, int MaxConnection = 0) private ServerSocket(System.Net.Sockets.Socket Instance, int ServerPort, int MaxConnection = 0)
{ {
this.Instance = Instance; this.Instance = Instance;
this.ServerPort = ServerPort; this.ServerPort = ServerPort;
if (MaxConnection <= 0) if (MaxConnection <= 0)
OnlineClients = new ModelManager(); OnlineClients = new();
else else
OnlineClients = new ModelManager(MaxConnection); OnlineClients = new(MaxConnection);
} }
public static ServerSocket StartListening(int Port = 22222, int MaxConnection = 0) public static ServerSocket StartListening(int Port = 22222, int MaxConnection = 0)

View File

@ -1,9 +1,8 @@
using System.Collections.Concurrent; using System.Collections.Concurrent;
using Milimoe.FunGame.Core.Interface.Base;
namespace Milimoe.FunGame.Core.Service namespace Milimoe.FunGame.Core.Service
{ {
internal class ModelManager internal class ModelManager<T>
{ {
/// <summary> /// <summary>
/// 目前的Model数量 /// 目前的Model数量
@ -18,7 +17,7 @@ namespace Milimoe.FunGame.Core.Service
/// <summary> /// <summary>
/// 可参与高并发的字典,但添加效率较低 /// 可参与高并发的字典,但添加效率较低
/// </summary> /// </summary>
private ConcurrentDictionary<string, IServerModel> Models { get; } = new(); private ConcurrentDictionary<string, T> Models { get; } = new();
/// <summary> /// <summary>
/// Init ModelManager /// Init ModelManager
@ -39,7 +38,7 @@ namespace Milimoe.FunGame.Core.Service
/// </summary> /// </summary>
/// <param name="name">Model的Key</param> /// <param name="name">Model的Key</param>
/// <returns>Model对象</returns> /// <returns>Model对象</returns>
internal IServerModel this[string name] => Models[name]; internal T this[string name] => Models[name];
/// <summary> /// <summary>
/// 向Model管理器中添加Model /// 向Model管理器中添加Model
@ -47,7 +46,7 @@ namespace Milimoe.FunGame.Core.Service
/// <param name="name">Model的Key</param> /// <param name="name">Model的Key</param>
/// <param name="t">Model对象</param> /// <param name="t">Model对象</param>
/// <returns>True操作成功</returns> /// <returns>True操作成功</returns>
internal bool Add(string name, IServerModel t) internal bool Add(string name, T t)
{ {
if (Models.Count + 1 > MaxModel) return false; if (Models.Count + 1 > MaxModel) return false;
return Models.TryAdd(name, t); return Models.TryAdd(name, t);
@ -69,7 +68,7 @@ namespace Milimoe.FunGame.Core.Service
/// <param name="name">Model的Key</param> /// <param name="name">Model的Key</param>
/// <param name="t">Model对象</param> /// <param name="t">Model对象</param>
/// <returns>被移除的Model</returns> /// <returns>被移除的Model</returns>
internal bool Remove(string name, ref IServerModel? t) internal bool Remove(string name, ref T? t)
{ {
return Models.TryRemove(name, out t); return Models.TryRemove(name, out t);
} }
@ -79,9 +78,9 @@ namespace Milimoe.FunGame.Core.Service
/// </summary> /// </summary>
/// <param name="name">Model的Key</param> /// <param name="name">Model的Key</param>
/// <returns>被移除的Model</returns> /// <returns>被移除的Model</returns>
internal IServerModel? RemoveAndGet(string name) internal T? RemoveAndGet(string name)
{ {
Models.TryRemove(name, out IServerModel? result); Models.TryRemove(name, out T? result);
return result; return result;
} }
@ -102,7 +101,7 @@ namespace Milimoe.FunGame.Core.Service
/// <summary> /// <summary>
/// 获取Model对象的列表 /// 获取Model对象的列表
/// </summary> /// </summary>
internal List<IServerModel> GetList() internal List<T> GetList()
{ {
return Models.Values.ToList(); return Models.Values.ToList();
} }