/* Minetest Copyright (C) 2010-2013 celeron55, Perttu Ahola This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser 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. */ #include "server.h" #include #include #include #include "network/networkprotocol.h" #include "network/serveropcodes.h" #include "ban.h" #include "environment.h" #include "map.h" #include "threading/mutex_auto_lock.h" #include "constants.h" #include "voxel.h" #include "config.h" #include "version.h" #include "filesys.h" #include "mapblock.h" #include "serverobject.h" #include "genericobject.h" #include "settings.h" #include "profiler.h" #include "log.h" #include "scripting_game.h" #include "nodedef.h" #include "itemdef.h" #include "craftdef.h" #include "emerge.h" #include "mapgen.h" #include "mg_biome.h" #include "content_mapnode.h" #include "content_nodemeta.h" #include "content_abm.h" #include "content_sao.h" #include "mods.h" #include "event_manager.h" #include "serverlist.h" #include "util/string.h" #include "rollback.h" #include "util/serialize.h" #include "util/thread.h" #include "defaultsettings.h" #include "util/base64.h" #include "util/sha1.h" #include "util/hex.h" class ClientNotFoundException : public BaseException { public: ClientNotFoundException(const char *s): BaseException(s) {} }; class ServerThread : public Thread { public: ServerThread(Server *server): Thread("Server"), m_server(server) {} void *run(); private: Server *m_server; }; void *ServerThread::run() { DSTACK(FUNCTION_NAME); BEGIN_DEBUG_EXCEPTION_HANDLER m_server->AsyncRunStep(true); while (!stopRequested()) { try { //TimeTaker timer("AsyncRunStep() + Receive()"); m_server->AsyncRunStep(); m_server->Receive(); } catch (con::NoIncomingDataException &e) { } catch (con::PeerNotFoundException &e) { infostream<<"Server: PeerNotFoundException"<setAsyncFatalError(e.what()); } catch (LuaError &e) { m_server->setAsyncFatalError( "ServerThread::run Lua: " + std::string(e.what())); } } END_DEBUG_EXCEPTION_HANDLER return NULL; } v3f ServerSoundParams::getPos(ServerEnvironment *env, bool *pos_exists) const { if(pos_exists) *pos_exists = false; switch(type){ case SSP_LOCAL: return v3f(0,0,0); case SSP_POSITIONAL: if(pos_exists) *pos_exists = true; return pos; case SSP_OBJECT: { if(object == 0) return v3f(0,0,0); ServerActiveObject *sao = env->getActiveObject(object); if(!sao) return v3f(0,0,0); if(pos_exists) *pos_exists = true; return sao->getBasePosition(); } } return v3f(0,0,0); } /* Server */ Server::Server( const std::string &path_world, const SubgameSpec &gamespec, bool simple_singleplayer_mode, bool ipv6, ChatInterface *iface ): m_path_world(path_world), m_gamespec(gamespec), m_simple_singleplayer_mode(simple_singleplayer_mode), m_async_fatal_error(""), m_env(NULL), m_con(PROTOCOL_ID, 512, CONNECTION_TIMEOUT, ipv6, this), m_banmanager(NULL), m_rollback(NULL), m_enable_rollback_recording(false), m_emerge(NULL), m_script(NULL), m_itemdef(createItemDefManager()), m_nodedef(createNodeDefManager()), m_craftdef(createCraftDefManager()), m_event(new EventManager()), m_thread(NULL), m_time_of_day_send_timer(0), m_uptime(0), m_clients(&m_con), m_shutdown_requested(false), m_shutdown_ask_reconnect(false), m_admin_chat(iface), m_ignore_map_edit_events(false), m_ignore_map_edit_events_peer_id(0), m_next_sound_id(0), m_mod_storage_save_timer(10.0f) { m_liquid_transform_timer = 0.0; m_liquid_transform_every = 1.0; m_masterserver_timer = 0.0; m_emergethread_trigger_timer = 0.0; m_savemap_timer = 0.0; m_step_dtime = 0.0; m_lag = g_settings->getFloat("dedicated_server_step"); if(path_world == "") throw ServerError("Supplied empty world path"); if(!gamespec.isValid()) throw ServerError("Supplied invalid gamespec"); infostream<<"Server created for gameid \""< unsatisfied_mods = modconf.getUnsatisfiedMods(); // complain about mods with unsatisfied dependencies if(!modconf.isConsistent()) { for(std::vector::iterator it = unsatisfied_mods.begin(); it != unsatisfied_mods.end(); ++it) { ModSpec mod = *it; errorstream << "mod \"" << mod.name << "\" has unsatisfied dependencies: "; for(std::set::iterator dep_it = mod.unsatisfied_depends.begin(); dep_it != mod.unsatisfied_depends.end(); ++dep_it) errorstream << " \"" << *dep_it << "\""; errorstream << std::endl; } } Settings worldmt_settings; std::string worldmt = m_path_world + DIR_DELIM + "world.mt"; worldmt_settings.readConfigFile(worldmt.c_str()); std::vector names = worldmt_settings.getNames(); std::set load_mod_names; for(std::vector::iterator it = names.begin(); it != names.end(); ++it) { std::string name = *it; if(name.compare(0,9,"load_mod_")==0 && worldmt_settings.getBool(name)) load_mod_names.insert(name.substr(9)); } // complain about mods declared to be loaded, but not found for(std::vector::iterator it = m_mods.begin(); it != m_mods.end(); ++it) load_mod_names.erase((*it).name); for(std::vector::iterator it = unsatisfied_mods.begin(); it != unsatisfied_mods.end(); ++it) load_mod_names.erase((*it).name); if(!load_mod_names.empty()) { errorstream << "The following mods could not be found:"; for(std::set::iterator it = load_mod_names.begin(); it != load_mod_names.end(); ++it) errorstream << " \"" << (*it) << "\""; errorstream << std::endl; } //lock environment MutexAutoLock envlock(m_env_mutex); // Create the Map (loads map_meta.txt, overriding configured mapgen params) ServerMap *servermap = new ServerMap(path_world, this, m_emerge); // Initialize scripting infostream<<"Server: Initializing Lua"<loadMod(script_path, BUILTIN_MOD_NAME); // Print mods infostream << "Server: Loading mods: "; for(std::vector::iterator i = m_mods.begin(); i != m_mods.end(); ++i) { const ModSpec &mod = *i; infostream << mod.name << " "; } infostream << std::endl; // Load and run "mod" scripts for (std::vector::iterator it = m_mods.begin(); it != m_mods.end(); ++it) { const ModSpec &mod = *it; if (!string_allowed(mod.name, MODNAME_ALLOWED_CHARS)) { throw ModError("Error loading mod \"" + mod.name + "\": Mod name does not follow naming conventions: " "Only chararacters [a-z0-9_] are allowed."); } std::string script_path = mod.path + DIR_DELIM + "init.lua"; infostream << " [" << padStringRight(mod.name, 12) << "] [\"" << script_path << "\"]" << std::endl; m_script->loadMod(script_path, mod.name); } // Read Textures and calculate sha1 sums fillMediaCache(); // Apply item aliases in the node definition manager m_nodedef->updateAliases(m_itemdef); // Apply texture overrides from texturepack/override.txt std::string texture_path = g_settings->get("texture_path"); if (texture_path != "" && fs::IsDir(texture_path)) m_nodedef->applyTextureOverrides(texture_path + DIR_DELIM + "override.txt"); m_nodedef->setNodeRegistrationStatus(true); // Perform pending node name resolutions m_nodedef->runNodeResolveCallbacks(); // unmap node names for connected nodeboxes m_nodedef->mapNodeboxConnections(); // init the recipe hashes to speed up crafting m_craftdef->initHashes(this); // Initialize Environment m_env = new ServerEnvironment(servermap, m_script, this, m_path_world); m_clients.setEnv(m_env); if (!servermap->settings_mgr.makeMapgenParams()) FATAL_ERROR("Couldn't create any mapgen type"); // Initialize mapgens m_emerge->initMapgens(servermap->getMapgenParams()); m_enable_rollback_recording = g_settings->getBool("enable_rollback_recording"); if (m_enable_rollback_recording) { // Create rollback manager m_rollback = new RollbackManager(m_path_world, this); } // Give environment reference to scripting api m_script->initializeEnvironment(m_env); // Register us to receive map edit events servermap->addEventReceiver(this); // If file exists, load environment metadata if (fs::PathExists(m_path_world + DIR_DELIM "env_meta.txt")) { infostream << "Server: Loading environment metadata" << std::endl; m_env->loadMeta(); } else { m_env->loadDefaultMeta(); } // Add some test ActiveBlockModifiers to environment add_legacy_abms(m_env, m_nodedef); m_liquid_transform_every = g_settings->getFloat("liquid_update"); m_max_chatmessage_length = g_settings->getU16("chat_message_max_size"); } Server::~Server() { infostream<<"Server destructing"<on_shutdown(); infostream << "Server: Saving players" << std::endl; m_env->saveLoadedPlayers(); infostream << "Server: Kicking players" << std::endl; std::string kick_msg; bool reconnect = false; if (getShutdownRequested()) { reconnect = m_shutdown_ask_reconnect; kick_msg = m_shutdown_msg; } if (kick_msg == "") { kick_msg = g_settings->get("kick_msg_shutdown"); } m_env->kickAllPlayers(SERVER_ACCESSDENIED_SHUTDOWN, kick_msg, reconnect); infostream << "Server: Saving environment metadata" << std::endl; m_env->saveMeta(); } // Stop threads stop(); delete m_thread; // stop all emerge threads before deleting players that may have // requested blocks to be emerged m_emerge->stopThreads(); // Delete things in the reverse order of creation delete m_emerge; delete m_env; delete m_rollback; delete m_banmanager; delete m_event; delete m_itemdef; delete m_nodedef; delete m_craftdef; // Deinitialize scripting infostream<<"Server: Deinitializing scripting"<::iterator i = m_detached_inventories.begin(); i != m_detached_inventories.end(); ++i) { delete i->second; } } void Server::start(Address bind_addr) { DSTACK(FUNCTION_NAME); m_bind_addr = bind_addr; infostream<<"Starting server on " << bind_addr.serializeString() <<"..."<stop(); // Initialize connection m_con.SetTimeoutMs(30); m_con.Serve(bind_addr); // Start thread m_thread->start(); // ASCII art for the win! actionstream <<" .__ __ __ "<__| \\___ >____ > |__| "<stop(); //m_emergethread.setRun(false); m_thread->wait(); //m_emergethread.stop(); infostream<<"Server: Threads stopped"< 2.0) dtime = 2.0; { MutexAutoLock lock(m_step_dtime_mutex); m_step_dtime += dtime; } // Throw if fatal error occurred in thread std::string async_err = m_async_fatal_error.get(); if (!async_err.empty()) { if (!m_simple_singleplayer_mode) { m_env->kickAllPlayers(SERVER_ACCESSDENIED_CRASH, g_settings->get("kick_msg_crash"), g_settings->getBool("ask_reconnect_on_crash")); } throw ServerError("AsyncErr: " + async_err); } } void Server::AsyncRunStep(bool initial_step) { DSTACK(FUNCTION_NAME); g_profiler->add("Server::AsyncRunStep (num)", 1); float dtime; { MutexAutoLock lock1(m_step_dtime_mutex); dtime = m_step_dtime; } { // Send blocks to clients SendBlocks(dtime); } if((dtime < 0.001) && (initial_step == false)) return; g_profiler->add("Server::AsyncRunStep with dtime (num)", 1); //infostream<<"Server steps "<getFloat("time_send_interval"); u16 time = m_env->getTimeOfDay(); float time_speed = g_settings->getFloat("time_speed"); SendTimeOfDay(PEER_ID_INEXISTENT, time, time_speed); } { MutexAutoLock lock(m_env_mutex); // Figure out and report maximum lag to environment float max_lag = m_env->getMaxLagEstimate(); max_lag *= 0.9998; // Decrease slowly (about half per 5 minutes) if(dtime > max_lag){ if(dtime > 0.1 && dtime > max_lag * 2.0) infostream<<"Server: Maximum lag peaked to "<reportMaxLagEstimate(max_lag); // Step environment ScopeProfiler sp(g_profiler, "SEnv step"); ScopeProfiler sp2(g_profiler, "SEnv step avg", SPT_AVG); m_env->step(dtime); } static const float map_timer_and_unload_dtime = 2.92; if(m_map_timer_and_unload_interval.step(dtime, map_timer_and_unload_dtime)) { MutexAutoLock lock(m_env_mutex); // Run Map's timers and unload unused data ScopeProfiler sp(g_profiler, "Server: map timer and unload"); m_env->getMap().timerUpdate(map_timer_and_unload_dtime, g_settings->getFloat("server_unload_unused_data_timeout"), U32_MAX); } /* Listen to the admin chat, if available */ if (m_admin_chat) { if (!m_admin_chat->command_queue.empty()) { MutexAutoLock lock(m_env_mutex); while (!m_admin_chat->command_queue.empty()) { ChatEvent *evt = m_admin_chat->command_queue.pop_frontNoEx(); handleChatInterfaceEvent(evt); delete evt; } } m_admin_chat->outgoing_queue.push_back( new ChatEventTimeInfo(m_env->getGameTime(), m_env->getTimeOfDay())); } /* Do background stuff */ /* Transform liquids */ m_liquid_transform_timer += dtime; if(m_liquid_transform_timer >= m_liquid_transform_every) { m_liquid_transform_timer -= m_liquid_transform_every; MutexAutoLock lock(m_env_mutex); ScopeProfiler sp(g_profiler, "Server: liquid transform"); std::map modified_blocks; m_env->getMap().transformLiquids(modified_blocks); #if 0 /* Update lighting */ core::map lighting_modified_blocks; ServerMap &map = ((ServerMap&)m_env->getMap()); map.updateLighting(modified_blocks, lighting_modified_blocks); // Add blocks modified by lighting to modified_blocks for(core::map::Iterator i = lighting_modified_blocks.getIterator(); i.atEnd() == false; i++) { MapBlock *block = i.getNode()->getValue(); modified_blocks.insert(block->getPos(), block); } #endif /* Set the modified blocks unsent for all the clients */ if(!modified_blocks.empty()) { SetBlocksNotSent(modified_blocks); } } m_clients.step(dtime); m_lag += (m_lag > dtime ? -1 : 1) * dtime/100; #if USE_CURL // send masterserver announce { float &counter = m_masterserver_timer; if(!isSingleplayer() && (!counter || counter >= 300.0) && g_settings->getBool("server_announce")) { ServerList::sendAnnounce(counter ? "update" : "start", m_bind_addr.getPort(), m_clients.getPlayerNames(), m_uptime.get(), m_env->getGameTime(), m_lag, m_gamespec.id, Mapgen::getMapgenName(m_emerge->mgparams->mgtype), m_mods); counter = 0.01; } counter += dtime; } #endif /* Check added and deleted active objects */ { //infostream<<"Server: Checking added and deleted active objects"< clients = m_clients.getClientList(); ScopeProfiler sp(g_profiler, "Server: checking added and deleted objs"); // Radius inside which objects are active static const s16 radius = g_settings->getS16("active_object_send_range_blocks") * MAP_BLOCKSIZE; // Radius inside which players are active static const bool is_transfer_limited = g_settings->exists("unlimited_player_transfer_distance") && !g_settings->getBool("unlimited_player_transfer_distance"); static const s16 player_transfer_dist = g_settings->getS16("player_transfer_distance") * MAP_BLOCKSIZE; s16 player_radius = player_transfer_dist; if (player_radius == 0 && is_transfer_limited) player_radius = radius; for (UNORDERED_MAP::iterator i = clients.begin(); i != clients.end(); ++i) { RemoteClient *client = i->second; // If definitions and textures have not been sent, don't // send objects either if (client->getState() < CS_DefinitionsSent) continue; RemotePlayer *player = m_env->getPlayer(client->peer_id); if (player == NULL) { // This can happen if the client timeouts somehow /*warningstream<peer_id <<" has no associated player"<getPlayerSAO(); if (playersao == NULL) continue; s16 my_radius = MYMIN(radius, playersao->getWantedRange() * MAP_BLOCKSIZE); if (my_radius <= 0) my_radius = radius; //infostream << "Server: Active Radius " << my_radius << std::endl; std::queue removed_objects; std::queue added_objects; m_env->getRemovedActiveObjects(playersao, my_radius, player_radius, client->m_known_objects, removed_objects); m_env->getAddedActiveObjects(playersao, my_radius, player_radius, client->m_known_objects, added_objects); // Ignore if nothing happened if (removed_objects.empty() && added_objects.empty()) { continue; } std::string data_buffer; char buf[4]; // Handle removed objects writeU16((u8*)buf, removed_objects.size()); data_buffer.append(buf, 2); while (!removed_objects.empty()) { // Get object u16 id = removed_objects.front(); ServerActiveObject* obj = m_env->getActiveObject(id); // Add to data buffer for sending writeU16((u8*)buf, id); data_buffer.append(buf, 2); // Remove from known objects client->m_known_objects.erase(id); if(obj && obj->m_known_by_count > 0) obj->m_known_by_count--; removed_objects.pop(); } // Handle added objects writeU16((u8*)buf, added_objects.size()); data_buffer.append(buf, 2); while (!added_objects.empty()) { // Get object u16 id = added_objects.front(); ServerActiveObject* obj = m_env->getActiveObject(id); // Get object type u8 type = ACTIVEOBJECT_TYPE_INVALID; if(obj == NULL) warningstream<getSendType(); // Add to data buffer for sending writeU16((u8*)buf, id); data_buffer.append(buf, 2); writeU8((u8*)buf, type); data_buffer.append(buf, 1); if(obj) data_buffer.append(serializeLongString( obj->getClientInitializationData(client->net_proto_version))); else data_buffer.append(serializeLongString("")); // Add to known objects client->m_known_objects.insert(id); if(obj) obj->m_known_by_count++; added_objects.pop(); } u32 pktSize = SendActiveObjectRemoveAdd(client->peer_id, data_buffer); verbosestream << "Server: Sent object remove/add: " << removed_objects.size() << " removed, " << added_objects.size() << " added, " << "packet size is " << pktSize << std::endl; } m_clients.unlock(); m_mod_storage_save_timer -= dtime; if (m_mod_storage_save_timer <= 0.0f) { infostream << "Saving registered mod storages." << std::endl; m_mod_storage_save_timer = g_settings->getFloat("server_map_save_interval"); for (UNORDERED_MAP::const_iterator it = m_mod_storages.begin(); it != m_mod_storages.end(); ++it) { if (it->second->isModified()) { it->second->save(getModStoragePath()); } } } } /* Send object messages */ { MutexAutoLock envlock(m_env_mutex); ScopeProfiler sp(g_profiler, "Server: sending object messages"); // Key = object id // Value = data sent by object UNORDERED_MAP* > buffered_messages; // Get active object messages from environment for(;;) { ActiveObjectMessage aom = m_env->getActiveObjectMessage(); if (aom.id == 0) break; std::vector* message_list = NULL; UNORDERED_MAP* >::iterator n; n = buffered_messages.find(aom.id); if (n == buffered_messages.end()) { message_list = new std::vector; buffered_messages[aom.id] = message_list; } else { message_list = n->second; } message_list->push_back(aom); } m_clients.lock(); UNORDERED_MAP clients = m_clients.getClientList(); // Route data to every client for (UNORDERED_MAP::iterator i = clients.begin(); i != clients.end(); ++i) { RemoteClient *client = i->second; std::string reliable_data; std::string unreliable_data; // Go through all objects in message buffer for (UNORDERED_MAP* >::iterator j = buffered_messages.begin(); j != buffered_messages.end(); ++j) { // If object is not known by client, skip it u16 id = j->first; if (client->m_known_objects.find(id) == client->m_known_objects.end()) continue; // Get message list of object std::vector* list = j->second; // Go through every message for (std::vector::iterator k = list->begin(); k != list->end(); ++k) { // Compose the full new data with header ActiveObjectMessage aom = *k; std::string new_data; // Add object id char buf[2]; writeU16((u8*)&buf[0], aom.id); new_data.append(buf, 2); // Add data new_data += serializeString(aom.datastring); // Add data to buffer if(aom.reliable) reliable_data += new_data; else unreliable_data += new_data; } } /* reliable_data and unreliable_data are now ready. Send them. */ if(reliable_data.size() > 0) { SendActiveObjectMessages(client->peer_id, reliable_data); } if(unreliable_data.size() > 0) { SendActiveObjectMessages(client->peer_id, unreliable_data, false); } } m_clients.unlock(); // Clear buffered_messages for (UNORDERED_MAP* >::iterator i = buffered_messages.begin(); i != buffered_messages.end(); ++i) { delete i->second; } } /* Send queued-for-sending map edit events. */ { // We will be accessing the environment MutexAutoLock lock(m_env_mutex); // Don't send too many at a time //u32 count = 0; // Single change sending is disabled if queue size is not small bool disable_single_change_sending = false; if(m_unsent_map_edit_queue.size() >= 4) disable_single_change_sending = true; int event_count = m_unsent_map_edit_queue.size(); // We'll log the amount of each Profiler prof; while(m_unsent_map_edit_queue.size() != 0) { MapEditEvent* event = m_unsent_map_edit_queue.front(); m_unsent_map_edit_queue.pop(); // Players far away from the change are stored here. // Instead of sending the changes, MapBlocks are set not sent // for them. std::vector far_players; switch (event->type) { case MEET_ADDNODE: case MEET_SWAPNODE: prof.add("MEET_ADDNODE", 1); sendAddNode(event->p, event->n, event->already_known_by_peer, &far_players, disable_single_change_sending ? 5 : 30, event->type == MEET_ADDNODE); break; case MEET_REMOVENODE: prof.add("MEET_REMOVENODE", 1); sendRemoveNode(event->p, event->already_known_by_peer, &far_players, disable_single_change_sending ? 5 : 30); break; case MEET_BLOCK_NODE_METADATA_CHANGED: infostream << "Server: MEET_BLOCK_NODE_METADATA_CHANGED" << std::endl; prof.add("MEET_BLOCK_NODE_METADATA_CHANGED", 1); setBlockNotSent(event->p); break; case MEET_OTHER: infostream << "Server: MEET_OTHER" << std::endl; prof.add("MEET_OTHER", 1); for(std::set::iterator i = event->modified_blocks.begin(); i != event->modified_blocks.end(); ++i) { setBlockNotSent(*i); } break; default: prof.add("unknown", 1); warningstream << "Server: Unknown MapEditEvent " << ((u32)event->type) << std::endl; break; } /* Set blocks not sent to far players */ if(!far_players.empty()) { // Convert list format to that wanted by SetBlocksNotSent std::map modified_blocks2; for(std::set::iterator i = event->modified_blocks.begin(); i != event->modified_blocks.end(); ++i) { modified_blocks2[*i] = m_env->getMap().getBlockNoCreateNoEx(*i); } // Set blocks not sent for(std::vector::iterator i = far_players.begin(); i != far_players.end(); ++i) { if(RemoteClient *client = getClient(*i)) client->SetBlocksNotSent(modified_blocks2); } } delete event; /*// Don't send too many at a time count++; if(count >= 1 && m_unsent_map_edit_queue.size() < 100) break;*/ } if(event_count >= 5){ infostream<<"Server: MapEditEvents:"<= 2.0) { counter = 0.0; m_emerge->startThreads(); } } // Save map, players and auth stuff { float &counter = m_savemap_timer; counter += dtime; static const float save_interval = g_settings->getFloat("server_map_save_interval"); if (counter >= save_interval) { counter = 0.0; MutexAutoLock lock(m_env_mutex); ScopeProfiler sp(g_profiler, "Server: saving stuff"); // Save ban file if (m_banmanager->isModified()) { m_banmanager->save(); } // Save changed parts of map m_env->getMap().save(MOD_STATE_WRITE_NEEDED); // Save players m_env->saveLoadedPlayers(); // Save environment metadata m_env->saveMeta(); } } } void Server::Receive() { DSTACK(FUNCTION_NAME); SharedBuffer data; u16 peer_id; try { NetworkPacket pkt; m_con.Receive(&pkt); peer_id = pkt.getPeerId(); ProcessData(&pkt); } catch(con::InvalidIncomingDataException &e) { infostream<<"Server::Receive(): " "InvalidIncomingDataException: what()=" <getName(); playersao = emergePlayer(playername.c_str(), peer_id, client->net_proto_version); } } catch (std::exception &e) { m_clients.unlock(); throw; } m_clients.unlock(); RemotePlayer *player = m_env->getPlayer(playername.c_str()); // If failed, cancel if ((playersao == NULL) || (player == NULL)) { if (player && player->peer_id != 0) { actionstream << "Server: Failed to emerge player \"" << playername << "\" (player allocated to an another client)" << std::endl; DenyAccess_Legacy(peer_id, L"Another client is connected with this " L"name. If your client closed unexpectedly, try again in " L"a minute."); } else { errorstream << "Server: " << playername << ": Failed to emerge player" << std::endl; DenyAccess_Legacy(peer_id, L"Could not allocate player."); } return NULL; } /* Send complete position information */ SendMovePlayer(peer_id); // Send privileges SendPlayerPrivileges(peer_id); // Send inventory formspec SendPlayerInventoryFormspec(peer_id); // Send inventory SendInventory(playersao); // Send HP SendPlayerHPOrDie(playersao); // Send Breath SendPlayerBreath(playersao); // Show death screen if necessary if (playersao->isDead()) SendDeathscreen(peer_id, false, v3f(0,0,0)); // Note things in chat if not in simple singleplayer mode if (!m_simple_singleplayer_mode && g_settings->getBool("show_statusline_on_connect")) { // Send information about server to player in chat SendChatMessage(peer_id, getStatusString()); } Address addr = getPeerAddress(player->peer_id); std::string ip_str = addr.serializeString(); actionstream<getName() <<" [" << ip_str << "] joins game. " << std::endl; /* Print out action */ { const std::vector &names = m_clients.getPlayerNames(); actionstream << player->getName() << " joins game. List of players: "; for (std::vector::const_iterator i = names.begin(); i != names.end(); ++i) { actionstream << *i << " "; } actionstream << player->getName() <getCommand()]; (this->*opHandle.handler)(pkt); } void Server::ProcessData(NetworkPacket *pkt) { DSTACK(FUNCTION_NAME); // Environment is locked first. MutexAutoLock envlock(m_env_mutex); ScopeProfiler sp(g_profiler, "Server::ProcessData"); u32 peer_id = pkt->getPeerId(); try { Address address = getPeerAddress(peer_id); std::string addr_s = address.serializeString(); if(m_banmanager->isIpBanned(addr_s)) { std::string ban_name = m_banmanager->getBanName(addr_s); infostream << "Server: A banned client tried to connect from " << addr_s << "; banned name was " << ban_name << std::endl; // This actually doesn't seem to transfer to the client DenyAccess_Legacy(peer_id, L"Your ip is banned. Banned name was " + utf8_to_wide(ban_name)); return; } } catch(con::PeerNotFoundException &e) { /* * no peer for this packet found * most common reason is peer timeout, e.g. peer didn't * respond for some time, your server was overloaded or * things like that. */ infostream << "Server::ProcessData(): Canceling: peer " << peer_id << " not found" << std::endl; return; } try { ToServerCommand command = (ToServerCommand) pkt->getCommand(); // Command must be handled into ToServerCommandHandler if (command >= TOSERVER_NUM_MSG_TYPES) { infostream << "Server: Ignoring unknown command " << command << std::endl; return; } if (toServerCommandTable[command].state == TOSERVER_STATE_NOT_CONNECTED) { handleCommand(pkt); return; } u8 peer_ser_ver = getClient(peer_id, CS_InitDone)->serialization_version; if(peer_ser_ver == SER_FMT_VER_INVALID) { errorstream << "Server::ProcessData(): Cancelling: Peer" " serialization format invalid or not initialized." " Skipping incoming command=" << command << std::endl; return; } /* Handle commands related to client startup */ if (toServerCommandTable[command].state == TOSERVER_STATE_STARTUP) { handleCommand(pkt); return; } if (m_clients.getClientState(peer_id) < CS_Active) { if (command == TOSERVER_PLAYERPOS) return; errorstream << "Got packet command: " << command << " for peer id " << peer_id << " but client isn't active yet. Dropping packet " << std::endl; return; } handleCommand(pkt); } catch (SendFailedException &e) { errorstream << "Server::ProcessData(): SendFailedException: " << "what=" << e.what() << std::endl; } catch (PacketError &e) { actionstream << "Server::ProcessData(): PacketError: " << "what=" << e.what() << std::endl; } } void Server::setTimeOfDay(u32 time) { m_env->setTimeOfDay(time); m_time_of_day_send_timer = 0; } void Server::onMapEditEvent(MapEditEvent *event) { if(m_ignore_map_edit_events) return; if(m_ignore_map_edit_events_area.contains(event->getArea())) return; MapEditEvent *e = event->clone(); m_unsent_map_edit_queue.push(e); } Inventory* Server::getInventory(const InventoryLocation &loc) { switch (loc.type) { case InventoryLocation::UNDEFINED: case InventoryLocation::CURRENT_PLAYER: break; case InventoryLocation::PLAYER: { RemotePlayer *player = dynamic_cast(m_env->getPlayer(loc.name.c_str())); if(!player) return NULL; PlayerSAO *playersao = player->getPlayerSAO(); if(!playersao) return NULL; return playersao->getInventory(); } break; case InventoryLocation::NODEMETA: { NodeMetadata *meta = m_env->getMap().getNodeMetadata(loc.p); if(!meta) return NULL; return meta->getInventory(); } break; case InventoryLocation::DETACHED: { if(m_detached_inventories.count(loc.name) == 0) return NULL; return m_detached_inventories[loc.name]; } break; default: sanity_check(false); // abort break; } return NULL; } void Server::setInventoryModified(const InventoryLocation &loc, bool playerSend) { switch(loc.type){ case InventoryLocation::UNDEFINED: break; case InventoryLocation::PLAYER: { if (!playerSend) return; RemotePlayer *player = dynamic_cast(m_env->getPlayer(loc.name.c_str())); if (!player) return; PlayerSAO *playersao = player->getPlayerSAO(); if(!playersao) return; SendInventory(playersao); } break; case InventoryLocation::NODEMETA: { v3s16 blockpos = getNodeBlockPos(loc.p); MapBlock *block = m_env->getMap().getBlockNoCreateNoEx(blockpos); if(block) block->raiseModified(MOD_STATE_WRITE_NEEDED); setBlockNotSent(blockpos); } break; case InventoryLocation::DETACHED: { sendDetachedInventory(loc.name,PEER_ID_INEXISTENT); } break; default: sanity_check(false); // abort break; } } void Server::SetBlocksNotSent(std::map& block) { std::vector clients = m_clients.getClientIDs(); m_clients.lock(); // Set the modified blocks unsent for all the clients for (std::vector::iterator i = clients.begin(); i != clients.end(); ++i) { if (RemoteClient *client = m_clients.lockedGetClientNoEx(*i)) client->SetBlocksNotSent(block); } m_clients.unlock(); } void Server::peerAdded(con::Peer *peer) { DSTACK(FUNCTION_NAME); verbosestream<<"Server::peerAdded(): peer->id=" <id<id; c.timeout = false; m_peer_change_queue.push(c); } void Server::deletingPeer(con::Peer *peer, bool timeout) { DSTACK(FUNCTION_NAME); verbosestream<<"Server::deletingPeer(): peer->id=" <id<<", timeout="<id, CSE_Disconnect); con::PeerChange c; c.type = con::PEER_REMOVED; c.peer_id = peer->id; c.timeout = timeout; m_peer_change_queue.push(c); } bool Server::getClientConInfo(u16 peer_id, con::rtt_stat_type type, float* retval) { *retval = m_con.getPeerStat(peer_id,type); if (*retval == -1) return false; return true; } bool Server::getClientInfo( u16 peer_id, ClientState* state, u32* uptime, u8* ser_vers, u16* prot_vers, u8* major, u8* minor, u8* patch, std::string* vers_string ) { *state = m_clients.getClientState(peer_id); m_clients.lock(); RemoteClient* client = m_clients.lockedGetClientNoEx(peer_id, CS_Invalid); if (client == NULL) { m_clients.unlock(); return false; } *uptime = client->uptime(); *ser_vers = client->serialization_version; *prot_vers = client->net_proto_version; *major = client->getMajor(); *minor = client->getMinor(); *patch = client->getPatch(); *vers_string = client->getPatch(); m_clients.unlock(); return true; } void Server::handlePeerChanges() { while(m_peer_change_queue.size() > 0) { con::PeerChange c = m_peer_change_queue.front(); m_peer_change_queue.pop(); verbosestream<<"Server: Handling peer change: " <<"id="<outgoing_queue.push_back( new ChatEventChat("", utf8_to_wide(text))); } else { std::cout << text << std::endl; } } void Server::Send(NetworkPacket* pkt) { m_clients.send(pkt->getPeerId(), clientCommandFactoryTable[pkt->getCommand()].channel, pkt, clientCommandFactoryTable[pkt->getCommand()].reliable); } void Server::SendMovement(u16 peer_id) { DSTACK(FUNCTION_NAME); std::ostringstream os(std::ios_base::binary); NetworkPacket pkt(TOCLIENT_MOVEMENT, 12 * sizeof(float), peer_id); pkt << g_settings->getFloat("movement_acceleration_default"); pkt << g_settings->getFloat("movement_acceleration_air"); pkt << g_settings->getFloat("movement_acceleration_fast"); pkt << g_settings->getFloat("movement_speed_walk"); pkt << g_settings->getFloat("movement_speed_crouch"); pkt << g_settings->getFloat("movement_speed_fast"); pkt << g_settings->getFloat("movement_speed_climb"); pkt << g_settings->getFloat("movement_speed_jump"); pkt << g_settings->getFloat("movement_liquid_fluidity"); pkt << g_settings->getFloat("movement_liquid_fluidity_smooth"); pkt << g_settings->getFloat("movement_liquid_sink"); pkt << g_settings->getFloat("movement_gravity"); Send(&pkt); } void Server::SendPlayerHPOrDie(PlayerSAO *playersao) { if (!g_settings->getBool("enable_damage")) return; u16 peer_id = playersao->getPeerID(); bool is_alive = playersao->getHP() > 0; if (is_alive) SendPlayerHP(peer_id); else DiePlayer(peer_id); } void Server::SendHP(u16 peer_id, u8 hp) { DSTACK(FUNCTION_NAME); NetworkPacket pkt(TOCLIENT_HP, 1, peer_id); pkt << hp; Send(&pkt); } void Server::SendBreath(u16 peer_id, u16 breath) { DSTACK(FUNCTION_NAME); NetworkPacket pkt(TOCLIENT_BREATH, 2, peer_id); pkt << (u16) breath; Send(&pkt); } void Server::SendAccessDenied(u16 peer_id, AccessDeniedCode reason, const std::string &custom_reason, bool reconnect) { assert(reason < SERVER_ACCESSDENIED_MAX); NetworkPacket pkt(TOCLIENT_ACCESS_DENIED, 1, peer_id); pkt << (u8)reason; if (reason == SERVER_ACCESSDENIED_CUSTOM_STRING) pkt << custom_reason; else if (reason == SERVER_ACCESSDENIED_SHUTDOWN || reason == SERVER_ACCESSDENIED_CRASH) pkt << custom_reason << (u8)reconnect; Send(&pkt); } void Server::SendAccessDenied_Legacy(u16 peer_id,const std::wstring &reason) { DSTACK(FUNCTION_NAME); NetworkPacket pkt(TOCLIENT_ACCESS_DENIED_LEGACY, 0, peer_id); pkt << reason; Send(&pkt); } void Server::SendDeathscreen(u16 peer_id,bool set_camera_point_target, v3f camera_point_target) { DSTACK(FUNCTION_NAME); NetworkPacket pkt(TOCLIENT_DEATHSCREEN, 1 + sizeof(v3f), peer_id); pkt << set_camera_point_target << camera_point_target; Send(&pkt); } void Server::SendItemDef(u16 peer_id, IItemDefManager *itemdef, u16 protocol_version) { DSTACK(FUNCTION_NAME); NetworkPacket pkt(TOCLIENT_ITEMDEF, 0, peer_id); /* u16 command u32 length of the next item zlib-compressed serialized ItemDefManager */ std::ostringstream tmp_os(std::ios::binary); itemdef->serialize(tmp_os, protocol_version); std::ostringstream tmp_os2(std::ios::binary); compressZlib(tmp_os.str(), tmp_os2); pkt.putLongString(tmp_os2.str()); // Make data buffer verbosestream << "Server: Sending item definitions to id(" << peer_id << "): size=" << pkt.getSize() << std::endl; Send(&pkt); } void Server::SendNodeDef(u16 peer_id, INodeDefManager *nodedef, u16 protocol_version) { DSTACK(FUNCTION_NAME); NetworkPacket pkt(TOCLIENT_NODEDEF, 0, peer_id); /* u16 command u32 length of the next item zlib-compressed serialized NodeDefManager */ std::ostringstream tmp_os(std::ios::binary); nodedef->serialize(tmp_os, protocol_version); std::ostringstream tmp_os2(std::ios::binary); compressZlib(tmp_os.str(), tmp_os2); pkt.putLongString(tmp_os2.str()); // Make data buffer verbosestream << "Server: Sending node definitions to id(" << peer_id << "): size=" << pkt.getSize() << std::endl; Send(&pkt); } /* Non-static send methods */ void Server::SendInventory(PlayerSAO* playerSAO) { DSTACK(FUNCTION_NAME); UpdateCrafting(playerSAO->getPlayer()); /* Serialize it */ NetworkPacket pkt(TOCLIENT_INVENTORY, 0, playerSAO->getPeerID()); std::ostringstream os; playerSAO->getInventory()->serialize(os); std::string s = os.str(); pkt.putRawString(s.c_str(), s.size()); Send(&pkt); } void Server::SendChatMessage(u16 peer_id, const std::wstring &message) { DSTACK(FUNCTION_NAME); NetworkPacket pkt(TOCLIENT_CHAT_MESSAGE, 0, peer_id); pkt << message; if (peer_id != PEER_ID_INEXISTENT) { Send(&pkt); } else { m_clients.sendToAll(0, &pkt, true); } } void Server::SendShowFormspecMessage(u16 peer_id, const std::string &formspec, const std::string &formname) { DSTACK(FUNCTION_NAME); NetworkPacket pkt(TOCLIENT_SHOW_FORMSPEC, 0 , peer_id); if (formspec == "" ){ //the client should close the formspec pkt.putLongString(""); } else { pkt.putLongString(FORMSPEC_VERSION_STRING + formspec); } pkt << formname; Send(&pkt); } // Spawns a particle on peer with peer_id void Server::SendSpawnParticle(u16 peer_id, u16 protocol_version, v3f pos, v3f velocity, v3f acceleration, float expirationtime, float size, bool collisiondetection, bool collision_removal, bool vertical, const std::string &texture, const struct TileAnimationParams &animation, u8 glow) { DSTACK(FUNCTION_NAME); if (peer_id == PEER_ID_INEXISTENT) { // This sucks and should be replaced by a better solution in a refactor: std::vector clients = m_clients.getClientIDs(); for (std::vector::iterator i = clients.begin(); i != clients.end(); ++i) { RemotePlayer *player = m_env->getPlayer(*i); if (!player) continue; SendSpawnParticle(*i, player->protocol_version, pos, velocity, acceleration, expirationtime, size, collisiondetection, collision_removal, vertical, texture, animation, glow); } return; } NetworkPacket pkt(TOCLIENT_SPAWN_PARTICLE, 0, peer_id); pkt << pos << velocity << acceleration << expirationtime << size << collisiondetection; pkt.putLongString(texture); pkt << vertical; pkt << collision_removal; // This is horrible but required (why are there two ways to serialize pkts?) std::ostringstream os(std::ios_base::binary); animation.serialize(os, protocol_version); pkt.putRawString(os.str()); pkt << glow; Send(&pkt); } // Adds a ParticleSpawner on peer with peer_id void Server::SendAddParticleSpawner(u16 peer_id, u16 protocol_version, u16 amount, float spawntime, v3f minpos, v3f maxpos, v3f minvel, v3f maxvel, v3f minacc, v3f maxacc, float minexptime, float maxexptime, float minsize, float maxsize, bool collisiondetection, bool collision_removal, u16 attached_id, bool vertical, const std::string &texture, u32 id, const struct TileAnimationParams &animation, u8 glow) { DSTACK(FUNCTION_NAME); if (peer_id == PEER_ID_INEXISTENT) { // This sucks and should be replaced: std::vector clients = m_clients.getClientIDs(); for (std::vector::iterator i = clients.begin(); i != clients.end(); ++i) { RemotePlayer *player = m_env->getPlayer(*i); if (!player) continue; SendAddParticleSpawner(*i, player->protocol_version, amount, spawntime, minpos, maxpos, minvel, maxvel, minacc, maxacc, minexptime, maxexptime, minsize, maxsize, collisiondetection, collision_removal, attached_id, vertical, texture, id, animation, glow); } return; } NetworkPacket pkt(TOCLIENT_ADD_PARTICLESPAWNER, 0, peer_id); pkt << amount << spawntime << minpos << maxpos << minvel << maxvel << minacc << maxacc << minexptime << maxexptime << minsize << maxsize << collisiondetection; pkt.putLongString(texture); pkt << id << vertical; pkt << collision_removal; pkt << attached_id; // This is horrible but required std::ostringstream os(std::ios_base::binary); animation.serialize(os, protocol_version); pkt.putRawString(os.str()); pkt << glow; Send(&pkt); } void Server::SendDeleteParticleSpawner(u16 peer_id, u32 id) { DSTACK(FUNCTION_NAME); NetworkPacket pkt(TOCLIENT_DELETE_PARTICLESPAWNER_LEGACY, 2, peer_id); // Ugly error in this packet pkt << (u16) id; if (peer_id != PEER_ID_INEXISTENT) { Send(&pkt); } else { m_clients.sendToAll(0, &pkt, true); } } void Server::SendHUDAdd(u16 peer_id, u32 id, HudElement *form) { NetworkPacket pkt(TOCLIENT_HUDADD, 0 , peer_id); pkt << id << (u8) form->type << form->pos << form->name << form->scale << form->text << form->number << form->item << form->dir << form->align << form->offset << form->world_pos << form->size; Send(&pkt); } void Server::SendHUDRemove(u16 peer_id, u32 id) { NetworkPacket pkt(TOCLIENT_HUDRM, 4, peer_id); pkt << id; Send(&pkt); } void Server::SendHUDChange(u16 peer_id, u32 id, HudElementStat stat, void *value) { NetworkPacket pkt(TOCLIENT_HUDCHANGE, 0, peer_id); pkt << id << (u8) stat; switch (stat) { case HUD_STAT_POS: case HUD_STAT_SCALE: case HUD_STAT_ALIGN: case HUD_STAT_OFFSET: pkt << *(v2f *) value; break; case HUD_STAT_NAME: case HUD_STAT_TEXT: pkt << *(std::string *) value; break; case HUD_STAT_WORLD_POS: pkt << *(v3f *) value; break; case HUD_STAT_SIZE: pkt << *(v2s32 *) value; break; case HUD_STAT_NUMBER: case HUD_STAT_ITEM: case HUD_STAT_DIR: default: pkt << *(u32 *) value; break; } Send(&pkt); } void Server::SendHUDSetFlags(u16 peer_id, u32 flags, u32 mask) { NetworkPacket pkt(TOCLIENT_HUD_SET_FLAGS, 4 + 4, peer_id); flags &= ~(HUD_FLAG_HEALTHBAR_VISIBLE | HUD_FLAG_BREATHBAR_VISIBLE); pkt << flags << mask; Send(&pkt); } void Server::SendHUDSetParam(u16 peer_id, u16 param, const std::string &value) { NetworkPacket pkt(TOCLIENT_HUD_SET_PARAM, 0, peer_id); pkt << param << value; Send(&pkt); } void Server::SendSetSky(u16 peer_id, const video::SColor &bgcolor, const std::string &type, const std::vector ¶ms) { NetworkPacket pkt(TOCLIENT_SET_SKY, 0, peer_id); pkt << bgcolor << type << (u16) params.size(); for(size_t i=0; igetHP()); m_script->player_event(playersao,"health_changed"); // Send to other clients std::string str = gob_cmd_punched(playersao->readDamage(), playersao->getHP()); ActiveObjectMessage aom(playersao->getId(), true, str); playersao->m_messages_out.push(aom); } void Server::SendPlayerBreath(PlayerSAO *sao) { DSTACK(FUNCTION_NAME); assert(sao); m_script->player_event(sao, "breath_changed"); SendBreath(sao->getPeerID(), sao->getBreath()); } void Server::SendMovePlayer(u16 peer_id) { DSTACK(FUNCTION_NAME); RemotePlayer *player = m_env->getPlayer(peer_id); assert(player); PlayerSAO *sao = player->getPlayerSAO(); assert(sao); NetworkPacket pkt(TOCLIENT_MOVE_PLAYER, sizeof(v3f) + sizeof(f32) * 2, peer_id); pkt << sao->getBasePosition() << sao->getPitch() << sao->getYaw(); { v3f pos = sao->getBasePosition(); verbosestream << "Server: Sending TOCLIENT_MOVE_PLAYER" << " pos=(" << pos.X << "," << pos.Y << "," << pos.Z << ")" << " pitch=" << sao->getPitch() << " yaw=" << sao->getYaw() << std::endl; } Send(&pkt); } void Server::SendLocalPlayerAnimations(u16 peer_id, v2s32 animation_frames[4], f32 animation_speed) { NetworkPacket pkt(TOCLIENT_LOCAL_PLAYER_ANIMATIONS, 0, peer_id); pkt << animation_frames[0] << animation_frames[1] << animation_frames[2] << animation_frames[3] << animation_speed; Send(&pkt); } void Server::SendEyeOffset(u16 peer_id, v3f first, v3f third) { NetworkPacket pkt(TOCLIENT_EYE_OFFSET, 0, peer_id); pkt << first << third; Send(&pkt); } void Server::SendPlayerPrivileges(u16 peer_id) { RemotePlayer *player = m_env->getPlayer(peer_id); assert(player); if(player->peer_id == PEER_ID_INEXISTENT) return; std::set privs; m_script->getAuth(player->getName(), NULL, &privs); NetworkPacket pkt(TOCLIENT_PRIVILEGES, 0, peer_id); pkt << (u16) privs.size(); for(std::set::const_iterator i = privs.begin(); i != privs.end(); ++i) { pkt << (*i); } Send(&pkt); } void Server::SendPlayerInventoryFormspec(u16 peer_id) { RemotePlayer *player = m_env->getPlayer(peer_id); assert(player); if(player->peer_id == PEER_ID_INEXISTENT) return; NetworkPacket pkt(TOCLIENT_INVENTORY_FORMSPEC, 0, peer_id); pkt.putLongString(FORMSPEC_VERSION_STRING + player->inventory_formspec); Send(&pkt); } u32 Server::SendActiveObjectRemoveAdd(u16 peer_id, const std::string &datas) { NetworkPacket pkt(TOCLIENT_ACTIVE_OBJECT_REMOVE_ADD, datas.size(), peer_id); pkt.putRawString(datas.c_str(), datas.size()); Send(&pkt); return pkt.getSize(); } void Server::SendActiveObjectMessages(u16 peer_id, const std::string &datas, bool reliable) { NetworkPacket pkt(TOCLIENT_ACTIVE_OBJECT_MESSAGES, datas.size(), peer_id); pkt.putRawString(datas.c_str(), datas.size()); m_clients.send(pkt.getPeerId(), reliable ? clientCommandFactoryTable[pkt.getCommand()].channel : 1, &pkt, reliable); } s32 Server::playSound(const SimpleSoundSpec &spec, const ServerSoundParams ¶ms) { // Find out initial position of sound bool pos_exists = false; v3f pos = params.getPos(m_env, &pos_exists); // If position is not found while it should be, cancel sound if(pos_exists != (params.type != ServerSoundParams::SSP_LOCAL)) return -1; // Filter destination clients std::vector dst_clients; if(params.to_player != "") { RemotePlayer *player = m_env->getPlayer(params.to_player.c_str()); if(!player){ infostream<<"Server::playSound: Player \""<peer_id == PEER_ID_INEXISTENT){ infostream<<"Server::playSound: Player \""<peer_id); } else { std::vector clients = m_clients.getClientIDs(); for (std::vector::iterator i = clients.begin(); i != clients.end(); ++i) { RemotePlayer *player = m_env->getPlayer(*i); if (!player) continue; PlayerSAO *sao = player->getPlayerSAO(); if (!sao) continue; if (pos_exists) { if(sao->getBasePosition().getDistanceFrom(pos) > params.max_hear_distance) continue; } dst_clients.push_back(*i); } } if(dst_clients.empty()) return -1; // Create the sound s32 id = m_next_sound_id++; // The sound will exist as a reference in m_playing_sounds m_playing_sounds[id] = ServerPlayingSound(); ServerPlayingSound &psound = m_playing_sounds[id]; psound.params = params; NetworkPacket pkt(TOCLIENT_PLAY_SOUND, 0); pkt << id << spec.name << (float) (spec.gain * params.gain) << (u8) params.type << pos << params.object << params.loop; for(std::vector::iterator i = dst_clients.begin(); i != dst_clients.end(); ++i) { psound.clients.insert(*i); m_clients.send(*i, 0, &pkt, true); } return id; } void Server::stopSound(s32 handle) { // Get sound reference UNORDERED_MAP::iterator i = m_playing_sounds.find(handle); if (i == m_playing_sounds.end()) return; ServerPlayingSound &psound = i->second; NetworkPacket pkt(TOCLIENT_STOP_SOUND, 4); pkt << handle; for (UNORDERED_SET::iterator i = psound.clients.begin(); i != psound.clients.end(); ++i) { // Send as reliable m_clients.send(*i, 0, &pkt, true); } // Remove sound reference m_playing_sounds.erase(i); } void Server::sendRemoveNode(v3s16 p, u16 ignore_id, std::vector *far_players, float far_d_nodes) { float maxd = far_d_nodes*BS; v3f p_f = intToFloat(p, BS); NetworkPacket pkt(TOCLIENT_REMOVENODE, 6); pkt << p; std::vector clients = m_clients.getClientIDs(); for (std::vector::iterator i = clients.begin(); i != clients.end(); ++i) { if (far_players) { // Get player if (RemotePlayer *player = m_env->getPlayer(*i)) { PlayerSAO *sao = player->getPlayerSAO(); if (!sao) continue; // If player is far away, only set modified blocks not sent v3f player_pos = sao->getBasePosition(); if (player_pos.getDistanceFrom(p_f) > maxd) { far_players->push_back(*i); continue; } } } // Send as reliable m_clients.send(*i, 0, &pkt, true); } } void Server::sendAddNode(v3s16 p, MapNode n, u16 ignore_id, std::vector *far_players, float far_d_nodes, bool remove_metadata) { float maxd = far_d_nodes*BS; v3f p_f = intToFloat(p, BS); std::vector clients = m_clients.getClientIDs(); for(std::vector::iterator i = clients.begin(); i != clients.end(); ++i) { if (far_players) { // Get player if (RemotePlayer *player = m_env->getPlayer(*i)) { PlayerSAO *sao = player->getPlayerSAO(); if (!sao) continue; // If player is far away, only set modified blocks not sent v3f player_pos = sao->getBasePosition(); if(player_pos.getDistanceFrom(p_f) > maxd) { far_players->push_back(*i); continue; } } } NetworkPacket pkt(TOCLIENT_ADDNODE, 6 + 2 + 1 + 1 + 1); m_clients.lock(); RemoteClient* client = m_clients.lockedGetClientNoEx(*i); if (client != 0) { pkt << p << n.param0 << n.param1 << n.param2 << (u8) (remove_metadata ? 0 : 1); if (!remove_metadata) { if (client->net_proto_version <= 21) { // Old clients always clear metadata; fix it // by sending the full block again. client->SetBlockNotSent(getNodeBlockPos(p)); } } } m_clients.unlock(); // Send as reliable if (pkt.getSize() > 0) m_clients.send(*i, 0, &pkt, true); } } void Server::setBlockNotSent(v3s16 p) { std::vector clients = m_clients.getClientIDs(); m_clients.lock(); for(std::vector::iterator i = clients.begin(); i != clients.end(); ++i) { RemoteClient *client = m_clients.lockedGetClientNoEx(*i); client->SetBlockNotSent(p); } m_clients.unlock(); } void Server::SendBlockNoLock(u16 peer_id, MapBlock *block, u8 ver, u16 net_proto_version) { DSTACK(FUNCTION_NAME); v3s16 p = block->getPos(); /* Create a packet with the block in the right format */ std::ostringstream os(std::ios_base::binary); block->serialize(os, ver, false); block->serializeNetworkSpecific(os, net_proto_version); std::string s = os.str(); NetworkPacket pkt(TOCLIENT_BLOCKDATA, 2 + 2 + 2 + 2 + s.size(), peer_id); pkt << p; pkt.putRawString(s.c_str(), s.size()); Send(&pkt); } void Server::SendBlocks(float dtime) { DSTACK(FUNCTION_NAME); MutexAutoLock envlock(m_env_mutex); //TODO check if one big lock could be faster then multiple small ones ScopeProfiler sp(g_profiler, "Server: sel and send blocks to clients"); std::vector queue; s32 total_sending = 0; { ScopeProfiler sp(g_profiler, "Server: selecting blocks for sending"); std::vector clients = m_clients.getClientIDs(); m_clients.lock(); for(std::vector::iterator i = clients.begin(); i != clients.end(); ++i) { RemoteClient *client = m_clients.lockedGetClientNoEx(*i, CS_Active); if (client == NULL) continue; total_sending += client->SendingCount(); client->GetNextBlocks(m_env,m_emerge, dtime, queue); } m_clients.unlock(); } // Sort. // Lowest priority number comes first. // Lowest is most important. std::sort(queue.begin(), queue.end()); m_clients.lock(); for(u32 i=0; i= g_settings->getS32 ("max_simultaneous_block_sends_server_total")) break; PrioritySortedBlockTransfer q = queue[i]; MapBlock *block = NULL; try { block = m_env->getMap().getBlockNoCreate(q.pos); } catch(InvalidPositionException &e) { continue; } RemoteClient *client = m_clients.lockedGetClientNoEx(q.peer_id, CS_Active); if(!client) continue; SendBlockNoLock(q.peer_id, block, client->serialization_version, client->net_proto_version); client->SentBlock(q.pos); total_sending++; } m_clients.unlock(); } void Server::fillMediaCache() { DSTACK(FUNCTION_NAME); infostream<<"Server: Calculating media file checksums"< paths; for(std::vector::iterator i = m_mods.begin(); i != m_mods.end(); ++i) { const ModSpec &mod = *i; paths.push_back(mod.path + DIR_DELIM + "textures"); paths.push_back(mod.path + DIR_DELIM + "sounds"); paths.push_back(mod.path + DIR_DELIM + "media"); paths.push_back(mod.path + DIR_DELIM + "models"); } paths.push_back(porting::path_user + DIR_DELIM + "textures" + DIR_DELIM + "server"); // Collect media file information from paths into cache for(std::vector::iterator i = paths.begin(); i != paths.end(); ++i) { std::string mediapath = *i; std::vector dirlist = fs::GetDirListing(mediapath); for (u32 j = 0; j < dirlist.size(); j++) { if (dirlist[j].dir) // Ignode dirs continue; std::string filename = dirlist[j].name; // If name contains illegal characters, ignore the file if (!string_allowed(filename, TEXTURENAME_ALLOWED_CHARS)) { infostream<<"Server: ignoring illegal file name: \"" << filename << "\"" << std::endl; continue; } // If name is not in a supported format, ignore it const char *supported_ext[] = { ".png", ".jpg", ".bmp", ".tga", ".pcx", ".ppm", ".psd", ".wal", ".rgb", ".ogg", ".x", ".b3d", ".md2", ".obj", NULL }; if (removeStringEnd(filename, supported_ext) == ""){ infostream << "Server: ignoring unsupported file extension: \"" << filename << "\"" << std::endl; continue; } // Ok, attempt to load the file and add to cache std::string filepath = mediapath + DIR_DELIM + filename; // Read data std::ifstream fis(filepath.c_str(), std::ios_base::binary); if (!fis.good()) { errorstream << "Server::fillMediaCache(): Could not open \"" << filename << "\" for reading" << std::endl; continue; } std::ostringstream tmp_os(std::ios_base::binary); bool bad = false; for(;;) { char buf[1024]; fis.read(buf, 1024); std::streamsize len = fis.gcount(); tmp_os.write(buf, len); if (fis.eof()) break; if (!fis.good()) { bad = true; break; } } if(bad) { errorstream<<"Server::fillMediaCache(): Failed to read \"" << filename << "\"" << std::endl; continue; } if(tmp_os.str().length() == 0) { errorstream << "Server::fillMediaCache(): Empty file \"" << filepath << "\"" << std::endl; continue; } SHA1 sha1; sha1.addBytes(tmp_os.str().c_str(), tmp_os.str().length()); unsigned char *digest = sha1.getDigest(); std::string sha1_base64 = base64_encode(digest, 20); std::string sha1_hex = hex_encode((char*)digest, 20); free(digest); // Put in list m_media[filename] = MediaInfo(filepath, sha1_base64); verbosestream << "Server: " << sha1_hex << " is " << filename << std::endl; } } } void Server::sendMediaAnnouncement(u16 peer_id) { DSTACK(FUNCTION_NAME); verbosestream << "Server: Announcing files to id(" << peer_id << ")" << std::endl; // Make packet std::ostringstream os(std::ios_base::binary); NetworkPacket pkt(TOCLIENT_ANNOUNCE_MEDIA, 0, peer_id); pkt << (u16) m_media.size(); for (UNORDERED_MAP::iterator i = m_media.begin(); i != m_media.end(); ++i) { pkt << i->first << i->second.sha1_digest; } pkt << g_settings->get("remote_media"); Send(&pkt); } struct SendableMedia { std::string name; std::string path; std::string data; SendableMedia(const std::string &name_="", const std::string &path_="", const std::string &data_=""): name(name_), path(path_), data(data_) {} }; void Server::sendRequestedMedia(u16 peer_id, const std::vector &tosend) { DSTACK(FUNCTION_NAME); verbosestream<<"Server::sendRequestedMedia(): " <<"Sending files to client"< > file_bunches; file_bunches.push_back(std::vector()); u32 file_size_bunch_total = 0; for(std::vector::const_iterator i = tosend.begin(); i != tosend.end(); ++i) { const std::string &name = *i; if (m_media.find(name) == m_media.end()) { errorstream<<"Server::sendRequestedMedia(): Client asked for " <<"unknown file \""<<(name)<<"\""<= bytes_per_bunch) { file_bunches.push_back(std::vector()); file_size_bunch_total = 0; } } /* Create and send packets */ u16 num_bunches = file_bunches.size(); for(u16 i = 0; i < num_bunches; i++) { /* u16 command u16 total number of texture bunches u16 index of this bunch u32 number of files in this bunch for each file { u16 length of name string name u32 length of data data } */ NetworkPacket pkt(TOCLIENT_MEDIA, 4 + 0, peer_id); pkt << num_bunches << i << (u32) file_bunches[i].size(); for(std::vector::iterator j = file_bunches[i].begin(); j != file_bunches[i].end(); ++j) { pkt << j->name; pkt.putLongString(j->data); } verbosestream << "Server::sendRequestedMedia(): bunch " << i << "/" << num_bunches << " files=" << file_bunches[i].size() << " size=" << pkt.getSize() << std::endl; Send(&pkt); } } void Server::sendDetachedInventory(const std::string &name, u16 peer_id) { if(m_detached_inventories.count(name) == 0) { errorstream<serialize(os); // Make data buffer std::string s = os.str(); Netw video::SColor skycolor ) { /* save current info */ irr::core::vector3df oldPosition = camera.getCameraNode()->getPosition(); irr::core::vector3df oldTarget = camera.getCameraNode()->getTarget(); irr::core::matrix4 startMatrix = camera.getCameraNode()->getAbsoluteTransformation(); irr::core::vector3df focusPoint = (camera.getCameraNode()->getTarget() - camera.getCameraNode()->getAbsolutePosition()).setLength(1) + camera.getCameraNode()->getAbsolutePosition(); /* create left view */ video::ITexture* left_image = draw_image(screensize, LEFT, startMatrix, focusPoint, show_hud, driver, camera, smgr, hud, draw_wield_tool, client, guienv, skycolor); /* create right view */ video::ITexture* right_image = draw_image(screensize, RIGHT, startMatrix, focusPoint, show_hud, driver, camera, smgr, hud, draw_wield_tool, client, guienv, skycolor); /* create hud overlay */ video::ITexture* hudtexture = draw_hud(driver, screensize, show_hud, hud, client, false, skycolor, guienv, camera ); driver->makeColorKeyTexture(hudtexture, irr::video::SColor(255, 0, 0, 0)); //makeColorKeyTexture mirrors texture so we do it twice to get it right again driver->makeColorKeyTexture(hudtexture, irr::video::SColor(255, 0, 0, 0)); draw2DImageFilterScaled(driver, left_image, irr::core::rect<s32>(0, 0, screensize.X/2, screensize.Y), irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, false); draw2DImageFilterScaled(driver, hudtexture, irr::core::rect<s32>(0, 0, screensize.X/2, screensize.Y), irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, true); draw2DImageFilterScaled(driver, right_image, irr::core::rect<s32>(screensize.X/2, 0, screensize.X, screensize.Y), irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, false); draw2DImageFilterScaled(driver, hudtexture, irr::core::rect<s32>(screensize.X/2, 0, screensize.X, screensize.Y), irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, true); left_image = NULL; right_image = NULL; /* cleanup */ camera.getCameraNode()->setPosition(oldPosition); camera.getCameraNode()->setTarget(oldTarget); } void draw_top_bottom_3d_mode(Camera& camera, bool show_hud, Hud& hud, video::IVideoDriver* driver, scene::ISceneManager* smgr, const v2u32& screensize, bool draw_wield_tool, Client& client, gui::IGUIEnvironment* guienv, video::SColor skycolor ) { /* save current info */ irr::core::vector3df oldPosition = camera.getCameraNode()->getPosition(); irr::core::vector3df oldTarget = camera.getCameraNode()->getTarget(); irr::core::matrix4 startMatrix = camera.getCameraNode()->getAbsoluteTransformation(); irr::core::vector3df focusPoint = (camera.getCameraNode()->getTarget() - camera.getCameraNode()->getAbsolutePosition()).setLength(1) + camera.getCameraNode()->getAbsolutePosition(); /* create left view */ video::ITexture* left_image = draw_image(screensize, LEFT, startMatrix, focusPoint, show_hud, driver, camera, smgr, hud, draw_wield_tool, client, guienv, skycolor); /* create right view */ video::ITexture* right_image = draw_image(screensize, RIGHT, startMatrix, focusPoint, show_hud, driver, camera, smgr, hud, draw_wield_tool, client, guienv, skycolor); /* create hud overlay */ video::ITexture* hudtexture = draw_hud(driver, screensize, show_hud, hud, client, false, skycolor, guienv, camera ); driver->makeColorKeyTexture(hudtexture, irr::video::SColor(255, 0, 0, 0)); //makeColorKeyTexture mirrors texture so we do it twice to get it right again driver->makeColorKeyTexture(hudtexture, irr::video::SColor(255, 0, 0, 0)); draw2DImageFilterScaled(driver, left_image, irr::core::rect<s32>(0, 0, screensize.X, screensize.Y/2), irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, false); draw2DImageFilterScaled(driver, hudtexture, irr::core::rect<s32>(0, 0, screensize.X, screensize.Y/2), irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, true); draw2DImageFilterScaled(driver, right_image, irr::core::rect<s32>(0, screensize.Y/2, screensize.X, screensize.Y), irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, false); draw2DImageFilterScaled(driver, hudtexture, irr::core::rect<s32>(0, screensize.Y/2, screensize.X, screensize.Y), irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, true); left_image = NULL; right_image = NULL; /* cleanup */ camera.getCameraNode()->setPosition(oldPosition); camera.getCameraNode()->setTarget(oldTarget); } void draw_pageflip_3d_mode(Camera& camera, bool show_hud, Hud& hud, video::IVideoDriver* driver, scene::ISceneManager* smgr, const v2u32& screensize, bool draw_wield_tool, Client& client, gui::IGUIEnvironment* guienv, video::SColor skycolor) { #if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR > 8 errorstream << "Pageflip 3D mode is not supported" << " with your Irrlicht version!" << std::endl; #else /* preserve old setup*/ irr::core::vector3df oldPosition = camera.getCameraNode()->getPosition(); irr::core::vector3df oldTarget = camera.getCameraNode()->getTarget(); irr::core::matrix4 startMatrix = camera.getCameraNode()->getAbsoluteTransformation(); irr::core::vector3df focusPoint = (camera.getCameraNode()->getTarget() - camera.getCameraNode()->getAbsolutePosition()).setLength(1) + camera.getCameraNode()->getAbsolutePosition(); //Left eye... driver->setRenderTarget