mirror of
https://github.com/2dust/v2rayN.git
synced 2025-11-27 02:02:52 +00:00
Fix: tcping high latency and speedtest displayed 0 (#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>
This commit is contained in:
parent
693afe3560
commit
830dc89c32
2 changed files with 37 additions and 34 deletions
|
|
@ -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,10 +99,19 @@ 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);
|
||||
}
|
||||
else
|
||||
{
|
||||
progress.Report("0");
|
||||
}
|
||||
}
|
||||
};
|
||||
//progress.Report("......");
|
||||
|
|
|
|||
|
|
@ -323,8 +323,6 @@ public class SpeedtestService(Config config, Func<SpeedTestResult, Task> updateF
|
|||
{
|
||||
var responseTime = -1;
|
||||
|
||||
try
|
||||
{
|
||||
if (!IPAddress.TryParse(url, out var ipAddress))
|
||||
{
|
||||
var ipHostInfo = await Dns.GetHostEntryAsync(url);
|
||||
|
|
@ -335,19 +333,18 @@ public class SpeedtestService(Config config, Func<SpeedTestResult, Task> 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)))
|
||||
try
|
||||
{
|
||||
throw new TimeoutException("connect timeout (5s): " + url);
|
||||
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
|
||||
await clientSocket.ConnectAsync(endPoint, cts.Token).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
}
|
||||
finally
|
||||
{
|
||||
timer.Stop();
|
||||
responseTime = (int)timer.Elapsed.TotalMilliseconds;
|
||||
|
||||
clientSocket.EndConnect(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logging.SaveLog(_tag, ex);
|
||||
}
|
||||
return responseTime;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue