mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-06 19:42:39 +08:00
997cd1044c
Co-authored-by: cocos-robot <cocos-robot@cocos.com>
121 lines
4.9 KiB
C++
121 lines
4.9 KiB
C++
/****************************************************************************
|
|
Copyright (c) 2015-2016 cocos2d-x.org
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
Copyright (c) 2017-2022 Xiamen Yaji Software Co., Ltd.
|
|
|
|
http://www.cocos.com
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated engine source code (the "Software"), a limited,
|
|
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
|
to use Cocos Creator solely to develop games on your target platforms. You shall
|
|
not use Cocos Creator software for developing other software or tools that's
|
|
used for developing games. You are not granted to publish, distribute,
|
|
sublicense, and/or sell copies of Cocos Creator.
|
|
|
|
The software or tools in this License Agreement are licensed, not sold.
|
|
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
|
|
|
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 <memory>
|
|
#include "base/Macros.h"
|
|
#include "base/std/container/string.h"
|
|
#include "base/std/container/unordered_map.h"
|
|
#include "base/std/container/vector.h"
|
|
|
|
namespace cc {
|
|
namespace network {
|
|
|
|
class IDownloadTask;
|
|
class IDownloaderImpl;
|
|
class Downloader;
|
|
|
|
class CC_DLL DownloadTask final {
|
|
public:
|
|
const static int ERROR_NO_ERROR = 0;
|
|
const static int ERROR_INVALID_PARAMS = -1;
|
|
const static int ERROR_FILE_OP_FAILED = -2;
|
|
const static int ERROR_IMPL_INTERNAL = -3;
|
|
const static int ERROR_ABORT = -4;
|
|
|
|
ccstd::string identifier;
|
|
ccstd::string requestURL;
|
|
ccstd::string storagePath;
|
|
ccstd::unordered_map<ccstd::string, ccstd::string> header;
|
|
|
|
DownloadTask();
|
|
virtual ~DownloadTask();
|
|
|
|
private:
|
|
friend class Downloader;
|
|
std::unique_ptr<IDownloadTask> _coTask;
|
|
};
|
|
|
|
struct CC_DLL DownloaderHints {
|
|
uint32_t countOfMaxProcessingTasks;
|
|
uint32_t timeoutInSeconds;
|
|
ccstd::string tempFileNameSuffix;
|
|
};
|
|
|
|
class CC_DLL Downloader final {
|
|
public:
|
|
Downloader();
|
|
explicit Downloader(const DownloaderHints &hints);
|
|
~Downloader();
|
|
|
|
std::function<void(const DownloadTask & task,
|
|
const ccstd::vector<unsigned char> &data)>
|
|
onDataTaskSuccess;
|
|
|
|
std::function<void(const DownloadTask &task)> onFileTaskSuccess;
|
|
|
|
std::function<void(const DownloadTask &task,
|
|
int64_t bytesReceived,
|
|
int64_t totalBytesReceived,
|
|
int64_t totalBytesExpected)>
|
|
onTaskProgress;
|
|
|
|
std::function<void(const DownloadTask & task,
|
|
int errorCode,
|
|
int errorCodeInternal,
|
|
const ccstd::string &errorStr)>
|
|
onTaskError;
|
|
|
|
void setOnFileTaskSuccess(const std::function<void(const DownloadTask &task)> &callback) { onFileTaskSuccess = callback; };
|
|
|
|
void setOnTaskProgress(const std::function<void(const DownloadTask &task,
|
|
int64_t bytesReceived,
|
|
int64_t totalBytesReceived,
|
|
int64_t totalBytesExpected)> &callback) { onTaskProgress = callback; };
|
|
|
|
void setOnTaskError(const std::function<void(const DownloadTask & task,
|
|
int errorCode,
|
|
int errorCodeInternal,
|
|
const ccstd::string &errorStr)> &callback) { onTaskError = callback; };
|
|
|
|
std::shared_ptr<const DownloadTask> createDownloadDataTask(const ccstd::string &srcUrl, const ccstd::string &identifier = "");
|
|
|
|
std::shared_ptr<const DownloadTask> createDownloadFileTask(const ccstd::string &srcUrl, const ccstd::string &storagePath, const ccstd::string &identifier = "");
|
|
|
|
std::shared_ptr<const DownloadTask> createDownloadFileTask(const ccstd::string &srcUrl, const ccstd::string &storagePath, const ccstd::unordered_map<ccstd::string, ccstd::string> &header, const ccstd::string &identifier = "");
|
|
|
|
void abort(const DownloadTask &task);
|
|
|
|
private:
|
|
std::unique_ptr<IDownloaderImpl> _impl;
|
|
};
|
|
|
|
} // namespace network
|
|
} // namespace cc
|