Skip to content

Commit 9f08b5c

Browse files
Merge branch 'develop'
2 parents 715f5de + f906d45 commit 9f08b5c

File tree

12 files changed

+174
-161
lines changed

12 files changed

+174
-161
lines changed

.editorconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@
22

33
# S4457: Parameter validation in "async"/"await" methods should be wrapped
44
dotnet_diagnostic.S4457.severity = silent
5+
6+
# CA1822: Mark members as static
7+
dotnet_diagnostic.CA1822.severity = silent
8+
9+
# S1172: Unused method parameters should be removed
10+
dotnet_diagnostic.S1172.severity = silent

HyperbolicDownloader/HyperbolicDownloaderCli.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>

HyperbolicDownloader/UserInterface/InputHandler.cs renamed to HyperbolicDownloader/InputHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
using Stone_Red_Utilities.ConsoleExtentions;
77

8-
namespace HyperbolicDownloaderApi.UserInterface;
8+
namespace HyperbolicDownloaderApi;
99

1010
internal class InputHandler
1111
{

HyperbolicDownloader/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using HyperbolicDownloaderApi.FileProcessing;
22
using HyperbolicDownloaderApi.Managment;
33
using HyperbolicDownloaderApi.Networking;
4-
using HyperbolicDownloaderApi.UserInterface;
54

65
using Stone_Red_Utilities.ConsoleExtentions;
76

HyperbolicDownloaderApi/Commands/ClientCommands.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ public void ShowInfo(string _)
99
{
1010
if (ApiManager.PublicIpAddress is not null)
1111
{
12-
ApiManager.SendMessageNewLine($"The public IP address is: {ApiManager.PublicIpAddress}", NotificationMessageType.Info);
13-
ApiManager.SendMessageNewLine($"The public port is: {ApiConfiguration.PublicPort}", NotificationMessageType.Info);
14-
ApiManager.SendMessageNewLine(string.Empty, NotificationMessageType.Info);
12+
ApiManager.SendNotificationMessageNewLine($"The public IP address is: {ApiManager.PublicIpAddress}", NotificationMessageType.Info);
13+
ApiManager.SendNotificationMessageNewLine($"The public port is: {ApiConfiguration.PublicPort}", NotificationMessageType.Info);
14+
ApiManager.SendNotificationMessageNewLine(string.Empty, NotificationMessageType.Info);
1515
}
1616

17-
ApiManager.SendMessageNewLine($"The private IP address is: {NetworkUtilities.GetIP4Adress()}", NotificationMessageType.Info);
18-
ApiManager.SendMessageNewLine($"The private port is: {ApiConfiguration.PrivatePort}", NotificationMessageType.Info);
17+
ApiManager.SendNotificationMessageNewLine($"The private IP address is: {NetworkUtilities.GetIP4Adress()}", NotificationMessageType.Info);
18+
ApiManager.SendNotificationMessageNewLine($"The private port is: {ApiConfiguration.PrivatePort}", NotificationMessageType.Info);
1919
}
2020
}

HyperbolicDownloaderApi/Commands/DownloadCommands.cs

Lines changed: 89 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@ public void GetFileFrom(string path)
2727
{
2828
if (string.IsNullOrWhiteSpace(path))
2929
{
30-
ApiManager.SendMessageNewLine("Path is empty!", NotificationMessageType.Error);
30+
ApiManager.SendNotificationMessageNewLine("Path is empty!", NotificationMessageType.Error);
3131
return;
3232
}
3333

3434
string fullPath = Path.GetFullPath(path);
3535

3636
if (!File.Exists(fullPath))
3737
{
38-
ApiManager.SendMessageNewLine("Invalid file path!", NotificationMessageType.Error);
38+
ApiManager.SendNotificationMessageNewLine("Invalid file path!", NotificationMessageType.Error);
3939
}
4040

4141
string json = File.ReadAllText(fullPath);
4242

4343
PublicHyperFileInfo? publicHyperFileInfo = JsonSerializer.Deserialize<PublicHyperFileInfo>(json);
4444
if (publicHyperFileInfo == null)
4545
{
46-
ApiManager.SendMessageNewLine("Parsing file failed!", NotificationMessageType.Error);
46+
ApiManager.SendNotificationMessageNewLine("Parsing file failed!", NotificationMessageType.Error);
4747
return;
4848
}
4949

@@ -55,7 +55,7 @@ public void GetFile(string hash)
5555
{
5656
if (string.IsNullOrEmpty(hash))
5757
{
58-
ApiManager.SendMessageNewLine("No hash value specified!", NotificationMessageType.Error);
58+
ApiManager.SendNotificationMessageNewLine("No hash value specified!", NotificationMessageType.Error);
5959
return;
6060
}
6161

@@ -71,7 +71,7 @@ public void GetFile(string hash)
7171
continue;
7272
}
7373

74-
ApiManager.SendMessage($"{host.IPAddress}:{host.Port} > ???", NotificationMessageType.Warning);
74+
ApiManager.SendNotificationMessage($"{host.IPAddress}:{host.Port} > ???", NotificationMessageType.Warning);
7575

7676
Console.CursorLeft = 0;
7777

@@ -82,22 +82,22 @@ public void GetFile(string hash)
8282
if (!sendTask.IsCompletedSuccessfully)
8383
{
8484
Console.CursorLeft = 0;
85-
ApiManager.SendMessageNewLine($"{host.IPAddress}:{host.Port} > Inactive", NotificationMessageType.Error);
85+
ApiManager.SendNotificationMessageNewLine($"{host.IPAddress}:{host.Port} > Inactive", NotificationMessageType.Error);
8686

8787
hostsManager.Remove(host);
8888
continue;
8989
}
9090
else if (!sendTask.Result)
9191
{
9292
host.LastActive = DateTime.Now;
93-
ApiManager.SendMessageNewLine($"{host.IPAddress}:{host.Port} > Does not have the requested file", NotificationMessageType.Error);
93+
ApiManager.SendNotificationMessageNewLine($"{host.IPAddress}:{host.Port} > Does not have the requested file", NotificationMessageType.Error);
9494
continue;
9595
}
9696

9797
host.LastActive = DateTime.Now;
9898

99-
ApiManager.SendMessageNewLine($"{host.IPAddress}:{host.Port} > Has the requested file", NotificationMessageType.Success);
100-
ApiManager.SendMessageNewLine("Requesting file...");
99+
ApiManager.SendNotificationMessageNewLine($"{host.IPAddress}:{host.Port} > Has the requested file", NotificationMessageType.Success);
100+
ApiManager.SendNotificationMessageNewLine("Requesting file...");
101101

102102
using TcpClient tcpClient = new TcpClient();
103103
tcpClient.Connect(ipAddress!, host.Port);
@@ -114,122 +114,123 @@ public void GetFile(string hash)
114114
int bytesRead;
115115
try
116116
{
117-
bytesRead = nwStream.Read(buffer, 0, tcpClient.ReceiveBufferSize);
117+
bytesRead = nwStream.Read(buffer, 0, 1000);
118118
}
119119
catch (IOException)
120120
{
121-
ApiManager.SendMessageNewLine(string.Empty);
122-
ApiManager.SendMessageNewLine("Lost connection to other host!", NotificationMessageType.Error);
121+
ApiManager.SendNotificationMessageNewLine(string.Empty);
122+
ApiManager.SendNotificationMessageNewLine("Lost connection to other host!", NotificationMessageType.Error);
123123
continue;
124124
}
125125

126126
string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);
127127

128128
string[] parts = dataReceived.Split('/');
129129

130-
if (parts.Length == 2) //If received data does not contain 2 parts -> error
130+
if (parts.Length != 2) //If received data does not contain 2 parts -> error
131131
{
132-
bool validFileSize = int.TryParse(parts[0], out int fileSize);
132+
ApiManager.SendNotificationMessageNewLine(dataReceived, NotificationMessageType.Error);
133+
continue;
134+
}
133135

134-
if (!validFileSize || fileSize <= 0)
135-
{
136-
ApiManager.SendMessageNewLine("Invalid file size!", NotificationMessageType.Error);
137-
continue;
138-
}
136+
bool validFileSize = int.TryParse(parts[0], out int fileSize);
139137

140-
string fileName = parts[1].ToFileName();
141-
string directoryPath = Path.Combine(ApiConfiguration.BasePath, "Downloads");
142-
string filePath = Path.Combine(directoryPath, fileName);
138+
if (!validFileSize || fileSize <= 0)
139+
{
140+
ApiManager.SendNotificationMessageNewLine("Invalid file size!", NotificationMessageType.Error);
141+
continue;
142+
}
143143

144-
ApiManager.SendMessageNewLine($"File name: {fileName}");
145-
ApiManager.SendMessageNewLine($"Starting download...");
144+
string fileName = parts[1].ToFileName();
145+
string directoryPath = Path.Combine(ApiConfiguration.BasePath, "Downloads");
146+
string filePath = Path.Combine(directoryPath, fileName);
146147

147-
int totalBytesRead = 0;
148+
ApiManager.SendNotificationMessageNewLine($"File name: {fileName}");
149+
ApiManager.SendNotificationMessageNewLine($"Starting download...");
148150

149-
if (!Directory.Exists(directoryPath))
150-
{
151-
Directory.CreateDirectory(directoryPath);
152-
}
151+
int totalBytesRead = 0;
153152

154-
using FileStream? fileStream = new FileStream(filePath, FileMode.Create);
153+
if (!Directory.Exists(directoryPath))
154+
{
155+
Directory.CreateDirectory(directoryPath);
156+
}
157+
158+
using FileStream? fileStream = new FileStream(filePath, FileMode.Create);
155159

156-
int bytesInOneSecond = 0;
157-
int unitsPerSecond = 0;
158-
string unit = "Kb";
160+
int bytesInOneSecond = 0;
161+
int unitsPerSecond = 0;
162+
string unit = "Kb";
159163

160-
Stopwatch stopWatch = new Stopwatch();
161-
stopWatch.Start();
162-
while (totalBytesRead < fileSize)
164+
Stopwatch stopWatch = new Stopwatch();
165+
stopWatch.Start();
166+
while (totalBytesRead < fileSize)
167+
{
168+
try
163169
{
164-
try
165-
{
166-
bytesRead = nwStream.Read(reciveBuffer, 0, reciveBuffer.Length);
167-
}
168-
catch (IOException)
169-
{
170-
ApiManager.SendMessageNewLine(string.Empty);
171-
ApiManager.SendMessageNewLine("Lost connection to other host!", NotificationMessageType.Error);
172-
break;
173-
}
170+
bytesRead = nwStream.Read(reciveBuffer, 0, reciveBuffer.Length);
171+
}
172+
catch (IOException ex)
173+
{
174+
Debug.WriteLine(ex);
175+
ApiManager.SendNotificationMessageNewLine(string.Empty);
176+
ApiManager.SendNotificationMessageNewLine("Lost connection to other host!", NotificationMessageType.Error);
177+
break;
178+
}
174179

175-
bytesRead = Math.Min(bytesRead, fileSize - totalBytesRead);
180+
bytesRead = Math.Min(bytesRead, fileSize - totalBytesRead);
176181

177-
fileStream.Write(reciveBuffer, 0, bytesRead);
178-
totalBytesRead += bytesRead;
182+
fileStream.Write(reciveBuffer, 0, bytesRead);
183+
totalBytesRead += bytesRead;
179184

180-
bytesInOneSecond += bytesRead;
185+
bytesInOneSecond += bytesRead;
181186

182-
if (stopWatch.ElapsedMilliseconds >= 1000)
187+
if (stopWatch.Elapsed.TotalSeconds >= 1)
188+
{
189+
unitsPerSecond = (int)(bytesInOneSecond * stopWatch.Elapsed.TotalSeconds);
190+
if (unitsPerSecond > 125000)
183191
{
184-
unitsPerSecond = (unitsPerSecond + bytesInOneSecond) / 2;
185-
if (unitsPerSecond > 125000)
186-
{
187-
unitsPerSecond /= 125000;
188-
unit = "Mb";
189-
}
190-
else
191-
{
192-
unitsPerSecond /= 125;
193-
unit = "Kb";
194-
}
195-
bytesInOneSecond = 0;
196-
stopWatch.Restart();
192+
unitsPerSecond /= 125000;
193+
unit = "Mb";
197194
}
198-
199-
ApiManager.SendMessage($"\rDownloading: {Math.Clamp(Math.Ceiling(100d / fileSize * totalBytesRead), 0, 100)}% {totalBytesRead / 1000}/{fileSize / 1000}KB [{unitsPerSecond}{unit}/s] ");
195+
else
196+
{
197+
unitsPerSecond /= 125;
198+
unit = "Kb";
199+
}
200+
bytesInOneSecond = 0;
201+
stopWatch.Restart();
200202
}
201203

202-
fileStream.Close();
204+
ApiManager.SendNotificationMessage($"\rDownloading: {Math.Clamp(Math.Ceiling(100d / fileSize * totalBytesRead), 0, 100)}% {totalBytesRead / 1000}/{fileSize / 1000}KB [{unitsPerSecond}{unit}/s] ");
205+
}
203206

204-
if (totalBytesRead < fileSize)
205-
{
206-
continue;
207-
}
207+
fileStream.Close();
208208

209-
ApiManager.SendMessageNewLine(string.Empty);
209+
if (totalBytesRead < fileSize)
210+
{
211+
continue;
212+
}
210213

211-
ApiManager.SendMessageNewLine("Validating file...");
212-
if (FileValidator.ValidateHash(filePath, hash))
213-
{
214-
_ = filesManager.TryAdd(filePath, out _, out _);
215-
}
216-
else
217-
{
218-
ApiManager.SendMessageNewLine("Warning: File hash does not match! File might me corrupted or manipulated!", NotificationMessageType.Warning);
219-
}
214+
ApiManager.SendNotificationMessageNewLine(string.Empty);
220215

221-
ApiManager.SendMessageNewLine($"File saved at: {Path.GetFullPath(filePath)}");
222-
ApiManager.SendMessageNewLine("Done", NotificationMessageType.Success);
223-
stopWatch.Stop();
224-
hostsManager.SaveHosts();
225-
return;
216+
ApiManager.SendNotificationMessageNewLine("Validating file...");
217+
if (FileValidator.ValidateHash(filePath, hash))
218+
{
219+
_ = filesManager.TryAdd(filePath, out _, out _);
226220
}
227221
else
228222
{
229-
ApiManager.SendMessageNewLine(dataReceived, NotificationMessageType.Error);
223+
ApiManager.SendNotificationMessageNewLine("Warning: File hash does not match! File might me corrupted or manipulated! Trying next host...", NotificationMessageType.Warning);
224+
continue;
230225
}
226+
227+
ApiManager.SendNotificationMessageNewLine($"File saved at: {Path.GetFullPath(filePath)}");
228+
ApiManager.SendNotificationMessageNewLine("Done", NotificationMessageType.Success);
229+
stopWatch.Stop();
230+
hostsManager.SaveHosts();
231+
return;
231232
}
232-
ApiManager.SendMessageNewLine("None of the available hosts have the requested file!", NotificationMessageType.Error);
233+
ApiManager.SendNotificationMessageNewLine("None of the available hosts have the requested file!", NotificationMessageType.Error);
233234
hostsManager.SaveHosts();
234235
}
235236
}

0 commit comments

Comments
 (0)