From d23c6597d1f277a14bab5a1190fe2fc8a6cf93c5 Mon Sep 17 00:00:00 2001
From: milimoe <110188673+milimoe@users.noreply.github.com>
Date: Fri, 17 Jan 2025 18:58:51 +0800
Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=20HTTPClient=EF=BC=9B?=
=?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=97=A5=E5=BF=97=E7=BA=A7=E5=88=AB=EF=BC=9B?=
=?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8C=BF=E5=90=8D=E6=9C=8D=E5=8A=A1=E5=99=A8?=
=?UTF-8?q?=E6=A8=A1=E7=BB=84=EF=BC=9B=E4=BF=AE=E5=A4=8D=E4=B8=8D=E5=90=8C?=
=?UTF-8?q?=E6=97=B6=E9=97=B4=E5=A4=9A=E5=AE=A2=E6=88=B7=E7=AB=AF=E8=BF=9E?=
=?UTF-8?q?=E6=8E=A5=E6=B8=B8=E6=88=8F=E6=A8=A1=E7=BB=84=E6=97=B6=E5=8F=AF?=
=?UTF-8?q?=E8=83=BD=E4=BA=A7=E7=94=9F=E7=9A=84=E7=BA=BF=E7=A8=8B=E5=AE=89?=
=?UTF-8?q?=E5=85=A8=E9=97=AE=E9=A2=98=20(#106)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* 完善 HTTPClient;添加日志级别;添加匿名服务器模组(此模组不强制要求登录、客户端安装)
* 添加参数
* 添加 null 检查
* 修复不同时间多客户端连接游戏模组时可能产生的线程安全问题
---
Api/Transmittal/DataRequest.cs | 32 +++---
Api/Utility/General.cs | 10 +-
Api/Utility/TextReader.cs | 6 +-
Controller/BaseAddonController.cs | 20 +++-
Controller/RunTimeController.cs | 71 +++++++-----
Controller/ServerAddonController.cs | 4 +-
Interface/Base/Addons/IGameModuleServer.cs | 2 +-
.../Common/Addon/Example/ExampleGameModule.cs | 103 +++++++++++++-----
Library/Common/Addon/GameModuleServer.cs | 97 ++++++++++++++++-
Library/Common/Architecture/HeartBeat.cs | 70 +++++++-----
Library/Common/Network/HTTPClient.cs | 71 ++++++++++--
Library/Common/Network/Socket.cs | 13 +++
Library/Constant/ConstantSet.cs | 18 +++
Library/Constant/TypeEnum.cs | 13 ++-
Service/HTTPManager.cs | 14 +--
15 files changed, 410 insertions(+), 134 deletions(-)
diff --git a/Api/Transmittal/DataRequest.cs b/Api/Transmittal/DataRequest.cs
index 90b1a5b..083eb2b 100644
--- a/Api/Transmittal/DataRequest.cs
+++ b/Api/Transmittal/DataRequest.cs
@@ -70,13 +70,13 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
/// 插件则使用 中的 创建一个新的请求
/// 此数据请求只能调用异步方法 请求数据
///
- ///
+ ///
///
///
///
- internal DataRequest(HTTPClient WebSocket, DataRequestType RequestType, bool IsLongRunning = false, SocketRuntimeType RuntimeType = SocketRuntimeType.Client)
+ internal DataRequest(HTTPClient HTTPClient, DataRequestType RequestType, bool IsLongRunning = false, SocketRuntimeType RuntimeType = SocketRuntimeType.Client)
{
- Worker = new(WebSocket, RequestType, Guid.NewGuid(), IsLongRunning, RuntimeType);
+ Worker = new(HTTPClient, RequestType, Guid.NewGuid(), IsLongRunning, RuntimeType);
}
///
@@ -99,13 +99,13 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
/// 此构造方法是给 提供的
/// 此数据请求只能调用异步方法 请求数据
///
- ///
+ ///
///
///
///
- internal DataRequest(HTTPClient WebSocket, GamingType GamingType, bool IsLongRunning = false, SocketRuntimeType RuntimeType = SocketRuntimeType.Client)
+ internal DataRequest(HTTPClient Client, GamingType GamingType, bool IsLongRunning = false, SocketRuntimeType RuntimeType = SocketRuntimeType.Client)
{
- GamingWorker = new(WebSocket, GamingType, Guid.NewGuid(), IsLongRunning, RuntimeType);
+ GamingWorker = new(Client, GamingType, Guid.NewGuid(), IsLongRunning, RuntimeType);
}
///
@@ -194,7 +194,7 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
public string Error => _Error;
private readonly Socket? Socket = null;
- private readonly HTTPClient? WebSocket = null;
+ private readonly HTTPClient? HTTPClient = null;
private readonly DataRequestType RequestType = DataRequestType.UnKnown;
private readonly Guid RequestID = Guid.Empty;
private readonly bool IsLongRunning = false;
@@ -212,9 +212,9 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
this.RuntimeType = RuntimeType;
}
- public SocketRequest(HTTPClient? WebSocket, DataRequestType RequestType, Guid RequestID, bool IsLongRunning = false, SocketRuntimeType RuntimeType = SocketRuntimeType.Client) : base(WebSocket)
+ public SocketRequest(HTTPClient? HTTPClient, DataRequestType RequestType, Guid RequestID, bool IsLongRunning = false, SocketRuntimeType RuntimeType = SocketRuntimeType.Client) : base(HTTPClient)
{
- this.WebSocket = WebSocket;
+ this.HTTPClient = HTTPClient;
this.RequestType = RequestType;
this.RequestID = RequestID;
this.IsLongRunning = IsLongRunning;
@@ -236,7 +236,7 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
{
WaitForWorkDone();
}
- else if (WebSocket != null)
+ else if (HTTPClient != null)
{
throw new AsyncSendException();
}
@@ -265,7 +265,7 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
{
await WaitForWorkDoneAsync();
}
- else if (WebSocket != null && await WebSocket.Send(SocketMessageType.DataRequest, RequestType, RequestID, RequestData) == SocketResult.Success)
+ else if (HTTPClient != null && await HTTPClient.Send(SocketMessageType.DataRequest, RequestType, RequestID, RequestData) == SocketResult.Success)
{
await WaitForWorkDoneAsync();
}
@@ -317,7 +317,7 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
public string Error => _Error;
private readonly Socket? Socket = null;
- private readonly HTTPClient? WebSocket = null;
+ private readonly HTTPClient? HTTPClient = null;
private readonly GamingType GamingType = GamingType.None;
private readonly Guid RequestID = Guid.Empty;
private readonly bool IsLongRunning = false;
@@ -335,9 +335,9 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
this.RuntimeType = RuntimeType;
}
- public GamingRequest(HTTPClient? WebSocket, GamingType GamingType, Guid RequestID, bool IsLongRunning = false, SocketRuntimeType RuntimeType = SocketRuntimeType.Client) : base(WebSocket)
+ public GamingRequest(HTTPClient? HTTPClient, GamingType GamingType, Guid RequestID, bool IsLongRunning = false, SocketRuntimeType RuntimeType = SocketRuntimeType.Client) : base(HTTPClient)
{
- this.WebSocket = WebSocket;
+ this.HTTPClient = HTTPClient;
this.GamingType = GamingType;
this.RequestID = RequestID;
this.IsLongRunning = IsLongRunning;
@@ -359,7 +359,7 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
{
WaitForWorkDone();
}
- else if (WebSocket != null)
+ else if (HTTPClient != null)
{
throw new AsyncSendException();
}
@@ -388,7 +388,7 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
{
await WaitForWorkDoneAsync();
}
- else if (WebSocket != null && await WebSocket.Send(SocketMessageType.GamingRequest, GamingType, RequestID, RequestData) == SocketResult.Success)
+ else if (HTTPClient != null && await HTTPClient.Send(SocketMessageType.GamingRequest, GamingType, RequestID, RequestData) == SocketResult.Success)
{
await WaitForWorkDoneAsync();
}
diff --git a/Api/Utility/General.cs b/Api/Utility/General.cs
index 87e6ffb..bd54cc8 100644
--- a/Api/Utility/General.cs
+++ b/Api/Utility/General.cs
@@ -443,7 +443,7 @@ namespace Milimoe.FunGame.Core.Api.Utility
byte[] key_bytes = General.DefaultEncoding.GetBytes(key);
HMACSHA512 hmacsha512 = new(key_bytes);
byte[] hash_bytes = hmacsha512.ComputeHash(text_bytes);
- string hmac = BitConverter.ToString(hash_bytes).Replace("-", "");
+ string hmac = Convert.ToHexString(hash_bytes);
return hmac.ToLower();
}
@@ -452,12 +452,12 @@ namespace Milimoe.FunGame.Core.Api.Utility
///
/// 要计算哈希值的文件路径
/// 文件的 SHA-256 哈希值
- public static string FileSha512(string file_path)
+ public static string FileSha256(string file_path)
{
using SHA256 sha256 = SHA256.Create();
using FileStream stream = File.OpenRead(file_path);
byte[] hash = sha256.ComputeHash(stream);
- return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
+ return Convert.ToHexStringLower(hash);
}
///
@@ -471,7 +471,7 @@ namespace Milimoe.FunGame.Core.Api.Utility
byte[] plain = General.DefaultEncoding.GetBytes(plain_text);
using RSACryptoServiceProvider rsa = new();
rsa.FromXmlString(plublic_key);
- byte[] encrypted = rsa.Encrypt(plain, false);
+ byte[] encrypted = rsa.Encrypt(plain, true);
return Convert.ToBase64String(encrypted);
}
@@ -486,7 +486,7 @@ namespace Milimoe.FunGame.Core.Api.Utility
byte[] secret = Convert.FromBase64String(secret_text);
using RSACryptoServiceProvider rsa = new();
rsa.FromXmlString(private_key);
- byte[] decrypted = rsa.Decrypt(secret, false);
+ byte[] decrypted = rsa.Decrypt(secret, true);
return General.DefaultEncoding.GetString(decrypted);
}
}
diff --git a/Api/Utility/TextReader.cs b/Api/Utility/TextReader.cs
index 8768534..6ed1091 100644
--- a/Api/Utility/TextReader.cs
+++ b/Api/Utility/TextReader.cs
@@ -76,12 +76,16 @@ namespace Milimoe.FunGame.Core.Api.Utility
WriteINI("Account", "AutoKey", "");
break;
case FunGameInfo.FunGame.FunGame_Server:
+ /**
+ * Console
+ */
+ WriteINI("Console", "LogLevel", "INFO");
/**
* Server
*/
WriteINI("Server", "Name", "FunGame Server");
WriteINI("Server", "Password", "");
- WriteINI("Server", "Describe", "Just Another FunGame Server.");
+ WriteINI("Server", "Description", "Just Another FunGame Server.");
WriteINI("Server", "Notice", "This is the FunGame Server's Notice.");
WriteINI("Server", "Key", "");
WriteINI("Server", "Status", "1");
diff --git a/Controller/BaseAddonController.cs b/Controller/BaseAddonController.cs
index 4824c47..88497da 100644
--- a/Controller/BaseAddonController.cs
+++ b/Controller/BaseAddonController.cs
@@ -1,5 +1,6 @@
using Milimoe.FunGame.Core.Interface.Addons;
using Milimoe.FunGame.Core.Library.Common.Addon;
+using Milimoe.FunGame.Core.Library.Constant;
namespace Milimoe.FunGame.Core.Controller
{
@@ -17,7 +18,7 @@ namespace Milimoe.FunGame.Core.Controller
///
/// 输出系统消息
///
- protected Action MaskMethod_WriteLine { get; set; }
+ protected Action MaskMethod_WriteLine { get; set; }
///
/// 输出错误消息
@@ -28,8 +29,10 @@ namespace Milimoe.FunGame.Core.Controller
/// 输出系统消息
///
///
+ ///
+ ///
///
- public void WriteLine(string msg) => MaskMethod_WriteLine(msg);
+ public void WriteLine(string msg, LogLevel level = LogLevel.Info, bool useLevel = true) => MaskMethod_WriteLine(Addon.Name, msg, level, useLevel);
///
/// 输出错误消息
@@ -46,7 +49,7 @@ namespace Milimoe.FunGame.Core.Controller
public BaseAddonController(IAddon addon, Dictionary delegates)
{
Addon = (T)addon;
- if (delegates.TryGetValue("WriteLine", out object? value)) MaskMethod_WriteLine = value != null ? (Action)value : new(DefaultPrint);
+ if (delegates.TryGetValue("WriteLine", out object? value)) MaskMethod_WriteLine = value != null ? (Action)value : new(DefaultPrint);
if (delegates.TryGetValue("Error", out value)) MaskMethod_Error = value != null ? (Action)value : new(DefaultPrint);
MaskMethod_WriteLine ??= new(DefaultPrint);
MaskMethod_Error ??= new(DefaultPrint);
@@ -55,15 +58,22 @@ namespace Milimoe.FunGame.Core.Controller
///
/// 默认的输出错误消息方法
///
+ ///
///
+ ///
+ ///
///
- private void DefaultPrint(string msg) => Console.Write("\r" + msg + "\n\r> ");
+ private void DefaultPrint(string name, string msg, LogLevel level = LogLevel.Info, bool useLevel = true)
+ {
+ DateTime now = DateTime.Now;
+ Console.Write("\r" + now.AddMilliseconds(-now.Millisecond).ToString() + $" {CommonSet.GetLogLevelPrefix(level)}/[Addon] {Addon.Name}:\n\r> ");
+ }
///
/// 输出错误消息
///
///
///
- private void DefaultPrint(Exception e) => DefaultPrint(e.ToString());
+ private void DefaultPrint(Exception e) => DefaultPrint(Addon.Name, e.ToString(), LogLevel.Error);
}
}
diff --git a/Controller/RunTimeController.cs b/Controller/RunTimeController.cs
index 420d9c0..b70b1d6 100644
--- a/Controller/RunTimeController.cs
+++ b/Controller/RunTimeController.cs
@@ -21,7 +21,7 @@ namespace Milimoe.FunGame.Core.Controller
///
/// 与服务器的连接套接字实例(WebSocket)
///
- public HTTPClient? WebSocket => _WebSocket;
+ public HTTPClient? HTTPClient => _HTTPClient;
///
/// 套接字是否已经连接
@@ -41,7 +41,7 @@ namespace Milimoe.FunGame.Core.Controller
///
/// 用于类内赋值
///
- protected HTTPClient? _WebSocket;
+ protected HTTPClient? _HTTPClient;
///
/// 是否正在接收服务器信息
@@ -204,6 +204,7 @@ namespace Milimoe.FunGame.Core.Controller
}
}
});
+ _Socket.ConnectionLost += Error;
}
}
}
@@ -231,12 +232,12 @@ namespace Milimoe.FunGame.Core.Controller
string serverName = "";
string notice = "";
- _WebSocket?.Close();
- _WebSocket = await HTTPClient.Connect(address, ssl, port, subUrl, connectArgs.Cast