Luanti 5.17.0-dev
Loading...
Searching...
No Matches
clientmedia.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include "irrlichttypes.h"
8#include "filecache.h"
9#include "util/basic_macros.h"
10#include <map>
11#include <set>
12#include <vector>
13#include <unordered_map>
14
15class Client;
16struct HTTPFetchResult;
17
18#define MTHASHSET_FILE_SIGNATURE 0x4d544853 // 'MTHS'
19#define MTHASHSET_FILE_NAME "index.mth"
20
21// Store file into media cache (unless it exists already)
22// Caller should check the hash.
23// return true if something was updated
24bool clientMediaUpdateCache(const std::string &raw_hash,
25 const std::string &filedata);
26
27// Copy file on disk(!) into media cache (unless it exists already)
28bool clientMediaUpdateCacheCopy(const std::string &raw_hash,
29 const std::string &path);
30
31// more of a base class than an interface but this name was most convenient...
33{
34public:
36
37 virtual bool isStarted() const = 0;
38
39 // If this returns true, the downloader is done and can be deleted
40 virtual bool isDone() const = 0;
41
42 // Add a file to the list of required file (but don't fetch it yet)
43 virtual void addFile(const std::string &name, const std::string &sha1) = 0;
44
45 // Add a remote server to the list; ignored if not built with cURL
46 virtual void addRemoteServer(const std::string &baseurl) = 0;
47
48 // Steps the media downloader:
49 // - May load media into client by calling client->loadMedia()
50 // - May check media cache for files
51 // - May add files to media cache
52 // - May start remote transfers by calling httpfetch_async
53 // - May check for completion of current remote transfers
54 // - May start conventional transfers by calling client->request_media()
55 // - May inform server that all media has been loaded
56 // by calling client->received_media()
57 // After step has been called once, don't call addFile/addRemoteServer.
58 virtual void step(Client *client) = 0;
59
60 // Must be called for each file received through TOCLIENT_MEDIA
61 // returns true if this file belongs to this downloader
62 virtual bool conventionalTransferDone(const std::string &name,
63 const std::string &data, Client *client) = 0;
64
65protected:
67 virtual ~IClientMediaDownloader() = default;
68
69 // Forwards the call to the appropriate Client method
70 virtual bool loadMedia(Client *client, const std::string &data,
71 const std::string &name) = 0;
72
73 bool tryLoadFromCache(const std::string &name, const std::string &sha1,
74 Client *client);
75
76 bool checkAndLoad(const std::string &name, const std::string &sha1,
77 const std::string &data, bool is_from_cache, Client *client);
78
79 // Filesystem-based media cache
82};
83
85{
86public:
89
90 void getProgress(s32 &received, s32 &total, size_t &received_size) const {
92 total = m_uncached_count;
93 received_size = m_received_file_size;
94 }
95
96 bool isStarted() const override {
98 }
99
100 bool isDone() const override {
101 return m_initial_step_done &&
103 }
104
105 void addFile(const std::string &name, const std::string &sha1) override;
106
107 void addRemoteServer(const std::string &baseurl) override;
108
109 void step(Client *client) override;
110
112 const std::string &name,
113 const std::string &data,
114 Client *client) override;
115
116protected:
117 bool loadMedia(Client *client, const std::string &data,
118 const std::string &name) override;
119
120 static std::string makeReferer(Client *client);
121
122private:
123 struct FileStatus {
125 std::string sha1;
127 std::vector<s32> available_remotes;
128 };
129
131 std::string baseurl;
133 };
134
136 void remoteHashSetReceived(const HTTPFetchResult &fetch_result);
137 void remoteMediaReceived(const HTTPFetchResult &fetch_result,
138 Client *client);
139 s32 selectRemoteServer(FileStatus *filestatus);
142
143 static void deSerializeHashSet(const std::string &data,
144 std::set<std::string> &result);
145
146 // Maps filename to file status
147 std::map<std::string, FileStatus*> m_files;
148
149 // Array of remote media servers
150 std::vector<RemoteServerStatus*> m_remotes;
151
152 // Has an attempt been made to load media files from the file cache?
153 // Have hash sets been requested from remote servers?
155
156 // Total number of media files to load
158
159 // Number of media files that have been received
161
162 // Stores the total size of all received files in bytes
164
165 // Status of remote transfers
171 std::unordered_map<u64, std::string> m_remote_file_transfers;
172
173 // All files up to this name have either been received from a
174 // remote server or failed on all remote servers, so those files
175 // don't need to be looked at again
176 // (use m_files.upper_bound(m_name_bound) to get an iterator)
177 std::string m_name_bound = "";
178
179};
180
181// A media downloader that only downloads a single file.
182// It does/doesn't do several things the normal downloader does:
183// - won't fetch hash sets from remote servers
184// - will mark loaded media as coming from file push
185// - writing to file cache is optional
187{
188public:
189 SingleMediaDownloader(bool write_to_cache);
191
192 bool isStarted() const override {
193 return m_stage > STAGE_INIT;
194 }
195
196 bool isDone() const override {
197 return m_stage >= STAGE_DONE;
198 }
199
200 void addFile(const std::string &name, const std::string &sha1) override;
201
202 void addRemoteServer(const std::string &baseurl) override;
203
204 void step(Client *client) override;
205
206 bool conventionalTransferDone(const std::string &name,
207 const std::string &data, Client *client) override;
208
209protected:
210 bool loadMedia(Client *client, const std::string &data,
211 const std::string &name) override;
212
213private:
214 void initialStep(Client *client);
215 void remoteMediaReceived(const HTTPFetchResult &fetch_result, Client *client);
216 void startRemoteMediaTransfer();
217 void startConventionalTransfer(Client *client);
218
219 enum Stage {
221 STAGE_CACHE_CHECKED, // we have tried to load the file from cache
223 };
224
225 // Information about the one file we want to fetch
226 std::string m_file_name;
227 std::string m_file_sha1;
229
230 // Array of remote media servers
231 std::vector<std::string> m_remotes;
232
234
235 // Status of remote transfers
236 unsigned long m_httpfetch_caller;
237 unsigned long m_httpfetch_next_id = 0;
238
239};
#define DISABLE_CLASS_COPY(C)
Definition basic_macros.h:26
s32 m_uncached_received_count
Definition clientmedia.h:160
ClientMediaDownloader()
Definition clientmedia.cpp:47
bool isStarted() const override
Definition clientmedia.h:96
u64 m_httpfetch_caller
Definition clientmedia.h:166
void startConventionalTransfers(Client *client)
Definition clientmedia.cpp:457
std::string m_name_bound
Definition clientmedia.h:177
s32 m_uncached_count
Definition clientmedia.h:157
void startRemoteMediaTransfers()
Definition clientmedia.cpp:393
bool isDone() const override
Definition clientmedia.h:100
u64 m_httpfetch_next_id
Definition clientmedia.h:167
s32 m_outstanding_hash_sets
Definition clientmedia.h:170
size_t m_received_file_size
Definition clientmedia.h:163
s32 m_httpfetch_active
Definition clientmedia.h:168
bool m_initial_step_done
Definition clientmedia.h:154
void remoteHashSetReceived(const HTTPFetchResult &fetch_result)
Definition clientmedia.cpp:280
void initialStep(Client *client)
Definition clientmedia.cpp:180
s32 m_httpfetch_active_limit
Definition clientmedia.h:169
static void deSerializeHashSet(const std::string &data, std::set< std::string > &result)
Definition clientmedia.cpp:593
std::vector< RemoteServerStatus * > m_remotes
Definition clientmedia.h:150
void remoteMediaReceived(const HTTPFetchResult &fetch_result, Client *client)
Definition clientmedia.cpp:316
s32 selectRemoteServer(FileStatus *filestatus)
Definition clientmedia.cpp:358
std::map< std::string, FileStatus * > m_files
Definition clientmedia.h:147
void getProgress(s32 &received, s32 &total, size_t &received_size) const
Definition clientmedia.h:90
std::unordered_map< u64, std::string > m_remote_file_transfers
Definition clientmedia.h:171
Definition client.h:107
Definition filecache.h:13
virtual bool loadMedia(Client *client, const std::string &data, const std::string &name)=0
IClientMediaDownloader()
Definition clientmedia.cpp:518
virtual void addRemoteServer(const std::string &baseurl)=0
bool checkAndLoad(const std::string &name, const std::string &sha1, const std::string &data, bool is_from_cache, Client *client)
Definition clientmedia.cpp:536
virtual bool conventionalTransferDone(const std::string &name, const std::string &data, Client *client)=0
virtual void addFile(const std::string &name, const std::string &sha1)=0
FileCache m_media_cache
Definition clientmedia.h:80
virtual bool isStarted() const =0
virtual void step(Client *client)=0
bool m_write_to_cache
Definition clientmedia.h:81
virtual bool isDone() const =0
bool tryLoadFromCache(const std::string &name, const std::string &sha1, Client *client)
Definition clientmedia.cpp:523
std::string m_file_name
Definition clientmedia.h:226
unsigned long m_httpfetch_caller
Definition clientmedia.h:236
bool isDone() const override
Definition clientmedia.h:196
bool isStarted() const override
Definition clientmedia.h:192
Stage
Definition clientmedia.h:219
@ STAGE_DONE
Definition clientmedia.h:222
@ STAGE_CACHE_CHECKED
Definition clientmedia.h:221
@ STAGE_INIT
Definition clientmedia.h:220
s32 m_current_remote
Definition clientmedia.h:228
std::string m_file_sha1
Definition clientmedia.h:227
enum Stage m_stage
Definition clientmedia.h:233
unsigned long m_httpfetch_next_id
Definition clientmedia.h:237
std::vector< std::string > m_remotes
Definition clientmedia.h:231
SingleMediaDownloader(bool write_to_cache)
Definition clientmedia.cpp:627
bool clientMediaUpdateCacheCopy(const std::string &raw_hash, const std::string &path)
Definition clientmedia.cpp:34
bool clientMediaUpdateCache(const std::string &raw_hash, const std::string &filedata)
Definition clientmedia.cpp:25
Definition activeobjectmgr.cpp:11
Definition clientmedia.h:123
s32 current_remote
Definition clientmedia.h:126
bool received
Definition clientmedia.h:124
std::string sha1
Definition clientmedia.h:125
std::vector< s32 > available_remotes
Definition clientmedia.h:127
Definition clientmedia.h:130
std::string baseurl
Definition clientmedia.h:131
s32 active_count
Definition clientmedia.h:132
Definition httpfetch.h:84