Refactor ping and HTTP helpers, update usages

Moved GetRealPingTime from HttpClientHelper to ConnectionHandler and refactored related methods for clarity. Removed unused and redundant HTTP helper methods. Updated DownloadService and SpeedtestService to use the new method signatures. Simplified UpdateService constructor using primary constructor syntax.
This commit is contained in:
2dust
2025-11-03 19:41:02 +08:00
parent 1aee3950f4
commit 8d86aa2b72
5 changed files with 41 additions and 175 deletions
@@ -71,7 +71,7 @@ public class DownloadService
AllowAutoRedirect = false,
Proxy = await GetWebProxy(blProxy)
};
HttpClient client = new(webRequestHandler);
var client = new HttpClient(webRequestHandler);
var response = await client.GetAsync(url);
if (response.StatusCode == HttpStatusCode.Redirect && response.Headers.Location is not null)
@@ -156,7 +156,7 @@ public class DownloadService
}
using var cts = new CancellationTokenSource();
var result = await HttpClientHelper.Instance.GetAsync(client, url, cts.Token).WaitAsync(TimeSpan.FromSeconds(timeout), cts.Token);
var result = await client.GetStringAsync(url, cts.Token).WaitAsync(TimeSpan.FromSeconds(timeout), cts.Token);
return result;
}
catch (Exception ex)
@@ -272,7 +272,7 @@ public class SpeedtestService(Config config, Func<SpeedTestResult, Task> updateF
private async Task<int> DoRealPing(ServerTestItem it)
{
var webProxy = new WebProxy($"socks5://{Global.Loopback}:{it.Port}");
var responseTime = await HttpClientHelper.Instance.GetRealPingTime(_config.SpeedTestItem.SpeedPingTestUrl, webProxy, 10);
var responseTime = await ConnectionHandler.GetRealPingTime(_config.SpeedTestItem.SpeedPingTestUrl, webProxy, 10);
ProfileExManager.Instance.SetTestDelay(it.IndexId, responseTime);
await UpdateFunc(it.IndexId, responseTime.ToString());
+3 -9
View File
@@ -1,18 +1,12 @@
namespace ServiceLib.Services;
public class UpdateService
public class UpdateService(Config config, Func<bool, string, Task> updateFunc)
{
private readonly Config? _config;
private readonly Func<bool, string, Task>? _updateFunc;
private readonly Config? _config = config;
private readonly Func<bool, string, Task>? _updateFunc = updateFunc;
private readonly int _timeout = 30;
private static readonly string _tag = "UpdateService";
public UpdateService(Config config, Func<bool, string, Task> updateFunc)
{
_config = config;
_updateFunc = updateFunc;
}
public async Task CheckUpdateGuiN(bool preRelease)
{
var url = string.Empty;