clientmedia.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include "irrlichttypes.h"
  18. #include "filecache.h"
  19. #include <ostream>
  20. #include <map>
  21. #include <set>
  22. #include <vector>
  23. #include <unordered_map>
  24. class Client;
  25. struct HTTPFetchResult;
  26. #define MTHASHSET_FILE_SIGNATURE 0x4d544853 // 'MTHS'
  27. #define MTHASHSET_FILE_NAME "index.mth"
  28. // Store file into media cache (unless it exists already)
  29. // Validating the hash is responsibility of the caller
  30. bool clientMediaUpdateCache(const std::string &raw_hash,
  31. const std::string &filedata);
  32. class ClientMediaDownloader
  33. {
  34. public:
  35. ClientMediaDownloader();
  36. ~ClientMediaDownloader();
  37. float getProgress() const {
  38. if (m_uncached_count >= 1)
  39. return 1.0f * m_uncached_received_count /
  40. m_uncached_count;
  41. return 0.0f;
  42. }
  43. bool isStarted() const {
  44. return m_initial_step_done;
  45. }
  46. // If this returns true, the downloader is done and can be deleted
  47. bool isDone() const {
  48. return m_initial_step_done &&
  49. m_uncached_received_count == m_uncached_count;
  50. }
  51. // Add a file to the list of required file (but don't fetch it yet)
  52. void addFile(const std::string &name, const std::string &sha1);
  53. // Add a remote server to the list; ignored if not built with cURL
  54. void addRemoteServer(const std::string &baseurl);
  55. // Steps the media downloader:
  56. // - May load media into client by calling client->loadMedia()
  57. // - May check media cache for files
  58. // - May add files to media cache
  59. // - May start remote transfers by calling httpfetch_async
  60. // - May check for completion of current remote transfers
  61. // - May start conventional transfers by calling client->request_media()
  62. // - May inform server that all media has been loaded
  63. // by calling client->received_media()
  64. // After step has been called once, don't call addFile/addRemoteServer.
  65. void step(Client *client);
  66. // Must be called for each file received through TOCLIENT_MEDIA
  67. void conventionalTransferDone(
  68. const std::string &name,
  69. const std::string &data,
  70. Client *client);
  71. private:
  72. struct FileStatus {
  73. bool received;
  74. std::string sha1;
  75. s32 current_remote;
  76. std::vector<s32> available_remotes;
  77. };
  78. struct RemoteServerStatus {
  79. std::string baseurl;
  80. s32 active_count;
  81. };
  82. void initialStep(Client *client);
  83. void remoteHashSetReceived(const HTTPFetchResult &fetch_result);
  84. void remoteMediaReceived(const HTTPFetchResult &fetch_result,
  85. Client *client);
  86. s32 selectRemoteServer(FileStatus *filestatus);
  87. void startRemoteMediaTransfers();
  88. void startConventionalTransfers(Client *client);
  89. bool checkAndLoad(const std::string &name, const std::string &sha1,
  90. const std::string &data, bool is_from_cache,
  91. Client *client);
  92. std::string serializeRequiredHashSet();
  93. static void deSerializeHashSet(const std::string &data,
  94. std::set<std::string> &result);
  95. // Maps filename to file status
  96. std::map<std::string, FileStatus*> m_files;
  97. // Array of remote media servers
  98. std::vector<RemoteServerStatus*> m_remotes;
  99. // Filesystem-based media cache
  100. FileCache m_media_cache;
  101. // Has an attempt been made to load media files from the file cache?
  102. // Have hash sets been requested from remote servers?
  103. bool m_initial_step_done = false;
  104. // Total number of media files to load
  105. s32 m_uncached_count = 0;
  106. // Number of media files that have been received
  107. s32 m_uncached_received_count = 0;
  108. // Status of remote transfers
  109. unsigned long m_httpfetch_caller;
  110. unsigned long m_httpfetch_next_id = 0;
  111. long m_httpfetch_timeout = 0;
  112. s32 m_httpfetch_active = 0;
  113. s32 m_httpfetch_active_limit = 0;
  114. s32 m_outstanding_hash_sets = 0;
  115. std::unordered_map<unsigned long, std::string> m_remote_file_transfers;
  116. // All files up to this name have either been received from a
  117. // remote server or failed on all remote servers, so those files
  118. // don't need to be looked at again
  119. // (use m_files.upper_bound(m_name_bound) to get an iterator)
  120. std::string m_name_bound = "";
  121. };