CCDownloader.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /****************************************************************************
  2. Copyright (c) 2015-2016 cocos2d-x.org
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #pragma once
  22. #include <functional>
  23. #include <string>
  24. #include <memory>
  25. #include <vector>
  26. #include "platform/CCPlatformMacros.h"
  27. namespace cocos2d { namespace network {
  28. class IDownloadTask;
  29. class IDownloaderImpl;
  30. class Downloader;
  31. class CC_DLL DownloadTask final
  32. {
  33. public:
  34. const static int ERROR_NO_ERROR = 0;
  35. const static int ERROR_INVALID_PARAMS = -1;
  36. const static int ERROR_FILE_OP_FAILED = -2;
  37. const static int ERROR_IMPL_INTERNAL = -3;
  38. std::string identifier;
  39. std::string requestURL;
  40. std::string storagePath;
  41. DownloadTask();
  42. virtual ~DownloadTask();
  43. private:
  44. friend class Downloader;
  45. std::unique_ptr<IDownloadTask> _coTask;
  46. };
  47. class CC_DLL DownloaderHints
  48. {
  49. public:
  50. uint32_t countOfMaxProcessingTasks;
  51. uint32_t timeoutInSeconds;
  52. std::string tempFileNameSuffix;
  53. };
  54. class CC_DLL Downloader final
  55. {
  56. public:
  57. Downloader();
  58. Downloader(const DownloaderHints& hints);
  59. ~Downloader();
  60. std::function<void(const DownloadTask& task,
  61. std::vector<unsigned char>& data)> onDataTaskSuccess;
  62. std::function<void(const DownloadTask& task)> onFileTaskSuccess;
  63. std::function<void(const DownloadTask& task,
  64. int64_t bytesReceived,
  65. int64_t totalBytesReceived,
  66. int64_t totalBytesExpected)> onTaskProgress;
  67. std::function<void(const DownloadTask& task,
  68. int errorCode,
  69. int errorCodeInternal,
  70. const std::string& errorStr)> onTaskError;
  71. void setOnFileTaskSuccess(const std::function<void(const DownloadTask& task)>& callback) {onFileTaskSuccess = callback;};
  72. void setOnTaskProgress(const std::function<void(const DownloadTask& task,
  73. int64_t bytesReceived,
  74. int64_t totalBytesReceived,
  75. int64_t totalBytesExpected)>& callback) {onTaskProgress = callback;};
  76. void setOnTaskError(const std::function<void(const DownloadTask& task,
  77. int errorCode,
  78. int errorCodeInternal,
  79. const std::string& errorStr)>& callback) {onTaskError = callback;};
  80. std::shared_ptr<const DownloadTask> createDownloadDataTask(const std::string& srcUrl, const std::string& identifier = "");
  81. std::shared_ptr<const DownloadTask> createDownloadFileTask(const std::string& srcUrl, const std::string& storagePath, const std::string& identifier = "");
  82. private:
  83. std::unique_ptr<IDownloaderImpl> _impl;
  84. };
  85. }} // namespace cocos2d::network