server.h 21 KB

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