From 6d15ddc0a468c8a72b76188d4378c6387b231878 Mon Sep 17 00:00:00 2001 From: jiuqianyuan <39406781+jiuqianyuan@users.noreply.github.com> Date: Sun, 23 Nov 2025 20:05:57 +0800 Subject: [PATCH 1/2] Fix: High latency in tcping test due to thread blocking --- v2rayN/ServiceLib/Services/SpeedtestService.cs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/v2rayN/ServiceLib/Services/SpeedtestService.cs b/v2rayN/ServiceLib/Services/SpeedtestService.cs index 00f54427..4389702a 100644 --- a/v2rayN/ServiceLib/Services/SpeedtestService.cs +++ b/v2rayN/ServiceLib/Services/SpeedtestService.cs @@ -335,15 +335,14 @@ public class SpeedtestService(Config config, Func updateF using Socket clientSocket = new(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); var timer = Stopwatch.StartNew(); - var result = clientSocket.BeginConnect(endPoint, null, null); - if (!result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(5))) - { - throw new TimeoutException("connect timeout (5s): " + url); - } + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)); + await clientSocket.ConnectAsync(endPoint, cts.Token).ConfigureAwait(false); timer.Stop(); responseTime = (int)timer.Elapsed.TotalMilliseconds; - - clientSocket.EndConnect(result); + } + catch (OperationCanceledException) + { + // 超时情况,responseTime保持为-1 } catch (Exception ex) { From fd45c592273b086c55d58cc0f7d5e49a1fe059b3 Mon Sep 17 00:00:00 2001 From: jiuqianyuan <39406781+jiuqianyuan@users.noreply.github.com> Date: Mon, 24 Nov 2025 02:41:15 +0800 Subject: [PATCH 2/2] Fix: download to fast, speed displayed as 0. --- v2rayN/ServiceLib/Helper/DownloaderHelper.cs | 26 ++++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/v2rayN/ServiceLib/Helper/DownloaderHelper.cs b/v2rayN/ServiceLib/Helper/DownloaderHelper.cs index 000685a0..b4466b72 100644 --- a/v2rayN/ServiceLib/Helper/DownloaderHelper.cs +++ b/v2rayN/ServiceLib/Helper/DownloaderHelper.cs @@ -71,28 +71,25 @@ public class DownloaderHelper } }; - var totalDatetime = DateTime.Now; - var totalSecond = 0; + var lastUpdateTime = DateTime.Now; var hasValue = false; double maxSpeed = 0; await using var downloader = new Downloader.DownloadService(downloadOpt); - //downloader.DownloadStarted += (sender, value) => - //{ - // if (progress != null) - // { - // progress.Report("Start download data..."); - // } - //}; + downloader.DownloadProgressChanged += (sender, value) => { - var ts = DateTime.Now - totalDatetime; - if (progress != null && ts.Seconds > totalSecond) + if (progress != null && value.BytesPerSecondSpeed > 0) { hasValue = true; - totalSecond = ts.Seconds; if (value.BytesPerSecondSpeed > maxSpeed) { maxSpeed = value.BytesPerSecondSpeed; + } + + var ts = DateTime.Now - lastUpdateTime; + if (ts.TotalMilliseconds >= 1000) + { + lastUpdateTime = DateTime.Now; var speed = (maxSpeed / 1000 / 1000).ToString("#0.0"); progress.Report(speed); } @@ -102,8 +99,11 @@ public class DownloaderHelper { if (progress != null) { - if (!hasValue && value.Error != null) + if (hasValue && maxSpeed > 0) { + var finalSpeed = (maxSpeed / 1000 / 1000).ToString("#0.0"); + progress.Report(finalSpeed); + }else if (value.Error != null) { progress.Report(value.Error?.Message); } }