mirror of
https://github.com/Stone-Red-Code/HyperbolicDownloader.git
synced 2026-09-04 09:06:10 +02:00
Add download speed to NetworkSocket
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using HyperbolicDownloaderApi.FileProcessing;
|
||||
using HyperbolicDownloaderApi.Managment;
|
||||
using HyperbolicDownloaderApi.Networking;
|
||||
using HyperbolicDownloaderApi.Utilities;
|
||||
|
||||
using Stone_Red_Utilities.StringExtentions;
|
||||
|
||||
@@ -187,10 +188,11 @@ public class DownloadCommands
|
||||
{
|
||||
transferRate = bytesPerSecond;
|
||||
bytesPerSecond = 0;
|
||||
host.DownloadSpeed = (host.DownloadSpeed + bytesPerSecond) / 2;
|
||||
stopWatch.Restart();
|
||||
}
|
||||
|
||||
ApiManager.SendNotificationMessage($"\rDownloading: {Math.Clamp(Math.Ceiling(100d / fileSize * totalBytesRead), 0, 100)}% {DisplayFileSize(totalBytesRead)}/{DisplayFileSize(fileSize)} [{DisplayTransferRate(transferRate)}] ");
|
||||
ApiManager.SendNotificationMessage($"\rDownloading: {Math.Clamp(Math.Ceiling(100d / fileSize * totalBytesRead), 0, 100)}% {UnitFormatter.FileSize(totalBytesRead)}/{UnitFormatter.FileSize(fileSize)} [{UnitFormatter.TransferRate(transferRate)}] ");
|
||||
}
|
||||
|
||||
fileStream.Close();
|
||||
@@ -222,38 +224,4 @@ public class DownloadCommands
|
||||
ApiManager.SendNotificationMessageNewLine("None of the available hosts have the requested file!", NotificationMessageType.Error);
|
||||
hostsManager.SaveHosts();
|
||||
}
|
||||
|
||||
private string DisplayTransferRate(long bytesPerSecond)
|
||||
{
|
||||
string[] ordinals = new[] { "", "K", "M", "G", "T", "P", "E" };
|
||||
|
||||
decimal rate = bytesPerSecond * 8;
|
||||
|
||||
int ordinal = 0;
|
||||
|
||||
while (rate > 1000)
|
||||
{
|
||||
rate /= 1000;
|
||||
ordinal++;
|
||||
}
|
||||
|
||||
return $"{Math.Round(rate, 0, MidpointRounding.AwayFromZero)}{ordinals[ordinal]}bps";
|
||||
}
|
||||
|
||||
private string DisplayFileSize(long bytes)
|
||||
{
|
||||
string[] ordinals = new[] { "", "K", "M", "G", "T", "P", "E" };
|
||||
|
||||
decimal rate = bytes;
|
||||
|
||||
int ordinal = 0;
|
||||
|
||||
while (rate > 1000)
|
||||
{
|
||||
rate /= 1000;
|
||||
ordinal++;
|
||||
}
|
||||
|
||||
return $"{Math.Round(rate, 0, MidpointRounding.AwayFromZero)}{ordinals[ordinal]}B";
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using HyperbolicDownloaderApi.Managment;
|
||||
using HyperbolicDownloaderApi.Networking;
|
||||
using HyperbolicDownloaderApi.Utilities;
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
@@ -58,6 +59,7 @@ public class HostCommands
|
||||
index++;
|
||||
ApiManager.SendNotificationMessageNewLine($"{index}) {host.IPAddress}:{host.Port}", NotificationMessageType.Info);
|
||||
ApiManager.SendNotificationMessageNewLine($"Last active: {host.LastActive}", NotificationMessageType.Info);
|
||||
ApiManager.SendNotificationMessageNewLine($"Download speed: {(host.DownloadSpeed <= 0 ? "N/A" : UnitFormatter.TransferRate(host.DownloadSpeed))}", NotificationMessageType.Info);
|
||||
ApiManager.SendNotificationMessageNewLine(string.Empty, NotificationMessageType.Info);
|
||||
}
|
||||
Console.CursorTop--;
|
||||
|
||||
@@ -6,6 +6,8 @@ public class NetworkSocket
|
||||
public int Port { get; set; }
|
||||
public DateTime LastActive { get; set; }
|
||||
|
||||
public long DownloadSpeed { get; set; }
|
||||
|
||||
public NetworkSocket(string ipAddress, int port, DateTime lastActive)
|
||||
{
|
||||
IPAddress = ipAddress;
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
namespace HyperbolicDownloaderApi.Utilities;
|
||||
internal static class UnitFormatter
|
||||
{
|
||||
public static string TransferRate(long bytesPerSecond)
|
||||
{
|
||||
string[] ordinals = new[] { "", "K", "M", "G", "T", "P", "E" };
|
||||
|
||||
decimal rate = bytesPerSecond * 8;
|
||||
|
||||
int ordinal = 0;
|
||||
|
||||
while (rate > 1000)
|
||||
{
|
||||
rate /= 1000;
|
||||
ordinal++;
|
||||
}
|
||||
|
||||
return $"{Math.Round(rate, 0, MidpointRounding.AwayFromZero)}{ordinals[ordinal]}bps";
|
||||
}
|
||||
|
||||
public static string FileSize(long bytes)
|
||||
{
|
||||
string[] ordinals = new[] { "", "K", "M", "G", "T", "P", "E" };
|
||||
|
||||
decimal rate = bytes;
|
||||
|
||||
int ordinal = 0;
|
||||
|
||||
while (rate > 1000)
|
||||
{
|
||||
rate /= 1000;
|
||||
ordinal++;
|
||||
}
|
||||
|
||||
return $"{Math.Round(rate, 0, MidpointRounding.AwayFromZero)}{ordinals[ordinal]}B";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user