Skip to content

Commit a5c1f91

Browse files
committed
refactor: 优化部分代码
1 parent 0fbd6a8 commit a5c1f91

File tree

15 files changed

+134
-171
lines changed

15 files changed

+134
-171
lines changed

CSharp-OpenBMCLAPI/Modules/AvroParser.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,12 @@
33

44
namespace CSharpOpenBMCLAPI.Modules
55
{
6-
public class AvroParser
6+
public class AvroParser(byte[] data)
77
{
8-
private MemoryStream _stream;
9-
private List<ApiFileInfo> _files;
8+
private MemoryStream _stream = new(data);
9+
private List<ApiFileInfo> _files = new();
1010
private byte[] data;
1111

12-
public AvroParser(byte[] data)
13-
{
14-
this.data = data;
15-
this._stream = new MemoryStream(data);
16-
this._files = new List<ApiFileInfo>();
17-
}
18-
1912
public List<ApiFileInfo> Parse()
2013
{
2114
this._files.Clear();
@@ -56,7 +49,7 @@ public long ReadLong()
5649
public string ReadString()
5750
{
5851
byte[] buffer = new byte[this.ReadLong()];
59-
this._stream.Read(buffer, 0, buffer.Length);
52+
this._stream.ReadExactly(buffer, 0, buffer.Length);
6053
return Encoding.UTF8.GetString(buffer);
6154
}
6255
}

CSharp-OpenBMCLAPI/Modules/Cluster.cs

Lines changed: 88 additions & 107 deletions
Large diffs are not rendered by default.

CSharp-OpenBMCLAPI/Modules/ClusterInfo.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,9 @@
33
/// <summary>
44
/// 负责存储 Cluster 的 secret 和 id
55
/// </summary>
6-
public struct ClusterInfo
6+
public struct ClusterInfo(string id, string secret)
77
{
8-
public string ClusterID, ClusterSecret;
9-
10-
public ClusterInfo(string id, string secret)
11-
{
12-
this.ClusterID = id;
13-
this.ClusterSecret = secret;
14-
}
8+
public readonly string clusterId = id;
9+
public readonly string clusterSecret = secret;
1510
}
1611
}

CSharp-OpenBMCLAPI/Modules/ClusterRequiredData.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

CSharp-OpenBMCLAPI/Modules/HttpRequest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ static HttpRequest()
1111
client = new HttpClient()
1212
{
1313
// 设置基础地址
14-
BaseAddress = new Uri(ClusterRequiredData.Config.CenterServerAddress),
14+
BaseAddress = new Uri(PublicData.Config.CenterServerAddress),
1515
Timeout = TimeSpan.FromMinutes(5)
1616
};
1717
// 添加UserAgent,用于标识请求来源
18-
string ua = $"openbmclapi-cluster/{ClusterRequiredData.Config.clusterVersion} (CSharp-OpenBMCLAPI; .NET runtime v{Environment.Version}; {Environment.OSVersion}, {System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture}; {System.Globalization.CultureInfo.InstalledUICulture.Name})";
18+
string ua = $"openbmclapi-cluster/{PublicData.Config.clusterVersion} (CSharp-OpenBMCLAPI; .NET runtime v{Environment.Version}; {Environment.OSVersion}, {System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture}; {System.Globalization.CultureInfo.InstalledUICulture.Name})";
1919
client.DefaultRequestHeaders.Add("User-Agent", ua);
2020
Console.WriteLine($"User-Agent: {ua}");
2121
}

CSharp-OpenBMCLAPI/Modules/HttpServiceProvider.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class HttpServiceProvider
1515
/// <returns></returns>
1616
public static void LogAccess(HttpContext context)
1717
{
18-
if (!ClusterRequiredData.Config.DisableAccessLog)
18+
if (!PublicData.Config.DisableAccessLog)
1919
{
2020
context.Request.Headers.TryGetValue("user-agent", out StringValues value);
2121
Logger.Instance.LogInfo($"{context.Request.Method} {context.Request.Path.Value} {context.Request.Protocol} <{context.Response.StatusCode}> - [{context.Connection.RemoteIpAddress}] {value.FirstOrDefault()}");
@@ -32,7 +32,7 @@ public static async Task Measure(HttpContext context, Cluster cluster, int size)
3232
context.Request.Query.TryGetValue("s", out StringValues s);
3333
context.Request.Query.TryGetValue("e", out StringValues e);
3434
bool valid = Utils.CheckSign(context.Request.Path.Value?.Split('?').First()
35-
, cluster.requiredData.ClusterInfo.ClusterSecret
35+
, cluster.requiredData.ClusterInfo.clusterSecret
3636
, s.FirstOrDefault()
3737
, e.FirstOrDefault()
3838
);
@@ -68,7 +68,7 @@ public static async Task<FileAccessInfo> DownloadHash(HttpContext context, Clust
6868
context.Request.Query.TryGetValue("s", out StringValues s);
6969
context.Request.Query.TryGetValue("e", out StringValues e);
7070

71-
bool isValid = Utils.CheckSign(hash, cluster.clusterInfo.ClusterSecret, s.FirstOrDefault(), e.FirstOrDefault());
71+
bool isValid = Utils.CheckSign(hash, cluster.clusterInfo.clusterSecret, s.FirstOrDefault(), e.FirstOrDefault());
7272

7373
if (!isValid)
7474
{
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace CSharpOpenBMCLAPI.Modules
2+
{
3+
public class PublicData(ClusterInfo info)
4+
{
5+
public static Config Config { get; set; } = new Config();
6+
public Logger Logger => Logger.Instance;
7+
public ClusterInfo ClusterInfo { get; set; } = info;
8+
public TokenManager Token { get; set; } = new(info);
9+
public SemaphoreSlim SemaphoreSlim { get; set; } = new SemaphoreSlim(0);
10+
11+
internal int maxThreadCount;
12+
}
13+
}

CSharp-OpenBMCLAPI/Modules/Storage/AlistStorage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public AlistStorage()
1414
{
1515
this.client = new HttpClient();
1616
this.baseAddr = "BMCLAPI/cache";
17-
this.client.BaseAddress = new Uri(ClusterRequiredData.Config.clusterFileDirectory);
18-
this.user = ClusterRequiredData.Config.storageUser;
17+
this.client.BaseAddress = new Uri(PublicData.Config.clusterFileDirectory);
18+
this.user = PublicData.Config.storageUser;
1919
}
2020

2121
public bool Exists(string hashPath)

CSharp-OpenBMCLAPI/Modules/Storage/CachedStorage.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class CachedStorage : ICachedStorage
3838
public CachedStorage(IStorage storage)
3939
{
4040
this.storage = storage;
41-
if (ClusterRequiredData.Config.maxCachedMemory == 0) this.cacheEnabled = false;
41+
if (PublicData.Config.maxCachedMemory == 0) this.cacheEnabled = false;
4242
}
4343

4444
public bool Exists(string hashPath)
@@ -146,17 +146,17 @@ public void Initialize()
146146
public void MemoryWatchdog()
147147
{
148148
double memory = GetCachedMemory();
149-
if (memory > 0 && memory > ClusterRequiredData.Config.maxCachedMemory * 1048576)
149+
if (memory > 0 && memory > PublicData.Config.maxCachedMemory * 1048576)
150150
{
151-
Logger.Instance.LogWarn($"缓存存储已大于 {ClusterRequiredData.Config.maxCachedMemory}(当前 {memory / 1048576}),已开始清理缓存");
151+
Logger.Instance.LogWarn($"缓存存储已大于 {PublicData.Config.maxCachedMemory}(当前 {memory / 1048576}),已开始清理缓存");
152152
int count = 0;
153153
do
154154
{
155155
string biggestFileKey = this.cache.OrderBy(kvp => kvp.Value.content.Length).Last().Key;
156156
this.cache.Remove(biggestFileKey);
157157
count++;
158158
}
159-
while (GetCachedMemory() > ClusterRequiredData.Config.maxCachedMemory || count <= 5); // 限制只有清理到内存低于指定值,并且清理次数大于五次才停止
159+
while (GetCachedMemory() > PublicData.Config.maxCachedMemory || count <= 5); // 限制只有清理到内存低于指定值,并且清理次数大于五次才停止
160160
}
161161
}
162162

CSharp-OpenBMCLAPI/Modules/Storage/FileStorage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public FileStorage(string workingDirectory)
1717
this.workingDirectory = workingDirectory;
1818
if (workingDirectory.StartsWith(@"\\"))
1919
{
20-
StorageUser user = ClusterRequiredData.Config.storageUser;
20+
StorageUser user = PublicData.Config.storageUser;
2121
this.connection = new SambaConnection(user.UserName, user.Password, Regex.Match(workingDirectory, @"\\\\(.*?)\\").Groups[1].Value);
2222
}
2323
}

0 commit comments

Comments
 (0)