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.
19 #ifndef _CLIENTIFACE_H_
20 #define _CLIENTIFACE_H_
22 #include "irr_v3d.h" // for irrlicht datatypes
24 #include "constants.h"
25 #include "serialization.h" // for SER_FMT_VER_INVALID
26 #include "threading/mutex.h"
27 #include "network/networkpacket.h"
28 #include "util/cpp11_container.h"
36 class ServerEnvironment;
51 | depending of the incoming packet
52 +---------------------------------------
54 +-----------------------------+ +-----------------------------+
56 | TOSERVER_INIT_LEGACY |----- | TOSERVER_INIT | invalid playername,
57 +-----------------------------+ | +-----------------------------+ password (for _LEGACY),
58 | | | or denied by mod
59 | Auth ok -------------------+---------------------------------
61 +-----------------------------+ +-----------------------------+ |
63 | TOCLIENT_INIT_LEGACY | | TOCLIENT_HELLO | |
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 \-----------------/ | TOSERVER_RECEIVED_MEDIA | |
105 | +-----------------------------+ |
107 | ----------------------------- |
109 +-----------------------------+ --------------------------------+
111 | TOSERVER_CLIENT_READY | v |
112 +-----------------------------+ +-------------------------------+ |
114 v | TOCLIENT_ACCESS_DENIED_LEGAGY | |
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(float a_priority, v3s16 a_pos, u16 a_peer_id)
210 priority = a_priority;
214 bool operator < (const PrioritySortedBlockTransfer &other) const
216 return priority < other.priority;
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.
231 // The serialization version to use with the client
232 u8 serialization_version;
234 u16 net_proto_version;
236 /* Authentication information */
238 bool create_player_on_auth_success;
239 AuthMechanism chosen_mech;
241 u32 allowed_auth_mechs;
242 u32 allowed_sudo_mechs;
244 bool isSudoMechAllowed(AuthMechanism mech)
245 { return allowed_sudo_mechs & mech; }
246 bool isMechAllowed(AuthMechanism mech)
247 { return allowed_auth_mechs & mech; }
250 peer_id(PEER_ID_INEXISTENT),
251 serialization_version(SER_FMT_VER_INVALID),
252 net_proto_version(0),
253 create_player_on_auth_success(false),
254 chosen_mech(AUTH_MECHANISM_NONE),
256 m_time_from_building(9999),
257 m_pending_serialization_version(SER_FMT_VER_INVALID),
259 m_nearest_unsent_d(0),
260 m_nearest_unsent_reset_timer(0.0),
261 m_excess_gotblocks(0),
262 m_nothing_to_send_pause_timer(0.0),
267 m_full_version("unknown"),
268 m_deployed_compression(0),
269 m_connection_time(porting::getTime(PRECISION_SECONDS))
277 Finds block that should be sent next to the client.
278 Environment should be locked when this is called.
279 dtime is used for resetting send radius at slow interval
281 void GetNextBlocks(ServerEnvironment *env, EmergeManager* emerge,
282 float dtime, std::vector<PrioritySortedBlockTransfer> &dest);
284 void GotBlock(v3s16 p);
286 void SentBlock(v3s16 p);
288 void SetBlockNotSent(v3s16 p);
289 void SetBlocksNotSent(std::map<v3s16, MapBlock*> &blocks);
292 * tell client about this block being modified right now.
293 * this information is required to requeue the block in case it's "on wire"
294 * while modification is processed by server
295 * @param p position of modified block
297 void ResendBlockIfOnWire(v3s16 p);
301 return m_blocks_sending.size();
304 // Increments timeouts and removes timed-out blocks from list
305 // NOTE: This doesn't fix the server-not-sending-block bug
306 // because it is related to emerging, not sending.
307 //void RunSendingTimeouts(float dtime, float timeout);
309 void PrintInfo(std::ostream &o)
311 o<<"RemoteClient "<<peer_id<<": "
312 <<"m_blocks_sent.size()="<<m_blocks_sent.size()
313 <<", m_blocks_sending.size()="<<m_blocks_sending.size()
314 <<", m_nearest_unsent_d="<<m_nearest_unsent_d
315 <<", m_excess_gotblocks="<<m_excess_gotblocks
317 m_excess_gotblocks = 0;
320 // Time from last placing or removing blocks
321 float m_time_from_building;
324 List of active objects that the client knows of.
326 std::set<u16> m_known_objects;
328 ClientState getState() const { return m_state; }
330 std::string getName() const { return m_name; }
332 void setName(const std::string &name) { m_name = name; }
334 /* update internal client state */
335 void notifyEvent(ClientStateEvent event);
337 /* set expected serialization version */
338 void setPendingSerializationVersion(u8 version)
339 { m_pending_serialization_version = version; }
341 void setDeployedCompressionMode(u16 byteFlag)
342 { m_deployed_compression = byteFlag; }
344 void confirmSerializationVersion()
345 { serialization_version = m_pending_serialization_version; }
350 /* set version information */
351 void setVersionInfo(u8 major, u8 minor, u8 patch, const std::string &full)
353 m_version_major = major;
354 m_version_minor = minor;
355 m_version_patch = patch;
356 m_full_version = full;
359 /* read version information */
360 u8 getMajor() const { return m_version_major; }
361 u8 getMinor() const { return m_version_minor; }
362 u8 getPatch() const { return m_version_patch; }
363 std::string getVersion() const { return m_full_version; }
365 // Version is stored in here after INIT before INIT2
366 u8 m_pending_serialization_version;
368 /* current state of client */
372 Blocks that have been sent to client.
373 - These don't have to be sent again.
374 - A block is cleared from here when client says it has
375 deleted it from it's memory
377 List of block positions.
378 No MapBlock* is stored here because the blocks can get deleted.
380 std::set<v3s16> m_blocks_sent;
381 s16 m_nearest_unsent_d;
383 float m_nearest_unsent_reset_timer;
386 Blocks that are currently on the line.
387 This is used for throttling the sending of blocks.
388 - The size of this list is limited to some value
389 Block is added when it is sent with BLOCKDATA.
390 Block is removed when GOTBLOCKS is received.
391 Value is time from sending. (not used at the moment)
393 std::map<v3s16, float> m_blocks_sending;
396 Blocks that have been modified since last sending them.
397 These blocks will not be marked as sent, even if the
398 client reports it has received them to account for blocks
399 that are being modified while on the line.
401 List of block positions.
403 std::set<v3s16> m_blocks_modified;
406 Count of excess GotBlocks().
407 There is an excess amount because the client sometimes
408 gets a block so late that the server sends it again,
409 and the client then sends two GOTBLOCKs.
410 This is resetted by PrintInfo()
412 u32 m_excess_gotblocks;
414 // CPU usage optimization
415 float m_nothing_to_send_pause_timer;
418 name of player using this client
429 std::string m_full_version;
431 u16 m_deployed_compression;
434 time this client was created
436 const u32 m_connection_time;
439 class ClientInterface {
444 ClientInterface(con::Connection* con);
448 void step(float dtime);
450 /* get list of active client id's */
451 std::vector<u16> getClientIDs(ClientState min_state=CS_Active);
453 /* get list of client player names */
454 const std::vector<std::string> &getPlayerNames() const { return m_clients_names; }
456 /* send message to client */
457 void send(u16 peer_id, u8 channelnum, NetworkPacket* pkt, bool reliable);
459 /* send to all clients */
460 void sendToAll(NetworkPacket *pkt);
462 /* delete a client */
463 void DeleteClient(u16 peer_id);
466 void CreateClient(u16 peer_id);
468 /* get a client by peer_id */
469 RemoteClient* getClientNoEx(u16 peer_id, ClientState state_min=CS_Active);
471 /* get client by peer_id (make sure you have list lock before!*/
472 RemoteClient* lockedGetClientNoEx(u16 peer_id, ClientState state_min=CS_Active);
474 /* get state of client by id*/
475 ClientState getClientState(u16 peer_id);
477 /* set client playername */
478 void setPlayerName(u16 peer_id,std::string name);
480 /* get protocol version of client */
481 u16 getProtocolVersion(u16 peer_id);
483 /* set client version */
484 void setClientVersion(u16 peer_id, u8 major, u8 minor, u8 patch, std::string full);
486 /* event to update client state */
487 void event(u16 peer_id, ClientStateEvent event);
489 /* Set environment. Do not call this function if environment is already set */
490 void setEnv(ServerEnvironment *env)
492 assert(m_env == NULL); // pre-condition
496 static std::string state2Name(ClientState state);
499 //TODO find way to avoid this functions
500 void lock() { m_clients_mutex.lock(); }
501 void unlock() { m_clients_mutex.unlock(); }
503 UNORDERED_MAP<u16, RemoteClient*>& getClientList() { return m_clients; }
506 /* update internal player list */
507 void UpdatePlayerList();
510 con::Connection* m_con;
511 Mutex m_clients_mutex;
512 // Connected clients (behind the con mutex)
513 UNORDERED_MAP<u16, RemoteClient*> m_clients;
514 std::vector<std::string> m_clients_names; //for announcing masterserver
517 ServerEnvironment *m_env;
520 float m_print_info_timer;
522 static const char *statenames[];