From b6b6a15284111d75a0384c6a2295e05724785320 Mon Sep 17 00:00:00 2001 From: halx99 Date: Sat, 22 Jun 2024 08:29:21 +0800 Subject: [PATCH 1/4] Improve curl downloader (#1999) --- core/network/Downloader-curl.cpp | 320 ++++-------------- .../DownloaderTest/DownloaderTest.cpp | 22 +- 2 files changed, 93 insertions(+), 249 deletions(-) diff --git a/core/network/Downloader-curl.cpp b/core/network/Downloader-curl.cpp index ac77cab0a0eb..c4e80852965e 100644 --- a/core/network/Downloader-curl.cpp +++ b/core/network/Downloader-curl.cpp @@ -25,22 +25,23 @@ ****************************************************************************/ #if !defined(__EMSCRIPTEN__) -#include "network/Downloader-curl.h" - -#include -#include - -#include -#include -#include "base/Utils.h" -#include "base/UTF8.h" -#include "base/Director.h" -#include "base/Scheduler.h" -#include "platform/FileUtils.h" -#include "network/Downloader.h" -#include "platform/FileStream.h" -#include "openssl/md5.h" -#include "yasio/xxsocket.hpp" +# include "network/Downloader-curl.h" + +# include +# include + +# include +# include +# include "base/Utils.h" +# include "base/UTF8.h" +# include "base/Director.h" +# include "base/Scheduler.h" +# include "platform/FileUtils.h" +# include "network/Downloader.h" +# include "platform/FileStream.h" +# include "openssl/md5.h" +# include "yasio/xxsocket.hpp" +# include "yasio/thread_name.hpp" // **NOTE** // In the file: @@ -51,7 +52,7 @@ // https://curl.se/libcurl/c/curl_easy_getinfo.html // https://curl.se/libcurl/c/curl_easy_setopt.html -#define AX_CURL_POLL_TIMEOUT_MS 50 // wait until DNS query done +# define AX_CURL_POLL_TIMEOUT_MS 1000 // wait until DNS query done enum { @@ -169,19 +170,21 @@ class DownloadTaskCURL : public IDownloadTask _errDescription.append(_tempFileName); break; } + _totalBytesReceived = _transferOffset = std::max(_fs->size(), 0); // init md5 state - _checksumFileName = _tempFileName + ".chksum"; + _checksumFileName = _tempFileName + ".digest"; _fsMd5 = FileUtils::getInstance()->openFileStream(_checksumFileName, IFileStream::Mode::OVERLAPPED); - if(!_fsMd5) { + if (!_fsMd5) + { _errCode = DownloadTask::ERROR_OPEN_FILE_FAILED; _errCodeInternal = 0; _errDescription = "Can't open checksum file:"; _errDescription.append(_checksumFileName); break; } - + _fsMd5->seek(0, SEEK_END); if (_fsMd5->tell() != sizeof(_md5State)) { @@ -207,7 +210,8 @@ class DownloadTaskCURL : public IDownloadTask _cancelled = true; if (this->_sockfd != -1) { - if(::shutdown(this->_sockfd, SD_BOTH) == -1) // may cause curl CURLE_SEND_ERROR(55) or CURLE_RECV_ERROR(56) + // may cause curl CURLE_SEND_ERROR(55) or CURLE_RECV_ERROR(56) + if (::shutdown(this->_sockfd, SD_BOTH) == -1) ::closesocket(this->_sockfd); this->_sockfd = -1; } @@ -229,7 +233,7 @@ class DownloadTaskCURL : public IDownloadTask /* retval: 0. don't check, 1. check succeed, 2. check failed */ - int checkFileMd5(std::string_view requiredsum, std::string* outsum = nullptr) + int verifyFileIntegrity(std::string_view requiredsum, std::string* outsum = nullptr) { int status = 0; if (!requiredsum.empty()) @@ -246,12 +250,6 @@ class DownloadTaskCURL : public IDownloadTask return status; } - void initProc() - { - std::lock_guard lock(_mutex); - _initInternal(); - } - void setErrorProc(int code, int codeInternal, const char* desc) { std::lock_guard lock(_mutex); @@ -269,7 +267,7 @@ class DownloadTaskCURL : public IDownloadTask if (_fs) { - ret = _fs->write(buffer, static_cast(bytes_transferred)); // fwrite(buffer, size, count, _fp); + ret = _fs->write(buffer, static_cast(bytes_transferred)); } else { @@ -308,7 +306,6 @@ class DownloadTaskCURL : public IDownloadTask // header info bool _acceptRanges; - bool _headerAchieved; int64_t _totalBytesExpected; double _speed; @@ -316,11 +313,10 @@ class DownloadTaskCURL : public IDownloadTask curl_socket_t _sockfd = -1; // store the sockfd to support cancel download manually bool _cancelled = false; - std::string _header; // temp buffer for receive header string, only used in thread proc - curl_slist* _requestHeaders; // progress + int64_t _transferOffset; int64_t _bytesReceived; int64_t _totalBytesReceived; @@ -342,17 +338,13 @@ class DownloadTaskCURL : public IDownloadTask void _initInternal() { - _acceptRanges = (false); - _headerAchieved = (false); _bytesReceived = (0); _totalBytesReceived = (0); - _totalBytesExpected = (0); + _totalBytesExpected = (-1); // some server may not replay data size, so set it to -1 _speed = 0; _curl = nullptr; _errCode = (DownloadTask::ERROR_NO_ERROR); _errCodeInternal = (CURLE_OK); - _header.resize(0); - _header.reserve(384); // pre alloc header string buffer } }; int DownloadTaskCURL::_sSerialId; @@ -376,7 +368,7 @@ class DownloaderCURL::Impl : public std::enable_shared_from_this task, DownloadTaskCURL* coTask) { - int status = coTask->checkFileMd5(task->checksum); + int status = coTask->verifyFileIntegrity(task->checksum); if (status & kCheckSumStateSucceed || DownloadTask::ERROR_NO_ERROR != coTask->_errCode) { @@ -451,41 +443,34 @@ class DownloaderCURL::Impl : public std::enable_shared_from_thiswriteDataProc((unsigned char*)buffer, size, count); } - static int _progressCallbackProc(void* ptr, - double totalToDownload, - double nowDownloaded, - double totalToUpLoad, - double nowUpLoaded) - { - auto task = (DownloadTask*)ptr; - if (!task || !task->background) - return 0; - auto& coTask = task->_coTask; - if (coTask) + static int _progressCallbackProc(DownloadTask* task, + curl_off_t dltotal, + curl_off_t dlnow, + curl_off_t /*ultotal*/, + curl_off_t /*ulnow*/) + { + if (!task) + return -1; + auto coTask = static_cast(task->_coTask.get()); + if (!coTask) + return -1; + if (coTask->_cancelled) + return 1; + if (coTask->_totalBytesExpected < 0 && dltotal > 0) + coTask->_totalBytesExpected = dltotal + coTask->_transferOffset; + if (dlnow > 0 && task->background) { - auto& downloaderImpl = static_cast(coTask.get())->owner; + auto& downloaderImpl = coTask->owner; downloaderImpl._updateTaskProgressInfo(*task); downloaderImpl.onTaskProgress(*task, downloaderImpl._transferDataToBuffer); } - return 0; } @@ -497,7 +482,7 @@ class DownloaderCURL::Impl : public std::enable_shared_from_this& task, bool forContent = false) + CURLcode _initCurlHandleProc(CURL* handle, std::shared_ptr& task) { DownloadTaskCURL* coTask = static_cast(task->_coTask.get()); @@ -508,55 +493,31 @@ class DownloaderCURL::Impl : public std::enable_shared_from_thisbackground) - { - curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 0L); - curl_easy_setopt(handle, CURLOPT_PROGRESSDATA, task.get()); - curl_easy_setopt(handle, CURLOPT_PROGRESSFUNCTION, _progressCallbackProc); - } - else - { - curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 1L); - } curl_easy_setopt(handle, CURLOPT_FAILONERROR, 1L); curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1L); curl_easy_setopt(handle, CURLOPT_OPENSOCKETFUNCTION, _openSocketCallback); curl_easy_setopt(handle, CURLOPT_OPENSOCKETDATA, coTask); + curl_easy_setopt(handle, CURLOPT_HEADER, 0L); - if (forContent) - { - /** if server acceptRanges and local has part of file, we continue to download **/ - if (coTask->_acceptRanges && coTask->_totalBytesReceived > 0) - { - char buf[128]; - snprintf(buf, sizeof(buf), "%" PRId64 "-", coTask->_totalBytesReceived); - curl_easy_setopt(handle, CURLOPT_RANGE, buf); - curl_easy_setopt(handle, CURLOPT_RESUME_FROM_LARGE, (curl_off_t)coTask->_totalBytesReceived); - } - } - else + /** if server acceptRanges and local has part of file, we continue to download **/ + if (coTask->_totalBytesReceived > 0) { - // get header options - curl_easy_setopt(handle, CURLOPT_HEADER, 1L); - curl_easy_setopt(handle, CURLOPT_NOBODY, 1L); + char buf[128]; + snprintf(buf, sizeof(buf), "%" PRId64 "-", coTask->_totalBytesReceived); + curl_easy_setopt(handle, CURLOPT_RANGE, buf); + curl_easy_setopt(handle, CURLOPT_RESUME_FROM_LARGE, (curl_off_t)coTask->_totalBytesReceived); } - // if (!sProxy.empty()) - // { - // curl_easy_setopt(curl, CURLOPT_PROXY, sProxy.c_str()); - // } if (hints.timeoutInSeconds) { curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, hints.timeoutInSeconds); @@ -564,13 +525,14 @@ class DownloaderCURL::Impl : public std::enable_shared_from_thiscacertPath.empty()) + + if (task->cacertPath.empty()) { curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0L); curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0L); } - else { + else + { curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 1L); curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 2L); curl_easy_setopt(handle, CURLOPT_CAINFO, task->cacertPath.c_str()); @@ -584,68 +546,10 @@ class DownloaderCURL::Impl : public std::enable_shared_from_thisrequestURL.c_str() - , httpResponseCode); - coTask.setErrorProc(DownloadTask::ERROR_IMPL_INTERNAL, CURLE_OK, buf); - }*/ - - // curl_easy_getinfo(handle, CURLINFO_EFFECTIVE_URL, &effectiveUrl); - // curl_easy_getinfo(handle, CURLINFO_CONTENT_TYPE, &contentType); - double contentLen = 0; - rc = curl_easy_getinfo(handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &contentLen); - if (CURLE_OK != rc) - { - break; - } - - // std::transform(coTask._header.begin(), coTask._header.end(), coTask._header.begin(), ::toupper); - bool acceptRanges = true; // (string::npos != coTask._header.find("ACCEPT-RANGES")) ? true : false; - - // get current file size - int64_t fileSize = 0; - if (acceptRanges && coTask->_tempFileName.length()) - { - fileSize = FileUtils::getInstance()->getFileSize(coTask->_tempFileName); - } - - // set header info to coTask - std::lock_guard lock(coTask->_mutex); - coTask->_totalBytesExpected = static_cast(contentLen); - coTask->_acceptRanges = acceptRanges; - if (acceptRanges && fileSize > 0) - { - coTask->_totalBytesReceived = fileSize; - } - coTask->_headerAchieved = true; - } while (0); - - if (CURLE_OK != rc) - { - coTask->setErrorProc(DownloadTask::ERROR_IMPL_INTERNAL, rc, curl_easy_strerror(rc)); - } - return coTask->_headerAchieved; - } - void _threadProc() { + yasio::set_thread_name("axmol-dl"); + AXLOGD("++++DownloaderCURL::Impl::_threadProc begin {}", fmt::ptr(this)); // the holder prevent DownloaderCURL::Impl class instance be destruct in main thread auto holder = this->shared_from_this(); @@ -673,47 +577,8 @@ class DownloaderCURL::Impl : public std::enable_shared_from_this_headerAchieved) - { - break; - } - - // the task is get header task - // first, we get info from response - if (!_getHeaderInfoProc(curlHandle, coTask)) - { - // the error info has been set in _getHeaderInfoProc - break; - } - // after get header info success // wrapper.second->_totalBytesReceived inited by local file size // if the local file size equal with the content size from header, the file has @@ -782,31 +633,11 @@ class DownloaderCURL::Impl : public std::enable_shared_from_thissetErrorProc(DownloadTask::ERROR_IMPL_INTERNAL, error, - curl_easy_strerror(error)); - break; - } - mcode = curl_multi_add_handle(curlmHandle, curlHandle); - if (CURLM_OK != mcode) - { - coTask->setErrorProc(DownloadTask::ERROR_IMPL_INTERNAL, mcode, - curl_multi_strerror(mcode)); - break; - } - reinited = true; } while (0); - if (reinited) - { - continue; - } curl_easy_cleanup(curlHandle); - AXLOGD(" _threadProc task clean cur handle :{} with errCode:{}", fmt::ptr(curlHandle), static_cast(errCode)); + AXLOGD(" _threadProc task clean cur handle :{} with errCode:{}", fmt::ptr(curlHandle), + static_cast(errCode)); // remove from coTaskMap coTaskMap.erase(curlHandle); @@ -853,8 +684,6 @@ class DownloaderCURL::Impl : public std::enable_shared_from_this(task->_coTask.get()); - coTask->initProc(); - // create curl handle from task and add into curl multi handle CURL* curlHandle = curl_easy_init(); @@ -957,8 +786,7 @@ void DownloaderCURL::startTask(std::shared_ptr& task) } else { - AXLOGE("DownloaderCURL createTask fail, error: {}, detail: {}", coTask->_errCode, - coTask->_errDescription); + AXLOGE("DownloaderCURL createTask fail, error: {}, detail: {}", coTask->_errCode, coTask->_errDescription); task.reset(); } } @@ -1101,7 +929,7 @@ void DownloaderCURL::_onDownloadFinished(DownloadTask& task, int checkState) // Try check sum with md5 digest std::string realMd5; - if (coTask.checkFileMd5(task.checksum, &realMd5) & kCheckSumStateFailed) + if (coTask.verifyFileIntegrity(task.checksum, &realMd5) & kCheckSumStateFailed) { coTask._errCode = DownloadTask::ERROR_CHECK_SUM_FAILED; coTask._errCodeInternal = 0; diff --git a/tests/cpp-tests/Source/NetworkTest/DownloaderTest/DownloaderTest.cpp b/tests/cpp-tests/Source/NetworkTest/DownloaderTest/DownloaderTest.cpp index 95339e2a5dee..d839dba6394f 100644 --- a/tests/cpp-tests/Source/NetworkTest/DownloaderTest/DownloaderTest.cpp +++ b/tests/cpp-tests/Source/NetworkTest/DownloaderTest/DownloaderTest.cpp @@ -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://www.openssl.org/source/openssl-3.0.10.tar.gz" + "https://ash-speed.hetzner.com/1GB.bin" }; const static int sListSize = (sizeof(sURLList) / sizeof(sURLList[0])); static const char* sNameList[sListSize] = { @@ -114,6 +114,19 @@ struct DownloaderTest : public TestCase return bg; } + static void sbtoa(double speedInBytes, char* buf, size_t buf_len) + { + double speedInBits = speedInBytes; + if (speedInBits < 1024) + snprintf(buf, buf_len, "%gB", speedInBits); + else if (speedInBits < 1024 * 1024) + snprintf(buf, buf_len, "%.1lfKB", speedInBits / 1024); + else if (speedInBits < 1024 * 1024 * 1024) + snprintf(buf, buf_len, "%.1lfMB", speedInBits / 1024 / 1024); + else + snprintf(buf, buf_len, "%.1lfGB", speedInBits / 1024 / 1024 / 1024); + } + virtual void onEnter() override { TestCase::onEnter(); @@ -195,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], - "5d689e1534373e0b0540b5c087b5d99a", false); + auto task = this->downloader->createDownloadFileTask(sURLList[3], path, sNameList[3], "5fa2035a209e73f5727a72aafd332916", false); task->progressInfo.totalBytesExpected = 89945032; }); bottomRightView->setName(sNameList[3]); @@ -212,8 +224,12 @@ struct DownloaderTest : public TestCase bar->setPercent(percent); char buf[128]; sprintf(buf, "%.1f%%[total %d KB]", percent, int(task.progressInfo.totalBytesExpected / 1024)); + auto status = (Label*)view->getChildByTag(TAG_STATUS); status->setString(buf); + + sbtoa(task.progressInfo.speedInBytes, buf, 128); + AXLOGI("[{}%] speed: {}/s", percent, buf); }; // define success callback From d1ca753de4a41720e4b41af32b3e01e0ae8c9fb4 Mon Sep 17 00:00:00 2001 From: halx99 Date: Sat, 22 Jun 2024 13:55:34 +0800 Subject: [PATCH 2/4] Fix unit-test --- tests/unit-tests/Source/core/base/VectorTests.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/unit-tests/Source/core/base/VectorTests.cpp b/tests/unit-tests/Source/core/base/VectorTests.cpp index d8cd624cf192..d9908b8611d6 100644 --- a/tests/unit-tests/Source/core/base/VectorTests.cpp +++ b/tests/unit-tests/Source/core/base/VectorTests.cpp @@ -258,7 +258,8 @@ TEST_SUITE("base/Vector") { // get random object // Set the seed by time - std::srand((unsigned)time(nullptr)); + auto seed = (unsigned)time(nullptr); + std::srand(seed); Vector vecForRandom = createVector(); auto different = 0; for (int i = 0; i < vecForRandom.size(); ++i) { @@ -268,7 +269,9 @@ TEST_SUITE("base/Vector") { different++; } CHECK(different > 0); - CHECK(different > vecForRandom.size() * 3 / 4); // At least 75% different + auto count75 = vecForRandom.size() * 3 / 4; + CHECK(different >= count75); // At least 75% different + AXLOGI("## seed={}, different={}, count75={}", seed, different, count75); // Self assignment Vector vecSelfAssign = createVector(); From 042c9b712e17faa5b6e3ae870387f20f7c508ec0 Mon Sep 17 00:00:00 2001 From: RH Date: Fri, 28 Jun 2024 01:47:42 +1000 Subject: [PATCH 3/4] Make debug and verbose level logging in release builds optional (#2006) * Debug (AXLOGD) and verbose (AXLOGV) level logging should be disabled by default in release builds. * Fix preprocessor check --- core/base/Logging.h | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/core/base/Logging.h b/core/base/Logging.h index a0d793329fc5..650543442dc4 100644 --- a/core/base/Logging.h +++ b/core/base/Logging.h @@ -139,8 +139,20 @@ inline void printLogT(_FmtType&& fmt, LogItem& item, _Types&&... args) #define AXLOG_WITH_LEVEL(level, fmtOrMsg, ...) \ ax::printLogT(FMT_COMPILE("{}" fmtOrMsg "\n"), ax::preprocessLog(ax::LogItem{level}), ##__VA_ARGS__) -#define AXLOGV(fmtOrMsg, ...) AXLOG_WITH_LEVEL(ax::LogLevel::Verbose, fmtOrMsg, ##__VA_ARGS__) -#define AXLOGD(fmtOrMsg, ...) AXLOG_WITH_LEVEL(ax::LogLevel::Debug, fmtOrMsg, ##__VA_ARGS__) +#if defined(_AX_DEBUG) && _AX_DEBUG > 0 +# define AXLOGV(fmtOrMsg, ...) AXLOG_WITH_LEVEL(ax::LogLevel::Verbose, fmtOrMsg, ##__VA_ARGS__) +# define AXLOGD(fmtOrMsg, ...) AXLOG_WITH_LEVEL(ax::LogLevel::Debug, fmtOrMsg, ##__VA_ARGS__) +#else +# define AXLOGV(...) \ + do \ + { \ + } while (0) +# define AXLOGD(...) \ + do \ + { \ + } while (0) +#endif + #define AXLOGI(fmtOrMsg, ...) AXLOG_WITH_LEVEL(ax::LogLevel::Info, fmtOrMsg, ##__VA_ARGS__) #define AXLOGW(fmtOrMsg, ...) AXLOG_WITH_LEVEL(ax::LogLevel::Warn, fmtOrMsg, ##__VA_ARGS__) #define AXLOGE(fmtOrMsg, ...) AXLOG_WITH_LEVEL(ax::LogLevel::Error, fmtOrMsg, ##__VA_ARGS__) From 5ffd6ce09a2f6e02f676010ea36713558a9fd99b Mon Sep 17 00:00:00 2001 From: halx99 Date: Fri, 28 Jun 2024 01:03:31 +0800 Subject: [PATCH 4/4] Improve downloader - print http respone code when error - make checksum case-insensitive --- core/network/Downloader-curl.cpp | 20 ++++++++++++------- core/network/Downloader.cpp | 15 +++++++++----- .../DownloaderTest/DownloaderTest.cpp | 5 ++--- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/core/network/Downloader-curl.cpp b/core/network/Downloader-curl.cpp index c4e80852965e..3babb99047c8 100644 --- a/core/network/Downloader-curl.cpp +++ b/core/network/Downloader-curl.cpp @@ -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 lock(_mutex); _errCode = code; _errCodeInternal = codeInternal; - _errDescription = desc; + _errDescription = std::move(desc); } size_t writeDataProc(unsigned char* buffer, size_t size, size_t count) @@ -617,8 +617,14 @@ class DownloaderCURL::Impl : public std::enable_shared_from_this(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; } @@ -689,7 +695,7 @@ class DownloaderCURL::Impl : public std::enable_shared_from_thissetErrorProc(DownloadTask::ERROR_IMPL_INTERNAL, 0, "Alloc curl handle failed."); + coTask->setErrorDesc(DownloadTask::ERROR_IMPL_INTERNAL, 0, "Alloc curl handle failed."); _owner->_onDownloadFinished(*task); continue; } @@ -701,7 +707,7 @@ class DownloaderCURL::Impl : public std::enable_shared_from_thissetErrorProc(DownloadTask::ERROR_IMPL_INTERNAL, mcode, curl_multi_strerror(mcode)); + coTask->setErrorDesc(DownloadTask::ERROR_IMPL_INTERNAL, mcode, curl_multi_strerror(mcode)); _owner->_onDownloadFinished(*task); continue; } @@ -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) { diff --git a/core/network/Downloader.cpp b/core/network/Downloader.cpp index d2865ae2da00..78ba6e35ae2b 100644 --- a/core/network/Downloader.cpp +++ b/core/network/Downloader.cpp @@ -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 +#include + NS_AX_BEGIN namespace network @@ -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() diff --git a/tests/cpp-tests/Source/NetworkTest/DownloaderTest/DownloaderTest.cpp b/tests/cpp-tests/Source/NetworkTest/DownloaderTest/DownloaderTest.cpp index d839dba6394f..43c43d715c30 100644 --- a/tests/cpp-tests/Source/NetworkTest/DownloaderTest/DownloaderTest.cpp +++ b/tests/cpp-tests/Source/NetworkTest/DownloaderTest/DownloaderTest.cpp @@ -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] = { @@ -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));