]> git.lizzy.rs Git - dragonfireclient.git/blob - src/clientiface.h
Android: Modify touch screen GUI's buttons (#7240)
[dragonfireclient.git] / src / clientiface.h
1 /*
2 Minetest
3 Copyright (C) 2010-2014 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 #include "irr_v3d.h"                   // for irrlicht datatypes
23
24 #include "constants.h"
25 #include "serialization.h"             // for SER_FMT_VER_INVALID
26 #include "network/networkpacket.h"
27 #include "network/networkprotocol.h"
28 #include "porting.h"
29
30 #include <list>
31 #include <vector>
32 #include <set>
33 #include <mutex>
34
35 class MapBlock;
36 class ServerEnvironment;
37 class EmergeManager;
38
39 /*
40  * State Transitions
41
42       Start
43   (peer connect)
44         |
45         v
46       /-----------------\
47       |                 |
48       |    Created      |
49       |                 |
50       \-----------------/
51                |                  depending of the incoming packet
52                ----------------------------------------
53                                                       v
54                                        +-----------------------------+
55                                        |IN:                          |
56                                        | TOSERVER_INIT               |
57                                        +-----------------------------+
58                                                       | invalid playername
59                                                       | or denied by mod
60                                                       v
61                                        +-----------------------------+
62                                        |OUT:                         |
63                                        | TOCLIENT_HELLO              |
64                                        +-----------------------------+
65                                                       |
66                                                       |
67                                                       v
68       /-----------------\                    /-----------------\
69       |                 |                    |                 |
70       |  AwaitingInit2  |<---------          |    HelloSent    |
71       |                 |         |          |                 |
72       \-----------------/         |          \-----------------/
73                |                  |                   |
74 +-----------------------------+   |    *-----------------------------*     Auth fails
75 |IN:                          |   |    |Authentication, depending on |------------------
76 | TOSERVER_INIT2              |   |    | packet sent by client       |                 |
77 +-----------------------------+   |    *-----------------------------*                 |
78                |                  |                   |                                |
79                |                  |                   | Authentication                 |
80                v                  |                   |  successful                    |
81       /-----------------\         |                   v                                |
82       |                 |         |    +-----------------------------+                 |
83       |    InitDone     |         |    |OUT:                         |                 |
84       |                 |         |    | TOCLIENT_AUTH_ACCEPT        |                 |
85       \-----------------/         |    +-----------------------------+                 |
86                |                  |                   |                                |
87 +-----------------------------+   ---------------------                                |
88 |OUT:                         |                                                        |
89 | TOCLIENT_MOVEMENT           |                                                        |
90 | TOCLIENT_ITEMDEF            |                                                        |
91 | TOCLIENT_NODEDEF            |                                                        |
92 | TOCLIENT_ANNOUNCE_MEDIA     |                                                        |
93 | TOCLIENT_DETACHED_INVENTORY |                                                        |
94 | TOCLIENT_TIME_OF_DAY        |                                                        |
95 +-----------------------------+                                                        |
96                |                                                                       |
97                |                                                                       |
98                |      -----------------------------                                    |
99                v      |                           |                                    |
100       /-----------------\                         v                                    |
101       |                 |             +-----------------------------+                  |
102       | DefinitionsSent |             |IN:                          |                  |
103       |                 |             | TOSERVER_REQUEST_MEDIA      |                  |
104       \-----------------/             |                             |                  |
105                |                      +-----------------------------+                  |
106                |      ^                           |                                    |
107                |      -----------------------------                                    |
108                v                                                                       v
109 +-----------------------------+                        --------------------------------+
110 |IN:                          |                        |                               ^
111 | TOSERVER_CLIENT_READY       |                        v                               |
112 +-----------------------------+            +------------------------+                  |
113                |                           |OUT:                    |                  |
114                v                           | TOCLIENT_ACCESS_DENIED |                  |
115 +-----------------------------+            +------------------------+                  |
116 |OUT:                         |                        |                               |
117 | TOCLIENT_MOVE_PLAYER        |                        v                               |
118 | TOCLIENT_PRIVILEGES         |                /-----------------\                     |
119 | TOCLIENT_INVENTORY_FORMSPEC |                |                 |                     |
120 | UpdateCrafting              |                |     Denied      |                     |
121 | TOCLIENT_INVENTORY          |                |                 |                     |
122 | TOCLIENT_HP (opt)           |                \-----------------/                     |
123 | TOCLIENT_BREATH             |                                                        |
124 | TOCLIENT_DEATHSCREEN        |                                                        |
125 +-----------------------------+                                                        |
126               |                                                                        |
127               v                                                                        |
128       /-----------------\      async mod action (ban, kick)                            |
129       |                 |---------------------------------------------------------------
130  ---->|     Active      |
131  |    |                 |----------------------------------------------
132  |    \-----------------/      timeout                                v
133  |       |           |                                  +-----------------------------+
134  |       |           |                                  |OUT:                         |
135  |       |           |                                  | TOCLIENT_DISCONNECT         |
136  |       |           |                                  +-----------------------------+
137  |       |           |                                                |
138  |       |           v                                                v
139  |       |  +-----------------------------+                    /-----------------\
140  |       |  |IN:                          |                    |                 |
141  |       |  | TOSERVER_DISCONNECT         |------------------->|  Disconnecting  |
142  |       |  +-----------------------------+                    |                 |
143  |       |                                                     \-----------------/
144  |       | any auth packet which was
145  |       | allowed in TOCLIENT_AUTH_ACCEPT
146  |       v
147  |    *-----------------------------* Auth      +-------------------------------+
148  |    |Authentication, depending on | succeeds  |OUT:                           |
149  |    | packet sent by client       |---------->| TOCLIENT_ACCEPT_SUDO_MODE     |
150  |    *-----------------------------*           +-------------------------------+
151  |                  |                                            |
152  |                  | Auth fails                        /-----------------\
153  |                  v                                   |                 |
154  |    +-------------------------------+                 |    SudoMode     |
155  |    |OUT:                           |                 |                 |
156  |    | TOCLIENT_DENY_SUDO_MODE       |                 \-----------------/
157  |    +-------------------------------+                          |
158  |                  |                                            v
159  |                  |                               +-----------------------------+
160  |                  |    sets password accordingly  |IN:                          |
161  -------------------+-------------------------------| TOSERVER_FIRST_SRP          |
162                                                     +-----------------------------+
163
164 */
165 namespace con {
166         class Connection;
167 }
168
169
170 // Also make sure to update the ClientInterface::statenames
171 // array when modifying these enums
172
173 enum ClientState
174 {
175         CS_Invalid,
176         CS_Disconnecting,
177         CS_Denied,
178         CS_Created,
179         CS_AwaitingInit2,
180         CS_HelloSent,
181         CS_InitDone,
182         CS_DefinitionsSent,
183         CS_Active,
184         CS_SudoMode
185 };
186
187 enum ClientStateEvent
188 {
189         CSE_Hello,
190         CSE_AuthAccept,
191         CSE_InitLegacy,
192         CSE_GotInit2,
193         CSE_SetDenied,
194         CSE_SetDefinitionsSent,
195         CSE_SetClientReady,
196         CSE_SudoSuccess,
197         CSE_SudoLeave,
198         CSE_Disconnect
199 };
200
201 /*
202         Used for queueing and sorting block transfers in containers
203
204         Lower priority number means higher priority.
205 */
206 struct PrioritySortedBlockTransfer
207 {
208         PrioritySortedBlockTransfer(float a_priority, const v3s16 &a_pos, session_t a_peer_id)
209         {
210                 priority = a_priority;
211                 pos = a_pos;
212                 peer_id = a_peer_id;
213         }
214         bool operator < (const PrioritySortedBlockTransfer &other) const
215         {
216                 return priority < other.priority;
217         }
218         float priority;
219         v3s16 pos;
220         session_t peer_id;
221 };
222
223 class RemoteClient
224 {
225 public:
226         // peer_id=0 means this client has no associated peer
227         // NOTE: If client is made allowed to exist while peer doesn't,
228         //       this has to be set to 0 when there is no peer.
229         //       Also, the client must be moved to some other container.
230         session_t peer_id = PEER_ID_INEXISTENT;
231         // The serialization version to use with the client
232         u8 serialization_version = SER_FMT_VER_INVALID;
233         //
234         u16 net_proto_version = 0;
235
236         /* Authentication information */
237         std::string enc_pwd = "";
238         bool create_player_on_auth_success = false;
239         AuthMechanism chosen_mech  = AUTH_MECHANISM_NONE;
240         void *auth_data = nullptr;
241         u32 allowed_auth_mechs = 0;
242         u32 allowed_sudo_mechs = 0;
243
244         bool isSudoMechAllowed(AuthMechanism mech)
245         { return allowed_sudo_mechs & mech; }
246         bool isMechAllowed(AuthMechanism mech)
247         { return allowed_auth_mechs & mech; }
248
249         RemoteClient();
250         ~RemoteClient() = default;
251
252         /*
253                 Finds block that should be sent next to the client.
254                 Environment should be locked when this is called.
255                 dtime is used for resetting send radius at slow interval
256         */
257         void GetNextBlocks(ServerEnvironment *env, EmergeManager* emerge,
258                         float dtime, std::vector<PrioritySortedBlockTransfer> &dest);
259
260         void GotBlock(v3s16 p);
261
262         void SentBlock(v3s16 p);
263
264         void SetBlockNotSent(v3s16 p);
265         void SetBlocksNotSent(std::map<v3s16, MapBlock*> &blocks);
266
267         /**
268          * tell client about this block being modified right now.
269          * this information is required to requeue the block in case it's "on wire"
270          * while modification is processed by server
271          * @param p position of modified block
272          */
273         void ResendBlockIfOnWire(v3s16 p);
274
275         u32 getSendingCount() const { return m_blocks_sending.size(); }
276
277         // Increments timeouts and removes timed-out blocks from list
278         // NOTE: This doesn't fix the server-not-sending-block bug
279         //       because it is related to emerging, not sending.
280         //void RunSendingTimeouts(float dtime, float timeout);
281
282         void PrintInfo(std::ostream &o)
283         {
284                 o<<"RemoteClient "<<peer_id<<": "
285                                 <<"m_blocks_sent.size()="<<m_blocks_sent.size()
286                                 <<", m_blocks_sending.size()="<<m_blocks_sending.size()
287                                 <<", m_nearest_unsent_d="<<m_nearest_unsent_d
288                                 <<", m_excess_gotblocks="<<m_excess_gotblocks
289                                 <<std::endl;
290                 m_excess_gotblocks = 0;
291         }
292
293         // Time from last placing or removing blocks
294         float m_time_from_building = 9999;
295
296         /*
297                 List of active objects that the client knows of.
298         */
299         std::set<u16> m_known_objects;
300
301         ClientState getState() const { return m_state; }
302
303         std::string getName() const { return m_name; }
304
305         void setName(const std::string &name) { m_name = name; }
306
307         /* update internal client state */
308         void notifyEvent(ClientStateEvent event);
309
310         /* set expected serialization version */
311         void setPendingSerializationVersion(u8 version)
312                 { m_pending_serialization_version = version; }
313
314         void setDeployedCompressionMode(u16 byteFlag)
315                 { m_deployed_compression = byteFlag; }
316
317         void confirmSerializationVersion()
318                 { serialization_version = m_pending_serialization_version; }
319
320         /* get uptime */
321         u64 uptime() const;
322
323         /* set version information */
324         void setVersionInfo(u8 major, u8 minor, u8 patch, const std::string &full)
325         {
326                 m_version_major = major;
327                 m_version_minor = minor;
328                 m_version_patch = patch;
329                 m_full_version = full;
330         }
331
332         /* read version information */
333         u8 getMajor() const { return m_version_major; }
334         u8 getMinor() const { return m_version_minor; }
335         u8 getPatch() const { return m_version_patch; }
336 private:
337         // Version is stored in here after INIT before INIT2
338         u8 m_pending_serialization_version = SER_FMT_VER_INVALID;
339
340         /* current state of client */
341         ClientState m_state = CS_Created;
342
343         /*
344                 Blocks that have been sent to client.
345                 - These don't have to be sent again.
346                 - A block is cleared from here when client says it has
347                   deleted it from it's memory
348
349                 List of block positions.
350                 No MapBlock* is stored here because the blocks can get deleted.
351         */
352         std::set<v3s16> m_blocks_sent;
353         s16 m_nearest_unsent_d = 0;
354         v3s16 m_last_center;
355         float m_nearest_unsent_reset_timer = 0.0f;
356
357         const u16 m_max_simul_sends;
358         const float m_min_time_from_building;
359         const s16 m_max_send_distance;
360         const s16 m_block_optimize_distance;
361         const s16 m_max_gen_distance;
362         const bool m_occ_cull;
363
364         /*
365                 Blocks that are currently on the line.
366                 This is used for throttling the sending of blocks.
367                 - The size of this list is limited to some value
368                 Block is added when it is sent with BLOCKDATA.
369                 Block is removed when GOTBLOCKS is received.
370                 Value is time from sending. (not used at the moment)
371         */
372         std::map<v3s16, float> m_blocks_sending;
373
374         /*
375                 Blocks that have been modified since last sending them.
376                 These blocks will not be marked as sent, even if the
377                 client reports it has received them to account for blocks
378                 that are being modified while on the line.
379
380                 List of block positions.
381         */
382         std::set<v3s16> m_blocks_modified;
383
384         /*
385                 Count of excess GotBlocks().
386                 There is an excess amount because the client sometimes
387                 gets a block so late that the server sends it again,
388                 and the client then sends two GOTBLOCKs.
389                 This is resetted by PrintInfo()
390         */
391         u32 m_excess_gotblocks = 0;
392
393         // CPU usage optimization
394         float m_nothing_to_send_pause_timer = 0.0f;
395
396         /*
397                 name of player using this client
398         */
399         std::string m_name = "";
400
401         /*
402                 client information
403          */
404         u8 m_version_major = 0;
405         u8 m_version_minor = 0;
406         u8 m_version_patch = 0;
407
408         std::string m_full_version = "unknown";
409
410         u16 m_deployed_compression = 0;
411
412         /*
413                 time this client was created
414          */
415         const u64 m_connection_time = porting::getTimeS();
416 };
417
418 typedef std::unordered_map<u16, RemoteClient*> RemoteClientMap;
419
420 class ClientInterface {
421 public:
422
423         friend class Server;
424
425         ClientInterface(const std::shared_ptr<con::Connection> &con);
426         ~ClientInterface();
427
428         /* run sync step */
429         void step(float dtime);
430
431         /* get list of active client id's */
432         std::vector<session_t> getClientIDs(ClientState min_state=CS_Active);
433
434         /* mark block as not sent to active client sessions */
435         void markBlockposAsNotSent(const v3s16 &pos);
436
437         /* verify is server user limit was reached */
438         bool isUserLimitReached();
439
440         /* get list of client player names */
441         const std::vector<std::string> &getPlayerNames() const { return m_clients_names; }
442
443         /* send message to client */
444         void send(session_t peer_id, u8 channelnum, NetworkPacket *pkt, bool reliable);
445
446         /* send to all clients */
447         void sendToAll(NetworkPacket *pkt);
448         void sendToAllCompat(NetworkPacket *pkt, NetworkPacket *legacypkt, u16 min_proto_ver);
449
450         /* delete a client */
451         void DeleteClient(session_t peer_id);
452
453         /* create client */
454         void CreateClient(session_t peer_id);
455
456         /* get a client by peer_id */
457         RemoteClient *getClientNoEx(session_t peer_id,  ClientState state_min = CS_Active);
458
459         /* get client by peer_id (make sure you have list lock before!*/
460         RemoteClient *lockedGetClientNoEx(session_t peer_id,  ClientState state_min = CS_Active);
461
462         /* get state of client by id*/
463         ClientState getClientState(session_t peer_id);
464
465         /* set client playername */
466         void setPlayerName(session_t peer_id, const std::string &name);
467
468         /* get protocol version of client */
469         u16 getProtocolVersion(session_t peer_id);
470
471         /* set client version */
472         void setClientVersion(session_t peer_id, u8 major, u8 minor, u8 patch,
473                         const std::string &full);
474
475         /* event to update client state */
476         void event(session_t peer_id, ClientStateEvent event);
477
478         /* Set environment. Do not call this function if environment is already set */
479         void setEnv(ServerEnvironment *env)
480         {
481                 assert(m_env == NULL); // pre-condition
482                 m_env = env;
483         }
484
485         static std::string state2Name(ClientState state);
486 protected:
487         //TODO find way to avoid this functions
488         void lock() { m_clients_mutex.lock(); }
489         void unlock() { m_clients_mutex.unlock(); }
490
491         RemoteClientMap& getClientList() { return m_clients; }
492
493 private:
494         /* update internal player list */
495         void UpdatePlayerList();
496
497         // Connection
498         std::shared_ptr<con::Connection> m_con;
499         std::mutex m_clients_mutex;
500         // Connected clients (behind the con mutex)
501         RemoteClientMap m_clients;
502         std::vector<std::string> m_clients_names; //for announcing masterserver
503
504         // Environment
505         ServerEnvironment *m_env;
506
507         float m_print_info_timer;
508
509         static const char *statenames[];
510 };