Minetest 5.9.0-dev
 
Loading...
Searching...
No Matches
clientiface.h
Go to the documentation of this file.
1/*
2Minetest
3Copyright (C) 2010-2014 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU Lesser General Public License as published by
7the Free Software Foundation; either version 2.1 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU Lesser General Public License for more details.
14
15You should have received a copy of the GNU Lesser General Public License along
16with this program; if not, write to the Free Software Foundation, Inc.,
1751 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
28#include "network/address.h"
29#include "porting.h"
31#include "clientdynamicinfo.h"
32
33#include <list>
34#include <vector>
35#include <set>
36#include <unordered_set>
37#include <memory>
38#include <mutex>
39
40class MapBlock;
42class EmergeManager;
43
44/*
45 * State Transitions
46
47 Start
48 (peer connect)
49 |
50 v
51 /-----------------\
52 | |
53 | Created |
54 | |
55 \-----------------/
56 | depending of the incoming packet
57 ----------------------------------------
58 v
59 +-----------------------------+
60 |IN: |
61 | TOSERVER_INIT |
62 +-----------------------------+
63 | invalid playername
64 | or denied by mod
65 v
66 +-----------------------------+
67 |OUT: |
68 | TOCLIENT_HELLO |
69 +-----------------------------+
70 |
71 |
72 v
73 /-----------------\ /-----------------\
74 | | | |
75 | AwaitingInit2 |<--------- | HelloSent |
76 | | | | |
77 \-----------------/ | \-----------------/
78 | | |
79+-----------------------------+ | *-----------------------------* Auth fails
80|IN: | | |Authentication, depending on |------------------
81| TOSERVER_INIT2 | | | packet sent by client | |
82+-----------------------------+ | *-----------------------------* |
83 | | | |
84 | | | Authentication |
85 v | | successful |
86 /-----------------\ | v |
87 | | | +-----------------------------+ |
88 | InitDone | | |OUT: | |
89 | | | | TOCLIENT_AUTH_ACCEPT | |
90 \-----------------/ | +-----------------------------+ |
91 | | | |
92+-----------------------------+ --------------------- |
93|OUT: | |
94| TOCLIENT_MOVEMENT | |
95| TOCLIENT_ITEMDEF | |
96| TOCLIENT_NODEDEF | |
97| TOCLIENT_ANNOUNCE_MEDIA | |
98| TOCLIENT_DETACHED_INVENTORY | |
99| TOCLIENT_TIME_OF_DAY | |
100+-----------------------------+ |
101 | |
102 | |
103 | ----------------------------- |
104 v | | |
105 /-----------------\ v |
106 | | +-----------------------------+ |
107 | DefinitionsSent | |IN: | |
108 | | | TOSERVER_REQUEST_MEDIA | |
109 \-----------------/ | | |
110 | +-----------------------------+ |
111 | ^ | |
112 | ----------------------------- |
113 v v
114+-----------------------------+ --------------------------------+
115|IN: | | ^
116| TOSERVER_CLIENT_READY | v |
117+-----------------------------+ +------------------------+ |
118 | |OUT: | |
119 v | TOCLIENT_ACCESS_DENIED | |
120+-----------------------------+ +------------------------+ |
121|OUT: | | |
122| TOCLIENT_MOVE_PLAYER | v |
123| TOCLIENT_PRIVILEGES | /-----------------\ |
124| TOCLIENT_INVENTORY_FORMSPEC | | | |
125| UpdateCrafting | | Denied | |
126| TOCLIENT_INVENTORY | | | |
127| TOCLIENT_HP (opt) | \-----------------/ |
128| TOCLIENT_BREATH | |
129| TOCLIENT_DEATHSCREEN | |
130+-----------------------------+ |
131 | |
132 v |
133 /-----------------\ async mod action (ban, kick) |
134 | |---------------------------------------------------------------
135 ---->| Active |
136 | | |----------------------------------------------
137 | \-----------------/ timeout v
138 | | | +-----------------------------+
139 | | | |OUT: |
140 | | | | TOCLIENT_DISCONNECT |
141 | | | +-----------------------------+
142 | | | |
143 | | v v
144 | | +-----------------------------+ /-----------------\
145 | | |IN: | | |
146 | | | TOSERVER_DISCONNECT |------------------->| Disconnecting |
147 | | +-----------------------------+ | |
148 | | \-----------------/
149 | | any auth packet which was
150 | | allowed in TOCLIENT_AUTH_ACCEPT
151 | v
152 | *-----------------------------* Auth +-------------------------------+
153 | |Authentication, depending on | succeeds |OUT: |
154 | | packet sent by client |---------->| TOCLIENT_ACCEPT_SUDO_MODE |
155 | *-----------------------------* +-------------------------------+
156 | | |
157 | | Auth fails /-----------------\
158 | v | |
159 | +-------------------------------+ | SudoMode |
160 | |OUT: | | |
161 | | TOCLIENT_DENY_SUDO_MODE | \-----------------/
162 | +-------------------------------+ |
163 | | v
164 | | +-----------------------------+
165 | | sets password accordingly |IN: |
166 -------------------+-------------------------------| TOSERVER_FIRST_SRP |
167 +-----------------------------+
168
169*/
170namespace con {
171 class Connection;
172}
173
174
175// Also make sure to update the ClientInterface::statenames
176// array when modifying these enums
177
179{
191
193{
204
205/*
206 Used for queueing and sorting block transfers in containers
207
208 Lower priority number means higher priority.
209*/
211{
212 PrioritySortedBlockTransfer(float a_priority, const v3s16 &a_pos, session_t a_peer_id)
213 {
214 priority = a_priority;
215 pos = a_pos;
216 peer_id = a_peer_id;
217 }
219 {
220 return priority < other.priority;
221 }
222 float priority;
225};
226
228{
229public:
230 // peer_id=0 means this client has no associated peer
231 // NOTE: If client is made allowed to exist while peer doesn't,
232 // this has to be set to 0 when there is no peer.
233 // Also, the client must be moved to some other container.
235 // The serialization version to use with the client
237 //
239
240 /* Authentication information */
241 std::string enc_pwd = "";
244 void *auth_data = nullptr;
246
247 void resetChosenMech();
248
250 { return allowed_auth_mechs & mech; }
251
252 void setEncryptedPassword(const std::string& pwd);
253
254 RemoteClient();
255 ~RemoteClient() = default;
256
257 /*
258 Finds block that should be sent next to the client.
259 Environment should be locked when this is called.
260 dtime is used for resetting send radius at slow interval
261 */
263 float dtime, std::vector<PrioritySortedBlockTransfer> &dest);
264
265 void GotBlock(v3s16 p);
266
267 void SentBlock(v3s16 p);
268
269 void SetBlockNotSent(v3s16 p);
270 void SetBlocksNotSent(const std::vector<v3s16> &blocks);
271
279
280 u32 getSendingCount() const { return m_blocks_sending.size(); }
281
282 bool isBlockSent(v3s16 p) const
283 {
284 return m_blocks_sent.find(p) != m_blocks_sent.end();
285 }
286
287 bool markMediaSent(const std::string &name) {
288 auto insert_result = m_media_sent.emplace(name);
289 return insert_result.second; // true = was inserted
290 }
291
292 void PrintInfo(std::ostream &o)
293 {
294 o<<"RemoteClient "<<peer_id<<": "
295 <<"m_blocks_sent.size()="<<m_blocks_sent.size()
296 <<", m_blocks_sending.size()="<<m_blocks_sending.size()
297 <<", m_nearest_unsent_d="<<m_nearest_unsent_d
298 <<", m_excess_gotblocks="<<m_excess_gotblocks
299 <<std::endl;
301 }
302
303 // Time from last placing or removing blocks
305
306 /*
307 List of active objects that the client knows of.
308 */
309 std::set<u16> m_known_objects;
310
311 ClientState getState() const { return m_state; }
312
313 const std::string &getName() const { return m_name; }
314
315 void setName(const std::string &name) { m_name = name; }
316
317 /* update internal client state */
318 void notifyEvent(ClientStateEvent event);
319
320 /* set expected serialization version */
323
324 void setDeployedCompressionMode(u16 byteFlag)
325 { m_deployed_compression = byteFlag; }
326
329
330 /* get uptime */
331 u64 uptime() const { return porting::getTimeS() - m_connection_time; }
332
333 /* set version information */
334 void setVersionInfo(u8 major, u8 minor, u8 patch, const std::string &full);
335
336 /* read version information */
337 u8 getMajor() const { return m_version_major; }
338 u8 getMinor() const { return m_version_minor; }
339 u8 getPatch() const { return m_version_patch; }
340 const std::string &getFullVer() const { return m_full_version; }
341
342 void setLangCode(const std::string &code);
343 const std::string &getLangCode() const { return m_lang_code; }
344
345 void setCachedAddress(const Address &addr) { m_addr = addr; }
346 const Address &getAddress() const { return m_addr; }
347
348 void setDynamicInfo(const ClientDynamicInfo &info) { m_dynamic_info = info; }
350
351private:
352 // Version is stored in here after INIT before INIT2
354
355 /* current state of client */
357
358 // Cached here so retrieval doesn't have to go to connection API
360
361 // Client-sent language code
362 std::string m_lang_code;
363
364 // Client-sent dynamic info
366
367 /*
368 Blocks that have been sent to client.
369 - These don't have to be sent again.
370 - A block is cleared from here when client says it has
371 deleted it from it's memory
372
373 List of block positions.
374 No MapBlock* is stored here because the blocks can get deleted.
375 */
376 std::unordered_set<v3s16> m_blocks_sent;
377
378 /*
379 Cache of blocks that have been occlusion culled at the current distance.
380 As GetNextBlocks traverses the same distance multiple times, this saves
381 significant CPU time.
382 */
383 std::unordered_set<v3s16> m_blocks_occ;
384
388
395 const bool m_occ_cull;
396
397 /*
398 Set of media files the client has already requested
399 We won't send the same file twice to avoid bandwidth consumption attacks.
400 */
401 std::unordered_set<std::string> m_media_sent;
402
403 /*
404 Blocks that are currently on the line.
405 This is used for throttling the sending of blocks.
406 - The size of this list is limited to some value
407 Block is added when it is sent with BLOCKDATA.
408 Block is removed when GOTBLOCKS is received.
409 Value is time from sending. (not used at the moment)
410 */
411 std::unordered_map<v3s16, float> m_blocks_sending;
412
413 /*
414 Blocks that have been modified since blocks were
415 sent to the client last (getNextBlocks()).
416 This is used to reset the unsent distance, so that
417 modified blocks are resent to the client.
418
419 List of block positions.
420 */
421 std::unordered_set<v3s16> m_blocks_modified;
422
423 /*
424 Count of excess GotBlocks().
425 There is an excess amount because the client sometimes
426 gets a block so late that the server sends it again,
427 and the client then sends two GOTBLOCKs.
428 This is reset by PrintInfo()
429 */
431
432 // CPU usage optimization
434
435 // measure how long it takes the server to send the complete map
437
438 /*
439 name of player using this client
440 */
441 std::string m_name = "";
442
443 /*
444 client information
445 */
449
450 std::string m_full_version = "unknown";
451
453
454 /*
455 time this client was created
456 */
458};
459
460typedef std::unordered_map<u16, RemoteClient*> RemoteClientMap;
461
463public:
464
465 friend class Server;
466
467 ClientInterface(const std::shared_ptr<con::Connection> &con);
469
470 /* run sync step */
471 void step(float dtime);
472
473 /* get list of active client id's */
474 std::vector<session_t> getClientIDs(ClientState min_state=CS_Active);
475
476 /* mark blocks as not sent on all active clients */
477 void markBlocksNotSent(const std::vector<v3s16> &positions);
478
479 /* verify is server user limit was reached */
480 bool isUserLimitReached();
481
482 /* get list of client player names */
483 const std::vector<std::string> &getPlayerNames() const { return m_clients_names; }
484
485 /* send to one client */
486 void send(session_t peer_id, NetworkPacket *pkt);
487
488 /* send to one client, deviating from the standard params */
489 void sendCustom(session_t peer_id, u8 channel, NetworkPacket *pkt, bool reliable);
490
491 /* send to all clients */
492 void sendToAll(NetworkPacket *pkt);
493
494 /* delete a client */
495 void DeleteClient(session_t peer_id);
496
497 /* create client */
498 void CreateClient(session_t peer_id);
499
500 /* get a client by peer_id */
502
503 /* get client by peer_id (make sure you have list lock before!*/
505
506 /* get state of client by id*/
508
509 /* set client playername */
510 void setPlayerName(session_t peer_id, const std::string &name);
511
512 /* get protocol version of client */
513 u16 getProtocolVersion(session_t peer_id);
514
515 /* set client version */
516 void setClientVersion(session_t peer_id, u8 major, u8 minor, u8 patch,
517 const std::string &full);
518
519 /* event to update client state */
520 void event(session_t peer_id, ClientStateEvent event);
521
522 /* Set environment. Do not call this function if environment is already set */
524 {
525 assert(m_env == NULL); // pre-condition
526 m_env = env;
527 }
528
529 static std::string state2Name(ClientState state);
530protected:
531 class AutoLock {
532 public:
534
535 private:
537 };
538
540
541private:
542 /* update internal player list */
543 void UpdatePlayerList();
544
545 // Connection
546 std::shared_ptr<con::Connection> m_con;
547 std::recursive_mutex m_clients_mutex;
548 // Connected clients (behind the con mutex)
550 std::vector<std::string> m_clients_names; //for announcing masterserver
551
552 // Environment
554
557
558 static const char *statenames[];
559
560 static constexpr int LINGER_TIMEOUT = 10;
561};
Definition: address.h:43
Definition: clientiface.h:531
RecursiveMutexAutoLock m_lock
Definition: clientiface.h:536
AutoLock(ClientInterface &iface)
Definition: clientiface.h:533
Definition: clientiface.h:462
float m_check_linger_timer
Definition: clientiface.h:556
RemoteClientMap & getClientList()
Definition: clientiface.h:539
~ClientInterface()
Definition: clientiface.cpp:668
void CreateClient(session_t peer_id)
Definition: clientiface.cpp:893
ClientState getClientState(session_t peer_id)
Definition: clientiface.cpp:841
std::vector< std::string > m_clients_names
Definition: clientiface.h:550
void send(session_t peer_id, NetworkPacket *pkt)
Definition: clientiface.cpp:781
static std::string state2Name(ClientState state)
Definition: clientiface.cpp:63
RemoteClient * getClientNoEx(session_t peer_id, ClientState state_min=CS_Active)
Definition: clientiface.cpp:812
void sendToAll(NetworkPacket *pkt)
Definition: clientiface.cpp:798
void event(session_t peer_id, ClientStateEvent event)
Definition: clientiface.cpp:908
void markBlocksNotSent(const std::vector< v3s16 > &positions)
Definition: clientiface.cpp:696
const std::vector< std::string > & getPlayerNames() const
Definition: clientiface.h:483
std::vector< session_t > getClientIDs(ClientState min_state=CS_Active)
Definition: clientiface.cpp:683
u16 getProtocolVersion(session_t peer_id)
Definition: clientiface.cpp:930
void setEnv(ServerEnvironment *env)
Definition: clientiface.h:523
ServerEnvironment * m_env
Definition: clientiface.h:553
std::recursive_mutex m_clients_mutex
Definition: clientiface.h:547
float m_print_info_timer
Definition: clientiface.h:555
RemoteClient * lockedGetClientNoEx(session_t peer_id, ClientState state_min=CS_Active)
Definition: clientiface.cpp:827
void setClientVersion(session_t peer_id, u8 major, u8 minor, u8 patch, const std::string &full)
Definition: clientiface.cpp:944
std::shared_ptr< con::Connection > m_con
Definition: clientiface.h:546
void DeleteClient(session_t peer_id)
Definition: clientiface.cpp:863
bool isUserLimitReached()
Verify if user limit was reached.
Definition: clientiface.cpp:710
static constexpr int LINGER_TIMEOUT
Definition: clientiface.h:560
void setPlayerName(session_t peer_id, const std::string &name)
Definition: clientiface.cpp:853
void step(float dtime)
Definition: clientiface.cpp:715
void UpdatePlayerList()
Definition: clientiface.cpp:752
void sendCustom(session_t peer_id, u8 channel, NetworkPacket *pkt, bool reliable)
Definition: clientiface.cpp:789
RemoteClientMap m_clients
Definition: clientiface.h:549
static const char * statenames[]
Definition: clientiface.h:558
Definition: emerge.h:130
Definition: mapblock.h:73
Definition: networkpacket.h:27
Definition: clientiface.h:228
void confirmSerializationVersion()
Definition: clientiface.h:327
const Address & getAddress() const
Definition: clientiface.h:346
bool isMechAllowed(AuthMechanism mech)
Definition: clientiface.h:249
u8 getMinor() const
Definition: clientiface.h:338
void setPendingSerializationVersion(u8 version)
Definition: clientiface.h:321
const s16 m_block_optimize_distance
Definition: clientiface.h:392
u8 m_version_major
Definition: clientiface.h:446
void GetNextBlocks(ServerEnvironment *env, EmergeManager *emerge, float dtime, std::vector< PrioritySortedBlockTransfer > &dest)
Definition: clientiface.cpp:107
void setDynamicInfo(const ClientDynamicInfo &info)
Definition: clientiface.h:348
const s16 m_max_gen_distance
Definition: clientiface.h:394
void SetBlockNotSent(v3s16 p)
Definition: clientiface.cpp:460
u8 getMajor() const
Definition: clientiface.h:337
void PrintInfo(std::ostream &o)
Definition: clientiface.h:292
u32 getSendingCount() const
Definition: clientiface.h:280
std::string enc_pwd
Definition: clientiface.h:241
std::string m_full_version
Definition: clientiface.h:450
const s16 m_block_cull_optimize_distance
Definition: clientiface.h:393
ClientState m_state
Definition: clientiface.h:356
void SetBlocksNotSent(const std::vector< v3s16 > &blocks)
Definition: clientiface.cpp:470
ClientDynamicInfo m_dynamic_info
Definition: clientiface.h:365
v3s16 m_last_center
Definition: clientiface.h:386
const std::string & getName() const
Definition: clientiface.h:313
u32 allowed_auth_mechs
Definition: clientiface.h:245
const s16 m_max_send_distance
Definition: clientiface.h:391
std::unordered_set< v3s16 > m_blocks_occ
Definition: clientiface.h:383
void ResendBlockIfOnWire(v3s16 p)
tell client about this block being modified right now.
Definition: clientiface.cpp:80
s16 m_nearest_unsent_d
Definition: clientiface.h:385
void setName(const std::string &name)
Definition: clientiface.h:315
void setCachedAddress(const Address &addr)
Definition: clientiface.h:345
void setDeployedCompressionMode(u16 byteFlag)
Definition: clientiface.h:324
std::string m_lang_code
Definition: clientiface.h:362
void setVersionInfo(u8 major, u8 minor, u8 patch, const std::string &full)
Definition: clientiface.cpp:648
void * auth_data
Definition: clientiface.h:244
bool create_player_on_auth_success
Definition: clientiface.h:242
u8 m_version_minor
Definition: clientiface.h:447
u8 m_version_patch
Definition: clientiface.h:448
const ClientDynamicInfo & getDynamicInfo() const
Definition: clientiface.h:349
float m_map_send_completion_timer
Definition: clientiface.h:436
const float m_min_time_from_building
Definition: clientiface.h:390
u32 m_excess_gotblocks
Definition: clientiface.h:430
bool markMediaSent(const std::string &name)
Definition: clientiface.h:287
RemoteClient()
Definition: clientiface.cpp:68
const u64 m_connection_time
Definition: clientiface.h:457
const std::string & getLangCode() const
Definition: clientiface.h:343
u8 m_pending_serialization_version
Definition: clientiface.h:353
float m_time_from_building
Definition: clientiface.h:304
session_t peer_id
Definition: clientiface.h:234
void SentBlock(v3s16 p)
Definition: clientiface.cpp:451
u16 net_proto_version
Definition: clientiface.h:238
std::unordered_set< v3s16 > m_blocks_modified
Definition: clientiface.h:421
ClientState getState() const
Definition: clientiface.h:311
std::unordered_set< v3s16 > m_blocks_sent
Definition: clientiface.h:376
void resetChosenMech()
Definition: clientiface.cpp:631
void setEncryptedPassword(const std::string &pwd)
Definition: clientiface.cpp:640
~RemoteClient()=default
u8 serialization_version
Definition: clientiface.h:236
const std::string & getFullVer() const
Definition: clientiface.h:340
bool isBlockSent(v3s16 p) const
Definition: clientiface.h:282
std::set< u16 > m_known_objects
Definition: clientiface.h:309
Address m_addr
Definition: clientiface.h:359
v3f m_last_camera_dir
Definition: clientiface.h:387
u8 getPatch() const
Definition: clientiface.h:339
void GotBlock(v3s16 p)
Definition: clientiface.cpp:439
u16 m_deployed_compression
Definition: clientiface.h:452
void setLangCode(const std::string &code)
Definition: clientiface.cpp:656
std::string m_name
Definition: clientiface.h:441
void notifyEvent(ClientStateEvent event)
Definition: clientiface.cpp:482
const u16 m_max_simul_sends
Definition: clientiface.h:389
u64 uptime() const
Definition: clientiface.h:331
std::unordered_map< v3s16, float > m_blocks_sending
Definition: clientiface.h:411
std::unordered_set< std::string > m_media_sent
Definition: clientiface.h:401
const bool m_occ_cull
Definition: clientiface.h:395
AuthMechanism chosen_mech
Definition: clientiface.h:243
float m_nothing_to_send_pause_timer
Definition: clientiface.h:433
Definition: serverenvironment.h:220
Definition: server.h:146
ClientState
Definition: clientiface.h:179
@ CS_HelloSent
Definition: clientiface.h:184
@ CS_Disconnecting
Definition: clientiface.h:181
@ CS_Denied
Definition: clientiface.h:182
@ CS_Active
Definition: clientiface.h:188
@ CS_DefinitionsSent
Definition: clientiface.h:187
@ CS_AwaitingInit2
Definition: clientiface.h:185
@ CS_Created
Definition: clientiface.h:183
@ CS_SudoMode
Definition: clientiface.h:189
@ CS_InitDone
Definition: clientiface.h:186
@ CS_Invalid
Definition: clientiface.h:180
ClientStateEvent
Definition: clientiface.h:193
@ CSE_SetClientReady
Definition: clientiface.h:199
@ CSE_AuthAccept
Definition: clientiface.h:195
@ CSE_SetDefinitionsSent
Definition: clientiface.h:198
@ CSE_Hello
Definition: clientiface.h:194
@ CSE_GotInit2
Definition: clientiface.h:196
@ CSE_SudoLeave
Definition: clientiface.h:201
@ CSE_SudoSuccess
Definition: clientiface.h:200
@ CSE_Disconnect
Definition: clientiface.h:202
@ CSE_SetDenied
Definition: clientiface.h:197
std::unordered_map< u16, RemoteClient * > RemoteClientMap
Definition: clientiface.h:460
#define PEER_ID_INEXISTENT
Definition: constants.h:34
core::vector3d< s16 > v3s16
Definition: irr_v3d.h:28
core::vector3df v3f
Definition: irr_v3d.h:26
std::unique_lock< std::recursive_mutex > RecursiveMutexAutoLock
Definition: mutex_auto_lock.h:30
Definition: client.h:73
u64 getTimeS()
Definition: porting.h:183
AuthMechanism
Definition: networkprotocol.h:1066
@ AUTH_MECHANISM_NONE
Definition: networkprotocol.h:1068
u16 session_t
Definition: networkprotocol.h:251
#define SER_FMT_VER_INVALID
Definition: serialization.h:69
Definition: clientdynamicinfo.h:26
Definition: clientiface.h:211
bool operator<(const PrioritySortedBlockTransfer &other) const
Definition: clientiface.h:218
float priority
Definition: clientiface.h:222
PrioritySortedBlockTransfer(float a_priority, const v3s16 &a_pos, session_t a_peer_id)
Definition: clientiface.h:212
v3s16 pos
Definition: clientiface.h:223
session_t peer_id
Definition: clientiface.h:224
static std::string p(std::string path)
Definition: test_filesys.cpp:64