clientiface.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. /**
  221. * tell client about this block being modified right now.
  222. * this information is required to requeue the block in case it's "on wire"
  223. * while modification is processed by server
  224. * @param p position of modified block
  225. */
  226. void ResendBlockIfOnWire(v3s16 p);
  227. s32 SendingCount()
  228. {
  229. return m_blocks_sending.size();
  230. }
  231. // Increments timeouts and removes timed-out blocks from list
  232. // NOTE: This doesn't fix the server-not-sending-block bug
  233. // because it is related to emerging, not sending.
  234. //void RunSendingTimeouts(float dtime, float timeout);
  235. void PrintInfo(std::ostream &o)
  236. {
  237. o<<"RemoteClient "<<peer_id<<": "
  238. <<"m_blocks_sent.size()="<<m_blocks_sent.size()
  239. <<", m_blocks_sending.size()="<<m_blocks_sending.size()
  240. <<", m_nearest_unsent_d="<<m_nearest_unsent_d
  241. <<", m_excess_gotblocks="<<m_excess_gotblocks
  242. <<std::endl;
  243. m_excess_gotblocks = 0;
  244. }
  245. // Time from last placing or removing blocks
  246. float m_time_from_building;
  247. /*
  248. List of active objects that the client knows of.
  249. Value is dummy.
  250. */
  251. std::set<u16> m_known_objects;
  252. ClientState getState()
  253. { return m_state; }
  254. std::string getName()
  255. { return m_name; }
  256. void setName(std::string name)
  257. { m_name = name; }
  258. /* update internal client state */
  259. void notifyEvent(ClientStateEvent event);
  260. /* set expected serialization version */
  261. void setPendingSerializationVersion(u8 version)
  262. { m_pending_serialization_version = version; }
  263. void confirmSerializationVersion()
  264. { serialization_version = m_pending_serialization_version; }
  265. /* get uptime */
  266. u32 uptime();
  267. /* set version information */
  268. void setVersionInfo(u8 major, u8 minor, u8 patch, std::string full) {
  269. m_version_major = major;
  270. m_version_minor = minor;
  271. m_version_patch = patch;
  272. m_full_version = full;
  273. }
  274. /* read version information */
  275. u8 getMajor() { return m_version_major; }
  276. u8 getMinor() { return m_version_minor; }
  277. u8 getPatch() { return m_version_patch; }
  278. std::string getVersion() { return m_full_version; }
  279. private:
  280. // Version is stored in here after INIT before INIT2
  281. u8 m_pending_serialization_version;
  282. /* current state of client */
  283. ClientState m_state;
  284. /*
  285. Blocks that have been sent to client.
  286. - These don't have to be sent again.
  287. - A block is cleared from here when client says it has
  288. deleted it from it's memory
  289. Key is position, value is dummy.
  290. No MapBlock* is stored here because the blocks can get deleted.
  291. */
  292. std::set<v3s16> m_blocks_sent;
  293. s16 m_nearest_unsent_d;
  294. v3s16 m_last_center;
  295. float m_nearest_unsent_reset_timer;
  296. /*
  297. Blocks that are currently on the line.
  298. This is used for throttling the sending of blocks.
  299. - The size of this list is limited to some value
  300. Block is added when it is sent with BLOCKDATA.
  301. Block is removed when GOTBLOCKS is received.
  302. Value is time from sending. (not used at the moment)
  303. */
  304. std::map<v3s16, float> m_blocks_sending;
  305. /*
  306. Count of excess GotBlocks().
  307. There is an excess amount because the client sometimes
  308. gets a block so late that the server sends it again,
  309. and the client then sends two GOTBLOCKs.
  310. This is resetted by PrintInfo()
  311. */
  312. u32 m_excess_gotblocks;
  313. // CPU usage optimization
  314. float m_nothing_to_send_pause_timer;
  315. /*
  316. name of player using this client
  317. */
  318. std::string m_name;
  319. /*
  320. client information
  321. */
  322. u8 m_version_major;
  323. u8 m_version_minor;
  324. u8 m_version_patch;
  325. std::string m_full_version;
  326. /*
  327. time this client was created
  328. */
  329. const u32 m_connection_time;
  330. };
  331. class ClientInterface {
  332. public:
  333. friend class Server;
  334. ClientInterface(con::Connection* con);
  335. ~ClientInterface();
  336. /* run sync step */
  337. void step(float dtime);
  338. /* get list of active client id's */
  339. std::list<u16> getClientIDs(ClientState min_state=CS_Active);
  340. /* get list of client player names */
  341. std::vector<std::string> getPlayerNames();
  342. /* send message to client */
  343. void send(u16 peer_id, u8 channelnum, SharedBuffer<u8> data, bool reliable);
  344. /* send to all clients */
  345. void sendToAll(u16 channelnum, SharedBuffer<u8> data, bool reliable);
  346. /* delete a client */
  347. void DeleteClient(u16 peer_id);
  348. /* create client */
  349. void CreateClient(u16 peer_id);
  350. /* get a client by peer_id */
  351. RemoteClient* getClientNoEx(u16 peer_id, ClientState state_min=CS_Active);
  352. /* get client by peer_id (make sure you have list lock before!*/
  353. RemoteClient* lockedGetClientNoEx(u16 peer_id, ClientState state_min=CS_Active);
  354. /* get state of client by id*/
  355. ClientState getClientState(u16 peer_id);
  356. /* set client playername */
  357. void setPlayerName(u16 peer_id,std::string name);
  358. /* get protocol version of client */
  359. u16 getProtocolVersion(u16 peer_id);
  360. /* set client version */
  361. void setClientVersion(u16 peer_id, u8 major, u8 minor, u8 patch, std::string full);
  362. /* event to update client state */
  363. void event(u16 peer_id, ClientStateEvent event);
  364. /* set environment */
  365. void setEnv(ServerEnvironment* env)
  366. { assert(m_env == 0); m_env = env; }
  367. static std::string state2Name(ClientState state);
  368. protected:
  369. //TODO find way to avoid this functions
  370. void Lock()
  371. { m_clients_mutex.Lock(); }
  372. void Unlock()
  373. { m_clients_mutex.Unlock(); }
  374. std::map<u16, RemoteClient*>& getClientList()
  375. { return m_clients; }
  376. private:
  377. /* update internal player list */
  378. void UpdatePlayerList();
  379. // Connection
  380. con::Connection* m_con;
  381. JMutex m_clients_mutex;
  382. // Connected clients (behind the con mutex)
  383. std::map<u16, RemoteClient*> m_clients;
  384. std::vector<std::string> m_clients_names; //for announcing masterserver
  385. // Environment
  386. ServerEnvironment *m_env;
  387. JMutex m_env_mutex;
  388. float m_print_info_timer;
  389. static const char *statenames[];
  390. };
  391. #endif