Download.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <cpp3ds/System/FileSystem.hpp>
  4. #include <cpp3ds/System/I18n.hpp>
  5. #include <cpp3ds/System/Lock.hpp>
  6. #include <cpp3ds/System/Service.hpp>
  7. #include <cpp3ds/System/Sleep.hpp>
  8. #include "Download.hpp"
  9. #include "AssetManager.hpp"
  10. #include "DownloadQueue.hpp"
  11. #include "States/DialogState.hpp"
  12. #include "States/BrowseState.hpp"
  13. namespace FreeShop {
  14. Download::Download(const std::string &url, const std::string &destination)
  15. : m_thread(&Download::run, this)
  16. , m_progress(0.f)
  17. , m_canSendTop(true)
  18. , m_markedForDelete(false)
  19. , m_cancelFlag(false)
  20. , m_status(Queued)
  21. , m_downloadPos(0)
  22. , m_appItem(nullptr)
  23. , m_timesToRetry(3)
  24. , m_timeout(cpp3ds::seconds(10))
  25. , m_bufferSize(16*1024)
  26. {
  27. setUrl(url);
  28. setDestination(destination);
  29. // m_thread.setStackSize(64*1024);
  30. // m_thread.setAffinity(1);
  31. }
  32. Download::~Download()
  33. {
  34. cancel();
  35. }
  36. bool Download::setUrl(const std::string &url)
  37. {
  38. size_t pos;
  39. pos = url.find("://");
  40. if (pos != std::string::npos) {
  41. pos = url.find("/", pos + 3);
  42. if (pos == std::string::npos) {
  43. m_host = url;
  44. m_uri = "/";
  45. } else {
  46. m_host = url.substr(0, pos);
  47. m_uri = url.substr(pos);
  48. }
  49. }
  50. m_http.setHost(m_host);
  51. m_request.setUri(m_uri);
  52. return true;
  53. }
  54. void Download::start()
  55. {
  56. m_buffer.clear();
  57. m_thread.launch(); // run()
  58. }
  59. void Download::run()
  60. {
  61. if (!m_destination.empty())
  62. {
  63. m_file = fopen(cpp3ds::FileSystem::getFilePath(m_destination).c_str(), "w");
  64. if (!m_file)
  65. return;
  66. }
  67. m_status = Downloading;
  68. m_cancelFlag = false;
  69. bool failed = false;
  70. int retryCount = 0;
  71. m_request.setField("Range", _("bytes=%u-", m_downloadPos));
  72. cpp3ds::Http::RequestCallback dataCallback = [&](const void* data, size_t len, size_t processed, const cpp3ds::Http::Response& response)
  73. {
  74. cpp3ds::Lock lock(m_mutex);
  75. if (!m_destination.empty())
  76. {
  77. if (response.getStatus() == cpp3ds::Http::Response::Ok || response.getStatus() == cpp3ds::Http::Response::PartialContent)
  78. {
  79. const char *bufdata = reinterpret_cast<const char*>(data);
  80. m_buffer.insert(m_buffer.end(), bufdata, bufdata + len);
  81. if (m_buffer.size() > 1024 * 50)
  82. {
  83. fwrite(&m_buffer[0], sizeof(char), m_buffer.size(), m_file);
  84. m_buffer.clear();
  85. }
  86. }
  87. }
  88. if (!m_cancelFlag && m_onData)
  89. failed = !m_onData(data, len, processed, response);
  90. if (getStatus() != Suspended)
  91. m_downloadPos += len;
  92. if (getStatus() == Failed)
  93. failed = true;
  94. return !m_cancelFlag && !failed && getStatus() == Downloading;
  95. };
  96. // Loop in case there are URLs pushed to queue later
  97. while (1)
  98. {
  99. retryCount = 0;
  100. // Retry loop in case of connection failures
  101. do {
  102. // Wait in case of internet loss, but exit when canceled or suspended
  103. while (!cpp3ds::Service::isEnabled(cpp3ds::Httpc) && !m_cancelFlag && getStatus() != Suspended)
  104. {
  105. setProgressMessage(_("Waiting for internet..."));
  106. cpp3ds::sleep(cpp3ds::milliseconds(200));
  107. }
  108. m_response = m_http.sendRequest(m_request, m_timeout, dataCallback, m_bufferSize);
  109. // Follow all redirects
  110. while (m_response.getStatus() == cpp3ds::Http::Response::MovedPermanently || m_response.getStatus() == cpp3ds::Http::Response::MovedTemporarily)
  111. {
  112. setUrl(m_response.getField("Location"));
  113. m_response = m_http.sendRequest(m_request, m_timeout, dataCallback, m_bufferSize);
  114. }
  115. // Retry failed connections (all error codes >= 1000)
  116. if (m_response.getStatus() >= 1000)
  117. {
  118. if (retryCount >= m_timesToRetry)
  119. {
  120. if (m_timesToRetry == 0)
  121. setProgressMessage(_("Failed"));
  122. else
  123. setProgressMessage(_("Retry attempts exceeded"));
  124. failed = true;
  125. break;
  126. }
  127. std::cout << _("Retrying... %d", retryCount).toAnsiString() << std::endl;
  128. setProgressMessage(_("Retrying... %d", ++retryCount));
  129. m_request.setField("Range", _("bytes=%u-", m_downloadPos));
  130. cpp3ds::sleep(cpp3ds::milliseconds(1000));
  131. }
  132. } while (m_response.getStatus() >= 1000 &&
  133. !m_cancelFlag && getStatus() != Suspended);
  134. if (m_response.getStatus() != cpp3ds::Http::Response::Ok && m_response.getStatus() != cpp3ds::Http::Response::PartialContent)
  135. break;
  136. if (m_cancelFlag || failed || getStatus() == Suspended)
  137. break;
  138. // Download next in queue
  139. if (m_urlQueue.size() > 0)
  140. {
  141. auto nextUrl = m_urlQueue.front();
  142. setUrl(nextUrl.first);
  143. m_downloadPos = nextUrl.second;
  144. m_urlQueue.pop();
  145. m_request.setField("Range", _("bytes=%u-", m_downloadPos));
  146. }
  147. else
  148. break;
  149. }
  150. if (m_urlQueue.size() > 0 && getStatus() != Suspended)
  151. std::queue<std::pair<std::string,cpp3ds::Uint64>>().swap(m_urlQueue);
  152. if (getStatus() != Suspended)
  153. {
  154. m_canSendTop = false;
  155. if (m_cancelFlag)
  156. {
  157. m_markedForDelete = true;
  158. m_status = Canceled;
  159. }
  160. else if (failed)
  161. m_status = Failed;
  162. else
  163. m_status = Finished;
  164. }
  165. // Write remaining buffer and close downloaded file
  166. if (!m_destination.empty())
  167. {
  168. if (m_buffer.size() > 0)
  169. fwrite(&m_buffer[0], sizeof(char), m_buffer.size(), m_file);
  170. fclose(m_file);
  171. if (m_status != Finished)
  172. remove(cpp3ds::FileSystem::getFilePath(m_destination).c_str());
  173. }
  174. if (m_onFinish)
  175. m_onFinish(m_cancelFlag, failed);
  176. m_http.close();
  177. }
  178. void Download::cancel(bool wait)
  179. {
  180. m_cancelFlag = true;
  181. if (wait)
  182. m_thread.wait();
  183. }
  184. bool Download::isCanceled()
  185. {
  186. return m_cancelFlag;
  187. }
  188. void Download::setProgress(float progress)
  189. {
  190. m_progress = progress;
  191. m_progressBar.setSize(cpp3ds::Vector2f(m_progress * m_size.x, m_size.y));
  192. }
  193. float Download::getProgress() const
  194. {
  195. return m_progress;
  196. }
  197. void Download::setProgressMessage(const std::string &message)
  198. {
  199. m_progressMessage = message;
  200. m_textProgress.setString(message);
  201. }
  202. const std::string &Download::getProgressMessage() const
  203. {
  204. return m_progressMessage;
  205. }
  206. void Download::draw(cpp3ds::RenderTarget &target, cpp3ds::RenderStates states) const
  207. {
  208. states.transform *= getTransform();
  209. target.draw(m_background, states);
  210. target.draw(m_icon, states);
  211. target.draw(m_textTitle, states);
  212. target.draw(m_textProgress, states);
  213. if (!m_cancelFlag)
  214. {
  215. target.draw(m_textCancel, states);
  216. if (m_canSendTop && (m_status == Queued || m_status == Suspended || m_status == Downloading))
  217. target.draw(m_textSendTop, states);
  218. else if (m_status == Failed)
  219. target.draw(m_textRestart, states);
  220. }
  221. if (m_progress > 0.f && m_progress < 1.f)
  222. target.draw(m_progressBar, states);
  223. }
  224. const cpp3ds::Vector2f &Download::getSize() const
  225. {
  226. return m_size;
  227. }
  228. void Download::fillFromAppItem(std::shared_ptr<AppItem> app)
  229. {
  230. m_appItem = app;
  231. setProgressMessage(_("Queued"));
  232. m_background.setTexture(&AssetManager<cpp3ds::Texture>::get("images/listitembg.9.png"));
  233. m_background.setContentSize(320.f + m_background.getPadding().width - m_background.getTexture()->getSize().x + 2.f, 24.f);
  234. m_size = m_background.getSize();
  235. float paddingRight = m_size.x - m_background.getContentSize().x - m_background.getPadding().left;
  236. float paddingBottom = m_size.y - m_background.getContentSize().y - m_background.getPadding().top;
  237. m_icon.setSize(cpp3ds::Vector2f(48.f, 48.f));
  238. cpp3ds::IntRect textureRect;
  239. m_icon.setTexture(app->getIcon(textureRect), true);
  240. m_icon.setTextureRect(textureRect);
  241. m_icon.setPosition(m_background.getPadding().left, m_background.getPadding().top);
  242. m_icon.setScale(0.5f, 0.5f);
  243. m_textCancel.setFont(AssetManager<cpp3ds::Font>::get("fonts/fontawesome.ttf"));
  244. m_textCancel.setString(L"\uf00d");
  245. m_textCancel.setCharacterSize(18);
  246. m_textCancel.setFillColor(cpp3ds::Color::White);
  247. m_textCancel.setOutlineColor(cpp3ds::Color(0, 0, 0, 200));
  248. m_textCancel.setOutlineThickness(1.f);
  249. m_textCancel.setOrigin(0, m_textCancel.getLocalBounds().top + m_textCancel.getLocalBounds().height / 2.f);
  250. m_textCancel.setPosition(m_size.x - paddingRight - m_textCancel.getLocalBounds().width,
  251. m_background.getPadding().top + m_background.getContentSize().y / 2.f);
  252. m_textSendTop = m_textCancel;
  253. m_textSendTop.setString(L"\uf077");
  254. m_textSendTop.move(-25.f, 0);
  255. m_textRestart = m_textCancel;
  256. m_textRestart.setString(L"\uf01e");
  257. m_textRestart.move(-25.f, 0);
  258. m_textTitle.setString(app->getTitle());
  259. m_textTitle.setCharacterSize(10);
  260. m_textTitle.setPosition(m_background.getPadding().left + 30.f, m_background.getPadding().top);
  261. m_textTitle.setFillColor(cpp3ds::Color::Black);
  262. m_textTitle.useSystemFont();
  263. m_textProgress = m_textTitle;
  264. m_textProgress.setFillColor(cpp3ds::Color(130, 130, 130, 255));
  265. m_textProgress.move(0.f, 12.f);
  266. m_progressBar.setFillColor(cpp3ds::Color(0, 0, 0, 50));
  267. }
  268. void Download::processEvent(const cpp3ds::Event &event)
  269. {
  270. if (event.type == cpp3ds::Event::TouchBegan)
  271. {
  272. cpp3ds::FloatRect bounds = m_textCancel.getGlobalBounds();
  273. bounds.left += getPosition().x;
  274. bounds.top += getPosition().y + DownloadQueue::getInstance().getPosition().y;
  275. if (bounds.contains(event.touch.x, event.touch.y))
  276. {
  277. if (getStatus() != Downloading && getStatus() != Suspended)
  278. m_markedForDelete = true;
  279. else
  280. {
  281. g_browseState->requestStackPush(States::Dialog, false, [=](void *data) mutable {
  282. auto event = reinterpret_cast<DialogState::Event*>(data);
  283. if (event->type == DialogState::GetText)
  284. {
  285. auto str = reinterpret_cast<cpp3ds::String*>(event->data);
  286. *str = _("%s\n\nAre you sure you want to cancel\nthis installation and lose all progress?", m_appItem->getTitle().toAnsiString().c_str());
  287. return true;
  288. }
  289. else if (event->type == DialogState::Response)
  290. {
  291. bool *accepted = reinterpret_cast<bool*>(event->data);
  292. if (*accepted)
  293. {
  294. // Check status again in case it changed while dialog was open
  295. if (getStatus() == Downloading)
  296. {
  297. setProgressMessage(_("Canceling..."));
  298. cancel(false);
  299. }
  300. else if (getStatus() == Suspended)
  301. m_markedForDelete = true;
  302. }
  303. return true;
  304. }
  305. return false;
  306. });
  307. }
  308. }
  309. else if (m_canSendTop && m_onSendTop && (getStatus() == Queued || getStatus() == Suspended || getStatus() == Downloading))
  310. {
  311. bounds = m_textSendTop.getGlobalBounds();
  312. bounds.left += getPosition().x;
  313. bounds.top += getPosition().y + DownloadQueue::getInstance().getPosition().y;
  314. if (bounds.contains(event.touch.x, event.touch.y))
  315. {
  316. m_onSendTop(this);
  317. }
  318. }
  319. else if (getStatus() == Failed)
  320. {
  321. bounds = m_textRestart.getGlobalBounds();
  322. bounds.left += getPosition().x;
  323. bounds.top += getPosition().y + DownloadQueue::getInstance().getPosition().y;
  324. if (bounds.contains(event.touch.x, event.touch.y))
  325. {
  326. // Restart failed download
  327. DownloadQueue::getInstance().restartDownload(this);
  328. }
  329. }
  330. }
  331. }
  332. bool Download::markedForDelete()
  333. {
  334. return m_markedForDelete;
  335. }
  336. void Download::setDestination(const std::string &destination)
  337. {
  338. m_destination = destination;
  339. }
  340. void Download::setDataCallback(cpp3ds::Http::RequestCallback onData)
  341. {
  342. m_onData = onData;
  343. }
  344. void Download::setFinishCallback(DownloadFinishCallback onFinish)
  345. {
  346. m_onFinish = onFinish;
  347. }
  348. void Download::setSendTopCallback(SendTopCallback onSendTop)
  349. {
  350. m_onSendTop = onSendTop;
  351. }
  352. void Download::setField(const std::string &field, const std::string &value)
  353. {
  354. m_request.setField(field, value);
  355. }
  356. void Download::pushUrl(const std::string &url, cpp3ds::Uint64 position)
  357. {
  358. m_urlQueue.push(std::make_pair(url, position));
  359. }
  360. Download::Status Download::getStatus() const
  361. {
  362. return m_status;
  363. }
  364. void Download::suspend()
  365. {
  366. if (getStatus() != Downloading)
  367. return;
  368. cpp3ds::Lock lock(m_mutex);
  369. m_status = Suspended;
  370. }
  371. void Download::resume()
  372. {
  373. if (getStatus() == Suspended || getStatus() == Queued)
  374. {
  375. start();
  376. }
  377. }
  378. void Download::setRetryCount(int timesToRetry)
  379. {
  380. m_timesToRetry = timesToRetry;
  381. }
  382. const cpp3ds::Http::Response &Download::getLastResponse() const
  383. {
  384. return m_response;
  385. }
  386. void Download::setTimeout(cpp3ds::Time timeout)
  387. {
  388. m_timeout = timeout;
  389. }
  390. void Download::setBufferSize(size_t size)
  391. {
  392. if (size % 64 > 0)
  393. return;
  394. m_bufferSize = size;
  395. }
  396. } // namespace FreeShop