Merge pull request #1 from Stone-Red-Code/develop

Develop
This commit is contained in:
Stone_Red
2022-03-07 12:29:31 +01:00
committed by GitHub
6 changed files with 91 additions and 84 deletions
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
@@ -114,7 +114,7 @@ public class DownloadCommands
int bytesRead; int bytesRead;
try try
{ {
bytesRead = nwStream.Read(buffer, 0, tcpClient.ReceiveBufferSize); bytesRead = nwStream.Read(buffer, 0, 1000);
} }
catch (IOException) catch (IOException)
{ {
@@ -127,8 +127,12 @@ public class DownloadCommands
string[] parts = dataReceived.Split('/'); string[] parts = dataReceived.Split('/');
if (parts.Length == 2) //If received data does not contain 2 parts -> error if (parts.Length != 2) //If received data does not contain 2 parts -> error
{ {
ApiManager.SendMessageNewLine(dataReceived, NotificationMessageType.Error);
continue;
}
bool validFileSize = int.TryParse(parts[0], out int fileSize); bool validFileSize = int.TryParse(parts[0], out int fileSize);
if (!validFileSize || fileSize <= 0) if (!validFileSize || fileSize <= 0)
@@ -165,8 +169,9 @@ public class DownloadCommands
{ {
bytesRead = nwStream.Read(reciveBuffer, 0, reciveBuffer.Length); bytesRead = nwStream.Read(reciveBuffer, 0, reciveBuffer.Length);
} }
catch (IOException) catch (IOException ex)
{ {
Debug.WriteLine(ex);
ApiManager.SendMessageNewLine(string.Empty); ApiManager.SendMessageNewLine(string.Empty);
ApiManager.SendMessageNewLine("Lost connection to other host!", NotificationMessageType.Error); ApiManager.SendMessageNewLine("Lost connection to other host!", NotificationMessageType.Error);
break; break;
@@ -179,9 +184,9 @@ public class DownloadCommands
bytesInOneSecond += bytesRead; bytesInOneSecond += bytesRead;
if (stopWatch.ElapsedMilliseconds >= 1000) if (stopWatch.Elapsed.TotalSeconds >= 1)
{ {
unitsPerSecond = (unitsPerSecond + bytesInOneSecond) / 2; unitsPerSecond = (int)(bytesInOneSecond * stopWatch.Elapsed.TotalSeconds);
if (unitsPerSecond > 125000) if (unitsPerSecond > 125000)
{ {
unitsPerSecond /= 125000; unitsPerSecond /= 125000;
@@ -224,11 +229,6 @@ public class DownloadCommands
hostsManager.SaveHosts(); hostsManager.SaveHosts();
return; return;
} }
else
{
ApiManager.SendMessageNewLine(dataReceived, NotificationMessageType.Error);
}
}
ApiManager.SendMessageNewLine("None of the available hosts have the requested file!", NotificationMessageType.Error); ApiManager.SendMessageNewLine("None of the available hosts have the requested file!", NotificationMessageType.Error);
hostsManager.SaveHosts(); hostsManager.SaveHosts();
} }
@@ -28,7 +28,7 @@ public class FileCommands
} }
else else
{ {
ApiManager.SendMessageNewLine(message, NotificationMessageType.Error); ApiManager.SendMessageNewLine(message!, NotificationMessageType.Error);
} }
} }
@@ -108,9 +108,10 @@ public class HostCommands
string ipAddressInput = parts[0]; string ipAddressInput = parts[0];
string portInput = parts[1]; string portInput = parts[1];
IPAddress? ipAddress = null; IPAddress? ipAddress;
_ = int.TryParse(portInput, out int port); _ = int.TryParse(portInput, out int port);
if (port < 1000 || port >= 6000) if (port < 1000 || port >= 6000)
{ {
ApiManager.SendMessageNewLine("Invalid port number!", NotificationMessageType.Error); ApiManager.SendMessageNewLine("Invalid port number!", NotificationMessageType.Error);
@@ -58,7 +58,7 @@ public class HostsManager
try try
{ {
tcpClient.ConnectAsync(host.IPAddress, host.Port).Wait(1000); tcpClient.ConnectAsync(host.IPAddress, host.Port).Wait(500);
Console.CursorLeft = 0; Console.CursorLeft = 0;
if (tcpClient.Connected) if (tcpClient.Connected)
{ {
@@ -167,6 +167,9 @@ namespace HyperbolicDownloaderApi.Networking
FileInfo fileInfo = new FileInfo(hyperFileInfo.FilePath); FileInfo fileInfo = new FileInfo(hyperFileInfo.FilePath);
bytesToSend = Encoding.ASCII.GetBytes($"{fileInfo.Length}/{Path.GetFileName(hyperFileInfo.FilePath)}"); bytesToSend = Encoding.ASCII.GetBytes($"{fileInfo.Length}/{Path.GetFileName(hyperFileInfo.FilePath)}");
Array.Resize(ref bytesToSend, 1000);
await nwStream.WriteAsync(bytesToSend); await nwStream.WriteAsync(bytesToSend);
foreach (byte[]? chunk in FileCompressor.ReadChunks(hyperFileInfo.FilePath, 64000)) foreach (byte[]? chunk in FileCompressor.ReadChunks(hyperFileInfo.FilePath, 64000))
@@ -182,12 +185,15 @@ namespace HyperbolicDownloaderApi.Networking
bytesToSend = Encoding.ASCII.GetBytes("File not found!"); bytesToSend = Encoding.ASCII.GetBytes("File not found!");
await nwStream.WriteAsync(bytesToSend); await nwStream.WriteAsync(bytesToSend);
} }
client.Close();
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.WriteLine(ex); Debug.WriteLine(ex);
} }
finally
{
client.Close();
}
} }
public void StopListening() public void StopListening()