Fix important bug

- Fixed a bug that caused the content of the file to be sent as the filename
This commit is contained in:
Stone_Red
2022-02-28 20:28:05 +01:00
parent 9675361695
commit a90af811c1
2 changed files with 85 additions and 80 deletions
@@ -114,7 +114,7 @@ public class DownloadCommands
int bytesRead;
try
{
bytesRead = nwStream.Read(buffer, 0, tcpClient.ReceiveBufferSize);
bytesRead = nwStream.Read(buffer, 0, 1000);
}
catch (IOException)
{
@@ -127,8 +127,12 @@ public class DownloadCommands
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);
if (!validFileSize || fileSize <= 0)
@@ -224,11 +228,6 @@ public class DownloadCommands
hostsManager.SaveHosts();
return;
}
else
{
ApiManager.SendMessageNewLine(dataReceived, NotificationMessageType.Error);
}
}
ApiManager.SendMessageNewLine("None of the available hosts have the requested file!", NotificationMessageType.Error);
hostsManager.SaveHosts();
}
@@ -167,6 +167,9 @@ namespace HyperbolicDownloaderApi.Networking
FileInfo fileInfo = new FileInfo(hyperFileInfo.FilePath);
bytesToSend = Encoding.ASCII.GetBytes($"{fileInfo.Length}/{Path.GetFileName(hyperFileInfo.FilePath)}");
Array.Resize(ref bytesToSend, 1000);
await nwStream.WriteAsync(bytesToSend);
foreach (byte[]? chunk in FileCompressor.ReadChunks(hyperFileInfo.FilePath, 64000))
@@ -182,12 +185,15 @@ namespace HyperbolicDownloaderApi.Networking
bytesToSend = Encoding.ASCII.GetBytes("File not found!");
await nwStream.WriteAsync(bytesToSend);
}
client.Close();
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
finally
{
client.Close();
}
}
public void StopListening()