LAN installs fail properly on error

This commit is contained in:
Huntereb 2019-12-13 11:04:42 -05:00
parent 643c65777b
commit b0d23e60da
6 changed files with 49 additions and 44 deletions

View File

@ -75,7 +75,7 @@ namespace tin::network
HTTPDownload(std::string url);
void BufferDataRange(void* buffer, size_t offset, size_t size, std::function<void (size_t sizeRead)> progressFunc);
void StreamDataRange(size_t offset, size_t size, std::function<size_t (u8* bytes, size_t size)> streamFunc);
int StreamDataRange(size_t offset, size_t size, std::function<size_t (u8* bytes, size_t size)> streamFunc);
};
size_t WaitReceiveNetworkData(int sockfd, void* buf, size_t len);

View File

@ -31,8 +31,11 @@ SOFTWARE.
#include "sdInstall.hpp"
#include "util/util.hpp"
bool stopThreads;
namespace tin::install::nsp
{
HTTPNSP::HTTPNSP(std::string url) :
m_download(url)
{
@ -63,7 +66,7 @@ namespace tin::install::nsp
return streamBufSize;
};
args->download->StreamDataRange(args->pfs0Offset, args->ncaSize, streamFunc);
if (args->download->StreamDataRange(args->pfs0Offset, args->ncaSize, streamFunc) == 1) stopThreads = true;
return 0;
}
@ -71,7 +74,7 @@ namespace tin::install::nsp
{
StreamFuncArgs* args = reinterpret_cast<StreamFuncArgs*>(in);
while (!args->bufferedPlaceholderWriter->IsPlaceholderComplete())
while (!args->bufferedPlaceholderWriter->IsPlaceholderComplete() && !stopThreads)
{
if (args->bufferedPlaceholderWriter->CanWriteSegmentToPlaceholder())
args->bufferedPlaceholderWriter->WriteSegmentToPlaceholder();
@ -97,6 +100,7 @@ namespace tin::install::nsp
thrd_t curlThread;
thrd_t writeThread;
stopThreads = false;
thrd_create(&curlThread, CurlStreamFunc, &args);
thrd_create(&writeThread, PlaceholderWriteFunc, &args);
@ -106,7 +110,7 @@ namespace tin::install::nsp
double speed = 0.0;
inst::ui::setInstBarPerc(0);
while (!bufferedPlaceholderWriter.IsBufferDataComplete())
while (!bufferedPlaceholderWriter.IsBufferDataComplete() && !stopThreads)
{
u64 newTime = armGetSystemTick();
@ -130,7 +134,7 @@ namespace tin::install::nsp
inst::ui::setInstInfoText("Installing " + ncaFileName + "...");
inst::ui::setInstBarPerc(0);
while (!bufferedPlaceholderWriter.IsPlaceholderComplete())
while (!bufferedPlaceholderWriter.IsPlaceholderComplete() && !stopThreads)
{
int installProgress = (int)(((double)bufferedPlaceholderWriter.GetSizeWrittenToPlaceholder() / (double)bufferedPlaceholderWriter.GetTotalDataSize()) * 100.0);
@ -140,6 +144,7 @@ namespace tin::install::nsp
thrd_join(curlThread, NULL);
thrd_join(writeThread, NULL);
if (stopThreads) THROW_FORMAT("An error occured during data transfer. Check your network connection.");
}
void HTTPNSP::BufferData(void* buf, off_t offset, size_t size)

View File

@ -28,6 +28,8 @@ SOFTWARE.
#include "sdInstall.hpp"
#include "util/util.hpp"
bool stopThreads;
namespace tin::install::xci
{
HTTPXCI::HTTPXCI(std::string url) :
@ -60,7 +62,7 @@ namespace tin::install::xci
return streamBufSize;
};
args->download->StreamDataRange(args->pfs0Offset, args->ncaSize, streamFunc);
if (args->download->StreamDataRange(args->pfs0Offset, args->ncaSize, streamFunc) == 1) stopThreads = true;
return 0;
}
@ -68,7 +70,7 @@ namespace tin::install::xci
{
StreamFuncArgs* args = reinterpret_cast<StreamFuncArgs*>(in);
while (!args->bufferedPlaceholderWriter->IsPlaceholderComplete())
while (!args->bufferedPlaceholderWriter->IsPlaceholderComplete() && !stopThreads)
{
if (args->bufferedPlaceholderWriter->CanWriteSegmentToPlaceholder())
args->bufferedPlaceholderWriter->WriteSegmentToPlaceholder();
@ -94,6 +96,7 @@ namespace tin::install::xci
thrd_t curlThread;
thrd_t writeThread;
stopThreads = false;
thrd_create(&curlThread, CurlStreamFunc, &args);
thrd_create(&writeThread, PlaceholderWriteFunc, &args);
@ -103,7 +106,7 @@ namespace tin::install::xci
double speed = 0.0;
inst::ui::setInstBarPerc(0);
while (!bufferedPlaceholderWriter.IsBufferDataComplete())
while (!bufferedPlaceholderWriter.IsBufferDataComplete() && !stopThreads)
{
u64 newTime = armGetSystemTick();
@ -135,7 +138,7 @@ namespace tin::install::xci
inst::ui::setInstInfoText("Installing " + ncaFileName + "...");
inst::ui::setInstBarPerc(0);
while (!bufferedPlaceholderWriter.IsPlaceholderComplete())
while (!bufferedPlaceholderWriter.IsPlaceholderComplete() && !stopThreads)
{
int installProgress = (int)(((double)bufferedPlaceholderWriter.GetSizeWrittenToPlaceholder() / (double)bufferedPlaceholderWriter.GetTotalDataSize()) * 100.0);
#ifdef NXLINK_DEBUG
@ -148,6 +151,7 @@ namespace tin::install::xci
thrd_join(curlThread, NULL);
thrd_join(writeThread, NULL);
if (stopThreads) THROW_FORMAT("An error occured during data transfer. Check your network connection.");
}
void HTTPXCI::BufferData(void* buf, off_t offset, size_t size)

View File

@ -14,10 +14,11 @@
#include "util/util.hpp"
#include "util/usb_comms_awoo.h"
bool stopThreads;
std::string errorMessage;
namespace tin::install::nsp
{
bool stopThreads;
std::string errorMessage;
USBNSP::USBNSP(std::string nspName) :
m_nspName(nspName)
@ -44,7 +45,7 @@ namespace tin::install::nsp
try
{
while (sizeRemaining && !tin::install::nsp::stopThreads)
while (sizeRemaining && !stopThreads)
{
tmpSizeRead = awoo_usbCommsRead(buf, std::min(sizeRemaining, (u64)0x800000));
if (tmpSizeRead == 0) THROW_FORMAT("USB transfer timed out or failed");
@ -61,8 +62,8 @@ namespace tin::install::nsp
}
catch (std::exception& e)
{
tin::install::nsp::stopThreads = true;
tin::install::nsp::errorMessage = e.what();
stopThreads = true;
errorMessage = e.what();
}
free(buf);
@ -84,7 +85,7 @@ namespace tin::install::nsp
try
{
while (sizeRemaining && !tin::install::nsp::stopThreads)
while (sizeRemaining && !stopThreads)
{
if (!curRequestLeft) {
reqSize = std::min(sizeRemaining, (u64)0x800000);
@ -109,8 +110,8 @@ namespace tin::install::nsp
}
catch (std::exception& e)
{
tin::install::nsp::stopThreads = true;
tin::install::nsp::errorMessage = e.what();
stopThreads = true;
errorMessage = e.what();
}
free(buf);
@ -122,7 +123,7 @@ namespace tin::install::nsp
{
USBFuncArgs* args = reinterpret_cast<USBFuncArgs*>(in);
while (!args->bufferedPlaceholderWriter->IsPlaceholderComplete() && !tin::install::nsp::stopThreads)
while (!args->bufferedPlaceholderWriter->IsPlaceholderComplete() && !stopThreads)
{
if (args->bufferedPlaceholderWriter->CanWriteSegmentToPlaceholder())
args->bufferedPlaceholderWriter->WriteSegmentToPlaceholder();
@ -148,7 +149,7 @@ namespace tin::install::nsp
thrd_t usbThread;
thrd_t writeThread;
tin::install::nsp::stopThreads = false;
stopThreads = false;
if (m_nspName.substr(m_nspName.size() - 1, 1) == "z") thrd_create(&usbThread, USBThreadFuncNcz, &args);
else thrd_create(&usbThread, USBThreadFunc, &args);
thrd_create(&writeThread, USBPlaceholderWriteFunc, &args);
@ -159,7 +160,7 @@ namespace tin::install::nsp
double speed = 0.0;
inst::ui::setInstBarPerc(0);
while (!bufferedPlaceholderWriter.IsBufferDataComplete() && !tin::install::nsp::stopThreads)
while (!bufferedPlaceholderWriter.IsBufferDataComplete() && !stopThreads)
{
u64 newTime = armGetSystemTick();
@ -191,7 +192,7 @@ namespace tin::install::nsp
inst::ui::setInstInfoText("Installing " + ncaFileName + "...");
inst::ui::setInstBarPerc(0);
while (!bufferedPlaceholderWriter.IsPlaceholderComplete() && !tin::install::nsp::stopThreads)
while (!bufferedPlaceholderWriter.IsPlaceholderComplete() && !stopThreads)
{
int installProgress = (int)(((double)bufferedPlaceholderWriter.GetSizeWrittenToPlaceholder() / (double)bufferedPlaceholderWriter.GetTotalDataSize()) * 100.0);
#ifdef NXLINK_DEBUG
@ -204,7 +205,7 @@ namespace tin::install::nsp
thrd_join(usbThread, NULL);
thrd_join(writeThread, NULL);
if (tin::install::nsp::stopThreads) throw std::runtime_error(tin::install::nsp::errorMessage.c_str());
if (stopThreads) throw std::runtime_error(errorMessage.c_str());
}
void USBNSP::BufferData(void* buf, off_t offset, size_t size)

View File

@ -14,10 +14,11 @@
#include "util/util.hpp"
#include "util/usb_comms_awoo.h"
bool stopThreads;
std::string errorMessage;
namespace tin::install::xci
{
bool stopThreads;
std::string errorMessage;
USBXCI::USBXCI(std::string xciName) :
m_xciName(xciName)
@ -44,7 +45,7 @@ namespace tin::install::xci
try
{
while (sizeRemaining && !tin::install::xci::stopThreads)
while (sizeRemaining && !stopThreads)
{
tmpSizeRead = awoo_usbCommsRead(buf, std::min(sizeRemaining, (u64)0x800000));
if (tmpSizeRead == 0) THROW_FORMAT("USB transfer timed out or failed");
@ -61,8 +62,8 @@ namespace tin::install::xci
}
catch (std::exception& e)
{
tin::install::xci::stopThreads = true;
tin::install::xci::errorMessage = e.what();
stopThreads = true;
errorMessage = e.what();
}
free(buf);
@ -84,7 +85,7 @@ namespace tin::install::xci
try
{
while (sizeRemaining && !tin::install::xci::stopThreads)
while (sizeRemaining && !stopThreads)
{
if (!curRequestLeft) {
reqSize = std::min(sizeRemaining, (u64)0x800000);
@ -109,8 +110,8 @@ namespace tin::install::xci
}
catch (std::exception& e)
{
tin::install::xci::stopThreads = true;
tin::install::xci::errorMessage = e.what();
stopThreads = true;
errorMessage = e.what();
}
free(buf);
@ -122,7 +123,7 @@ namespace tin::install::xci
{
USBFuncArgs* args = reinterpret_cast<USBFuncArgs*>(in);
while (!args->bufferedPlaceholderWriter->IsPlaceholderComplete() && !tin::install::xci::stopThreads)
while (!args->bufferedPlaceholderWriter->IsPlaceholderComplete() && !stopThreads)
{
if (args->bufferedPlaceholderWriter->CanWriteSegmentToPlaceholder())
args->bufferedPlaceholderWriter->WriteSegmentToPlaceholder();
@ -148,7 +149,7 @@ namespace tin::install::xci
thrd_t usbThread;
thrd_t writeThread;
tin::install::xci::stopThreads = false;
stopThreads = false;
if (m_xciName.substr(m_xciName.size() - 1, 1) == "z") thrd_create(&usbThread, USBThreadFuncNcz, &args);
else thrd_create(&usbThread, USBThreadFunc, &args);
thrd_create(&writeThread, USBPlaceholderWriteFunc, &args);
@ -159,7 +160,7 @@ namespace tin::install::xci
double speed = 0.0;
inst::ui::setInstBarPerc(0);
while (!bufferedPlaceholderWriter.IsBufferDataComplete() && !tin::install::xci::stopThreads)
while (!bufferedPlaceholderWriter.IsBufferDataComplete() && !stopThreads)
{
u64 newTime = armGetSystemTick();
@ -191,7 +192,7 @@ namespace tin::install::xci
inst::ui::setInstInfoText("Installing " + ncaFileName + "...");
inst::ui::setInstBarPerc(0);
while (!bufferedPlaceholderWriter.IsPlaceholderComplete() && !tin::install::xci::stopThreads)
while (!bufferedPlaceholderWriter.IsPlaceholderComplete() && !stopThreads)
{
int installProgress = (int)(((double)bufferedPlaceholderWriter.GetSizeWrittenToPlaceholder() / (double)bufferedPlaceholderWriter.GetTotalDataSize()) * 100.0);
#ifdef NXLINK_DEBUG
@ -204,7 +205,7 @@ namespace tin::install::xci
thrd_join(usbThread, NULL);
thrd_join(writeThread, NULL);
if (tin::install::xci::stopThreads) throw std::runtime_error(tin::install::xci::errorMessage.c_str());
if (stopThreads) throw std::runtime_error(errorMessage.c_str());
}
void USBXCI::BufferData(void* buf, off_t offset, size_t size)

View File

@ -190,7 +190,7 @@ namespace tin::network
this->StreamDataRange(offset, size, streamFunc);
}
void HTTPDownload::StreamDataRange(size_t offset, size_t size, std::function<size_t (u8* bytes, size_t size)> streamFunc)
int HTTPDownload::StreamDataRange(size_t offset, size_t size, std::function<size_t (u8* bytes, size_t size)> streamFunc)
{
if (!m_rangesSupported)
{
@ -219,19 +219,13 @@ namespace tin::network
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &tin::network::HTTPDownload::ParseHTMLData);
rc = curl_easy_perform(curl);
if (rc != CURLE_OK)
{
THROW_FORMAT("Failed to perform range request: %s\n", curl_easy_strerror(rc));
}
u64 httpCode = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
curl_easy_cleanup(curl);
if (httpCode != 206)
{
THROW_FORMAT("Failed to request range! Response code is %lu\n", httpCode);
}
if (httpCode != 206 || rc != CURLE_OK) return 1;
return 0;
}
// End HTTPDownload