CCDownloader.h 4.1 KB

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