为ServerSocket添加UserList

This commit is contained in:
milimoe 2023-09-10 13:55:16 +08:00
parent c6c084c020
commit 4558110af9
Signed by: milimoe
GPG Key ID: 05D280912DA6C69E
2 changed files with 38 additions and 5 deletions

View File

@ -9,7 +9,7 @@ namespace Milimoe.FunGame.Core.Interface.Base
public abstract bool Running { get; } public abstract bool Running { get; }
public abstract ClientSocket? Socket { get; } public abstract ClientSocket? Socket { get; }
public abstract Task? Task { get; } public abstract Task? Task { get; }
public abstract User? User { get; } public abstract User User { get; }
public abstract string ClientName { get; } public abstract string ClientName { get; }
public abstract bool Read(ClientSocket socket); public abstract bool Read(ClientSocket socket);

View File

@ -15,20 +15,29 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
public string ServerNotice { get; } = ""; public string ServerNotice { get; } = "";
public bool Connected => Instance != null && Instance.Connected; public bool Connected => Instance != null && Instance.Connected;
public List<IServerModel> ClientList => OnlineClients.GetList(); public List<IServerModel> ClientList => OnlineClients.GetList();
public List<IServerModel> UserList => OnlineUsers.GetList();
public List<string> BannedList { get; } = new(); public List<string> BannedList { get; } = new();
public int ClientCount => OnlineClients.Count; public int ClientCount => OnlineClients.Count;
public int UserCount => OnlineUsers.Count;
public int BannedCount => BannedList.Count; public int BannedCount => BannedList.Count;
private readonly ModelManager<IServerModel> OnlineClients; private readonly ModelManager<IServerModel> OnlineClients;
private readonly ModelManager<IServerModel> OnlineUsers;
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(); OnlineClients = new();
OnlineUsers = new();
}
else else
{
OnlineClients = new(MaxConnection); OnlineClients = new(MaxConnection);
OnlineUsers = new(MaxConnection);
}
} }
public static ServerSocket StartListening(int Port = 22222, int MaxConnection = 0) public static ServerSocket StartListening(int Port = 22222, int MaxConnection = 0)
@ -51,30 +60,54 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
throw new SocketGetClientException(); throw new SocketGetClientException();
} }
public bool Add(string name, IServerModel t) public bool AddClient(string name, IServerModel t)
{ {
name = name.ToLower(); name = name.ToLower();
return OnlineClients.Add(name, t); return OnlineClients.Add(name, t);
} }
public bool Remove(string name) public bool RemoveClient(string name)
{ {
name = name.ToLower(); name = name.ToLower();
return OnlineClients.Remove(name); return OnlineClients.Remove(name);
} }
public bool Contains(string name) public bool ContainsClient(string name)
{ {
name = name.ToLower(); name = name.ToLower();
return OnlineClients.ContainsKey(name); return OnlineClients.ContainsKey(name);
} }
public IServerModel Get(string name) public IServerModel GetClient(string name)
{ {
name = name.ToLower(); name = name.ToLower();
return OnlineClients[name]; return OnlineClients[name];
} }
public bool AddUser(string name, IServerModel t)
{
name = name.ToLower();
return OnlineUsers.Add(name, t);
}
public bool RemoveUser(string name)
{
name = name.ToLower();
return OnlineUsers.Remove(name);
}
public bool ContainsUser(string name)
{
name = name.ToLower();
return OnlineUsers.ContainsKey(name);
}
public IServerModel GetUser(string name)
{
name = name.ToLower();
return OnlineUsers[name];
}
public void Close() public void Close()
{ {
Instance?.Close(); Instance?.Close();