clientiface.h 19 KB

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