clientiface.h 20 KB

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