clientiface.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. #ifndef _CLIENTIFACE_H_
  17. #define _CLIENTIFACE_H_
  18. #include "irr_v3d.h" // for irrlicht datatypes
  19. #include "constants.h"
  20. #include "serialization.h" // for SER_FMT_VER_INVALID
  21. #include "jthread/jmutex.h"
  22. #include <list>
  23. #include <vector>
  24. #include <map>
  25. #include <set>
  26. class MapBlock;
  27. class ServerEnvironment;
  28. class EmergeManager;
  29. /*
  30. * State Transitions
  31. Start
  32. (peer connect)
  33. |
  34. v
  35. /-----------------\
  36. | |
  37. | Created |
  38. | |
  39. \-----------------/
  40. |
  41. |
  42. +-----------------------------+ invalid playername, password
  43. |IN: | or denied by mod
  44. | TOSERVER_INIT |------------------------------
  45. +-----------------------------+ |
  46. | |
  47. | Auth ok |
  48. | |
  49. +-----------------------------+ |
  50. |OUT: | |
  51. | TOCLIENT_INIT | |
  52. +-----------------------------+ |
  53. | |
  54. v |
  55. /-----------------\ |
  56. | | |
  57. | InitSent | |
  58. | | |
  59. \-----------------/ +------------------
  60. | | |
  61. +-----------------------------+ +-----------------------------+ |
  62. |IN: | |OUT: | |
  63. | TOSERVER_INIT2 | | TOCLIENT_ACCESS_DENIED | |
  64. +-----------------------------+ +-----------------------------+ |
  65. | | |
  66. v v |
  67. /-----------------\ /-----------------\ |
  68. | | | | |
  69. | InitDone | | Denied | |
  70. | | | | |
  71. \-----------------/ \-----------------/ |
  72. | |
  73. +-----------------------------+ |
  74. |OUT: | |
  75. | TOCLIENT_MOVEMENT | |
  76. | TOCLIENT_ITEMDEF | |
  77. | TOCLIENT_NODEDEF | |
  78. | TOCLIENT_ANNOUNCE_MEDIA | |
  79. | TOCLIENT_DETACHED_INVENTORY | |
  80. | TOCLIENT_TIME_OF_DAY | |
  81. +-----------------------------+ |
  82. | |
  83. | |
  84. | ----------------------------------- |
  85. v | | |
  86. /-----------------\ v |
  87. | | +-----------------------------+ |
  88. | DefinitionsSent | |IN: | |
  89. | | | TOSERVER_REQUEST_MEDIA | |
  90. \-----------------/ | TOSERVER_RECEIVED_MEDIA | |
  91. | +-----------------------------+ |
  92. | ^ | |
  93. | ----------------------------------- |
  94. | |
  95. +-----------------------------+ |
  96. |IN: | |
  97. | TOSERVER_CLIENT_READY | |
  98. +-----------------------------+ |
  99. | async |
  100. v mod action |
  101. +-----------------------------+ (ban,kick) |
  102. |OUT: | |
  103. | TOCLIENT_MOVE_PLAYER | |
  104. | TOCLIENT_PRIVILEGES | |
  105. | TOCLIENT_INVENTORY_FORMSPEC | |
  106. | UpdateCrafting | |
  107. | TOCLIENT_INVENTORY | |
  108. | TOCLIENT_HP (opt) | |
  109. | TOCLIENT_BREATH | |
  110. | TOCLIENT_DEATHSCREEN | |
  111. +-----------------------------+ |
  112. | |
  113. v |
  114. /-----------------\ |
  115. | |------------------------------------------------------
  116. | Active |
  117. | |----------------------------------
  118. \-----------------/ timeout |
  119. | +-----------------------------+
  120. | |OUT: |
  121. | | TOCLIENT_DISCONNECT |
  122. | +-----------------------------+
  123. | |
  124. | v
  125. +-----------------------------+ /-----------------\
  126. |IN: | | |
  127. | TOSERVER_DISCONNECT |------------------->| Disconnecting |
  128. +-----------------------------+ | |
  129. \-----------------/
  130. */
  131. namespace con {
  132. class Connection;
  133. }
  134. #define CI_ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0]))
  135. enum ClientState
  136. {
  137. CS_Invalid,
  138. CS_Disconnecting,
  139. CS_Denied,
  140. CS_Created,
  141. CS_InitSent,
  142. CS_InitDone,
  143. CS_DefinitionsSent,
  144. CS_Active
  145. };
  146. enum ClientStateEvent
  147. {
  148. CSE_Init,
  149. CSE_GotInit2,
  150. CSE_SetDenied,
  151. CSE_SetDefinitionsSent,
  152. CSE_SetClientReady,
  153. CSE_Disconnect
  154. };
  155. /*
  156. Used for queueing and sorting block transfers in containers
  157. Lower priority number means higher priority.
  158. */
  159. struct PrioritySortedBlockTransfer
  160. {
  161. PrioritySortedBlockTransfer(float a_priority, v3s16 a_pos, u16 a_peer_id)
  162. {
  163. priority = a_priority;
  164. pos = a_pos;
  165. peer_id = a_peer_id;
  166. }
  167. bool operator < (const PrioritySortedBlockTransfer &other) const
  168. {
  169. return priority < other.priority;
  170. }
  171. float priority;
  172. v3s16 pos;
  173. u16 peer_id;
  174. };
  175. class RemoteClient
  176. {
  177. public:
  178. // peer_id=0 means this client has no associated peer
  179. // NOTE: If client is made allowed to exist while peer doesn't,
  180. // this has to be set to 0 when there is no peer.
  181. // Also, the client must be moved to some other container.
  182. u16 peer_id;
  183. // The serialization version to use with the client
  184. u8 serialization_version;
  185. //
  186. u16 net_proto_version;
  187. RemoteClient():
  188. peer_id(PEER_ID_INEXISTENT),
  189. serialization_version(SER_FMT_VER_INVALID),
  190. net_proto_version(0),
  191. m_time_from_building(9999),
  192. m_pending_serialization_version(SER_FMT_VER_INVALID),
  193. m_state(CS_Created),
  194. m_nearest_unsent_d(0),
  195. m_nearest_unsent_reset_timer(0.0),
  196. m_excess_gotblocks(0),
  197. m_nothing_to_send_pause_timer(0.0),
  198. m_name(""),
  199. m_version_major(0),
  200. m_version_minor(0),
  201. m_version_patch(0),
  202. m_full_version("unknown"),
  203. m_connection_time(getTime(PRECISION_SECONDS))
  204. {
  205. }
  206. ~RemoteClient()
  207. {
  208. }
  209. /*
  210. Finds block that should be sent next to the client.
  211. Environment should be locked when this is called.
  212. dtime is used for resetting send radius at slow interval
  213. */
  214. void GetNextBlocks(ServerEnvironment *env, EmergeManager* emerge,
  215. float dtime, std::vector<PrioritySortedBlockTransfer> &dest);
  216. void GotBlock(v3s16 p);
  217. void SentBlock(v3s16 p);
  218. void SetBlockNotSent(v3s16 p);
  219. void SetBlocksNotSent(std::map<v3s16, MapBlock*> &blocks);
  220. s32 SendingCount()
  221. {
  222. return m_blocks_sending.size();
  223. }
  224. // Increments timeouts and removes timed-out blocks from list
  225. // NOTE: This doesn't fix the server-not-sending-block bug
  226. // because it is related to emerging, not sending.
  227. //void RunSendingTimeouts(float dtime, float timeout);
  228. void PrintInfo(std::ostream &o)
  229. {
  230. o<<"RemoteClient "<<peer_id<<": "
  231. <<"m_blocks_sent.size()="<<m_blocks_sent.size()
  232. <<", m_blocks_sending.size()="<<m_blocks_sending.size()
  233. <<", m_nearest_unsent_d="<<m_nearest_unsent_d
  234. <<", m_excess_gotblocks="<<m_excess_gotblocks
  235. <<std::endl;
  236. m_excess_gotblocks = 0;
  237. }
  238. // Time from last placing or removing blocks
  239. float m_time_from_building;
  240. /*
  241. List of active objects that the client knows of.
  242. Value is dummy.
  243. */
  244. std::set<u16> m_known_objects;
  245. ClientState getState()
  246. { return m_state; }
  247. std::string getName()
  248. { return m_name; }
  249. void setName(std::string name)
  250. { m_name = name; }
  251. /* update internal client state */
  252. void notifyEvent(ClientStateEvent event);
  253. /* set expected serialization version */
  254. void setPendingSerializationVersion(u8 version)
  255. { m_pending_serialization_version = version; }
  256. void confirmSerializationVersion()
  257. { serialization_version = m_pending_serialization_version; }
  258. /* get uptime */
  259. u32 uptime();
  260. /* set version information */
  261. void setVersionInfo(u8 major, u8 minor, u8 patch, std::string full) {
  262. m_version_major = major;
  263. m_version_minor = minor;
  264. m_version_patch = patch;
  265. m_full_version = full;
  266. }
  267. /* read version information */
  268. u8 getMajor() { return m_version_major; }
  269. u8 getMinor() { return m_version_minor; }
  270. u8 getPatch() { return m_version_patch; }
  271. std::string getVersion() { return m_full_version; }
  272. private:
  273. // Version is stored in here after INIT before INIT2
  274. u8 m_pending_serialization_version;
  275. /* current state of client */
  276. ClientState m_state;
  277. /*
  278. Blocks that have been sent to client.
  279. - These don't have to be sent again.
  280. - A block is cleared from here when client says it has
  281. deleted it from it's memory
  282. Key is position, value is dummy.
  283. No MapBlock* is stored here because the blocks can get deleted.
  284. */
  285. std::set<v3s16> m_blocks_sent;
  286. s16 m_nearest_unsent_d;
  287. v3s16 m_last_center;
  288. float m_nearest_unsent_reset_timer;
  289. /*
  290. Blocks that are currently on the line.
  291. This is used for throttling the sending of blocks.
  292. - The size of this list is limited to some value
  293. Block is added when it is sent with BLOCKDATA.
  294. Block is removed when GOTBLOCKS is received.
  295. Value is time from sending. (not used at the moment)
  296. */
  297. std::map<v3s16, float> m_blocks_sending;
  298. /*
  299. Count of excess GotBlocks().
  300. There is an excess amount because the client sometimes
  301. gets a block so late that the server sends it again,
  302. and the client then sends two GOTBLOCKs.
  303. This is resetted by PrintInfo()
  304. */
  305. u32 m_excess_gotblocks;
  306. // CPU usage optimization
  307. float m_nothing_to_send_pause_timer;
  308. /*
  309. name of player using this client
  310. */
  311. std::string m_name;
  312. /*
  313. client information
  314. */
  315. u8 m_version_major;
  316. u8 m_version_minor;
  317. u8 m_version_patch;
  318. std::string m_full_version;
  319. /*
  320. time this client was created
  321. */
  322. const u32 m_connection_time;
  323. };
  324. class ClientInterface {
  325. public:
  326. friend class Server;
  327. ClientInterface(con::Connection* con);
  328. ~ClientInterface();
  329. /* run sync step */
  330. void step(float dtime);
  331. /* get list of active client id's */
  332. std::list<u16> getClientIDs(ClientState min_state=CS_Active);
  333. /* get list of client player names */
  334. std::vector<std::string> getPlayerNames();
  335. /* send message to client */
  336. void send(u16 peer_id, u8 channelnum, SharedBuffer<u8> data, bool reliable);
  337. /* send to all clients */
  338. void sendToAll(u16 channelnum, SharedBuffer<u8> data, bool reliable);
  339. /* delete a client */
  340. void DeleteClient(u16 peer_id);
  341. /* create client */
  342. void CreateClient(u16 peer_id);
  343. /* get a client by peer_id */
  344. RemoteClient* getClientNoEx(u16 peer_id, ClientState state_min=CS_Active);
  345. /* get client by peer_id (make sure you have list lock before!*/
  346. RemoteClient* lockedGetClientNoEx(u16 peer_id, ClientState state_min=CS_Active);
  347. /* get state of client by id*/
  348. ClientState getClientState(u16 peer_id);
  349. /* set client playername */
  350. void setPlayerName(u16 peer_id,std::string name);
  351. /* get protocol version of client */
  352. u16 getProtocolVersion(u16 peer_id);
  353. /* set client version */
  354. void setClientVersion(u16 peer_id, u8 major, u8 minor, u8 patch, std::string full);
  355. /* event to update client state */
  356. void event(u16 peer_id, ClientStateEvent event);
  357. /* set environment */
  358. void setEnv(ServerEnvironment* env)
  359. { assert(m_env == 0); m_env = env; }
  360. static std::string state2Name(ClientState state);
  361. protected:
  362. //TODO find way to avoid this functions
  363. void Lock()
  364. { m_clients_mutex.Lock(); }
  365. void Unlock()
  366. { m_clients_mutex.Unlock(); }
  367. std::map<u16, RemoteClient*>& getClientList()
  368. { return m_clients; }
  369. private:
  370. /* update internal player list */
  371. void UpdatePlayerList();
  372. // Connection
  373. con::Connection* m_con;
  374. JMutex m_clients_mutex;
  375. // Connected clients (behind the con mutex)
  376. std::map<u16, RemoteClient*> m_clients;
  377. std::vector<std::string> m_clients_names; //for announcing masterserver
  378. // Environment
  379. ServerEnvironment *m_env;
  380. JMutex m_env_mutex;
  381. float m_print_info_timer;
  382. static const char *statenames[];
  383. };
  384. #endif