From d383d4d15b52842d58198c3194021fef41ba9834 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Thu, 14 Dec 2023 12:29:08 +0100 Subject: [PATCH] Add download speed to `NetworkSocket` --- .../Commands/DownloadCommands.cs | 38 ++----------------- .../Commands/HostCommands.cs | 2 + .../Networking/NetworkSocket.cs | 2 + .../Utilities/UnitFormatter.cs | 37 ++++++++++++++++++ 4 files changed, 44 insertions(+), 35 deletions(-) create mode 100644 HyperbolicDownloaderApi/Utilities/UnitFormatter.cs diff --git a/HyperbolicDownloaderApi/Commands/DownloadCommands.cs b/HyperbolicDownloaderApi/Commands/DownloadCommands.cs index 63dd3ce..5a6b2af 100644 --- a/HyperbolicDownloaderApi/Commands/DownloadCommands.cs +++ b/HyperbolicDownloaderApi/Commands/DownloadCommands.cs @@ -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"; - } } \ No newline at end of file diff --git a/HyperbolicDownloaderApi/Commands/HostCommands.cs b/HyperbolicDownloaderApi/Commands/HostCommands.cs index 875464f..27aee4d 100644 --- a/HyperbolicDownloaderApi/Commands/HostCommands.cs +++ b/HyperbolicDownloaderApi/Commands/HostCommands.cs @@ -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--; diff --git a/HyperbolicDownloaderApi/Networking/NetworkSocket.cs b/HyperbolicDownloaderApi/Networking/NetworkSocket.cs index 18e3a74..bb23e14 100644 --- a/HyperbolicDownloaderApi/Networking/NetworkSocket.cs +++ b/HyperbolicDownloaderApi/Networking/NetworkSocket.cs @@ -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; diff --git a/HyperbolicDownloaderApi/Utilities/UnitFormatter.cs b/HyperbolicDownloaderApi/Utilities/UnitFormatter.cs new file mode 100644 index 0000000..7e5beb9 --- /dev/null +++ b/HyperbolicDownloaderApi/Utilities/UnitFormatter.cs @@ -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"; + } +}