server.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 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 SERVER_HEADER
  17. #define SERVER_HEADER
  18. #include "network/connection.h"
  19. #include "irr_v3d.h"
  20. #include "map.h"
  21. #include "hud.h"
  22. #include "gamedef.h"
  23. #include "serialization.h" // For SER_FMT_VER_INVALID
  24. #include "mods.h"
  25. #include "inventorymanager.h"
  26. #include "subgame.h"
  27. #include "tileanimation.h" // struct TileAnimationParams
  28. #include "util/numeric.h"
  29. #include "util/thread.h"
  30. #include "util/basic_macros.h"
  31. #include "serverenvironment.h"
  32. #include "chat_interface.h"
  33. #include "clientiface.h"
  34. #include "remoteplayer.h"
  35. #include "network/networkpacket.h"
  36. #include <string>
  37. #include <list>
  38. #include <map>
  39. #include <vector>
  40. class IWritableItemDefManager;
  41. class IWritableNodeDefManager;
  42. class IWritableCraftDefManager;
  43. class BanManager;
  44. class EventManager;
  45. class Inventory;
  46. class PlayerSAO;
  47. class IRollbackManager;
  48. struct RollbackAction;
  49. class EmergeManager;
  50. class ServerScripting;
  51. class ServerEnvironment;
  52. struct SimpleSoundSpec;
  53. class ServerThread;
  54. enum ClientDeletionReason {
  55. CDR_LEAVE,
  56. CDR_TIMEOUT,
  57. CDR_DENY
  58. };
  59. class MapEditEventAreaIgnorer
  60. {
  61. public:
  62. MapEditEventAreaIgnorer(VoxelArea *ignorevariable, const VoxelArea &a):
  63. m_ignorevariable(ignorevariable)
  64. {
  65. if(m_ignorevariable->getVolume() == 0)
  66. *m_ignorevariable = a;
  67. else
  68. m_ignorevariable = NULL;
  69. }
  70. ~MapEditEventAreaIgnorer()
  71. {
  72. if(m_ignorevariable)
  73. {
  74. assert(m_ignorevariable->getVolume() != 0);
  75. *m_ignorevariable = VoxelArea();
  76. }
  77. }
  78. private:
  79. VoxelArea *m_ignorevariable;
  80. };
  81. struct MediaInfo
  82. {
  83. std::string path;
  84. std::string sha1_digest;
  85. MediaInfo(const std::string &path_="",
  86. const std::string &sha1_digest_=""):
  87. path(path_),
  88. sha1_digest(sha1_digest_)
  89. {
  90. }
  91. };
  92. struct ServerSoundParams
  93. {
  94. enum Type {
  95. SSP_LOCAL,
  96. SSP_POSITIONAL,
  97. SSP_OBJECT
  98. } type = SSP_LOCAL;
  99. float gain = 1.0f;
  100. float fade = 0.0f;
  101. float pitch = 1.0f;
  102. bool loop = false;
  103. float max_hear_distance = 32 * BS;
  104. v3f pos;
  105. u16 object = 0;
  106. std::string to_player = "";
  107. v3f getPos(ServerEnvironment *env, bool *pos_exists) const;
  108. };
  109. struct ServerPlayingSound
  110. {
  111. ServerSoundParams params;
  112. SimpleSoundSpec spec;
  113. std::unordered_set<u16> clients; // peer ids
  114. };
  115. class Server : public con::PeerHandler, public MapEventReceiver,
  116. public InventoryManager, public IGameDef
  117. {
  118. public:
  119. /*
  120. NOTE: Every public method should be thread-safe
  121. */
  122. Server(
  123. const std::string &path_world,
  124. const SubgameSpec &gamespec,
  125. bool simple_singleplayer_mode,
  126. bool ipv6,
  127. bool dedicated,
  128. ChatInterface *iface = nullptr
  129. );
  130. ~Server();
  131. DISABLE_CLASS_COPY(Server);
  132. void start(Address bind_addr);
  133. void stop();
  134. // This is mainly a way to pass the time to the server.
  135. // Actual processing is done in an another thread.
  136. void step(float dtime);
  137. // This is run by ServerThread and does the actual processing
  138. void AsyncRunStep(bool initial_step=false);
  139. void Receive();
  140. PlayerSAO* StageTwoClientInit(u16 peer_id);
  141. /*
  142. * Command Handlers
  143. */
  144. void handleCommand(NetworkPacket* pkt);
  145. void handleCommand_Null(NetworkPacket* pkt) {};
  146. void handleCommand_Deprecated(NetworkPacket* pkt);
  147. void handleCommand_Init(NetworkPacket* pkt);
  148. void handleCommand_Init_Legacy(NetworkPacket* pkt);
  149. void handleCommand_Init2(NetworkPacket* pkt);
  150. void handleCommand_RequestMedia(NetworkPacket* pkt);
  151. void handleCommand_ClientReady(NetworkPacket* pkt);
  152. void handleCommand_GotBlocks(NetworkPacket* pkt);
  153. void handleCommand_PlayerPos(NetworkPacket* pkt);
  154. void handleCommand_DeletedBlocks(NetworkPacket* pkt);
  155. void handleCommand_InventoryAction(NetworkPacket* pkt);
  156. void handleCommand_ChatMessage(NetworkPacket* pkt);
  157. void handleCommand_Damage(NetworkPacket* pkt);
  158. void handleCommand_Password(NetworkPacket* pkt);
  159. void handleCommand_PlayerItem(NetworkPacket* pkt);
  160. void handleCommand_Respawn(NetworkPacket* pkt);
  161. void handleCommand_Interact(NetworkPacket* pkt);
  162. void handleCommand_RemovedSounds(NetworkPacket* pkt);
  163. void handleCommand_NodeMetaFields(NetworkPacket* pkt);
  164. void handleCommand_InventoryFields(NetworkPacket* pkt);
  165. void handleCommand_FirstSrp(NetworkPacket* pkt);
  166. void handleCommand_SrpBytesA(NetworkPacket* pkt);
  167. void handleCommand_SrpBytesM(NetworkPacket* pkt);
  168. void ProcessData(NetworkPacket *pkt);
  169. void Send(NetworkPacket* pkt);
  170. // Helper for handleCommand_PlayerPos and handleCommand_Interact
  171. void process_PlayerPos(RemotePlayer *player, PlayerSAO *playersao,
  172. NetworkPacket *pkt);
  173. // Both setter and getter need no envlock,
  174. // can be called freely from threads
  175. void setTimeOfDay(u32 time);
  176. /*
  177. Shall be called with the environment locked.
  178. This is accessed by the map, which is inside the environment,
  179. so it shouldn't be a problem.
  180. */
  181. void onMapEditEvent(MapEditEvent *event);
  182. /*
  183. Shall be called with the environment and the connection locked.
  184. */
  185. Inventory* getInventory(const InventoryLocation &loc);
  186. void setInventoryModified(const InventoryLocation &loc, bool playerSend = true);
  187. // Connection must be locked when called
  188. std::wstring getStatusString();
  189. inline double getUptime() const { return m_uptime.m_value; }
  190. // read shutdown state
  191. inline bool getShutdownRequested() const { return m_shutdown_requested; }
  192. // request server to shutdown
  193. void requestShutdown(const std::string &msg, bool reconnect, float delay = 0.0f);
  194. // Returns -1 if failed, sound handle on success
  195. // Envlock
  196. s32 playSound(const SimpleSoundSpec &spec, const ServerSoundParams &params);
  197. void stopSound(s32 handle);
  198. void fadeSound(s32 handle, float step, float gain);
  199. // Envlock
  200. std::set<std::string> getPlayerEffectivePrivs(const std::string &name);
  201. bool checkPriv(const std::string &name, const std::string &priv);
  202. void reportPrivsModified(const std::string &name=""); // ""=all
  203. void reportInventoryFormspecModified(const std::string &name);
  204. void setIpBanned(const std::string &ip, const std::string &name);
  205. void unsetIpBanned(const std::string &ip_or_name);
  206. std::string getBanDescription(const std::string &ip_or_name);
  207. void notifyPlayer(const char *name, const std::wstring &msg);
  208. void notifyPlayers(const std::wstring &msg);
  209. void spawnParticle(const std::string &playername,
  210. v3f pos, v3f velocity, v3f acceleration,
  211. float expirationtime, float size,
  212. bool collisiondetection, bool collision_removal,
  213. bool vertical, const std::string &texture,
  214. const struct TileAnimationParams &animation, u8 glow);
  215. u32 addParticleSpawner(u16 amount, float spawntime,
  216. v3f minpos, v3f maxpos,
  217. v3f minvel, v3f maxvel,
  218. v3f minacc, v3f maxacc,
  219. float minexptime, float maxexptime,
  220. float minsize, float maxsize,
  221. bool collisiondetection, bool collision_removal,
  222. ServerActiveObject *attached,
  223. bool vertical, const std::string &texture,
  224. const std::string &playername, const struct TileAnimationParams &animation,
  225. u8 glow);
  226. void deleteParticleSpawner(const std::string &playername, u32 id);
  227. // Creates or resets inventory
  228. Inventory* createDetachedInventory(const std::string &name, const std::string &player="");
  229. // Envlock and conlock should be locked when using scriptapi
  230. ServerScripting *getScriptIface(){ return m_script; }
  231. // actions: time-reversed list
  232. // Return value: success/failure
  233. bool rollbackRevertActions(const std::list<RollbackAction> &actions,
  234. std::list<std::string> *log);
  235. // IGameDef interface
  236. // Under envlock
  237. virtual IItemDefManager* getItemDefManager();
  238. virtual INodeDefManager* getNodeDefManager();
  239. virtual ICraftDefManager* getCraftDefManager();
  240. virtual u16 allocateUnknownNodeId(const std::string &name);
  241. virtual MtEventManager* getEventManager();
  242. IRollbackManager *getRollbackManager() { return m_rollback; }
  243. virtual EmergeManager *getEmergeManager() { return m_emerge; }
  244. IWritableItemDefManager* getWritableItemDefManager();
  245. IWritableNodeDefManager* getWritableNodeDefManager();
  246. IWritableCraftDefManager* getWritableCraftDefManager();
  247. virtual const std::vector<ModSpec> &getMods() const { return m_mods; }
  248. virtual const ModSpec* getModSpec(const std::string &modname) const;
  249. void getModNames(std::vector<std::string> &modlist);
  250. std::string getBuiltinLuaPath();
  251. virtual std::string getWorldPath() const { return m_path_world; }
  252. virtual std::string getModStoragePath() const;
  253. inline bool isSingleplayer()
  254. { return m_simple_singleplayer_mode; }
  255. inline void setAsyncFatalError(const std::string &error)
  256. { m_async_fatal_error.set(error); }
  257. bool showFormspec(const char *name, const std::string &formspec, const std::string &formname);
  258. Map & getMap() { return m_env->getMap(); }
  259. ServerEnvironment & getEnv() { return *m_env; }
  260. v3f findSpawnPos();
  261. u32 hudAdd(RemotePlayer *player, HudElement *element);
  262. bool hudRemove(RemotePlayer *player, u32 id);
  263. bool hudChange(RemotePlayer *player, u32 id, HudElementStat stat, void *value);
  264. bool hudSetFlags(RemotePlayer *player, u32 flags, u32 mask);
  265. bool hudSetHotbarItemcount(RemotePlayer *player, s32 hotbar_itemcount);
  266. s32 hudGetHotbarItemcount(RemotePlayer *player) const
  267. { return player->getHotbarItemcount(); }
  268. void hudSetHotbarImage(RemotePlayer *player, std::string name);
  269. std::string hudGetHotbarImage(RemotePlayer *player);
  270. void hudSetHotbarSelectedImage(RemotePlayer *player, std::string name);
  271. const std::string &hudGetHotbarSelectedImage(RemotePlayer *player) const
  272. {
  273. return player->getHotbarSelectedImage();
  274. }
  275. inline Address getPeerAddress(u16 peer_id)
  276. { return m_con.GetPeerAddress(peer_id); }
  277. bool setLocalPlayerAnimations(RemotePlayer *player, v2s32 animation_frames[4],
  278. f32 frame_speed);
  279. bool setPlayerEyeOffset(RemotePlayer *player, v3f first, v3f third);
  280. bool setSky(RemotePlayer *player, const video::SColor &bgcolor,
  281. const std::string &type, const std::vector<std::string> &params,
  282. bool &clouds);
  283. bool setClouds(RemotePlayer *player, float density,
  284. const video::SColor &color_bright,
  285. const video::SColor &color_ambient,
  286. float height,
  287. float thickness,
  288. const v2f &speed);
  289. bool overrideDayNightRatio(RemotePlayer *player, bool do_override, float brightness);
  290. /* con::PeerHandler implementation. */
  291. void peerAdded(con::Peer *peer);
  292. void deletingPeer(con::Peer *peer, bool timeout);
  293. void DenySudoAccess(u16 peer_id);
  294. void DenyAccessVerCompliant(u16 peer_id, u16 proto_ver, AccessDeniedCode reason,
  295. const std::string &str_reason = "", bool reconnect = false);
  296. void DenyAccess(u16 peer_id, AccessDeniedCode reason, const std::string &custom_reason="");
  297. void acceptAuth(u16 peer_id, bool forSudoMode);
  298. void DenyAccess_Legacy(u16 peer_id, const std::wstring &reason);
  299. bool getClientConInfo(u16 peer_id, con::rtt_stat_type type,float* retval);
  300. bool getClientInfo(u16 peer_id,ClientState* state, u32* uptime,
  301. u8* ser_vers, u16* prot_vers, u8* major, u8* minor, u8* patch,
  302. std::string* vers_string);
  303. void printToConsoleOnly(const std::string &text);
  304. void SendPlayerHPOrDie(PlayerSAO *player);
  305. void SendPlayerBreath(PlayerSAO *sao);
  306. void SendInventory(PlayerSAO* playerSAO);
  307. void SendMovePlayer(u16 peer_id);
  308. virtual bool registerModStorage(ModMetadata *storage);
  309. virtual void unregisterModStorage(const std::string &name);
  310. // Bind address
  311. Address m_bind_addr;
  312. // Environment mutex (envlock)
  313. std::mutex m_env_mutex;
  314. private:
  315. friend class EmergeThread;
  316. friend class RemoteClient;
  317. void SendMovement(u16 peer_id);
  318. void SendHP(u16 peer_id, u8 hp);
  319. void SendBreath(u16 peer_id, u16 breath);
  320. void SendAccessDenied(u16 peer_id, AccessDeniedCode reason,
  321. const std::string &custom_reason, bool reconnect = false);
  322. void SendAccessDenied_Legacy(u16 peer_id, const std::wstring &reason);
  323. void SendDeathscreen(u16 peer_id,bool set_camera_point_target, v3f camera_point_target);
  324. void SendItemDef(u16 peer_id,IItemDefManager *itemdef, u16 protocol_version);
  325. void SendNodeDef(u16 peer_id,INodeDefManager *nodedef, u16 protocol_version);
  326. /* mark blocks not sent for all clients */
  327. void SetBlocksNotSent(std::map<v3s16, MapBlock *>& block);
  328. void SendChatMessage(u16 peer_id, const std::wstring &message);
  329. void SendTimeOfDay(u16 peer_id, u16 time, f32 time_speed);
  330. void SendPlayerHP(u16 peer_id);
  331. void SendLocalPlayerAnimations(u16 peer_id, v2s32 animation_frames[4], f32 animation_speed);
  332. void SendEyeOffset(u16 peer_id, v3f first, v3f third);
  333. void SendPlayerPrivileges(u16 peer_id);
  334. void SendPlayerInventoryFormspec(u16 peer_id);
  335. void SendShowFormspecMessage(u16 peer_id, const std::string &formspec, const std::string &formname);
  336. void SendHUDAdd(u16 peer_id, u32 id, HudElement *form);
  337. void SendHUDRemove(u16 peer_id, u32 id);
  338. void SendHUDChange(u16 peer_id, u32 id, HudElementStat stat, void *value);
  339. void SendHUDSetFlags(u16 peer_id, u32 flags, u32 mask);
  340. void SendHUDSetParam(u16 peer_id, u16 param, const std::string &value);
  341. void SendSetSky(u16 peer_id, const video::SColor &bgcolor,
  342. const std::string &type, const std::vector<std::string> &params,
  343. bool &clouds);
  344. void SendCloudParams(u16 peer_id, float density,
  345. const video::SColor &color_bright,
  346. const video::SColor &color_ambient,
  347. float height,
  348. float thickness,
  349. const v2f &speed);
  350. void SendOverrideDayNightRatio(u16 peer_id, bool do_override, float ratio);
  351. /*
  352. Send a node removal/addition event to all clients except ignore_id.
  353. Additionally, if far_players!=NULL, players further away than
  354. far_d_nodes are ignored and their peer_ids are added to far_players
  355. */
  356. // Envlock and conlock should be locked when calling these
  357. void sendRemoveNode(v3s16 p, u16 ignore_id=0,
  358. std::vector<u16> *far_players=NULL, float far_d_nodes=100);
  359. void sendAddNode(v3s16 p, MapNode n, u16 ignore_id=0,
  360. std::vector<u16> *far_players=NULL, float far_d_nodes=100,
  361. bool remove_metadata=true);
  362. void setBlockNotSent(v3s16 p);
  363. // Environment and Connection must be locked when called
  364. void SendBlockNoLock(u16 peer_id, MapBlock *block, u8 ver, u16 net_proto_version);
  365. // Sends blocks to clients (locks env and con on its own)
  366. void SendBlocks(float dtime);
  367. void fillMediaCache();
  368. void sendMediaAnnouncement(u16 peer_id);
  369. void sendRequestedMedia(u16 peer_id,
  370. const std::vector<std::string> &tosend);
  371. void sendDetachedInventory(const std::string &name, u16 peer_id);
  372. void sendDetachedInventories(u16 peer_id);
  373. // Adds a ParticleSpawner on peer with peer_id (PEER_ID_INEXISTENT == all)
  374. void SendAddParticleSpawner(u16 peer_id, u16 protocol_version,
  375. u16 amount, float spawntime,
  376. v3f minpos, v3f maxpos,
  377. v3f minvel, v3f maxvel,
  378. v3f minacc, v3f maxacc,
  379. float minexptime, float maxexptime,
  380. float minsize, float maxsize,
  381. bool collisiondetection, bool collision_removal,
  382. u16 attached_id,
  383. bool vertical, const std::string &texture, u32 id,
  384. const struct TileAnimationParams &animation, u8 glow);
  385. void SendDeleteParticleSpawner(u16 peer_id, u32 id);
  386. // Spawns particle on peer with peer_id (PEER_ID_INEXISTENT == all)
  387. void SendSpawnParticle(u16 peer_id, u16 protocol_version,
  388. v3f pos, v3f velocity, v3f acceleration,
  389. float expirationtime, float size,
  390. bool collisiondetection, bool collision_removal,
  391. bool vertical, const std::string &texture,
  392. const struct TileAnimationParams &animation, u8 glow);
  393. u32 SendActiveObjectRemoveAdd(u16 peer_id, const std::string &datas);
  394. void SendActiveObjectMessages(u16 peer_id, const std::string &datas, bool reliable = true);
  395. /*
  396. Something random
  397. */
  398. void DiePlayer(u16 peer_id);
  399. void RespawnPlayer(u16 peer_id);
  400. void DeleteClient(u16 peer_id, ClientDeletionReason reason);
  401. void UpdateCrafting(RemotePlayer *player);
  402. void handleChatInterfaceEvent(ChatEvent *evt);
  403. // This returns the answer to the sender of wmessage, or "" if there is none
  404. std::wstring handleChat(const std::string &name, const std::wstring &wname,
  405. std::wstring wmessage_input,
  406. bool check_shout_priv = false,
  407. RemotePlayer *player = NULL);
  408. void handleAdminChat(const ChatEventChat *evt);
  409. // When called, connection mutex should be locked
  410. RemoteClient* getClient(u16 peer_id,ClientState state_min=CS_Active);
  411. RemoteClient* getClientNoEx(u16 peer_id,ClientState state_min=CS_Active);
  412. // When called, environment mutex should be locked
  413. std::string getPlayerName(u16 peer_id);
  414. PlayerSAO* getPlayerSAO(u16 peer_id);
  415. /*
  416. Get a player from memory or creates one.
  417. If player is already connected, return NULL
  418. Does not verify/modify auth info and password.
  419. Call with env and con locked.
  420. */
  421. PlayerSAO *emergePlayer(const char *name, u16 peer_id, u16 proto_version);
  422. void handlePeerChanges();
  423. /*
  424. Variables
  425. */
  426. // World directory
  427. std::string m_path_world;
  428. // Subgame specification
  429. SubgameSpec m_gamespec;
  430. // If true, do not allow multiple players and hide some multiplayer
  431. // functionality
  432. bool m_simple_singleplayer_mode;
  433. u16 m_max_chatmessage_length;
  434. // For "dedicated" server list flag
  435. bool m_dedicated;
  436. // Thread can set; step() will throw as ServerError
  437. MutexedVariable<std::string> m_async_fatal_error;
  438. // Some timers
  439. float m_liquid_transform_timer = 0.0f;
  440. float m_liquid_transform_every = 1.0f;
  441. float m_masterserver_timer = 0.0f;
  442. float m_emergethread_trigger_timer = 0.0f;
  443. float m_savemap_timer = 0.0f;
  444. IntervalLimiter m_map_timer_and_unload_interval;
  445. // Environment
  446. ServerEnvironment *m_env = nullptr;
  447. // server connection
  448. con::Connection m_con;
  449. // Ban checking
  450. BanManager *m_banmanager = nullptr;
  451. // Rollback manager (behind m_env_mutex)
  452. IRollbackManager *m_rollback = nullptr;
  453. bool m_enable_rollback_recording = false; // Updated once in a while
  454. // Emerge manager
  455. EmergeManager *m_emerge = nullptr;
  456. // Scripting
  457. // Envlock and conlock should be locked when using Lua
  458. ServerScripting *m_script = nullptr;
  459. // Item definition manager
  460. IWritableItemDefManager *m_itemdef;
  461. // Node definition manager
  462. IWritableNodeDefManager *m_nodedef;
  463. // Craft definition manager
  464. IWritableCraftDefManager *m_craftdef;
  465. // Event manager
  466. EventManager *m_event;
  467. // Mods
  468. std::vector<ModSpec> m_mods;
  469. /*
  470. Threads
  471. */
  472. // A buffer for time steps
  473. // step() increments and AsyncRunStep() run by m_thread reads it.
  474. float m_step_dtime = 0.0f;
  475. std::mutex m_step_dtime_mutex;
  476. // current server step lag counter
  477. float m_lag;
  478. // The server mainly operates in this thread
  479. ServerThread *m_thread = nullptr;
  480. /*
  481. Time related stuff
  482. */
  483. // Timer for sending time of day over network
  484. float m_time_of_day_send_timer = 0.0f;
  485. // Uptime of server in seconds
  486. MutexedVariable<double> m_uptime;
  487. /*
  488. Client interface
  489. */
  490. ClientInterface m_clients;
  491. /*
  492. Peer change queue.
  493. Queues stuff from peerAdded() and deletingPeer() to
  494. handlePeerChanges()
  495. */
  496. std::queue<con::PeerChange> m_peer_change_queue;
  497. /*
  498. Random stuff
  499. */
  500. bool m_shutdown_requested = false;
  501. std::string m_shutdown_msg;
  502. bool m_shutdown_ask_reconnect = false;
  503. float m_shutdown_timer = 0.0f;
  504. ChatInterface *m_admin_chat;
  505. std::string m_admin_nick;
  506. /*
  507. Map edit event queue. Automatically receives all map edits.
  508. The constructor of this class registers us to receive them through
  509. onMapEditEvent
  510. NOTE: Should these be moved to actually be members of
  511. ServerEnvironment?
  512. */
  513. /*
  514. Queue of map edits from the environment for sending to the clients
  515. This is behind m_env_mutex
  516. */
  517. std::queue<MapEditEvent*> m_unsent_map_edit_queue;
  518. /*
  519. Set to true when the server itself is modifying the map and does
  520. all sending of information by itself.
  521. This is behind m_env_mutex
  522. */
  523. bool m_ignore_map_edit_events = false;
  524. /*
  525. If a non-empty area, map edit events contained within are left
  526. unsent. Done at map generation time to speed up editing of the
  527. generated area, as it will be sent anyway.
  528. This is behind m_env_mutex
  529. */
  530. VoxelArea m_ignore_map_edit_events_area;
  531. /*
  532. If set to !=0, the incoming MapEditEvents are modified to have
  533. this peed id as the disabled recipient
  534. This is behind m_env_mutex
  535. */
  536. u16 m_ignore_map_edit_events_peer_id = 0;
  537. // media files known to server
  538. std::unordered_map<std::string, MediaInfo> m_media;
  539. /*
  540. Sounds
  541. */
  542. std::unordered_map<s32, ServerPlayingSound> m_playing_sounds;
  543. s32 m_next_sound_id = 0;
  544. /*
  545. Detached inventories (behind m_env_mutex)
  546. */
  547. // key = name
  548. std::map<std::string, Inventory*> m_detached_inventories;
  549. // value = "" (visible to all players) or player name
  550. std::map<std::string, std::string> m_detached_inventories_player;
  551. std::unordered_map<std::string, ModMetadata *> m_mod_storages;
  552. float m_mod_storage_save_timer = 10.0f;
  553. };
  554. /*
  555. Runs a simple dedicated server loop.
  556. Shuts down when kill is set to true.
  557. */
  558. void dedicated_server_loop(Server &server, bool &kill);
  559. #endif