Compare commits

..

No commits in common. "386209b83547afbead197ad3bfc619e23e0a65b2" and "693afe35604dae12e61ef4afccb978b3ffca94b6" have entirely different histories.

2 changed files with 35 additions and 38 deletions

View file

@ -71,25 +71,28 @@ public class DownloaderHelper
} }
}; };
var lastUpdateTime = DateTime.Now; var totalDatetime = DateTime.Now;
var totalSecond = 0;
var hasValue = false; var hasValue = false;
double maxSpeed = 0; double maxSpeed = 0;
await using var downloader = new Downloader.DownloadService(downloadOpt); await using var downloader = new Downloader.DownloadService(downloadOpt);
//downloader.DownloadStarted += (sender, value) =>
//{
// if (progress != null)
// {
// progress.Report("Start download data...");
// }
//};
downloader.DownloadProgressChanged += (sender, value) => downloader.DownloadProgressChanged += (sender, value) =>
{ {
if (progress != null && value.BytesPerSecondSpeed > 0) var ts = DateTime.Now - totalDatetime;
if (progress != null && ts.Seconds > totalSecond)
{ {
hasValue = true; hasValue = true;
totalSecond = ts.Seconds;
if (value.BytesPerSecondSpeed > maxSpeed) if (value.BytesPerSecondSpeed > maxSpeed)
{ {
maxSpeed = value.BytesPerSecondSpeed; maxSpeed = value.BytesPerSecondSpeed;
}
var ts = DateTime.Now - lastUpdateTime;
if (ts.TotalMilliseconds >= 1000)
{
lastUpdateTime = DateTime.Now;
var speed = (maxSpeed / 1000 / 1000).ToString("#0.0"); var speed = (maxSpeed / 1000 / 1000).ToString("#0.0");
progress.Report(speed); progress.Report(speed);
} }
@ -99,19 +102,10 @@ public class DownloaderHelper
{ {
if (progress != null) if (progress != null)
{ {
if (hasValue && maxSpeed > 0) if (!hasValue && value.Error != null)
{
var finalSpeed = (maxSpeed / 1000 / 1000).ToString("#0.0");
progress.Report(finalSpeed);
}
else if (value.Error != null)
{ {
progress.Report(value.Error?.Message); progress.Report(value.Error?.Message);
} }
else
{
progress.Report("0");
}
} }
}; };
//progress.Report("......"); //progress.Report("......");

View file

@ -323,6 +323,8 @@ public class SpeedtestService(Config config, Func<SpeedTestResult, Task> updateF
{ {
var responseTime = -1; var responseTime = -1;
try
{
if (!IPAddress.TryParse(url, out var ipAddress)) if (!IPAddress.TryParse(url, out var ipAddress))
{ {
var ipHostInfo = await Dns.GetHostEntryAsync(url); var ipHostInfo = await Dns.GetHostEntryAsync(url);
@ -333,18 +335,19 @@ public class SpeedtestService(Config config, Func<SpeedTestResult, Task> updateF
using Socket clientSocket = new(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); using Socket clientSocket = new(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
var timer = Stopwatch.StartNew(); var timer = Stopwatch.StartNew();
try var result = clientSocket.BeginConnect(endPoint, null, null);
if (!result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(5)))
{ {
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)); throw new TimeoutException("connect timeout (5s): " + url);
await clientSocket.ConnectAsync(endPoint, cts.Token).ConfigureAwait(false);
responseTime = (int)timer.ElapsedMilliseconds;
} }
catch (OperationCanceledException)
{
}
finally
{
timer.Stop(); timer.Stop();
responseTime = (int)timer.Elapsed.TotalMilliseconds;
clientSocket.EndConnect(result);
}
catch (Exception ex)
{
Logging.SaveLog(_tag, ex);
} }
return responseTime; return responseTime;
} }