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.
|
2015-08-13 15:14:10 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-08-28 16:40:11 +08:00
|
|
|
#pragma once
|
2015-08-13 15:14:10 +08:00
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
|
|
|
#include <memory>
|
2015-09-07 11:55:00 +08:00
|
|
|
#include <vector>
|
|
|
|
|
2015-08-28 16:40:11 +08:00
|
|
|
#include "platform/CCPlatformMacros.h"
|
2015-08-13 15:14:10 +08:00
|
|
|
|
2015-08-28 16:40:11 +08:00
|
|
|
namespace cocos2d { namespace network {
|
2015-08-13 15:14:10 +08:00
|
|
|
|
2015-08-28 16:40:11 +08:00
|
|
|
class IDownloadTask;
|
|
|
|
class IDownloaderImpl;
|
|
|
|
class Downloader;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-09-15 18:30:45 +08:00
|
|
|
class CC_DLL DownloadTask final
|
2015-08-13 15:14:10 +08:00
|
|
|
{
|
2015-09-15 18:30:45 +08:00
|
|
|
public:
|
2015-08-28 16:40:11 +08:00
|
|
|
const static int ERROR_NO_ERROR = 0;
|
|
|
|
const static int ERROR_INVALID_PARAMS = -1;
|
2019-11-24 15:43:28 +08:00
|
|
|
const static int ERROR_OPEN_FILE_FAILED = -2;
|
2015-08-28 16:40:11 +08:00
|
|
|
const static int ERROR_IMPL_INTERNAL = -3;
|
2019-11-24 15:43:28 +08:00
|
|
|
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;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-08-28 16:40:11 +08:00
|
|
|
std::string identifier;
|
|
|
|
std::string requestURL;
|
|
|
|
std::string storagePath;
|
2015-08-13 15:14:10 +08:00
|
|
|
|
2019-11-24 15:43:28 +08:00
|
|
|
struct {
|
|
|
|
// progress
|
|
|
|
int64_t totalBytesExpected = 0;
|
|
|
|
int64_t bytesReceived = 0;
|
|
|
|
int64_t totalBytesReceived = 0;
|
|
|
|
// speed
|
|
|
|
double speedInBytes = 0;
|
|
|
|
} mutable progressInfo;
|
|
|
|
|
2015-08-28 16:40:11 +08:00
|
|
|
DownloadTask();
|
2019-11-24 15:43:28 +08:00
|
|
|
DownloadTask(const std::string& srcUrl, const std::string& identifier);
|
|
|
|
DownloadTask(const std::string& srcUrl,
|
|
|
|
const std::string& storagePath,
|
|
|
|
const std::string& md5checksum,
|
|
|
|
const std::string& identifier);
|
|
|
|
|
2015-08-28 16:40:11 +08:00
|
|
|
virtual ~DownloadTask();
|
2019-11-24 15:43:28 +08:00
|
|
|
|
|
|
|
// Cancel the download, it's useful for ios platform switch wifi to 4g
|
|
|
|
void cancel();
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2019-11-24 15:43:28 +08:00
|
|
|
std::string md5checksum; // For check only when download finished.
|
|
|
|
|
2015-08-28 16:40:11 +08:00
|
|
|
private:
|
|
|
|
friend class Downloader;
|
|
|
|
std::unique_ptr<IDownloadTask> _coTask;
|
2015-08-13 15:14:10 +08:00
|
|
|
};
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-09-15 18:30:45 +08:00
|
|
|
class CC_DLL DownloaderHints
|
2015-09-02 18:26:42 +08:00
|
|
|
{
|
2015-09-15 18:30:45 +08:00
|
|
|
public:
|
2015-09-02 18:26:42 +08:00
|
|
|
uint32_t countOfMaxProcessingTasks;
|
|
|
|
uint32_t timeoutInSeconds;
|
|
|
|
std::string tempFileNameSuffix;
|
|
|
|
};
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-08-28 16:40:11 +08:00
|
|
|
class CC_DLL Downloader final
|
2015-08-13 15:14:10 +08:00
|
|
|
{
|
2015-08-28 16:40:11 +08:00
|
|
|
public:
|
|
|
|
Downloader();
|
2015-09-02 18:26:42 +08:00
|
|
|
Downloader(const DownloaderHints& hints);
|
2015-08-28 16:40:11 +08:00
|
|
|
~Downloader();
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-08-28 16:40:11 +08:00
|
|
|
std::function<void(const DownloadTask& task,
|
2015-09-02 18:26:42 +08:00
|
|
|
std::vector<unsigned char>& data)> onDataTaskSuccess;
|
2015-09-14 15:33:34 +08:00
|
|
|
|
2015-08-28 16:40:11 +08:00
|
|
|
std::function<void(const DownloadTask& task)> onFileTaskSuccess;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2019-11-24 15:43:28 +08:00
|
|
|
std::function<void(const DownloadTask& task)> onTaskProgress;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-08-28 16:40:11 +08:00
|
|
|
std::function<void(const DownloadTask& task,
|
|
|
|
int errorCode,
|
|
|
|
int errorCodeInternal,
|
|
|
|
const std::string& errorStr)> onTaskError;
|
2017-03-15 16:09:02 +08:00
|
|
|
|
|
|
|
void setOnFileTaskSuccess(const std::function<void(const DownloadTask& task)>& callback) {onFileTaskSuccess = callback;};
|
|
|
|
|
2019-11-24 15:43:28 +08:00
|
|
|
void setOnTaskProgress(const std::function<void(const DownloadTask& task)>& callback) {onTaskProgress = callback;};
|
2017-03-15 16:09:02 +08:00
|
|
|
|
|
|
|
void setOnTaskError(const std::function<void(const DownloadTask& task,
|
|
|
|
int errorCode,
|
|
|
|
int errorCodeInternal,
|
|
|
|
const std::string& errorStr)>& callback) {onTaskError = callback;};
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2019-11-24 15:43:28 +08:00
|
|
|
std::shared_ptr<DownloadTask> createDownloadDataTask(const std::string& srcUrl, const std::string& identifier = "");
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2019-11-24 15:43:28 +08:00
|
|
|
std::shared_ptr<DownloadTask> createDownloadFileTask(const std::string& srcUrl, const std::string& storagePath, const std::string& md5checksum = "", const std::string& identifier = "");
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-08-28 16:40:11 +08:00
|
|
|
private:
|
|
|
|
std::unique_ptr<IDownloaderImpl> _impl;
|
2015-08-13 15:14:10 +08:00
|
|
|
};
|
|
|
|
|
2015-08-28 16:40:11 +08:00
|
|
|
}} // namespace cocos2d::network
|
2016-05-18 12:54:16 +08:00
|
|
|
|