2015-08-13 15:14:10 +08:00
|
|
|
/****************************************************************************
|
2016-05-18 12:54:16 +08:00
|
|
|
Copyright (c) 2015-2016 cocos2d-x.org
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-07-06 21:15:02 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2022-07-09 01:23:11 +08:00
|
|
|
https://axis-project.github.io/
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-08-13 15:14:10 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-08-13 15:14:10 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-08-13 15:14:10 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "network/CCDownloader.h"
|
|
|
|
|
2015-09-07 11:55:00 +08:00
|
|
|
#include "network/CCDownloader-curl.h"
|
2021-04-28 15:10:28 +08:00
|
|
|
#define DownloaderImpl DownloaderCURL
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace network
|
|
|
|
{
|
2021-04-28 15:10:28 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
DownloadTask::DownloadTask()
|
|
|
|
{
|
2021-04-28 15:10:28 +08:00
|
|
|
DLLOG("Construct DownloadTask %p", this);
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
DownloadTask::DownloadTask(std::string_view srcUrl, std::string_view identifier)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2021-04-28 15:10:28 +08:00
|
|
|
this->requestURL = srcUrl;
|
|
|
|
this->identifier = identifier;
|
2021-07-04 16:40:34 +08:00
|
|
|
this->background = false;
|
2021-04-28 15:10:28 +08:00
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
DownloadTask::DownloadTask(std::string_view srcUrl,
|
|
|
|
std::string_view storagePath,
|
|
|
|
std::string_view checksum,
|
|
|
|
std::string_view identifier,
|
2022-01-20 19:15:37 +08:00
|
|
|
bool background,
|
|
|
|
std::string_view cacertPath)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2021-04-28 15:10:28 +08:00
|
|
|
this->requestURL = srcUrl;
|
|
|
|
this->storagePath = storagePath;
|
|
|
|
this->checksum = checksum;
|
|
|
|
this->identifier = identifier;
|
2021-07-04 16:40:34 +08:00
|
|
|
this->background = background;
|
2022-01-20 19:15:37 +08:00
|
|
|
this->cacertPath = cacertPath;
|
2021-04-28 15:10:28 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
DownloadTask::~DownloadTask()
|
|
|
|
{
|
2021-04-28 15:10:28 +08:00
|
|
|
DLLOG("Destruct DownloadTask %p", this);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void DownloadTask::cancel()
|
|
|
|
{
|
2021-04-28 15:10:28 +08:00
|
|
|
if (_coTask)
|
|
|
|
_coTask->cancel();
|
|
|
|
}
|
2019-11-24 15:43:28 +08:00
|
|
|
|
2015-08-28 16:40:11 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Implement Downloader
|
2021-04-28 15:10:28 +08:00
|
|
|
Downloader::Downloader() : Downloader(DownloaderHints{6, 45, ".tmp"}) {}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Downloader::Downloader(const DownloaderHints& hints)
|
|
|
|
{
|
2021-04-28 15:10:28 +08:00
|
|
|
DLLOG("Construct Downloader %p", this);
|
|
|
|
_impl.reset(new DownloaderImpl(hints));
|
|
|
|
_impl->onTaskProgress = [this](const DownloadTask& task,
|
2021-12-25 10:04:45 +08:00
|
|
|
std::function<int64_t(void* buffer, int64_t len)>& /*transferDataToBuffer*/) {
|
|
|
|
if (onTaskProgress)
|
|
|
|
{
|
2021-04-28 15:10:28 +08:00
|
|
|
onTaskProgress(task);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
_impl->onTaskFinish = [this](const DownloadTask& task, int errorCode, int errorCodeInternal,
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view errorStr, std::vector<unsigned char>& data) {
|
2021-12-25 10:04:45 +08:00
|
|
|
if (DownloadTask::ERROR_NO_ERROR != errorCode)
|
|
|
|
{
|
|
|
|
if (onTaskError)
|
|
|
|
{
|
2021-04-28 15:10:28 +08:00
|
|
|
onTaskError(task, errorCode, errorCodeInternal, errorStr);
|
2015-08-13 15:14:10 +08:00
|
|
|
}
|
2021-04-28 15:10:28 +08:00
|
|
|
return;
|
|
|
|
}
|
2015-08-28 16:40:11 +08:00
|
|
|
|
2021-04-28 15:10:28 +08:00
|
|
|
// success callback
|
2021-12-25 10:04:45 +08:00
|
|
|
if (task.storagePath.length())
|
|
|
|
{
|
|
|
|
if (onFileTaskSuccess)
|
|
|
|
{
|
2021-04-28 15:10:28 +08:00
|
|
|
onFileTaskSuccess(task);
|
2015-08-13 15:14:10 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-04-28 15:10:28 +08:00
|
|
|
// data task
|
2021-12-25 10:04:45 +08:00
|
|
|
if (onDataTaskSuccess)
|
|
|
|
{
|
2021-04-28 15:10:28 +08:00
|
|
|
onDataTaskSuccess(task, data);
|
2015-08-13 15:14:10 +08:00
|
|
|
}
|
2021-04-28 15:10:28 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Downloader::~Downloader()
|
|
|
|
{
|
2021-04-28 15:10:28 +08:00
|
|
|
DLLOG("Destruct Downloader %p", this);
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
std::shared_ptr<DownloadTask> Downloader::createDownloadDataTask(std::string_view srcUrl,
|
|
|
|
std::string_view identifier /* = ""*/)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2021-04-28 15:10:28 +08:00
|
|
|
auto task = std::make_shared<DownloadTask>(srcUrl, identifier);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if (srcUrl.empty())
|
|
|
|
{
|
|
|
|
if (onTaskError)
|
|
|
|
{
|
2021-04-28 15:10:28 +08:00
|
|
|
onTaskError(*task, DownloadTask::ERROR_INVALID_PARAMS, 0, "URL or is empty.");
|
2015-08-13 15:14:10 +08:00
|
|
|
}
|
2021-04-28 15:10:28 +08:00
|
|
|
task.reset();
|
|
|
|
break;
|
|
|
|
}
|
2021-07-06 21:15:02 +08:00
|
|
|
_impl->startTask(task);
|
2021-04-28 15:10:28 +08:00
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
std::shared_ptr<DownloadTask> Downloader::createDownloadFileTask(std::string_view srcUrl,
|
|
|
|
std::string_view storagePath,
|
|
|
|
std::string_view identifier,
|
|
|
|
std::string_view md5checksum,
|
2022-01-20 19:15:37 +08:00
|
|
|
bool background,
|
|
|
|
std::string_view cacertPath)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2022-01-20 19:15:37 +08:00
|
|
|
auto task = std::make_shared<DownloadTask>(srcUrl, storagePath, md5checksum, identifier, background, cacertPath);
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if (srcUrl.empty() || storagePath.empty())
|
|
|
|
{
|
|
|
|
if (onTaskError)
|
|
|
|
{
|
2021-04-28 15:10:28 +08:00
|
|
|
onTaskError(*task, DownloadTask::ERROR_INVALID_PARAMS, 0, "URL or storage path is empty.");
|
2015-08-13 15:14:10 +08:00
|
|
|
}
|
2021-04-28 15:10:28 +08:00
|
|
|
task.reset();
|
|
|
|
break;
|
|
|
|
}
|
2021-07-06 21:15:02 +08:00
|
|
|
_impl->startTask(task);
|
2021-04-28 15:10:28 +08:00
|
|
|
} while (0);
|
2015-08-28 16:40:11 +08:00
|
|
|
|
2021-04-28 15:10:28 +08:00
|
|
|
return task;
|
|
|
|
}
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
// std::string Downloader::getFileNameFromUrl(std::string_view srcUrl)
|
2015-08-28 16:40:11 +08:00
|
|
|
//{
|
|
|
|
// // Find file name and file extension
|
|
|
|
// std::string filename;
|
2021-09-02 13:39:28 +08:00
|
|
|
// unsigned int32_t found = srcUrl.find_last_of("/\\");
|
2015-08-28 16:40:11 +08:00
|
|
|
// if (found != std::string::npos)
|
|
|
|
// filename = srcUrl.substr(found+1);
|
|
|
|
// return filename;
|
|
|
|
//}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace network
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END // namespace axis
|