From 9f1cc15b155f39a9a5f14d2bfe8d0903beedcaa7 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Sun, 29 Sep 2024 01:15:57 +0200 Subject: [PATCH] fix bugs with new download --- src/Network/Resources.cpp | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/Network/Resources.cpp b/src/Network/Resources.cpp index 740dab4..c8651cb 100644 --- a/src/Network/Resources.cpp +++ b/src/Network/Resources.cpp @@ -220,6 +220,35 @@ SOCKET InitDSock() { return DSock; } +std::vector SingleNormalDownload(SOCKET MSock, uint64_t Size, const std::string& Name) { + DownloadSpeed = 0; + + uint64_t GRcv = 0; + + std::thread Au([&] { AsyncUpdate(GRcv, Size, Name); }); + + const std::vector MData = TCPRcvRaw(MSock, GRcv, Size); + + if (MData.empty()) { + KillSocket(MSock); + Terminate = true; + Au.join(); + return {}; + } + + // ensure that GRcv is good before joining the async update thread + GRcv = MData.size(); + if (GRcv != Size) { + error("Something went wrong during download; didn't get enough data. Expected " + std::to_string(Size) + " bytes, got " + std::to_string(GRcv) + " bytes instead"); + Terminate = true; + Au.join(); + return {}; + } + + Au.join(); + return MData; +} + std::vector MultiDownload(SOCKET MSock, SOCKET DSock, uint64_t Size, const std::string& Name) { DownloadSpeed = 0; @@ -381,7 +410,6 @@ void NewSyncResources(SOCKET Sock, const std::string& Mods, const std::vector DownloadedFile = MultiDownload(Sock, DSock, ModInfoIter->FileSize, Name); + std::vector DownloadedFile = SingleNormalDownload(Sock, ModInfoIter->FileSize, Name); if (Terminate) break; @@ -446,6 +474,7 @@ void NewSyncResources(SOCKET Sock, const std::string& Mods, const std::vector