X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fclientmedia.cpp;h=97931ee68fed4bb02ff8dafe44a5acfb92ef31f6;hb=0a83c42dfdf390cef75758096a481cdf8e2c828b;hp=b3e7e8e0664b1a8768bf85bb5fe5d5733bd0dcea;hpb=c03d7dc8a7c35708a39f9c14e2df243e212b283b;p=minetest.git diff --git a/src/clientmedia.cpp b/src/clientmedia.cpp index b3e7e8e06..97931ee68 100644 --- a/src/clientmedia.cpp +++ b/src/clientmedia.cpp @@ -20,21 +20,19 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "clientmedia.h" #include "httpfetch.h" #include "client.h" -#include "clientserver.h" #include "filecache.h" -#include "hex.h" -#include "sha1.h" -#include "debug.h" +#include "filesys.h" #include "log.h" #include "porting.h" #include "settings.h" -#include "main.h" +#include "util/hex.h" #include "util/serialize.h" +#include "util/sha1.h" #include "util/string.h" static std::string getMediaCacheDir() { - return porting::path_user + DIR_DELIM + "cache" + DIR_DELIM + "media"; + return porting::path_cache + DIR_DELIM + "media"; } /* @@ -42,18 +40,9 @@ static std::string getMediaCacheDir() */ ClientMediaDownloader::ClientMediaDownloader(): - m_media_cache(getMediaCacheDir()) + m_media_cache(getMediaCacheDir()), + m_httpfetch_caller(HTTPFETCH_DISCARD) { - m_initial_step_done = false; - m_name_bound = ""; // works because "" is an invalid file name - m_uncached_count = 0; - m_uncached_received_count = 0; - m_httpfetch_caller = HTTPFETCH_DISCARD; - m_httpfetch_active = 0; - m_httpfetch_active_limit = 0; - m_httpfetch_next_id = 0; - m_httpfetch_timeout = 0; - m_outstanding_hash_sets = 0; } ClientMediaDownloader::~ClientMediaDownloader() @@ -61,17 +50,16 @@ ClientMediaDownloader::~ClientMediaDownloader() if (m_httpfetch_caller != HTTPFETCH_DISCARD) httpfetch_caller_free(m_httpfetch_caller); - for (std::map::iterator it = m_files.begin(); - it != m_files.end(); ++it) - delete it->second; + for (auto &file_it : m_files) + delete file_it.second; - for (u32 i = 0; i < m_remotes.size(); ++i) - delete m_remotes[i]; + for (auto &remote : m_remotes) + delete remote; } -void ClientMediaDownloader::addFile(std::string name, std::string sha1) +void ClientMediaDownloader::addFile(const std::string &name, const std::string &sha1) { - assert(!m_initial_step_done); + assert(!m_initial_step_done); // pre-condition // if name was already announced, ignore the new announcement if (m_files.count(name) != 0) { @@ -97,16 +85,16 @@ void ClientMediaDownloader::addFile(std::string name, std::string sha1) return; } - FileStatus *filestatus = new FileStatus; + FileStatus *filestatus = new FileStatus(); filestatus->received = false; filestatus->sha1 = sha1; filestatus->current_remote = -1; m_files.insert(std::make_pair(name, filestatus)); } -void ClientMediaDownloader::addRemoteServer(std::string baseurl) +void ClientMediaDownloader::addRemoteServer(const std::string &baseurl) { - assert(!m_initial_step_done); + assert(!m_initial_step_done); // pre-condition #ifdef USE_CURL @@ -114,7 +102,7 @@ void ClientMediaDownloader::addRemoteServer(std::string baseurl) infostream << "Client: Adding remote server \"" << baseurl << "\" for media download" << std::endl; - RemoteServerStatus *remote = new RemoteServerStatus; + RemoteServerStatus *remote = new RemoteServerStatus(); remote->baseurl = baseurl; remote->active_count = 0; remote->request_by_filename = false; @@ -140,17 +128,17 @@ void ClientMediaDownloader::step(Client *client) // Remote media: check for completion of fetches if (m_httpfetch_active) { bool fetched_something = false; - HTTPFetchResult fetchresult; + HTTPFetchResult fetch_result; - while (httpfetch_async_get(m_httpfetch_caller, fetchresult)) { + while (httpfetch_async_get(m_httpfetch_caller, fetch_result)) { m_httpfetch_active--; fetched_something = true; // Is this a hashset (index.mth) or a media file? - if (fetchresult.request_id < m_remotes.size()) - remoteHashSetReceived(fetchresult); + if (fetch_result.request_id < m_remotes.size()) + remoteHashSetReceived(fetch_result); else - remoteMediaReceived(fetchresult, client); + remoteMediaReceived(fetch_result, client); } if (fetched_something) @@ -175,11 +163,9 @@ void ClientMediaDownloader::initialStep(Client *client) { // Check media cache m_uncached_count = m_files.size(); - for (std::map::iterator - it = m_files.begin(); - it != m_files.end(); ++it) { - std::string name = it->first; - FileStatus *filestatus = it->second; + for (auto &file_it : m_files) { + std::string name = file_it.first; + FileStatus *filestatus = file_it.second; const std::string &sha1 = filestatus->sha1; std::ostringstream tmp_os(std::ios_base::binary); @@ -259,17 +245,17 @@ void ClientMediaDownloader::initialStep(Client *client) actionstream << "Client: Contacting remote server \"" << remote->baseurl << "\"" << std::endl; - HTTPFetchRequest fetchrequest; - fetchrequest.url = + HTTPFetchRequest fetch_request; + fetch_request.url = remote->baseurl + MTHASHSET_FILE_NAME; - fetchrequest.caller = m_httpfetch_caller; - fetchrequest.request_id = m_httpfetch_next_id; // == i - fetchrequest.timeout = m_httpfetch_timeout; - fetchrequest.connect_timeout = m_httpfetch_timeout; - fetchrequest.post_fields = required_hash_set; - fetchrequest.extra_headers.push_back( + fetch_request.caller = m_httpfetch_caller; + fetch_request.request_id = m_httpfetch_next_id; // == i + fetch_request.timeout = m_httpfetch_timeout; + fetch_request.connect_timeout = m_httpfetch_timeout; + fetch_request.post_data = required_hash_set; + fetch_request.extra_headers.emplace_back( "Content-Type: application/octet-stream"); - httpfetch_async(fetchrequest); + httpfetch_async(fetch_request); m_httpfetch_active++; m_httpfetch_next_id++; @@ -279,21 +265,21 @@ void ClientMediaDownloader::initialStep(Client *client) } void ClientMediaDownloader::remoteHashSetReceived( - const HTTPFetchResult &fetchresult) + const HTTPFetchResult &fetch_result) { - u32 remote_id = fetchresult.request_id; + u32 remote_id = fetch_result.request_id; assert(remote_id < m_remotes.size()); RemoteServerStatus *remote = m_remotes[remote_id]; m_outstanding_hash_sets--; - if (fetchresult.succeeded) { + if (fetch_result.succeeded) { try { // Server sent a list of file hashes that are // available on it, try to parse the list std::set sha1_set; - deSerializeHashSet(fetchresult.data, sha1_set); + deSerializeHashSet(fetch_result.data, sha1_set); // Parsing succeeded: For every file that is // available on this server, add this server @@ -320,7 +306,7 @@ void ClientMediaDownloader::remoteHashSetReceived( // Do NOT check for any particular response code (e.g. 404) here, // because different servers respond differently - if (!fetchresult.succeeded && !fetchresult.timeout) { + if (!fetch_result.succeeded && !fetch_result.timeout) { infostream << "Client: Enabling compatibility mode for remote " << "server \"" << remote->baseurl << "\"" << std::endl; remote->request_by_filename = true; @@ -338,7 +324,7 @@ void ClientMediaDownloader::remoteHashSetReceived( } void ClientMediaDownloader::remoteMediaReceived( - const HTTPFetchResult &fetchresult, + const HTTPFetchResult &fetch_result, Client *client) { // Some remote server sent us a file. @@ -348,18 +334,18 @@ void ClientMediaDownloader::remoteMediaReceived( std::string name; { - std::map::iterator it = - m_remote_file_transfers.find(fetchresult.request_id); + std::unordered_map::iterator it = + m_remote_file_transfers.find(fetch_result.request_id); assert(it != m_remote_file_transfers.end()); name = it->second; m_remote_file_transfers.erase(it); } - assert(m_files.count(name) != 0); + sanity_check(m_files.count(name) != 0); FileStatus *filestatus = m_files[name]; - assert(!filestatus->received); - assert(filestatus->current_remote >= 0); + sanity_check(!filestatus->received); + sanity_check(filestatus->current_remote >= 0); RemoteServerStatus *remote = m_remotes[filestatus->current_remote]; @@ -368,9 +354,9 @@ void ClientMediaDownloader::remoteMediaReceived( // If fetch succeeded, try to load media file - if (fetchresult.succeeded) { + if (fetch_result.succeeded) { bool success = checkAndLoad(name, filestatus->sha1, - fetchresult.data, false, client); + fetch_result.data, false, client); if (success) { filestatus->received = true; assert(m_uncached_received_count < m_uncached_count); @@ -381,36 +367,37 @@ void ClientMediaDownloader::remoteMediaReceived( s32 ClientMediaDownloader::selectRemoteServer(FileStatus *filestatus) { + // Pre-conditions assert(filestatus != NULL); assert(!filestatus->received); assert(filestatus->current_remote < 0); if (filestatus->available_remotes.empty()) return -1; - else { - // Of all servers that claim to provide the file (and haven't - // been unsuccessfully tried before), find the one with the - // smallest number of currently active transfers - - s32 best = 0; - s32 best_remote_id = filestatus->available_remotes[best]; - s32 best_active_count = m_remotes[best_remote_id]->active_count; - - for (u32 i = 1; i < filestatus->available_remotes.size(); ++i) { - s32 remote_id = filestatus->available_remotes[i]; - s32 active_count = m_remotes[remote_id]->active_count; - if (active_count < best_active_count) { - best = i; - best_remote_id = remote_id; - best_active_count = active_count; - } + + // Of all servers that claim to provide the file (and haven't + // been unsuccessfully tried before), find the one with the + // smallest number of currently active transfers + + s32 best = 0; + s32 best_remote_id = filestatus->available_remotes[best]; + s32 best_active_count = m_remotes[best_remote_id]->active_count; + + for (u32 i = 1; i < filestatus->available_remotes.size(); ++i) { + s32 remote_id = filestatus->available_remotes[i]; + s32 active_count = m_remotes[remote_id]->active_count; + if (active_count < best_active_count) { + best = i; + best_remote_id = remote_id; + best_active_count = active_count; } + } - filestatus->available_remotes.erase( - filestatus->available_remotes.begin() + best); + filestatus->available_remotes.erase( + filestatus->available_remotes.begin() + best); + + return best_remote_id; - return best_remote_id; - } } void ClientMediaDownloader::startRemoteMediaTransfers() @@ -445,14 +432,14 @@ void ClientMediaDownloader::startRemoteMediaTransfers() << "\"" << name << "\" " << "\"" << url << "\"" << std::endl; - HTTPFetchRequest fetchrequest; - fetchrequest.url = url; - fetchrequest.caller = m_httpfetch_caller; - fetchrequest.request_id = m_httpfetch_next_id; - fetchrequest.timeout = 0; // no data timeout! - fetchrequest.connect_timeout = + HTTPFetchRequest fetch_request; + fetch_request.url = url; + fetch_request.caller = m_httpfetch_caller; + fetch_request.request_id = m_httpfetch_next_id; + fetch_request.timeout = 0; // no data timeout! + fetch_request.connect_timeout = m_httpfetch_timeout; - httpfetch_async(fetchrequest); + httpfetch_async(fetch_request); m_remote_file_transfers.insert(std::make_pair( m_httpfetch_next_id, @@ -482,17 +469,15 @@ void ClientMediaDownloader::startRemoteMediaTransfers() void ClientMediaDownloader::startConventionalTransfers(Client *client) { - assert(m_httpfetch_active == 0); + assert(m_httpfetch_active == 0); // pre-condition if (m_uncached_received_count != m_uncached_count) { // Some media files have not been received yet, use the // conventional slow method (minetest protocol) to get them - std::list file_requests; - for (std::map::iterator - it = m_files.begin(); - it != m_files.end(); ++it) { - if (!it->second->received) - file_requests.push_back(it->first); + std::vector file_requests; + for (auto &file : m_files) { + if (!file.second->received) + file_requests.push_back(file.first); } assert((s32) file_requests.size() == m_uncached_count - m_uncached_received_count); @@ -615,7 +600,7 @@ std::string ClientMediaDownloader::serializeRequiredHashSet() it = m_files.begin(); it != m_files.end(); ++it) { if (!it->second->received) { - assert(it->second->sha1.size() == 20); + FATAL_ERROR_IF(it->second->sha1.size() != 20, "Invalid SHA1 size"); os << it->second->sha1; } }