diff --git a/v2rayN/ServiceLib/Services/DownloadService.cs b/v2rayN/ServiceLib/Services/DownloadService.cs index f37c8939..99170646 100644 --- a/v2rayN/ServiceLib/Services/DownloadService.cs +++ b/v2rayN/ServiceLib/Services/DownloadService.cs @@ -15,7 +15,7 @@ public class DownloadService private static readonly string _tag = "DownloadService"; - public async Task DownloadDataAsync(string url, WebProxy webProxy, int downloadTimeout, Action updateFunc) + public async Task DownloadDataAsync(string url, WebProxy webProxy, int downloadTimeout, Func updateFunc) { try { @@ -31,10 +31,10 @@ public class DownloadService } catch (Exception ex) { - updateFunc?.Invoke(false, ex.Message); + await updateFunc?.Invoke(false, ex.Message); if (ex.InnerException != null) { - updateFunc?.Invoke(false, ex.InnerException.Message); + await updateFunc?.Invoke(false, ex.InnerException.Message); } } return 0; diff --git a/v2rayN/ServiceLib/Services/SpeedtestService.cs b/v2rayN/ServiceLib/Services/SpeedtestService.cs index 002a6251..5266f4d0 100644 --- a/v2rayN/ServiceLib/Services/SpeedtestService.cs +++ b/v2rayN/ServiceLib/Services/SpeedtestService.cs @@ -5,26 +5,20 @@ using System.Net.Sockets; namespace ServiceLib.Services; -public class SpeedtestService +public class SpeedtestService(Config config, Func updateFunc) { private static readonly string _tag = "SpeedtestService"; - private Config? _config; - private Action? _updateFunc; + private readonly Config? _config = config; + private readonly Func? _updateFunc = updateFunc; private static readonly ConcurrentBag _lstExitLoop = new(); - public SpeedtestService(Config config, Action updateFunc) - { - _config = config; - _updateFunc = updateFunc; - } - public void RunLoop(ESpeedActionType actionType, List selecteds) { Task.Run(async () => { await RunAsync(actionType, selecteds); await ProfileExManager.Instance.SaveTo(); - UpdateFunc("", ResUI.SpeedtestingCompleted); + await UpdateFunc("", ResUI.SpeedtestingCompleted); }); } @@ -43,7 +37,7 @@ public class SpeedtestService var exitLoopKey = Utils.GetGuid(false); _lstExitLoop.Add(exitLoopKey); - var lstSelected = GetClearItem(actionType, selecteds); + var lstSelected = await GetClearItem(actionType, selecteds); switch (actionType) { @@ -65,7 +59,7 @@ public class SpeedtestService } } - private List GetClearItem(ESpeedActionType actionType, List selecteds) + private async Task> GetClearItem(ESpeedActionType actionType, List selecteds) { var lstSelected = new List(); foreach (var it in selecteds) @@ -97,17 +91,17 @@ public class SpeedtestService { case ESpeedActionType.Tcping: case ESpeedActionType.Realping: - UpdateFunc(it.IndexId, ResUI.Speedtesting, ""); + await UpdateFunc(it.IndexId, ResUI.Speedtesting, ""); ProfileExManager.Instance.SetTestDelay(it.IndexId, 0); break; case ESpeedActionType.Speedtest: - UpdateFunc(it.IndexId, "", ResUI.SpeedtestingWait); + await UpdateFunc(it.IndexId, "", ResUI.SpeedtestingWait); ProfileExManager.Instance.SetTestSpeed(it.IndexId, 0); break; case ESpeedActionType.Mixedtest: - UpdateFunc(it.IndexId, ResUI.Speedtesting, ResUI.SpeedtestingWait); + await UpdateFunc(it.IndexId, ResUI.Speedtesting, ResUI.SpeedtestingWait); ProfileExManager.Instance.SetTestDelay(it.IndexId, 0); ProfileExManager.Instance.SetTestSpeed(it.IndexId, 0); break; @@ -133,7 +127,7 @@ public class SpeedtestService var responseTime = await GetTcpingTime(it.Address, it.Port); ProfileExManager.Instance.SetTestDelay(it.IndexId, responseTime); - UpdateFunc(it.IndexId, responseTime.ToString()); + await UpdateFunc(it.IndexId, responseTime.ToString()); } catch (Exception ex) { @@ -169,11 +163,11 @@ public class SpeedtestService { if (_lstExitLoop.Any(p => p == exitLoopKey) == false) { - UpdateFunc("", ResUI.SpeedtestingSkip); + await UpdateFunc("", ResUI.SpeedtestingSkip); return; } - UpdateFunc("", string.Format(ResUI.SpeedtestingTestFailedPart, lstFailed.Count)); + await UpdateFunc("", string.Format(ResUI.SpeedtestingTestFailedPart, lstFailed.Count)); if (pageSizeNext > _config.SpeedTestItem.MixedConcurrencyCount) { @@ -239,7 +233,7 @@ public class SpeedtestService { if (_lstExitLoop.Any(p => p == exitLoopKey) == false) { - UpdateFunc(it.IndexId, "", ResUI.SpeedtestingSkip); + await UpdateFunc(it.IndexId, "", ResUI.SpeedtestingSkip); continue; } if (it.ConfigType == EConfigType.Custom) @@ -256,7 +250,7 @@ public class SpeedtestService pid = await CoreManager.Instance.LoadCoreConfigSpeedtest(it); if (pid < 0) { - UpdateFunc(it.IndexId, "", ResUI.FailedToRunCore); + await UpdateFunc(it.IndexId, "", ResUI.FailedToRunCore); } else { @@ -270,7 +264,7 @@ public class SpeedtestService } else { - UpdateFunc(it.IndexId, "", ResUI.SpeedtestingSkip); + await UpdateFunc(it.IndexId, "", ResUI.SpeedtestingSkip); } } } @@ -298,25 +292,25 @@ public class SpeedtestService var responseTime = await HttpClientHelper.Instance.GetRealPingTime(_config.SpeedTestItem.SpeedPingTestUrl, webProxy, 10); ProfileExManager.Instance.SetTestDelay(it.IndexId, responseTime); - UpdateFunc(it.IndexId, responseTime.ToString()); + await UpdateFunc(it.IndexId, responseTime.ToString()); return responseTime; } private async Task DoSpeedTest(DownloadService downloadHandle, ServerTestItem it) { - UpdateFunc(it.IndexId, "", ResUI.Speedtesting); + await UpdateFunc(it.IndexId, "", ResUI.Speedtesting); var webProxy = new WebProxy($"socks5://{Global.Loopback}:{it.Port}"); var url = _config.SpeedTestItem.SpeedTestUrl; var timeout = _config.SpeedTestItem.SpeedTestTimeout; - await downloadHandle.DownloadDataAsync(url, webProxy, timeout, (success, msg) => + await downloadHandle.DownloadDataAsync(url, webProxy, timeout, async (success, msg) => { decimal.TryParse(msg, out var dec); if (dec > 0) { ProfileExManager.Instance.SetTestSpeed(it.IndexId, dec); } - UpdateFunc(it.IndexId, "", msg); + await UpdateFunc(it.IndexId, "", msg); }); } @@ -371,9 +365,9 @@ public class SpeedtestService return lstTest; } - private void UpdateFunc(string indexId, string delay, string speed = "") + private async Task UpdateFunc(string indexId, string delay, string speed = "") { - _updateFunc?.Invoke(new() { IndexId = indexId, Delay = delay, Speed = speed }); + await _updateFunc?.Invoke(new() { IndexId = indexId, Delay = delay, Speed = speed }); if (indexId.IsNotEmpty() && speed.IsNotEmpty()) { ProfileExManager.Instance.SetTestMessage(indexId, speed);