forked from project-redbud/FunGame-Core
更新一些变量
This commit is contained in:
parent
ec4e6dd780
commit
e131fc44f6
@ -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不能用于发送信息。");
|
||||
|
||||
@ -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";
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,8 +30,9 @@ namespace Milimoe.FunGame.Core.Service
|
||||
/// <param name="Port">监听端口号</param>
|
||||
/// <param name="MaxConnection">最大连接数量</param>
|
||||
/// <returns>服务器端专用Socket</returns>
|
||||
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);
|
||||
|
||||
@ -9,10 +9,35 @@ namespace Milimoe.FunGame.Core.Service
|
||||
{
|
||||
internal class ThreadManager
|
||||
{
|
||||
internal static int MAX_THREAD { get; } = 20;
|
||||
/// <summary>
|
||||
/// 最大接受的线程数量
|
||||
/// </summary>
|
||||
private int MaxConnection { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 可参与高并发的字典,但添加效率较低
|
||||
/// </summary>
|
||||
private ConcurrentDictionary<string, Task> Threads { get; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Init ThreadManager
|
||||
/// </summary>
|
||||
/// <param name="MaxConnection">MaxConnection</param>
|
||||
internal ThreadManager(int MaxConnection = 0)
|
||||
{
|
||||
if (MaxConnection <= 0)
|
||||
this.MaxConnection = Library.Constant.General.MaxTask_General;
|
||||
else
|
||||
{
|
||||
this.MaxConnection = MaxConnection;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取Task对象
|
||||
/// </summary>
|
||||
/// <param name="name">Task的Key</param>
|
||||
/// <returns>Task对象</returns>
|
||||
internal Task this[string name]
|
||||
{
|
||||
get
|
||||
@ -21,16 +46,53 @@ namespace Milimoe.FunGame.Core.Service
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向线程管理器中添加Task
|
||||
/// </summary>
|
||||
/// <param name="name">Task的Key</param>
|
||||
/// <param name="t">Task对象</param>
|
||||
/// <returns>True:操作成功</returns>
|
||||
internal bool Add(string name, Task t)
|
||||
{
|
||||
if (Threads.Count + 1 > MaxConnection) return false;
|
||||
return Threads.TryAdd(name, t);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从线程管理器中移除Task
|
||||
/// </summary>
|
||||
/// <param name="name">Task的Key</param>
|
||||
/// <returns>True:操作成功</returns>
|
||||
internal bool Remove(string name)
|
||||
{
|
||||
return Threads.TryRemove(name, out _);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将Task移除,并取得这个Task
|
||||
/// </summary>
|
||||
/// <param name="name">Task的Key</param>
|
||||
/// <param name="t">Task对象</param>
|
||||
/// <returns>被移除的Task</returns>
|
||||
internal bool Remove(string name, ref Task? t)
|
||||
{
|
||||
return Threads.TryRemove(name, out t);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将Task移除,并取得这个Task
|
||||
/// </summary>
|
||||
/// <param name="name">Task的Key</param>
|
||||
/// <returns>被移除的Task</returns>
|
||||
internal Task? RemoveAndGet(string name)
|
||||
{
|
||||
Threads.TryRemove(name, out Task? result);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清空线程管理器
|
||||
/// </summary>
|
||||
internal void Clear()
|
||||
{
|
||||
Threads.Clear();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user