Skip to content

Commit 830dc89

Browse files
jiuqianyuan2dust
andauthored
Fix: tcping high latency and speedtest displayed 0 (2dust#8374)
* Fix: High latency in tcping test due to thread blocking * Fix: download to fast, speed displayed as 0. --------- Co-authored-by: 2dust <31833384+2dust@users.noreply.github.com>
1 parent 693afe3 commit 830dc89

2 files changed

Lines changed: 36 additions & 33 deletions

File tree

v2rayN/ServiceLib/Helper/DownloaderHelper.cs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,28 +71,25 @@ public async Task DownloadDataAsync4Speed(IWebProxy webProxy, string url, IProgr
7171
}
7272
};
7373

74-
var totalDatetime = DateTime.Now;
75-
var totalSecond = 0;
74+
var lastUpdateTime = DateTime.Now;
7675
var hasValue = false;
7776
double maxSpeed = 0;
7877
await using var downloader = new Downloader.DownloadService(downloadOpt);
79-
//downloader.DownloadStarted += (sender, value) =>
80-
//{
81-
// if (progress != null)
82-
// {
83-
// progress.Report("Start download data...");
84-
// }
85-
//};
78+
8679
downloader.DownloadProgressChanged += (sender, value) =>
8780
{
88-
var ts = DateTime.Now - totalDatetime;
89-
if (progress != null && ts.Seconds > totalSecond)
81+
if (progress != null && value.BytesPerSecondSpeed > 0)
9082
{
9183
hasValue = true;
92-
totalSecond = ts.Seconds;
9384
if (value.BytesPerSecondSpeed > maxSpeed)
9485
{
9586
maxSpeed = value.BytesPerSecondSpeed;
87+
}
88+
89+
var ts = DateTime.Now - lastUpdateTime;
90+
if (ts.TotalMilliseconds >= 1000)
91+
{
92+
lastUpdateTime = DateTime.Now;
9693
var speed = (maxSpeed / 1000 / 1000).ToString("#0.0");
9794
progress.Report(speed);
9895
}
@@ -102,10 +99,19 @@ public async Task DownloadDataAsync4Speed(IWebProxy webProxy, string url, IProgr
10299
{
103100
if (progress != null)
104101
{
105-
if (!hasValue && value.Error != null)
102+
if (hasValue && maxSpeed > 0)
103+
{
104+
var finalSpeed = (maxSpeed / 1000 / 1000).ToString("#0.0");
105+
progress.Report(finalSpeed);
106+
}
107+
else if (value.Error != null)
106108
{
107109
progress.Report(value.Error?.Message);
108110
}
111+
else
112+
{
113+
progress.Report("0");
114+
}
109115
}
110116
};
111117
//progress.Report("......");

v2rayN/ServiceLib/Services/SpeedtestService.cs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -323,31 +323,28 @@ private async Task<int> GetTcpingTime(string url, int port)
323323
{
324324
var responseTime = -1;
325325

326-
try
326+
if (!IPAddress.TryParse(url, out var ipAddress))
327327
{
328-
if (!IPAddress.TryParse(url, out var ipAddress))
329-
{
330-
var ipHostInfo = await Dns.GetHostEntryAsync(url);
331-
ipAddress = ipHostInfo.AddressList.First();
332-
}
333-
334-
IPEndPoint endPoint = new(ipAddress, port);
335-
using Socket clientSocket = new(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
328+
var ipHostInfo = await Dns.GetHostEntryAsync(url);
329+
ipAddress = ipHostInfo.AddressList.First();
330+
}
336331

337-
var timer = Stopwatch.StartNew();
338-
var result = clientSocket.BeginConnect(endPoint, null, null);
339-
if (!result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(5)))
340-
{
341-
throw new TimeoutException("connect timeout (5s): " + url);
342-
}
343-
timer.Stop();
344-
responseTime = (int)timer.Elapsed.TotalMilliseconds;
332+
IPEndPoint endPoint = new(ipAddress, port);
333+
using Socket clientSocket = new(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
345334

346-
clientSocket.EndConnect(result);
335+
var timer = Stopwatch.StartNew();
336+
try
337+
{
338+
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
339+
await clientSocket.ConnectAsync(endPoint, cts.Token).ConfigureAwait(false);
347340
}
348-
catch (Exception ex)
341+
catch (OperationCanceledException)
349342
{
350-
Logging.SaveLog(_tag, ex);
343+
}
344+
finally
345+
{
346+
timer.Stop();
347+
responseTime = (int)timer.Elapsed.TotalMilliseconds;
351348
}
352349
return responseTime;
353350
}

0 commit comments

Comments
 (0)