X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fserver.h;h=7065efacfe6e4d4d3f383402584bbf0b0490f196;hb=785a2fd23a47af3629db824adc5e4e1376880d4d;hp=9c5850bff69bd1c19281d7743ef8aac810895fb8;hpb=d10627a77fe022263e2319be282fbec68e713545;p=minetest.git diff --git a/src/server.h b/src/server.h index 9c5850bff..7065efacf 100644 --- a/src/server.h +++ b/src/server.h @@ -1,5 +1,20 @@ /* -(c) 2010 Perttu Ahola +Minetest-c55 +Copyright (C) 2010-2011 celeron55, Perttu Ahola + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #ifndef SERVER_HEADER @@ -9,15 +24,22 @@ #include "environment.h" #include "common_irrlicht.h" #include +#include "utility.h" +#include "porting.h" +#include "map.h" +#include "inventory.h" +#include "auth.h" +#include "ban.h" -#ifdef _WIN32 - #include - #define sleep_ms(x) Sleep(x) -#else - #include - #define sleep_ms(x) usleep(x*1000) -#endif +/* + Some random functions +*/ +v3f findSpawnPos(ServerMap &map); +/* + A structure containing the data needed for queueing the fetching + of blocks. +*/ struct QueuedBlockEmerge { v3s16 pos; @@ -127,44 +149,6 @@ class BlockEmergeQueue JMutex m_mutex; }; -class SimpleThread : public JThread -{ - bool run; - JMutex run_mutex; - -public: - - SimpleThread(): - JThread(), - run(true) - { - run_mutex.Init(); - } - - virtual ~SimpleThread() - {} - - virtual void * Thread() = 0; - - bool getRun() - { - JMutexAutoLock lock(run_mutex); - return run; - } - void setRun(bool a_run) - { - JMutexAutoLock lock(run_mutex); - run = a_run; - } - - void stop() - { - setRun(false); - while(IsRunning()) - sleep_ms(100); - } -}; - class Server; class ServerThread : public SimpleThread @@ -252,20 +236,23 @@ class RemoteClient u16 peer_id; // The serialization version to use with the client u8 serialization_version; + // + u16 net_proto_version; // Version is stored in here after INIT before INIT2 u8 pending_serialization_version; RemoteClient(): - m_time_from_building(0.0) - //m_num_blocks_in_emerge_queue(0) + m_time_from_building(9999), + m_excess_gotblocks(0) { peer_id = 0; serialization_version = SER_FMT_VER_INVALID; + net_proto_version = 0; pending_serialization_version = SER_FMT_VER_INVALID; m_nearest_unsent_d = 0; - - m_blocks_sent_mutex.Init(); - m_blocks_sending_mutex.Init(); + m_nearest_unsent_reset_timer = 0.0; + m_nothing_to_send_counter = 0; + m_nothing_to_send_pause_timer = 0; } ~RemoteClient() { @@ -279,9 +266,11 @@ class RemoteClient void GetNextBlocks(Server *server, float dtime, core::array &dest); - // Connection and environment should be locked when this is called - // steps() objects of blocks not found in active_blocks, then - // adds those blocks to active_blocks + /* + Connection and environment should be locked when this is called. + steps() objects of blocks not found in active_blocks, then + adds those blocks to active_blocks + */ void SendObjectData( Server *server, float dtime, @@ -295,17 +284,8 @@ class RemoteClient void SetBlockNotSent(v3s16 p); void SetBlocksNotSent(core::map &blocks); - //void BlockEmerged(); - - /*bool IsSendingBlock(v3s16 p) - { - JMutexAutoLock lock(m_blocks_sending_mutex); - return (m_blocks_sending.find(p) != NULL); - }*/ - s32 SendingCount() { - JMutexAutoLock lock(m_blocks_sending_mutex); return m_blocks_sending.size(); } @@ -316,36 +296,31 @@ class RemoteClient void PrintInfo(std::ostream &o) { - JMutexAutoLock l2(m_blocks_sent_mutex); - JMutexAutoLock l3(m_blocks_sending_mutex); o<<"RemoteClient "< *far_players=NULL, float far_d_nodes=100); + void sendAddNode(v3s16 p, MapNode n, u16 ignore_id=0, + core::list *far_players=NULL, float far_d_nodes=100); + void setBlockNotSent(v3s16 p); + + // Environment and Connection must be locked when called + void SendBlockNoLock(u16 peer_id, MapBlock *block, u8 ver); + + // Sends blocks to clients (locks env and con on its own) void SendBlocks(float dtime); + + /* + Something random + */ + + void UpdateCrafting(u16 peer_id); // When called, connection mutex should be locked RemoteClient* getClient(u16 peer_id); + // When called, environment mutex should be locked + std::string getPlayerName(u16 peer_id) + { + Player *player = m_env.getPlayer(peer_id); + if(player == NULL) + return "[id="+itos(peer_id); + return player->getName(); + } + + /* + Get a player from memory or creates one. + If player is already connected, return NULL + The password is not checked here - it is only used to + set the password if a new player is created. + + Call with env and con locked. + */ + Player *emergePlayer(const char *name, const char *password, u16 peer_id); + + // Locks environment and connection by its own + struct PeerChange; + void handlePeerChange(PeerChange &c); + void handlePeerChanges(); + + u64 getPlayerPrivs(Player *player); + + /* + Variables + */ + + // Some timers + float m_liquid_transform_timer; + float m_print_info_timer; + float m_objectdata_timer; + float m_emergethread_trigger_timer; + float m_savemap_timer; + IntervalLimiter m_map_timer_and_unload_interval; + // NOTE: If connection and environment are both to be locked, // environment shall be locked first. + // Environment + ServerEnvironment m_env; JMutex m_env_mutex; - Environment m_env; - - JMutex m_con_mutex; + + // Connection con::Connection m_con; - core::map m_clients; // Behind the con mutex + JMutex m_con_mutex; + // Connected clients (behind the con mutex) + core::map m_clients; + + // User authentication + AuthManager m_authmanager; + // Bann checking + BanManager m_banmanager; + + /* + Threads + */ + + // A buffer for time steps + // step() increments and AsyncRunStep() run by m_thread reads it. float m_step_dtime; JMutex m_step_dtime_mutex; + // The server mainly operates in this thread ServerThread m_thread; + // This thread fetches and generates map EmergeThread m_emergethread; - + // Queue of block coordinates to be processed by the emerge thread BlockEmergeQueue m_emerge_queue; + /* + Time related stuff + */ + + // 0-23999 + //MutexedVariable m_time_of_day; + // Used to buffer dtime for adding to m_time_of_day + float m_time_counter; + // Timer for sending time of day over network + float m_time_of_day_send_timer; + // Uptime of server in seconds + MutexedVariable m_uptime; + + /* + Peer change queue. + Queues stuff from peerAdded() and deletingPeer() to + handlePeerChanges() + */ + enum PeerChangeType + { + PEER_ADDED, + PEER_REMOVED + }; + struct PeerChange + { + PeerChangeType type; + u16 peer_id; + bool timeout; + }; + Queue m_peer_change_queue; + + /* + Random stuff + */ + + // Map directory + std::string m_mapsavedir; + + // Configuration path ("" = no configuration file) + std::string m_configpath; + + bool m_shutdown_requested; + + /* + Map edit event queue. Automatically receives all map edits. + The constructor of this class registers us to receive them through + onMapEditEvent + + NOTE: Should these be moved to actually be members of + ServerEnvironment? + */ + + /* + Queue of map edits from the environment for sending to the clients + This is behind m_env_mutex + */ + Queue m_unsent_map_edit_queue; + /* + Set to true when the server itself is modifying the map and does + all sending of information by itself. + This is behind m_env_mutex + */ + bool m_ignore_map_edit_events; + /* + If set to !=0, the incoming MapEditEvents are modified to have + this peed id as the disabled recipient + This is behind m_env_mutex + */ + u16 m_ignore_map_edit_events_peer_id; + + Profiler *m_profiler; + friend class EmergeThread; friend class RemoteClient; }; +/* + Runs a simple dedicated server loop. + + Shuts down when run is set to false. +*/ +void dedicated_server_loop(Server &server, bool &run); + #endif