3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "serialization.h" // For SER_FMT_VER_INVALID
27 #include "content/mods.h"
28 #include "inventorymanager.h"
29 #include "content/subgames.h"
30 #include "tileanimation.h" // TileAnimationParams
31 #include "particles.h" // ParticleParams
32 #include "network/peerhandler.h"
33 #include "network/address.h"
34 #include "util/numeric.h"
35 #include "util/thread.h"
36 #include "util/basic_macros.h"
37 #include "util/metricsbackend.h"
38 #include "serverenvironment.h"
39 #include "clientiface.h"
40 #include "chatmessage.h"
49 class IWritableItemDefManager;
51 class IWritableCraftDefManager;
58 struct PlayerHPChangeReason;
59 class IRollbackManager;
60 struct RollbackAction;
62 class ServerScripting;
63 class ServerEnvironment;
64 struct SimpleSoundSpec;
71 class ServerModManager;
72 class ServerInventoryManager;
74 enum ClientDeletionReason {
83 std::string sha1_digest;
85 MediaInfo(const std::string &path_="",
86 const std::string &sha1_digest_=""):
88 sha1_digest(sha1_digest_)
93 struct ServerSoundParams
104 float max_hear_distance = 32 * BS;
107 std::string to_player = "";
108 std::string exclude_player = "";
110 v3f getPos(ServerEnvironment *env, bool *pos_exists) const;
113 struct ServerPlayingSound
115 ServerSoundParams params;
116 SimpleSoundSpec spec;
117 std::unordered_set<session_t> clients; // peer ids
120 class Server : public con::PeerHandler, public MapEventReceiver,
125 NOTE: Every public method should be thread-safe
129 const std::string &path_world,
130 const SubgameSpec &gamespec,
131 bool simple_singleplayer_mode,
134 ChatInterface *iface = nullptr,
135 std::string *on_shutdown_errmsg = nullptr
138 DISABLE_CLASS_COPY(Server);
142 // This is mainly a way to pass the time to the server.
143 // Actual processing is done in an another thread.
144 void step(float dtime);
145 // This is run by ServerThread and does the actual processing
146 void AsyncRunStep(bool initial_step=false);
148 PlayerSAO* StageTwoClientInit(session_t peer_id);
154 void handleCommand(NetworkPacket* pkt);
156 void handleCommand_Null(NetworkPacket* pkt) {};
157 void handleCommand_Deprecated(NetworkPacket* pkt);
158 void handleCommand_Init(NetworkPacket* pkt);
159 void handleCommand_Init2(NetworkPacket* pkt);
160 void handleCommand_ModChannelJoin(NetworkPacket *pkt);
161 void handleCommand_ModChannelLeave(NetworkPacket *pkt);
162 void handleCommand_ModChannelMsg(NetworkPacket *pkt);
163 void handleCommand_RequestMedia(NetworkPacket* pkt);
164 void handleCommand_ClientReady(NetworkPacket* pkt);
165 void handleCommand_GotBlocks(NetworkPacket* pkt);
166 void handleCommand_PlayerPos(NetworkPacket* pkt);
167 void handleCommand_DeletedBlocks(NetworkPacket* pkt);
168 void handleCommand_InventoryAction(NetworkPacket* pkt);
169 void handleCommand_ChatMessage(NetworkPacket* pkt);
170 void handleCommand_Damage(NetworkPacket* pkt);
171 void handleCommand_PlayerItem(NetworkPacket* pkt);
172 void handleCommand_Respawn(NetworkPacket* pkt);
173 void handleCommand_Interact(NetworkPacket* pkt);
174 void handleCommand_RemovedSounds(NetworkPacket* pkt);
175 void handleCommand_NodeMetaFields(NetworkPacket* pkt);
176 void handleCommand_InventoryFields(NetworkPacket* pkt);
177 void handleCommand_FirstSrp(NetworkPacket* pkt);
178 void handleCommand_SrpBytesA(NetworkPacket* pkt);
179 void handleCommand_SrpBytesM(NetworkPacket* pkt);
181 void ProcessData(NetworkPacket *pkt);
183 void Send(NetworkPacket *pkt);
184 void Send(session_t peer_id, NetworkPacket *pkt);
186 // Helper for handleCommand_PlayerPos and handleCommand_Interact
187 void process_PlayerPos(RemotePlayer *player, PlayerSAO *playersao,
190 // Both setter and getter need no envlock,
191 // can be called freely from threads
192 void setTimeOfDay(u32 time);
195 Shall be called with the environment locked.
196 This is accessed by the map, which is inside the environment,
197 so it shouldn't be a problem.
199 void onMapEditEvent(const MapEditEvent &event);
201 // Connection must be locked when called
202 std::wstring getStatusString();
203 inline double getUptime() const { return m_uptime_counter->get(); }
205 // read shutdown state
206 inline bool isShutdownRequested() const { return m_shutdown_state.is_requested; }
208 // request server to shutdown
209 void requestShutdown(const std::string &msg, bool reconnect, float delay = 0.0f);
211 // Returns -1 if failed, sound handle on success
213 s32 playSound(const SimpleSoundSpec &spec, const ServerSoundParams ¶ms,
214 bool ephemeral=false);
215 void stopSound(s32 handle);
216 void fadeSound(s32 handle, float step, float gain);
219 std::set<std::string> getPlayerEffectivePrivs(const std::string &name);
220 bool checkPriv(const std::string &name, const std::string &priv);
221 void reportPrivsModified(const std::string &name=""); // ""=all
222 void reportInventoryFormspecModified(const std::string &name);
223 void reportFormspecPrependModified(const std::string &name);
225 void setIpBanned(const std::string &ip, const std::string &name);
226 void unsetIpBanned(const std::string &ip_or_name);
227 std::string getBanDescription(const std::string &ip_or_name);
229 void notifyPlayer(const char *name, const std::wstring &msg);
230 void notifyPlayers(const std::wstring &msg);
232 void spawnParticle(const std::string &playername,
233 const ParticleParameters &p);
235 u32 addParticleSpawner(const ParticleSpawnerParameters &p,
236 ServerActiveObject *attached, const std::string &playername);
238 void deleteParticleSpawner(const std::string &playername, u32 id);
240 bool dynamicAddMedia(const std::string &filepath);
242 ServerInventoryManager *getInventoryMgr() const { return m_inventory_mgr.get(); }
243 void sendDetachedInventory(Inventory *inventory, const std::string &name, session_t peer_id);
245 // Envlock and conlock should be locked when using scriptapi
246 ServerScripting *getScriptIface(){ return m_script; }
248 // actions: time-reversed list
249 // Return value: success/failure
250 bool rollbackRevertActions(const std::list<RollbackAction> &actions,
251 std::list<std::string> *log);
253 // IGameDef interface
255 virtual IItemDefManager* getItemDefManager();
256 virtual const NodeDefManager* getNodeDefManager();
257 virtual ICraftDefManager* getCraftDefManager();
258 virtual u16 allocateUnknownNodeId(const std::string &name);
259 IRollbackManager *getRollbackManager() { return m_rollback; }
260 virtual EmergeManager *getEmergeManager() { return m_emerge; }
262 IWritableItemDefManager* getWritableItemDefManager();
263 NodeDefManager* getWritableNodeDefManager();
264 IWritableCraftDefManager* getWritableCraftDefManager();
266 virtual const std::vector<ModSpec> &getMods() const;
267 virtual const ModSpec* getModSpec(const std::string &modname) const;
268 void getModNames(std::vector<std::string> &modlist);
269 std::string getBuiltinLuaPath();
270 virtual std::string getWorldPath() const { return m_path_world; }
271 virtual std::string getModStoragePath() const;
273 inline bool isSingleplayer()
274 { return m_simple_singleplayer_mode; }
276 inline void setAsyncFatalError(const std::string &error)
277 { m_async_fatal_error.set(error); }
279 bool showFormspec(const char *name, const std::string &formspec, const std::string &formname);
280 Map & getMap() { return m_env->getMap(); }
281 ServerEnvironment & getEnv() { return *m_env; }
284 u32 hudAdd(RemotePlayer *player, HudElement *element);
285 bool hudRemove(RemotePlayer *player, u32 id);
286 bool hudChange(RemotePlayer *player, u32 id, HudElementStat stat, void *value);
287 bool hudSetFlags(RemotePlayer *player, u32 flags, u32 mask);
288 bool hudSetHotbarItemcount(RemotePlayer *player, s32 hotbar_itemcount);
289 void hudSetHotbarImage(RemotePlayer *player, const std::string &name);
290 void hudSetHotbarSelectedImage(RemotePlayer *player, const std::string &name);
292 Address getPeerAddress(session_t peer_id);
294 void setLocalPlayerAnimations(RemotePlayer *player, v2s32 animation_frames[4],
296 void setPlayerEyeOffset(RemotePlayer *player, const v3f &first, const v3f &third);
298 void setSky(RemotePlayer *player, const SkyboxParams ¶ms);
299 void setSun(RemotePlayer *player, const SunParams ¶ms);
300 void setMoon(RemotePlayer *player, const MoonParams ¶ms);
301 void setStars(RemotePlayer *player, const StarParams ¶ms);
303 void setClouds(RemotePlayer *player, const CloudParams ¶ms);
305 void overrideDayNightRatio(RemotePlayer *player, bool do_override, float brightness);
307 /* con::PeerHandler implementation. */
308 void peerAdded(con::Peer *peer);
309 void deletingPeer(con::Peer *peer, bool timeout);
311 void DenySudoAccess(session_t peer_id);
312 void DenyAccessVerCompliant(session_t peer_id, u16 proto_ver, AccessDeniedCode reason,
313 const std::string &str_reason = "", bool reconnect = false);
314 void DenyAccess(session_t peer_id, AccessDeniedCode reason,
315 const std::string &custom_reason = "");
316 void acceptAuth(session_t peer_id, bool forSudoMode);
317 void DenyAccess_Legacy(session_t peer_id, const std::wstring &reason);
318 void DisconnectPeer(session_t peer_id);
319 bool getClientConInfo(session_t peer_id, con::rtt_stat_type type, float *retval);
320 bool getClientInfo(session_t peer_id, ClientState *state, u32 *uptime,
321 u8* ser_vers, u16* prot_vers, u8* major, u8* minor, u8* patch,
322 std::string* vers_string, std::string* lang_code);
324 void printToConsoleOnly(const std::string &text);
326 void SendPlayerHPOrDie(PlayerSAO *player, const PlayerHPChangeReason &reason);
327 void SendPlayerBreath(PlayerSAO *sao);
328 void SendInventory(PlayerSAO *playerSAO, bool incremental);
329 void SendMovePlayer(session_t peer_id);
330 void SendPlayerSpeed(session_t peer_id, const v3f &added_vel);
331 void SendPlayerFov(session_t peer_id);
333 void sendDetachedInventories(session_t peer_id, bool incremental);
335 virtual bool registerModStorage(ModMetadata *storage);
336 virtual void unregisterModStorage(const std::string &name);
338 bool joinModChannel(const std::string &channel);
339 bool leaveModChannel(const std::string &channel);
340 bool sendModChannelMessage(const std::string &channel, const std::string &message);
341 ModChannel *getModChannel(const std::string &channel);
343 // Send block to specific player only
344 bool SendBlock(session_t peer_id, const v3s16 &blockpos);
346 // Load translations for a language
347 void loadTranslationLanguage(const std::string &lang_code);
352 // Environment mutex (envlock)
353 std::mutex m_env_mutex;
356 friend class EmergeThread;
357 friend class RemoteClient;
358 friend class TestServerShutdownState;
360 struct ShutdownState {
361 friend class TestServerShutdownState;
363 bool is_requested = false;
364 bool should_reconnect = false;
368 void trigger(float delay, const std::string &msg, bool reconnect);
369 void tick(float dtime, Server *server);
370 std::wstring getShutdownTimerMessage() const;
371 bool isTimerRunning() const { return m_timer > 0.0f; }
373 float m_timer = 0.0f;
378 void SendMovement(session_t peer_id);
379 void SendHP(session_t peer_id, u16 hp);
380 void SendBreath(session_t peer_id, u16 breath);
381 void SendAccessDenied(session_t peer_id, AccessDeniedCode reason,
382 const std::string &custom_reason, bool reconnect = false);
383 void SendAccessDenied_Legacy(session_t peer_id, const std::wstring &reason);
384 void SendDeathscreen(session_t peer_id, bool set_camera_point_target,
385 v3f camera_point_target);
386 void SendItemDef(session_t peer_id, IItemDefManager *itemdef, u16 protocol_version);
387 void SendNodeDef(session_t peer_id, const NodeDefManager *nodedef,
388 u16 protocol_version);
390 /* mark blocks not sent for all clients */
391 void SetBlocksNotSent(std::map<v3s16, MapBlock *>& block);
394 virtual void SendChatMessage(session_t peer_id, const ChatMessage &message);
395 void SendTimeOfDay(session_t peer_id, u16 time, f32 time_speed);
396 void SendPlayerHP(session_t peer_id);
398 void SendLocalPlayerAnimations(session_t peer_id, v2s32 animation_frames[4],
399 f32 animation_speed);
400 void SendEyeOffset(session_t peer_id, v3f first, v3f third);
401 void SendPlayerPrivileges(session_t peer_id);
402 void SendPlayerInventoryFormspec(session_t peer_id);
403 void SendPlayerFormspecPrepend(session_t peer_id);
404 void SendShowFormspecMessage(session_t peer_id, const std::string &formspec,
405 const std::string &formname);
406 void SendHUDAdd(session_t peer_id, u32 id, HudElement *form);
407 void SendHUDRemove(session_t peer_id, u32 id);
408 void SendHUDChange(session_t peer_id, u32 id, HudElementStat stat, void *value);
409 void SendHUDSetFlags(session_t peer_id, u32 flags, u32 mask);
410 void SendHUDSetParam(session_t peer_id, u16 param, const std::string &value);
411 void SendSetSky(session_t peer_id, const SkyboxParams ¶ms);
412 void SendSetSun(session_t peer_id, const SunParams ¶ms);
413 void SendSetMoon(session_t peer_id, const MoonParams ¶ms);
414 void SendSetStars(session_t peer_id, const StarParams ¶ms);
415 void SendCloudParams(session_t peer_id, const CloudParams ¶ms);
416 void SendOverrideDayNightRatio(session_t peer_id, bool do_override, float ratio);
417 void broadcastModChannelMessage(const std::string &channel,
418 const std::string &message, session_t from_peer);
421 Send a node removal/addition event to all clients except ignore_id.
422 Additionally, if far_players!=NULL, players further away than
423 far_d_nodes are ignored and their peer_ids are added to far_players
425 // Envlock and conlock should be locked when calling these
426 void sendRemoveNode(v3s16 p, std::unordered_set<u16> *far_players = nullptr,
427 float far_d_nodes = 100);
428 void sendAddNode(v3s16 p, MapNode n,
429 std::unordered_set<u16> *far_players = nullptr,
430 float far_d_nodes = 100, bool remove_metadata = true);
432 void sendMetadataChanged(const std::list<v3s16> &meta_updates,
433 float far_d_nodes = 100);
435 // Environment and Connection must be locked when called
436 void SendBlockNoLock(session_t peer_id, MapBlock *block, u8 ver, u16 net_proto_version);
438 // Sends blocks to clients (locks env and con on its own)
439 void SendBlocks(float dtime);
441 bool addMediaFile(const std::string &filename, const std::string &filepath,
442 std::string *filedata = nullptr, std::string *digest = nullptr);
443 void fillMediaCache();
444 void sendMediaAnnouncement(session_t peer_id, const std::string &lang_code);
445 void sendRequestedMedia(session_t peer_id,
446 const std::vector<std::string> &tosend);
448 // Adds a ParticleSpawner on peer with peer_id (PEER_ID_INEXISTENT == all)
449 void SendAddParticleSpawner(session_t peer_id, u16 protocol_version,
450 const ParticleSpawnerParameters &p, u16 attached_id, u32 id);
452 void SendDeleteParticleSpawner(session_t peer_id, u32 id);
454 // Spawns particle on peer with peer_id (PEER_ID_INEXISTENT == all)
455 void SendSpawnParticle(session_t peer_id, u16 protocol_version,
456 const ParticleParameters &p);
458 void SendActiveObjectRemoveAdd(RemoteClient *client, PlayerSAO *playersao);
459 void SendActiveObjectMessages(session_t peer_id, const std::string &datas,
460 bool reliable = true);
461 void SendCSMRestrictionFlags(session_t peer_id);
467 void DiePlayer(session_t peer_id, const PlayerHPChangeReason &reason);
468 void RespawnPlayer(session_t peer_id);
469 void DeleteClient(session_t peer_id, ClientDeletionReason reason);
470 void UpdateCrafting(RemotePlayer *player);
471 bool checkInteractDistance(RemotePlayer *player, const f32 d, const std::string &what);
473 void handleChatInterfaceEvent(ChatEvent *evt);
475 // This returns the answer to the sender of wmessage, or "" if there is none
476 std::wstring handleChat(const std::string &name, const std::wstring &wname,
477 std::wstring wmessage_input,
478 bool check_shout_priv = false,
479 RemotePlayer *player = NULL);
480 void handleAdminChat(const ChatEventChat *evt);
482 // When called, connection mutex should be locked
483 RemoteClient* getClient(session_t peer_id, ClientState state_min = CS_Active);
484 RemoteClient* getClientNoEx(session_t peer_id, ClientState state_min = CS_Active);
486 // When called, environment mutex should be locked
487 std::string getPlayerName(session_t peer_id);
488 PlayerSAO *getPlayerSAO(session_t peer_id);
491 Get a player from memory or creates one.
492 If player is already connected, return NULL
493 Does not verify/modify auth info and password.
495 Call with env and con locked.
497 PlayerSAO *emergePlayer(const char *name, session_t peer_id, u16 proto_version);
499 void handlePeerChanges();
505 std::string m_path_world;
506 // Subgame specification
507 SubgameSpec m_gamespec;
508 // If true, do not allow multiple players and hide some multiplayer
510 bool m_simple_singleplayer_mode;
511 u16 m_max_chatmessage_length;
512 // For "dedicated" server list flag
515 // Thread can set; step() will throw as ServerError
516 MutexedVariable<std::string> m_async_fatal_error;
519 float m_liquid_transform_timer = 0.0f;
520 float m_liquid_transform_every = 1.0f;
521 float m_masterserver_timer = 0.0f;
522 float m_emergethread_trigger_timer = 0.0f;
523 float m_savemap_timer = 0.0f;
524 IntervalLimiter m_map_timer_and_unload_interval;
527 ServerEnvironment *m_env = nullptr;
530 std::shared_ptr<con::Connection> m_con;
533 BanManager *m_banmanager = nullptr;
535 // Rollback manager (behind m_env_mutex)
536 IRollbackManager *m_rollback = nullptr;
539 EmergeManager *m_emerge = nullptr;
542 // Envlock and conlock should be locked when using Lua
543 ServerScripting *m_script = nullptr;
545 // Item definition manager
546 IWritableItemDefManager *m_itemdef;
548 // Node definition manager
549 NodeDefManager *m_nodedef;
551 // Craft definition manager
552 IWritableCraftDefManager *m_craftdef;
555 EventManager *m_event;
558 std::unique_ptr<ServerModManager> m_modmgr;
563 // A buffer for time steps
564 // step() increments and AsyncRunStep() run by m_thread reads it.
565 float m_step_dtime = 0.0f;
566 std::mutex m_step_dtime_mutex;
568 // The server mainly operates in this thread
569 ServerThread *m_thread = nullptr;
574 // Timer for sending time of day over network
575 float m_time_of_day_send_timer = 0.0f;
580 ClientInterface m_clients;
584 Queues stuff from peerAdded() and deletingPeer() to
587 std::queue<con::PeerChange> m_peer_change_queue;
589 std::unordered_map<session_t, std::string> m_formspec_state_data;
595 ShutdownState m_shutdown_state;
597 ChatInterface *m_admin_chat;
598 std::string m_admin_nick;
600 // if a mod-error occurs in the on_shutdown callback, the error message will
601 // be written into this
602 std::string *const m_on_shutdown_errmsg;
605 Map edit event queue. Automatically receives all map edits.
606 The constructor of this class registers us to receive them through
609 NOTE: Should these be moved to actually be members of
614 Queue of map edits from the environment for sending to the clients
615 This is behind m_env_mutex
617 std::queue<MapEditEvent*> m_unsent_map_edit_queue;
619 If a non-empty area, map edit events contained within are left
620 unsent. Done at map generation time to speed up editing of the
621 generated area, as it will be sent anyway.
622 This is behind m_env_mutex
624 VoxelArea m_ignore_map_edit_events_area;
626 // media files known to server
627 std::unordered_map<std::string, MediaInfo> m_media;
632 std::unordered_map<s32, ServerPlayingSound> m_playing_sounds;
633 s32 m_next_sound_id = 0; // positive values only
636 std::unordered_map<std::string, ModMetadata *> m_mod_storages;
637 float m_mod_storage_save_timer = 10.0f;
639 // CSM restrictions byteflag
640 u64 m_csm_restriction_flags = CSMRestrictionFlags::CSM_RF_NONE;
641 u32 m_csm_restriction_noderange = 8;
643 // ModChannel manager
644 std::unique_ptr<ModChannelMgr> m_modchannel_mgr;
647 std::unique_ptr<ServerInventoryManager> m_inventory_mgr;
649 // Global server metrics backend
650 std::unique_ptr<MetricsBackend> m_metrics_backend;
653 MetricCounterPtr m_uptime_counter;
654 MetricGaugePtr m_player_gauge;
655 MetricGaugePtr m_timeofday_gauge;
656 // current server step lag
657 MetricGaugePtr m_lag_gauge;
658 MetricCounterPtr m_aom_buffer_counter;
659 MetricCounterPtr m_packet_recv_counter;
660 MetricCounterPtr m_packet_recv_processed_counter;
664 Runs a simple dedicated server loop.
666 Shuts down when kill is set to true.
668 void dedicated_server_loop(Server &server, bool &kill);