mirror of
https://github.com/project-redbud/FunGame-Core.git
synced 2025-12-05 00:06:02 +00:00
添加DataRequest
This commit is contained in:
parent
9d33d28b51
commit
03c579e88d
38
FunGame.Core/Api/Utility/ConcurrentQueue.cs
Normal file
38
FunGame.Core/Api/Utility/ConcurrentQueue.cs
Normal file
@ -0,0 +1,38 @@
|
||||
namespace Milimoe.FunGame.Core.Api.Utility
|
||||
{
|
||||
public class ConcurrentQueue<T>
|
||||
{
|
||||
public bool Lock { get; set; }
|
||||
|
||||
private System.Collections.Concurrent.ConcurrentQueue<T> Instance { get; }
|
||||
|
||||
public ConcurrentQueue()
|
||||
{
|
||||
Instance = new System.Collections.Concurrent.ConcurrentQueue<T>();
|
||||
}
|
||||
|
||||
public void Add(T obj)
|
||||
{
|
||||
if (Lock)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Lock = true;
|
||||
lock (Instance)
|
||||
{
|
||||
Instance.Enqueue(obj);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Delete()
|
||||
{
|
||||
bool result = false;
|
||||
lock (Instance)
|
||||
{
|
||||
result = Instance.TryDequeue(out _);
|
||||
}
|
||||
Lock = false;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
97
FunGame.Core/Library/Common/Architecture/DataRequest.cs
Normal file
97
FunGame.Core/Library/Common/Architecture/DataRequest.cs
Normal file
@ -0,0 +1,97 @@
|
||||
using System.Collections;
|
||||
using Milimoe.FunGame.Core.Api.Utility;
|
||||
using Milimoe.FunGame.Core.Library.Common.Network;
|
||||
using Milimoe.FunGame.Core.Library.Constant;
|
||||
|
||||
namespace Milimoe.FunGame.Core.Library.Common.Architecture
|
||||
{
|
||||
public class DataRequest
|
||||
{
|
||||
private static readonly ConcurrentQueue<Request> Queue = new();
|
||||
private readonly Request Worker;
|
||||
|
||||
public DataRequest(Socket Socket, DataRequestType RequestType)
|
||||
{
|
||||
Worker = new(Socket, RequestType);
|
||||
}
|
||||
|
||||
public void AddRequestData(string key, object value)
|
||||
{
|
||||
Worker.RequestData.Add(key, value);
|
||||
}
|
||||
|
||||
public async Task SendRequest()
|
||||
{
|
||||
Queue.Add(Worker);
|
||||
if (await Worker.SendRequest() == RequestResult.Success)
|
||||
{
|
||||
Queue.Delete();
|
||||
}
|
||||
}
|
||||
|
||||
public object? this[string key] => Worker.ResultData[key];
|
||||
|
||||
public T? GetResult<T>(string key)
|
||||
{
|
||||
object? obj = this[key];
|
||||
if (obj != null)
|
||||
{
|
||||
return (T)obj;
|
||||
}
|
||||
return default;
|
||||
}
|
||||
|
||||
private class Request : BaseModel
|
||||
{
|
||||
public Hashtable RequestData { get; } = new();
|
||||
public Hashtable ResultData { get; } = new();
|
||||
|
||||
private bool JobFinish = false;
|
||||
private readonly Socket? Socket;
|
||||
private readonly DataRequestType RequestType;
|
||||
|
||||
public async Task<RequestResult> SendRequest()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Socket?.Send(SocketMessageType.DataRequest, RequestData) == SocketResult.Success)
|
||||
{
|
||||
await Task.Run(() =>
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (JobFinish) break;
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return RequestResult.Fail;
|
||||
}
|
||||
return RequestResult.Success;
|
||||
}
|
||||
|
||||
public Request(Socket? socket, DataRequestType requestType) : base(socket)
|
||||
{
|
||||
Socket = socket;
|
||||
RequestType = requestType;
|
||||
}
|
||||
|
||||
public override void SocketHandler(SocketObject SocketObject)
|
||||
{
|
||||
if (SocketObject.SocketType == SocketMessageType.DataRequest)
|
||||
{
|
||||
Dispose();
|
||||
switch (RequestType)
|
||||
{
|
||||
default:
|
||||
break;
|
||||
}
|
||||
JobFinish = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -20,16 +20,16 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
||||
public int UsersCount => OnlineUsers.Count;
|
||||
public int BannedCount => BannedList.Count;
|
||||
|
||||
private readonly ThreadManager OnlineUsers;
|
||||
private readonly ModelManager OnlineUsers;
|
||||
|
||||
private ServerSocket(System.Net.Sockets.Socket Instance, int ServerPort, int MaxConnection = 0)
|
||||
{
|
||||
this.Instance = Instance;
|
||||
this.ServerPort = ServerPort;
|
||||
if (MaxConnection <= 0)
|
||||
OnlineUsers = new ThreadManager();
|
||||
OnlineUsers = new ModelManager();
|
||||
else
|
||||
OnlineUsers = new ThreadManager(MaxConnection);
|
||||
OnlineUsers = new ModelManager(MaxConnection);
|
||||
}
|
||||
|
||||
public static ServerSocket StartListening(int Port = 22222, int MaxConnection = 0)
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
namespace Milimoe.FunGame.Core.Library.Common.Network
|
||||
{
|
||||
[Serializable]
|
||||
public readonly struct SocketObject
|
||||
{
|
||||
public SocketMessageType SocketType { get; } = SocketMessageType.Unknown;
|
||||
|
||||
@ -12,7 +12,7 @@ namespace Milimoe.FunGame.Core.Library.Constant
|
||||
// Const
|
||||
public const int MaxRetryTimes = 20;
|
||||
public const int MaxTask_1C2G = 10;
|
||||
public const int MaxThread_General = 20;
|
||||
public const int MaxTask_2C2G = 20;
|
||||
public const int MaxTask_4C4G = 40;
|
||||
|
||||
public const int SocketByteSize = 512 * 1024;
|
||||
|
||||
@ -23,6 +23,13 @@
|
||||
NotSent,
|
||||
NotReceived
|
||||
}
|
||||
|
||||
public enum RequestResult
|
||||
{
|
||||
Success,
|
||||
Fail,
|
||||
Missing
|
||||
}
|
||||
|
||||
public enum ConnectResult
|
||||
{
|
||||
|
||||
@ -50,6 +50,7 @@
|
||||
public enum SocketMessageType
|
||||
{
|
||||
Unknown,
|
||||
DataRequest,
|
||||
Connect,
|
||||
GetNotice,
|
||||
Login,
|
||||
@ -67,6 +68,11 @@
|
||||
ChangeRoomSetting
|
||||
}
|
||||
|
||||
public enum DataRequestType
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public enum SocketRuntimeType
|
||||
{
|
||||
Client,
|
||||
|
||||
110
FunGame.Core/Service/ModelManager.cs
Normal file
110
FunGame.Core/Service/ModelManager.cs
Normal file
@ -0,0 +1,110 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Milimoe.FunGame.Core.Library.Server;
|
||||
|
||||
namespace Milimoe.FunGame.Core.Service
|
||||
{
|
||||
internal class ModelManager
|
||||
{
|
||||
/// <summary>
|
||||
/// 目前的Model数量
|
||||
/// </summary>
|
||||
internal int Count => Models.Count;
|
||||
|
||||
/// <summary>
|
||||
/// 最大接受的Model数量
|
||||
/// </summary>
|
||||
private int MaxModel { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 可参与高并发的字典,但添加效率较低
|
||||
/// </summary>
|
||||
private ConcurrentDictionary<string, BaseModel> Models { get; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Init ModelManager
|
||||
/// </summary>
|
||||
/// <param name="MaxModel">MaxModel</param>
|
||||
internal ModelManager(int MaxModel = 0)
|
||||
{
|
||||
if (MaxModel <= 0)
|
||||
this.MaxModel = Library.Constant.General.MaxTask_2C2G;
|
||||
else
|
||||
{
|
||||
this.MaxModel = MaxModel;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取Model对象
|
||||
/// </summary>
|
||||
/// <param name="name">Model的Key</param>
|
||||
/// <returns>Model对象</returns>
|
||||
internal BaseModel this[string name] => Models[name];
|
||||
|
||||
/// <summary>
|
||||
/// 向Model管理器中添加Model
|
||||
/// </summary>
|
||||
/// <param name="name">Model的Key</param>
|
||||
/// <param name="t">Model对象</param>
|
||||
/// <returns>True:操作成功</returns>
|
||||
internal bool Add(string name, BaseModel t)
|
||||
{
|
||||
if (Models.Count + 1 > MaxModel) return false;
|
||||
return Models.TryAdd(name, t);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从Model管理器中移除Model
|
||||
/// </summary>
|
||||
/// <param name="name">Model的Key</param>
|
||||
/// <returns>True:操作成功</returns>
|
||||
internal bool Remove(string name)
|
||||
{
|
||||
return Models.TryRemove(name, out _);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将Model移除,并取得这个Model
|
||||
/// </summary>
|
||||
/// <param name="name">Model的Key</param>
|
||||
/// <param name="t">Model对象</param>
|
||||
/// <returns>被移除的Model</returns>
|
||||
internal bool Remove(string name, ref BaseModel? t)
|
||||
{
|
||||
return Models.TryRemove(name, out t);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将Model移除,并取得这个Model
|
||||
/// </summary>
|
||||
/// <param name="name">Model的Key</param>
|
||||
/// <returns>被移除的Model</returns>
|
||||
internal BaseModel? RemoveAndGet(string name)
|
||||
{
|
||||
Models.TryRemove(name, out BaseModel? result);
|
||||
return result;
|
||||
}
|
||||
|
||||
internal bool ContainsKey(string name)
|
||||
{
|
||||
return Models.ContainsKey(name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清空Model管理器
|
||||
/// </summary>
|
||||
internal void Clear()
|
||||
{
|
||||
Models.Clear();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取Model对象的列表
|
||||
/// </summary>
|
||||
internal List<BaseModel> GetList()
|
||||
{
|
||||
return Models.Values.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,110 +0,0 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Milimoe.FunGame.Core.Library.Server;
|
||||
|
||||
namespace Milimoe.FunGame.Core.Service
|
||||
{
|
||||
internal class ThreadManager
|
||||
{
|
||||
/// <summary>
|
||||
/// 目前的线程数量
|
||||
/// </summary>
|
||||
internal int Count => Threads.Count;
|
||||
|
||||
/// <summary>
|
||||
/// 最大接受的线程数量
|
||||
/// </summary>
|
||||
private int MaxThread { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 可参与高并发的字典,但添加效率较低
|
||||
/// </summary>
|
||||
private ConcurrentDictionary<string, BaseModel> Threads { get; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Init ThreadManager
|
||||
/// </summary>
|
||||
/// <param name="MaxThread">MaxThread</param>
|
||||
internal ThreadManager(int MaxThread = 0)
|
||||
{
|
||||
if (MaxThread <= 0)
|
||||
this.MaxThread = Library.Constant.General.MaxThread_General;
|
||||
else
|
||||
{
|
||||
this.MaxThread = MaxThread;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取Thread对象
|
||||
/// </summary>
|
||||
/// <param name="name">Thread的Key</param>
|
||||
/// <returns>Thread对象</returns>
|
||||
internal BaseModel this[string name] => Threads[name];
|
||||
|
||||
/// <summary>
|
||||
/// 向线程管理器中添加Thread
|
||||
/// </summary>
|
||||
/// <param name="name">Thread的Key</param>
|
||||
/// <param name="t">Thread对象</param>
|
||||
/// <returns>True:操作成功</returns>
|
||||
internal bool Add(string name, BaseModel t)
|
||||
{
|
||||
if (Threads.Count + 1 > MaxThread) return false;
|
||||
return Threads.TryAdd(name, t);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从线程管理器中移除Thread
|
||||
/// </summary>
|
||||
/// <param name="name">Thread的Key</param>
|
||||
/// <returns>True:操作成功</returns>
|
||||
internal bool Remove(string name)
|
||||
{
|
||||
return Threads.TryRemove(name, out _);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将Thread移除,并取得这个Thread
|
||||
/// </summary>
|
||||
/// <param name="name">Thread的Key</param>
|
||||
/// <param name="t">Thread对象</param>
|
||||
/// <returns>被移除的Thread</returns>
|
||||
internal bool Remove(string name, ref BaseModel? t)
|
||||
{
|
||||
return Threads.TryRemove(name, out t);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将Thread移除,并取得这个Thread
|
||||
/// </summary>
|
||||
/// <param name="name">Thread的Key</param>
|
||||
/// <returns>被移除的Thread</returns>
|
||||
internal BaseModel? RemoveAndGet(string name)
|
||||
{
|
||||
Threads.TryRemove(name, out BaseModel? result);
|
||||
return result;
|
||||
}
|
||||
|
||||
internal bool ContainsKey(string name)
|
||||
{
|
||||
return Threads.ContainsKey(name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清空线程管理器
|
||||
/// </summary>
|
||||
internal void Clear()
|
||||
{
|
||||
Threads.Clear();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取线程对象的列表
|
||||
/// </summary>
|
||||
internal List<BaseModel> GetList()
|
||||
{
|
||||
return Threads.Values.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user