Skip to content

Commit

Permalink
Improve downloader
Browse files Browse the repository at this point in the history
- print http respone code when error
- make checksum case-insensitive
  • Loading branch information
halx99 committed Jun 27, 2024
1 parent 042c9b7 commit 5ffd6ce
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
20 changes: 13 additions & 7 deletions core/network/Downloader-curl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,12 @@ class DownloadTaskCURL : public IDownloadTask
return status;
}

void setErrorProc(int code, int codeInternal, const char* desc)
void setErrorDesc(int code, int codeInternal, std::string&& desc)
{
std::lock_guard<std::recursive_mutex> lock(_mutex);
_errCode = code;
_errCodeInternal = codeInternal;
_errDescription = desc;
_errDescription = std::move(desc);
}

size_t writeDataProc(unsigned char* buffer, size_t size, size_t count)
Expand Down Expand Up @@ -617,8 +617,14 @@ class DownloaderCURL::Impl : public std::enable_shared_from_this<DownloaderCURL:
auto coTask = static_cast<DownloadTaskCURL*>(task->_coTask.get());
if (CURLE_OK != errCode)
{
coTask->setErrorProc(DownloadTask::ERROR_IMPL_INTERNAL, errCode,
curl_easy_strerror(errCode));
std::string errorMsg = curl_easy_strerror(errCode);
if (errCode == CURLE_HTTP_RETURNED_ERROR) {
long responeCode = 0;
curl_easy_getinfo(curlHandle, CURLINFO_RESPONSE_CODE, &responeCode);
fmt::format_to(std::back_inserter(errorMsg), FMT_COMPILE(": {}"), responeCode);
}

coTask->setErrorDesc(DownloadTask::ERROR_IMPL_INTERNAL, errCode, std::move(errorMsg));
break;
}

Expand Down Expand Up @@ -689,7 +695,7 @@ class DownloaderCURL::Impl : public std::enable_shared_from_this<DownloaderCURL:

if (nullptr == curlHandle)
{
coTask->setErrorProc(DownloadTask::ERROR_IMPL_INTERNAL, 0, "Alloc curl handle failed.");
coTask->setErrorDesc(DownloadTask::ERROR_IMPL_INTERNAL, 0, "Alloc curl handle failed.");
_owner->_onDownloadFinished(*task);
continue;
}
Expand All @@ -701,7 +707,7 @@ class DownloaderCURL::Impl : public std::enable_shared_from_this<DownloaderCURL:
mcode = curl_multi_add_handle(curlmHandle, curlHandle);
if (CURLM_OK != mcode)
{
coTask->setErrorProc(DownloadTask::ERROR_IMPL_INTERNAL, mcode, curl_multi_strerror(mcode));
coTask->setErrorDesc(DownloadTask::ERROR_IMPL_INTERNAL, mcode, curl_multi_strerror(mcode));
_owner->_onDownloadFinished(*task);
continue;
}
Expand Down Expand Up @@ -903,7 +909,7 @@ void DownloaderCURL::_onDownloadFinished(DownloadTask& task, int checkState)
break;
}

if (coTask._fileName.empty() || DownloadTask::ERROR_NO_ERROR != coTask._errCode)
if (coTask._fileName.empty() || coTask._errCode != DownloadTask::ERROR_NO_ERROR)
{
if (coTask._errCodeInternal == CURLE_RANGE_ERROR)
{
Expand Down
15 changes: 10 additions & 5 deletions core/network/Downloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@
#include "network/Downloader.h"

#if EMSCRIPTEN
#include "network/Downloader-wasm.h"
#define DownloaderImpl DownloaderEmscripten
# include "network/Downloader-wasm.h"
# define DownloaderImpl DownloaderEmscripten
#else
#include "network/Downloader-curl.h"
#define DownloaderImpl DownloaderCURL
# include "network/Downloader-curl.h"
# define DownloaderImpl DownloaderCURL
#endif

#include <ctype.h>
#include <algorithm>

NS_AX_BEGIN

namespace network
Expand Down Expand Up @@ -63,7 +66,9 @@ DownloadTask::DownloadTask(std::string_view srcUrl,
this->checksum = checksum;
this->identifier = identifier;
this->background = background;
this->cacertPath = cacertPath;
this->cacertPath = cacertPath;
if (!this->checksum.empty())
std::transform(this->checksum.begin(), this->checksum.end(), this->checksum.begin(), ::tolower);
}

DownloadTask::~DownloadTask()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ USING_NS_AX;
static const char* sURLList[] = {
"https://www.cocos2d-x.org/attachments/802/cocos2dx_landscape.png", "https://cocos2d-x.org/images/logo.png",
"https://www.cocos2d-x.org/attachments/1503/no_exist.txt", // try to download no exist file
"https://ash-speed.hetzner.com/1GB.bin"
"https://github.com/axmolengine/axmol/releases/download/v2.1.3/axmol-2.1.3.zip"
};
const static int sListSize = (sizeof(sURLList) / sizeof(sURLList[0]));
static const char* sNameList[sListSize] = {
Expand Down Expand Up @@ -208,8 +208,7 @@ struct DownloaderTest : public TestCase
bar->setVisible(true);
bar->setEnabled(true);
auto path = FileUtils::getInstance()->getWritablePath() + "CppTests/DownloaderTest/" + sNameList[3];
auto task = this->downloader->createDownloadFileTask(sURLList[3], path, sNameList[3], "5fa2035a209e73f5727a72aafd332916", false);
task->progressInfo.totalBytesExpected = 89945032;
auto task = this->downloader->createDownloadFileTask(sURLList[3], path, sNameList[3], "1CF78E3F23A2B1A6806D8719A5771D34", false);
});
bottomRightView->setName(sNameList[3]);
bottomRightView->setAnchorPoint(Vec2(0, 1));
Expand Down

0 comments on commit 5ffd6ce

Please sign in to comment.