axmol/core/network/CCDownloader.h

155 lines
5.1 KiB
C
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2015-2016 cocos2d-x.org
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-07-06 21:15:02 +08:00
Copyright (c) 2021 Bytedance Inc.
2019-11-23 20:27:39 +08:00
https://axis-project.github.io/
2019-11-23 20:27:39 +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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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.
****************************************************************************/
#pragma once
#include <functional>
#include <string>
#include <memory>
#include <vector>
#include "platform/CCPlatformMacros.h"
NS_AX_BEGIN
2021-12-25 10:04:45 +08:00
namespace network
{
2021-07-04 16:40:34 +08:00
class IDownloadTask;
class IDownloaderImpl;
class Downloader;
2021-07-04 19:26:18 +08:00
class DownloaderCURL;
2021-07-04 16:40:34 +08:00
class CC_DLL DownloadTask final
2021-12-25 10:04:45 +08:00
{
2021-07-04 16:40:34 +08:00
public:
const static int ERROR_NO_ERROR = 0;
const static int ERROR_INVALID_PARAMS = -1;
const static int ERROR_OPEN_FILE_FAILED = -2;
const static int ERROR_IMPL_INTERNAL = -3;
const static int ERROR_TASK_DUPLICATED = -4;
const static int ERROR_CREATE_DIR_FAILED = -5;
const static int ERROR_REMOVE_FILE_FAILED = -6;
const static int ERROR_RENAME_FILE_FAILED = -7;
const static int ERROR_CHECK_SUM_FAILED = -8;
const static int ERROR_ORIGIN_FILE_MISSING = -9;
std::string identifier;
std::string requestURL;
std::string storagePath;
std::string cacertPath;
2021-07-04 16:40:34 +08:00
2021-12-25 10:04:45 +08:00
struct
{
2021-07-04 16:40:34 +08:00
// progress
int64_t totalBytesExpected = 0;
int64_t bytesReceived = 0;
int64_t totalBytesReceived = 0;
// speed
double speedInBytes = 0;
} mutable progressInfo;
DownloadTask();
DownloadTask(std::string_view srcUrl, std::string_view identifier);
DownloadTask(std::string_view srcUrl,
std::string_view storagePath,
std::string_view checksum, // currently is MD5
std::string_view identifier,
bool background,
std::string_view cacertPath);
2021-07-04 16:40:34 +08:00
virtual ~DownloadTask();
// Cancel the download, it's useful for ios platform switch wifi to 4g
void cancel();
2021-12-25 10:04:45 +08:00
std::string checksum; // The MD5 checksum for check only when download finished.
bool background; // Does the task is background (all callback will invoke on downloader thread)
2021-07-04 16:40:34 +08:00
private:
friend class Downloader;
2021-07-04 19:26:18 +08:00
friend class DownloaderCURL;
2021-07-04 16:40:34 +08:00
std::unique_ptr<IDownloadTask> _coTask;
};
class CC_DLL DownloaderHints
2021-12-25 10:04:45 +08:00
{
2021-07-04 16:40:34 +08:00
public:
uint32_t countOfMaxProcessingTasks;
uint32_t timeoutInSeconds;
std::string tempFileNameSuffix;
};
class CC_DLL Downloader final
2021-12-25 10:04:45 +08:00
{
2021-07-04 16:40:34 +08:00
public:
Downloader();
Downloader(const DownloaderHints& hints);
~Downloader();
std::function<void(const DownloadTask& task, std::vector<unsigned char>& data)> onDataTaskSuccess;
std::function<void(const DownloadTask& task)> onFileTaskSuccess;
std::function<void(const DownloadTask& task)> onTaskProgress;
std::function<void(const DownloadTask& task, int errorCode, int errorCodeInternal, std::string_view errorStr)>
2021-07-04 16:40:34 +08:00
onTaskError;
2021-12-25 10:04:45 +08:00
void setOnFileTaskSuccess(const std::function<void(const DownloadTask& task)>& callback)
{
2021-07-04 16:40:34 +08:00
onFileTaskSuccess = callback;
2019-11-23 20:27:39 +08:00
};
2021-12-25 10:04:45 +08:00
void setOnTaskProgress(const std::function<void(const DownloadTask& task)>& callback)
{
2021-07-04 16:40:34 +08:00
onTaskProgress = callback;
2019-11-23 20:27:39 +08:00
};
2021-12-25 10:04:45 +08:00
void setOnTaskError(
const std::function<
void(const DownloadTask& task, int errorCode, int errorCodeInternal, std::string_view errorStr)>& callback)
2021-12-25 10:04:45 +08:00
{
2021-07-04 16:40:34 +08:00
onTaskError = callback;
2019-11-23 20:27:39 +08:00
};
std::shared_ptr<DownloadTask> createDownloadDataTask(std::string_view srcUrl, std::string_view identifier = "");
2019-11-23 20:27:39 +08:00
std::shared_ptr<DownloadTask> createDownloadFileTask(std::string_view srcUrl,
std::string_view storagePath,
std::string_view identifier = "",
std::string_view checksum = "",
bool background = false,
std::string_view cacertPath = "");
2021-07-04 16:40:34 +08:00
private:
std::unique_ptr<IDownloaderImpl> _impl;
};
2021-12-25 10:04:45 +08:00
} // namespace network
NS_AX_END // namespace axis