3 Copyright (C) 2010-2014 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
22 #include "irr_v3d.h" // for irrlicht datatypes
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 "network/address.h"
30 #include "threading/mutex_auto_lock.h"
31 #include "clientdynamicinfo.h"
40 class ServerEnvironment;
55 | depending of the incoming packet
56 ----------------------------------------
58 +-----------------------------+
61 +-----------------------------+
65 +-----------------------------+
68 +-----------------------------+
72 /-----------------\ /-----------------\
74 | AwaitingInit2 |<--------- | HelloSent |
76 \-----------------/ | \-----------------/
78 +-----------------------------+ | *-----------------------------* Auth fails
79 |IN: | | |Authentication, depending on |------------------
80 | TOSERVER_INIT2 | | | packet sent by client | |
81 +-----------------------------+ | *-----------------------------* |
83 | | | Authentication |
85 /-----------------\ | v |
86 | | | +-----------------------------+ |
87 | InitDone | | |OUT: | |
88 | | | | TOCLIENT_AUTH_ACCEPT | |
89 \-----------------/ | +-----------------------------+ |
91 +-----------------------------+ --------------------- |
93 | TOCLIENT_MOVEMENT | |
94 | TOCLIENT_ITEMDEF | |
95 | TOCLIENT_NODEDEF | |
96 | TOCLIENT_ANNOUNCE_MEDIA | |
97 | TOCLIENT_DETACHED_INVENTORY | |
98 | TOCLIENT_TIME_OF_DAY | |
99 +-----------------------------+ |
102 | ----------------------------- |
104 /-----------------\ v |
105 | | +-----------------------------+ |
106 | DefinitionsSent | |IN: | |
107 | | | TOSERVER_REQUEST_MEDIA | |
108 \-----------------/ | | |
109 | +-----------------------------+ |
111 | ----------------------------- |
113 +-----------------------------+ --------------------------------+
115 | TOSERVER_CLIENT_READY | v |
116 +-----------------------------+ +------------------------+ |
118 v | TOCLIENT_ACCESS_DENIED | |
119 +-----------------------------+ +------------------------+ |
121 | TOCLIENT_MOVE_PLAYER | v |
122 | TOCLIENT_PRIVILEGES | /-----------------\ |
123 | TOCLIENT_INVENTORY_FORMSPEC | | | |
124 | UpdateCrafting | | Denied | |
125 | TOCLIENT_INVENTORY | | | |
126 | TOCLIENT_HP (opt) | \-----------------/ |
127 | TOCLIENT_BREATH | |
128 | TOCLIENT_DEATHSCREEN | |
129 +-----------------------------+ |
132 /-----------------\ async mod action (ban, kick) |
133 | |---------------------------------------------------------------
135 | | |----------------------------------------------
136 | \-----------------/ timeout v
137 | | | +-----------------------------+
139 | | | | TOCLIENT_DISCONNECT |
140 | | | +-----------------------------+
143 | | +-----------------------------+ /-----------------\
145 | | | TOSERVER_DISCONNECT |------------------->| Disconnecting |
146 | | +-----------------------------+ | |
147 | | \-----------------/
148 | | any auth packet which was
149 | | allowed in TOCLIENT_AUTH_ACCEPT
151 | *-----------------------------* Auth +-------------------------------+
152 | |Authentication, depending on | succeeds |OUT: |
153 | | packet sent by client |---------->| TOCLIENT_ACCEPT_SUDO_MODE |
154 | *-----------------------------* +-------------------------------+
156 | | Auth fails /-----------------\
158 | +-------------------------------+ | SudoMode |
160 | | TOCLIENT_DENY_SUDO_MODE | \-----------------/
161 | +-------------------------------+ |
163 | | +-----------------------------+
164 | | sets password accordingly |IN: |
165 -------------------+-------------------------------| TOSERVER_FIRST_SRP |
166 +-----------------------------+
174 // Also make sure to update the ClientInterface::statenames
175 // array when modifying these enums
191 enum ClientStateEvent
197 CSE_SetDefinitionsSent,
205 Used for queueing and sorting block transfers in containers
207 Lower priority number means higher priority.
209 struct PrioritySortedBlockTransfer
211 PrioritySortedBlockTransfer(float a_priority, const v3s16 &a_pos, session_t a_peer_id)
213 priority = a_priority;
217 bool operator < (const PrioritySortedBlockTransfer &other) const
219 return priority < other.priority;
229 // peer_id=0 means this client has no associated peer
230 // NOTE: If client is made allowed to exist while peer doesn't,
231 // this has to be set to 0 when there is no peer.
232 // Also, the client must be moved to some other container.
233 session_t peer_id = PEER_ID_INEXISTENT;
234 // The serialization version to use with the client
235 u8 serialization_version = SER_FMT_VER_INVALID;
237 u16 net_proto_version = 0;
239 /* Authentication information */
240 std::string enc_pwd = "";
241 bool create_player_on_auth_success = false;
242 AuthMechanism chosen_mech = AUTH_MECHANISM_NONE;
243 void *auth_data = nullptr;
244 u32 allowed_auth_mechs = 0;
245 u32 allowed_sudo_mechs = 0;
247 void resetChosenMech();
249 bool isSudoMechAllowed(AuthMechanism mech)
250 { return allowed_sudo_mechs & mech; }
251 bool isMechAllowed(AuthMechanism mech)
252 { return allowed_auth_mechs & mech; }
255 ~RemoteClient() = default;
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
262 void GetNextBlocks(ServerEnvironment *env, EmergeManager* emerge,
263 float dtime, std::vector<PrioritySortedBlockTransfer> &dest);
265 void GotBlock(v3s16 p);
267 void SentBlock(v3s16 p);
269 void SetBlockNotSent(v3s16 p);
270 void SetBlocksNotSent(std::map<v3s16, MapBlock*> &blocks);
273 * tell client about this block being modified right now.
274 * this information is required to requeue the block in case it's "on wire"
275 * while modification is processed by server
276 * @param p position of modified block
278 void ResendBlockIfOnWire(v3s16 p);
280 u32 getSendingCount() const { return m_blocks_sending.size(); }
282 bool isBlockSent(v3s16 p) const
284 return m_blocks_sent.find(p) != m_blocks_sent.end();
287 // Increments timeouts and removes timed-out blocks from list
288 // NOTE: This doesn't fix the server-not-sending-block bug
289 // because it is related to emerging, not sending.
290 //void RunSendingTimeouts(float dtime, float timeout);
292 void PrintInfo(std::ostream &o)
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
300 m_excess_gotblocks = 0;
303 // Time from last placing or removing blocks
304 float m_time_from_building = 9999;
307 List of active objects that the client knows of.
309 std::set<u16> m_known_objects;
311 ClientState getState() const { return m_state; }
313 std::string getName() const { return m_name; }
315 void setName(const std::string &name) { m_name = name; }
317 /* update internal client state */
318 void notifyEvent(ClientStateEvent event);
320 /* set expected serialization version */
321 void setPendingSerializationVersion(u8 version)
322 { m_pending_serialization_version = version; }
324 void setDeployedCompressionMode(u16 byteFlag)
325 { m_deployed_compression = byteFlag; }
327 void confirmSerializationVersion()
328 { serialization_version = m_pending_serialization_version; }
333 /* set version information */
334 void setVersionInfo(u8 major, u8 minor, u8 patch, const std::string &full)
336 m_version_major = major;
337 m_version_minor = minor;
338 m_version_patch = patch;
339 m_full_version = full;
342 /* read version information */
343 u8 getMajor() const { return m_version_major; }
344 u8 getMinor() const { return m_version_minor; }
345 u8 getPatch() const { return m_version_patch; }
346 const std::string &getFullVer() const { return m_full_version; }
348 void setLangCode(const std::string &code) { m_lang_code = code; }
349 const std::string &getLangCode() const { return m_lang_code; }
351 void setCachedAddress(const Address &addr) { m_addr = addr; }
352 const Address &getAddress() const { return m_addr; }
354 void setDynamicInfo(const ClientDynamicInfo &info) { m_dynamic_info = info; }
355 const ClientDynamicInfo &getDynamicInfo() const { return m_dynamic_info; }
358 // Version is stored in here after INIT before INIT2
359 u8 m_pending_serialization_version = SER_FMT_VER_INVALID;
361 /* current state of client */
362 ClientState m_state = CS_Created;
364 // Cached here so retrieval doesn't have to go to connection API
367 // Client-sent language code
368 std::string m_lang_code;
370 // Client-sent dynamic info
371 ClientDynamicInfo m_dynamic_info{};
374 Blocks that have been sent to client.
375 - These don't have to be sent again.
376 - A block is cleared from here when client says it has
377 deleted it from it's memory
379 List of block positions.
380 No MapBlock* is stored here because the blocks can get deleted.
382 std::set<v3s16> m_blocks_sent;
383 s16 m_nearest_unsent_d = 0;
385 v3f m_last_camera_dir;
387 const u16 m_max_simul_sends;
388 const float m_min_time_from_building;
389 const s16 m_max_send_distance;
390 const s16 m_block_optimize_distance;
391 const s16 m_max_gen_distance;
392 const bool m_occ_cull;
395 Blocks that are currently on the line.
396 This is used for throttling the sending of blocks.
397 - The size of this list is limited to some value
398 Block is added when it is sent with BLOCKDATA.
399 Block is removed when GOTBLOCKS is received.
400 Value is time from sending. (not used at the moment)
402 std::map<v3s16, float> m_blocks_sending;
405 Blocks that have been modified since blocks were
406 sent to the client last (getNextBlocks()).
407 This is used to reset the unsent distance, so that
408 modified blocks are resent to the client.
410 List of block positions.
412 std::set<v3s16> m_blocks_modified;
415 Count of excess GotBlocks().
416 There is an excess amount because the client sometimes
417 gets a block so late that the server sends it again,
418 and the client then sends two GOTBLOCKs.
419 This is reset by PrintInfo()
421 u32 m_excess_gotblocks = 0;
423 // CPU usage optimization
424 float m_nothing_to_send_pause_timer = 0.0f;
427 name of player using this client
429 std::string m_name = "";
434 u8 m_version_major = 0;
435 u8 m_version_minor = 0;
436 u8 m_version_patch = 0;
438 std::string m_full_version = "unknown";
440 u16 m_deployed_compression = 0;
443 time this client was created
445 const u64 m_connection_time = porting::getTimeS();
448 typedef std::unordered_map<u16, RemoteClient*> RemoteClientMap;
450 class ClientInterface {
455 ClientInterface(const std::shared_ptr<con::Connection> &con);
459 void step(float dtime);
461 /* get list of active client id's */
462 std::vector<session_t> getClientIDs(ClientState min_state=CS_Active);
464 /* mark block as not sent to active client sessions */
465 void markBlockposAsNotSent(const v3s16 &pos);
467 /* verify is server user limit was reached */
468 bool isUserLimitReached();
470 /* get list of client player names */
471 const std::vector<std::string> &getPlayerNames() const { return m_clients_names; }
473 /* send message to client */
474 void send(session_t peer_id, u8 channelnum, NetworkPacket *pkt, bool reliable);
476 /* send to all clients */
477 void sendToAll(NetworkPacket *pkt);
479 /* delete a client */
480 void DeleteClient(session_t peer_id);
483 void CreateClient(session_t peer_id);
485 /* get a client by peer_id */
486 RemoteClient *getClientNoEx(session_t peer_id, ClientState state_min = CS_Active);
488 /* get client by peer_id (make sure you have list lock before!*/
489 RemoteClient *lockedGetClientNoEx(session_t peer_id, ClientState state_min = CS_Active);
491 /* get state of client by id*/
492 ClientState getClientState(session_t peer_id);
494 /* set client playername */
495 void setPlayerName(session_t peer_id, const std::string &name);
497 /* get protocol version of client */
498 u16 getProtocolVersion(session_t peer_id);
500 /* set client version */
501 void setClientVersion(session_t peer_id, u8 major, u8 minor, u8 patch,
502 const std::string &full);
504 /* event to update client state */
505 void event(session_t peer_id, ClientStateEvent event);
507 /* Set environment. Do not call this function if environment is already set */
508 void setEnv(ServerEnvironment *env)
510 assert(m_env == NULL); // pre-condition
514 static std::string state2Name(ClientState state);
518 AutoLock(ClientInterface &iface): m_lock(iface.m_clients_mutex) {}
521 RecursiveMutexAutoLock m_lock;
524 RemoteClientMap& getClientList() { return m_clients; }
527 /* update internal player list */
528 void UpdatePlayerList();
531 std::shared_ptr<con::Connection> m_con;
532 std::recursive_mutex m_clients_mutex;
533 // Connected clients (behind the con mutex)
534 RemoteClientMap m_clients;
535 std::vector<std::string> m_clients_names; //for announcing masterserver
538 ServerEnvironment *m_env;
540 float m_print_info_timer;
542 static const char *statenames[];