mirror of
https://github.com/project-redbud/FunGame-Core.git
synced 2025-12-05 08:09:02 +00:00
使ModelManager成为支持泛型的工具类
This commit is contained in:
parent
9a59798543
commit
a74ff63d8e
@ -19,16 +19,16 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
||||
public int ClientCount => OnlineClients.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)
|
||||
{
|
||||
this.Instance = Instance;
|
||||
this.ServerPort = ServerPort;
|
||||
if (MaxConnection <= 0)
|
||||
OnlineClients = new ModelManager();
|
||||
OnlineClients = new();
|
||||
else
|
||||
OnlineClients = new ModelManager(MaxConnection);
|
||||
OnlineClients = new(MaxConnection);
|
||||
}
|
||||
|
||||
public static ServerSocket StartListening(int Port = 22222, int MaxConnection = 0)
|
||||
|
||||
@ -1,9 +1,8 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Milimoe.FunGame.Core.Interface.Base;
|
||||
|
||||
namespace Milimoe.FunGame.Core.Service
|
||||
{
|
||||
internal class ModelManager
|
||||
internal class ModelManager<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// 目前的Model数量
|
||||
@ -18,7 +17,7 @@ namespace Milimoe.FunGame.Core.Service
|
||||
/// <summary>
|
||||
/// 可参与高并发的字典,但添加效率较低
|
||||
/// </summary>
|
||||
private ConcurrentDictionary<string, IServerModel> Models { get; } = new();
|
||||
private ConcurrentDictionary<string, T> Models { get; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Init ModelManager
|
||||
@ -39,7 +38,7 @@ namespace Milimoe.FunGame.Core.Service
|
||||
/// </summary>
|
||||
/// <param name="name">Model的Key</param>
|
||||
/// <returns>Model对象</returns>
|
||||
internal IServerModel this[string name] => Models[name];
|
||||
internal T this[string name] => Models[name];
|
||||
|
||||
/// <summary>
|
||||
/// 向Model管理器中添加Model
|
||||
@ -47,7 +46,7 @@ namespace Milimoe.FunGame.Core.Service
|
||||
/// <param name="name">Model的Key</param>
|
||||
/// <param name="t">Model对象</param>
|
||||
/// <returns>True:操作成功</returns>
|
||||
internal bool Add(string name, IServerModel t)
|
||||
internal bool Add(string name, T t)
|
||||
{
|
||||
if (Models.Count + 1 > MaxModel) return false;
|
||||
return Models.TryAdd(name, t);
|
||||
@ -69,7 +68,7 @@ namespace Milimoe.FunGame.Core.Service
|
||||
/// <param name="name">Model的Key</param>
|
||||
/// <param name="t">Model对象</param>
|
||||
/// <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);
|
||||
}
|
||||
@ -79,9 +78,9 @@ namespace Milimoe.FunGame.Core.Service
|
||||
/// </summary>
|
||||
/// <param name="name">Model的Key</param>
|
||||
/// <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;
|
||||
}
|
||||
|
||||
@ -102,7 +101,7 @@ namespace Milimoe.FunGame.Core.Service
|
||||
/// <summary>
|
||||
/// 获取Model对象的列表
|
||||
/// </summary>
|
||||
internal List<IServerModel> GetList()
|
||||
internal List<T> GetList()
|
||||
{
|
||||
return Models.Values.ToList();
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user