CCDownloader.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /****************************************************************************
  2. Copyright (c) 2015-2016 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "network/CCDownloader.h"
  23. // include platform specific implement class
  24. #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  25. #include "network/CCDownloaderImpl-apple.h"
  26. #define DownloaderImpl DownloaderApple
  27. #elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  28. #include "network/CCDownloader-android.h"
  29. #define DownloaderImpl DownloaderAndroid
  30. #else
  31. #include "network/CCDownloader-curl.h"
  32. #define DownloaderImpl DownloaderCURL
  33. #endif
  34. namespace cocos2d { namespace network {
  35. DownloadTask::DownloadTask()
  36. {
  37. DLLOG("Construct DownloadTask %p", this);
  38. }
  39. DownloadTask::~DownloadTask()
  40. {
  41. DLLOG("Destruct DownloadTask %p", this);
  42. }
  43. ////////////////////////////////////////////////////////////////////////////////
  44. // Implement Downloader
  45. Downloader::Downloader()
  46. {
  47. DownloaderHints hints =
  48. {
  49. 6,
  50. 45,
  51. ".tmp"
  52. };
  53. new(this)Downloader(hints);
  54. }
  55. Downloader::Downloader(const DownloaderHints& hints)
  56. {
  57. DLLOG("Construct Downloader %p", this);
  58. _impl.reset(new DownloaderImpl(hints));
  59. _impl->onTaskProgress = [this](const DownloadTask& task,
  60. int64_t bytesReceived,
  61. int64_t totalBytesReceived,
  62. int64_t totalBytesExpected,
  63. std::function<int64_t(void *buffer, int64_t len)>& /*transferDataToBuffer*/)
  64. {
  65. if (onTaskProgress)
  66. {
  67. onTaskProgress(task, bytesReceived, totalBytesReceived, totalBytesExpected);
  68. }
  69. };
  70. _impl->onTaskFinish = [this](const DownloadTask& task,
  71. int errorCode,
  72. int errorCodeInternal,
  73. const std::string& errorStr,
  74. std::vector<unsigned char>& data)
  75. {
  76. if (DownloadTask::ERROR_NO_ERROR != errorCode)
  77. {
  78. if (onTaskError)
  79. {
  80. onTaskError(task, errorCode, errorCodeInternal, errorStr);
  81. }
  82. return;
  83. }
  84. // success callback
  85. if (task.storagePath.length())
  86. {
  87. if (onFileTaskSuccess)
  88. {
  89. onFileTaskSuccess(task);
  90. }
  91. }
  92. else
  93. {
  94. // data task
  95. if (onDataTaskSuccess)
  96. {
  97. onDataTaskSuccess(task, data);
  98. }
  99. }
  100. };
  101. }
  102. Downloader::~Downloader()
  103. {
  104. DLLOG("Destruct Downloader %p", this);
  105. }
  106. std::shared_ptr<const DownloadTask> Downloader::createDownloadDataTask(const std::string& srcUrl, const std::string& identifier/* = ""*/)
  107. {
  108. DownloadTask *task_ = new (std::nothrow) DownloadTask();
  109. std::shared_ptr<const DownloadTask> task(task_);
  110. do
  111. {
  112. task_->requestURL = srcUrl;
  113. task_->identifier = identifier;
  114. if (0 == srcUrl.length())
  115. {
  116. if (onTaskError)
  117. {
  118. onTaskError(*task, DownloadTask::ERROR_INVALID_PARAMS, 0, "URL or is empty.");
  119. }
  120. task.reset();
  121. break;
  122. }
  123. task_->_coTask.reset(_impl->createCoTask(task));
  124. } while (0);
  125. return task;
  126. }
  127. std::shared_ptr<const DownloadTask> Downloader::createDownloadFileTask(const std::string& srcUrl,
  128. const std::string& storagePath,
  129. const std::map<std::string, std::string> &header,
  130. const std::string& identifier/* = ""*/)
  131. {
  132. DownloadTask *task_ = new (std::nothrow) DownloadTask();
  133. std::shared_ptr<const DownloadTask> task(task_);
  134. do
  135. {
  136. task_->requestURL = srcUrl;
  137. task_->storagePath = storagePath;
  138. task_->identifier = identifier;
  139. task_->header = header;
  140. if (0 == srcUrl.length() || 0 == storagePath.length())
  141. {
  142. if (onTaskError)
  143. {
  144. onTaskError(*task, DownloadTask::ERROR_INVALID_PARAMS, 0, "URL or storage path is empty.");
  145. }
  146. task.reset();
  147. break;
  148. }
  149. task_->_coTask.reset(_impl->createCoTask(task));
  150. } while (0);
  151. return task;
  152. }
  153. std::shared_ptr<const DownloadTask> Downloader::createDownloadFileTask(const std::string& srcUrl,
  154. const std::string& storagePath,
  155. const std::string& identifier/* = ""*/) {
  156. const std::map<std::string, std::string> emptyHeader;
  157. return createDownloadFileTask(srcUrl, storagePath, emptyHeader, identifier);
  158. }
  159. void Downloader::abort(const DownloadTask& task) {
  160. _impl->abort(task._coTask);
  161. }
  162. //std::string Downloader::getFileNameFromUrl(const std::string& srcUrl)
  163. //{
  164. // // Find file name and file extension
  165. // std::string filename;
  166. // unsigned long found = srcUrl.find_last_of("/\\");
  167. // if (found != std::string::npos)
  168. // filename = srcUrl.substr(found+1);
  169. // return filename;
  170. //}
  171. }} // namespace cocos2d::network