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"
36 class ServerEnvironment;
51 | depending of the incoming packet
52 ----------------------------------------
54 +-----------------------------+
57 +-----------------------------+
61 +-----------------------------+
64 +-----------------------------+
68 /-----------------\ /-----------------\
70 | AwaitingInit2 |<--------- | HelloSent |
72 \-----------------/ | \-----------------/
74 +-----------------------------+ | *-----------------------------* Auth fails
75 |IN: | | |Authentication, depending on |------------------
76 | TOSERVER_INIT2 | | | packet sent by client | |
77 +-----------------------------+ | *-----------------------------* |
79 | | | Authentication |
81 /-----------------\ | v |
82 | | | +-----------------------------+ |
83 | InitDone | | |OUT: | |
84 | | | | TOCLIENT_AUTH_ACCEPT | |
85 \-----------------/ | +-----------------------------+ |
87 +-----------------------------+ --------------------- |
89 | TOCLIENT_MOVEMENT | |
90 | TOCLIENT_ITEMDEF | |
91 | TOCLIENT_NODEDEF | |
92 | TOCLIENT_ANNOUNCE_MEDIA | |
93 | TOCLIENT_DETACHED_INVENTORY | |
94 | TOCLIENT_TIME_OF_DAY | |
95 +-----------------------------+ |
98 | ----------------------------- |
100 /-----------------\ v |
101 | | +-----------------------------+ |
102 | DefinitionsSent | |IN: | |
103 | | | TOSERVER_REQUEST_MEDIA | |
104 \-----------------/ | | |
105 | +-----------------------------+ |
107 | ----------------------------- |
109 +-----------------------------+ --------------------------------+
111 | TOSERVER_CLIENT_READY | v |
112 +-----------------------------+ +------------------------+ |
114 v | TOCLIENT_ACCESS_DENIED | |
115 +-----------------------------+ +------------------------+ |
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 +-----------------------------+ |
128 /-----------------\ async mod action (ban, kick) |
129 | |---------------------------------------------------------------
131 | | |----------------------------------------------
132 | \-----------------/ timeout v
133 | | | +-----------------------------+
135 | | | | TOCLIENT_DISCONNECT |
136 | | | +-----------------------------+
139 | | +-----------------------------+ /-----------------\
141 | | | TOSERVER_DISCONNECT |------------------->| Disconnecting |
142 | | +-----------------------------+ | |
143 | | \-----------------/
144 | | any auth packet which was
145 | | allowed in TOCLIENT_AUTH_ACCEPT
147 | *-----------------------------* Auth +-------------------------------+
148 | |Authentication, depending on | succeeds |OUT: |
149 | | packet sent by client |---------->| TOCLIENT_ACCEPT_SUDO_MODE |
150 | *-----------------------------* +-------------------------------+
152 | | Auth fails /-----------------\
154 | +-------------------------------+ | SudoMode |
156 | | TOCLIENT_DENY_SUDO_MODE | \-----------------/
157 | +-------------------------------+ |
159 | | +-----------------------------+
160 | | sets password accordingly |IN: |
161 -------------------+-------------------------------| TOSERVER_FIRST_SRP |
162 +-----------------------------+
170 // Also make sure to update the ClientInterface::statenames
171 // array when modifying these enums
187 enum ClientStateEvent
194 CSE_SetDefinitionsSent,
202 Used for queueing and sorting block transfers in containers
204 Lower priority number means higher priority.
206 struct PrioritySortedBlockTransfer
208 PrioritySortedBlockTransfer(
209 float a_priority, const v3s16 &a_pos, session_t a_peer_id)
211 priority = a_priority;
215 bool operator<(const PrioritySortedBlockTransfer &other) const
217 return priority < other.priority;
227 // peer_id=0 means this client has no associated peer
228 // NOTE: If client is made allowed to exist while peer doesn't,
229 // this has to be set to 0 when there is no peer.
230 // Also, the client must be moved to some other container.
231 session_t peer_id = PEER_ID_INEXISTENT;
232 // The serialization version to use with the client
233 u8 serialization_version = SER_FMT_VER_INVALID;
235 u16 net_proto_version = 0;
237 /* Authentication information */
238 std::string enc_pwd = "";
239 bool create_player_on_auth_success = false;
240 AuthMechanism chosen_mech = AUTH_MECHANISM_NONE;
241 void *auth_data = nullptr;
242 u32 allowed_auth_mechs = 0;
243 u32 allowed_sudo_mechs = 0;
245 bool isSudoMechAllowed(AuthMechanism mech) { return allowed_sudo_mechs & mech; }
246 bool isMechAllowed(AuthMechanism mech) { return allowed_auth_mechs & mech; }
249 ~RemoteClient() = default;
252 Finds block that should be sent next to the client.
253 Environment should be locked when this is called.
254 dtime is used for resetting send radius at slow interval
256 void GetNextBlocks(ServerEnvironment *env, EmergeManager *emerge, float dtime,
257 std::vector<PrioritySortedBlockTransfer> &dest);
259 void GotBlock(v3s16 p);
261 void SentBlock(v3s16 p);
263 void SetBlockNotSent(v3s16 p);
264 void SetBlocksNotSent(std::map<v3s16, MapBlock *> &blocks);
267 * tell client about this block being modified right now.
268 * this information is required to requeue the block in case it's "on wire"
269 * while modification is processed by server
270 * @param p position of modified block
272 void ResendBlockIfOnWire(v3s16 p);
274 u32 getSendingCount() const { return m_blocks_sending.size(); }
276 bool isBlockSent(v3s16 p) const
278 return m_blocks_sent.find(p) != m_blocks_sent.end();
281 // Increments timeouts and removes timed-out blocks from list
282 // NOTE: This doesn't fix the server-not-sending-block bug
283 // because it is related to emerging, not sending.
284 // void RunSendingTimeouts(float dtime, float timeout);
286 void PrintInfo(std::ostream &o)
288 o << "RemoteClient " << peer_id << ": "
289 << "m_blocks_sent.size()=" << m_blocks_sent.size()
290 << ", m_blocks_sending.size()=" << m_blocks_sending.size()
291 << ", m_nearest_unsent_d=" << m_nearest_unsent_d
292 << ", m_excess_gotblocks=" << m_excess_gotblocks << std::endl;
293 m_excess_gotblocks = 0;
296 // Time from last placing or removing blocks
297 float m_time_from_building = 9999;
300 List of active objects that the client knows of.
302 std::set<u16> m_known_objects;
304 ClientState getState() const { return m_state; }
306 std::string getName() const { return m_name; }
308 void setName(const std::string &name) { m_name = name; }
310 /* update internal client state */
311 void notifyEvent(ClientStateEvent event);
313 /* set expected serialization version */
314 void setPendingSerializationVersion(u8 version)
316 m_pending_serialization_version = version;
319 void setDeployedCompressionMode(u16 byteFlag)
321 m_deployed_compression = byteFlag;
324 void confirmSerializationVersion()
326 serialization_version = m_pending_serialization_version;
332 /* set version information */
333 void setVersionInfo(u8 major, u8 minor, u8 patch, const std::string &full)
335 m_version_major = major;
336 m_version_minor = minor;
337 m_version_patch = patch;
338 m_full_version = full;
341 /* read version information */
342 u8 getMajor() const { return m_version_major; }
343 u8 getMinor() const { return m_version_minor; }
344 u8 getPatch() const { return m_version_patch; }
345 const std::string &getFull() const { return m_full_version; }
347 void setLangCode(const std::string &code) { m_lang_code = code; }
348 const std::string &getLangCode() const { return m_lang_code; }
351 // Version is stored in here after INIT before INIT2
352 u8 m_pending_serialization_version = SER_FMT_VER_INVALID;
354 /* current state of client */
355 ClientState m_state = CS_Created;
357 // Client sent language code
358 std::string m_lang_code;
361 Blocks that have been sent to client.
362 - These don't have to be sent again.
363 - A block is cleared from here when client says it has
364 deleted it from it's memory
366 List of block positions.
367 No MapBlock* is stored here because the blocks can get deleted.
369 std::set<v3s16> m_blocks_sent;
370 s16 m_nearest_unsent_d = 0;
372 float m_nearest_unsent_reset_timer = 0.0f;
374 const u16 m_max_simul_sends;
375 const float m_min_time_from_building;
376 const s16 m_max_send_distance;
377 const s16 m_block_optimize_distance;
378 const s16 m_max_gen_distance;
379 const bool m_occ_cull;
382 Blocks that are currently on the line.
383 This is used for throttling the sending of blocks.
384 - The size of this list is limited to some value
385 Block is added when it is sent with BLOCKDATA.
386 Block is removed when GOTBLOCKS is received.
387 Value is time from sending. (not used at the moment)
389 std::map<v3s16, float> m_blocks_sending;
392 Blocks that have been modified since last sending them.
393 These blocks will not be marked as sent, even if the
394 client reports it has received them to account for blocks
395 that are being modified while on the line.
397 List of block positions.
399 std::set<v3s16> m_blocks_modified;
402 Count of excess GotBlocks().
403 There is an excess amount because the client sometimes
404 gets a block so late that the server sends it again,
405 and the client then sends two GOTBLOCKs.
406 This is resetted by PrintInfo()
408 u32 m_excess_gotblocks = 0;
410 // CPU usage optimization
411 float m_nothing_to_send_pause_timer = 0.0f;
414 name of player using this client
416 std::string m_name = "";
421 u8 m_version_major = 0;
422 u8 m_version_minor = 0;
423 u8 m_version_patch = 0;
425 std::string m_full_version = "unknown";
427 u16 m_deployed_compression = 0;
430 time this client was created
432 const u64 m_connection_time = porting::getTimeS();
435 typedef std::unordered_map<u16, RemoteClient *> RemoteClientMap;
437 class ClientInterface
442 ClientInterface(const std::shared_ptr<con::Connection> &con);
446 void step(float dtime);
448 /* get list of active client id's */
449 std::vector<session_t> getClientIDs(ClientState min_state = CS_Active);
451 /* mark block as not sent to active client sessions */
452 void markBlockposAsNotSent(const v3s16 &pos);
454 /* verify is server user limit was reached */
455 bool isUserLimitReached();
457 /* get list of client player names */
458 const std::vector<std::string> &getPlayerNames() const { return m_clients_names; }
460 /* send message to client */
461 void send(session_t peer_id, u8 channelnum, NetworkPacket *pkt, bool reliable);
463 /* send to all clients */
464 void sendToAll(NetworkPacket *pkt);
465 void sendToAllCompat(
466 NetworkPacket *pkt, NetworkPacket *legacypkt, u16 min_proto_ver);
468 /* delete a client */
469 void DeleteClient(session_t peer_id);
472 void CreateClient(session_t peer_id);
474 /* get a client by peer_id */
475 RemoteClient *getClientNoEx(session_t peer_id, ClientState state_min = CS_Active);
477 /* get client by peer_id (make sure you have list lock before!*/
478 RemoteClient *lockedGetClientNoEx(
479 session_t peer_id, ClientState state_min = CS_Active);
481 /* get state of client by id*/
482 ClientState getClientState(session_t peer_id);
484 /* set client playername */
485 void setPlayerName(session_t peer_id, const std::string &name);
487 /* get protocol version of client */
488 u16 getProtocolVersion(session_t peer_id);
490 /* set client version */
491 void setClientVersion(session_t peer_id, u8 major, u8 minor, u8 patch,
492 const std::string &full);
494 /* event to update client state */
495 void event(session_t peer_id, ClientStateEvent event);
497 /* Set environment. Do not call this function if environment is already set */
498 void setEnv(ServerEnvironment *env)
500 assert(m_env == NULL); // pre-condition
504 static std::string state2Name(ClientState state);
507 // TODO find way to avoid this functions
508 void lock() { m_clients_mutex.lock(); }
509 void unlock() { m_clients_mutex.unlock(); }
511 RemoteClientMap &getClientList() { return m_clients; }
514 /* update internal player list */
515 void UpdatePlayerList();
518 std::shared_ptr<con::Connection> m_con;
519 std::recursive_mutex m_clients_mutex;
520 // Connected clients (behind the con mutex)
521 RemoteClientMap m_clients;
522 std::vector<std::string> m_clients_names; // for announcing masterserver
525 ServerEnvironment *m_env;
527 float m_print_info_timer;
529 static const char *statenames[];