X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fserver.cpp;h=778a932417f285b24edf3b80fe927068d1abba1e;hb=6c06330daf04ed1c390131755b64338ca7d79a7e;hp=09884f1bc0aa6ea7cb5c370545e5558606e53f2c;hpb=aa474e4501f460ba853dc70ff4d97cbd061e6704;p=dragonfireclient.git diff --git a/src/server.cpp b/src/server.cpp index 09884f1bc..778a93241 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -27,7 +27,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "environment.h" #include "map.h" #include "jthread/jmutexautolock.h" -#include "main.h" #include "constants.h" #include "voxel.h" #include "config.h" @@ -222,12 +221,9 @@ Server::Server( infostream<<"- world: "<loadScript(scriptpath)) - throw ModError("Failed to load and run " + scriptpath); + if (!m_script->loadMod(script_path, BUILTIN_MOD_NAME)) { + throw ModError("Failed to load and run " + script_path); + } - // Print 'em - infostream<<"Server: Loading mods: "; + // Print mods + infostream << "Server: Loading mods: "; for(std::vector::iterator i = m_mods.begin(); i != m_mods.end(); i++){ const ModSpec &mod = *i; - infostream<::iterator i = m_mods.begin(); - i != m_mods.end(); i++){ + for (std::vector::iterator i = m_mods.begin(); + i != m_mods.end(); i++) { const ModSpec &mod = *i; - std::string scriptpath = mod.path + DIR_DELIM + "init.lua"; - infostream<<" ["<loadMod(scriptpath, mod.name); - if(!success){ - errorstream<<"Server: Failed to load and run " - <loadMod(script_path, mod.name)) { + errorstream << "Server: Failed to load and run " + << script_path << std::endl; + throw ModError("Failed to load and run " + script_path); } } @@ -340,7 +338,10 @@ Server::Server( m_nodedef->setNodeRegistrationStatus(true); // Perform pending node name resolutions - m_nodedef->runNodeResolverCallbacks(); + m_nodedef->runNodeResolveCallbacks(); + + // 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); @@ -488,8 +489,16 @@ void Server::step(float dtime) } // Throw if fatal error occurred in thread std::string async_err = m_async_fatal_error.get(); - if(async_err != ""){ - throw ServerError(async_err); + if(async_err != "") { + if (m_simple_singleplayer_mode) { + throw ServerError(async_err); + } + else { + errorstream << "UNRECOVERABLE error occurred. Stopping server. " + << "Please fix the following error:" << std::endl + << async_err << std::endl; + FATAL_ERROR(async_err.c_str()); + } } } @@ -765,16 +774,11 @@ void Server::AsyncRunStep(bool initial_step) obj->m_known_by_count++; } - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_ACTIVE_OBJECT_REMOVE_ADD, 0, client->peer_id); - pkt->putRawString(data_buffer.c_str(), data_buffer.size()); - - + 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 " << pkt->getSize() << std::endl; - - Send(pkt); + << "packet size is " << pktSize << std::endl; } m_clients.Unlock(); } @@ -853,19 +857,11 @@ void Server::AsyncRunStep(bool initial_step) Send them. */ if(reliable_data.size() > 0) { - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_ACTIVE_OBJECT_MESSAGES, - 0, client->peer_id); - - pkt->putRawString(reliable_data.c_str(), reliable_data.size()); - Send(pkt); + SendActiveObjectMessages(client->peer_id, reliable_data); } if(unreliable_data.size() > 0) { - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_ACTIVE_OBJECT_MESSAGES, - 0, client->peer_id); - - pkt->putRawString(unreliable_data.c_str(), unreliable_data.size()); - Send(pkt); + SendActiveObjectMessages(client->peer_id, unreliable_data, false); } } m_clients.Unlock(); @@ -1030,10 +1026,11 @@ void Server::Receive() DSTACK(__FUNCTION_NAME); SharedBuffer data; u16 peer_id; - u32 datasize; try { - datasize = m_con.Receive(peer_id,data); - ProcessData(*data, datasize, peer_id); + NetworkPacket pkt; + m_con.Receive(&pkt); + peer_id = pkt.getPeerId(); + ProcessData(&pkt); } catch(con::InvalidIncomingDataException &e) { infostream<<"Server::Receive(): " @@ -1047,7 +1044,7 @@ void Server::Receive() } catch(ClientStateError &e) { errorstream << "ProcessData: peer=" << peer_id << e.what() << std::endl; - DenyAccess(peer_id, L"Your client sent something server didn't expect." + DenyAccess_Legacy(peer_id, L"Your client sent something server didn't expect." L"Try reconnecting or updating your client"); } catch(con::PeerNotFoundException &e) { @@ -1080,13 +1077,13 @@ PlayerSAO* Server::StageTwoClientInit(u16 peer_id) if(player && player->peer_id != 0) { errorstream<<"Server: "<getBool("enable_damage")) - SendPlayerHP(peer_id); + SendPlayerHPOrDie(peer_id, playersao->getHP() == 0); // Send Breath SendPlayerBreath(peer_id); // Show death screen if necessary - if(player->hp == 0) + if(player->isDead()) SendDeathscreen(peer_id, false, v3f(0,0,0)); // Note things in chat if not in simple singleplayer mode @@ -1162,13 +1158,14 @@ inline void Server::handleCommand(NetworkPacket* pkt) (this->*opHandle.handler)(pkt); } -void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) +void Server::ProcessData(NetworkPacket *pkt) { DSTACK(__FUNCTION_NAME); // Environment is locked first. JMutexAutoLock envlock(m_env_mutex); ScopeProfiler sp(g_profiler, "Server::ProcessData"); + u32 peer_id = pkt->getPeerId(); try { Address address = getPeerAddress(peer_id); @@ -1180,7 +1177,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) << addr_s << "; banned name was " << ban_name << std::endl; // This actually doesn't seem to transfer to the client - DenyAccess(peer_id, L"Your ip is banned. Banned name was " + DenyAccess_Legacy(peer_id, L"Your ip is banned. Banned name was " + narrow_to_wide(ban_name)); return; } @@ -1192,17 +1189,12 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) * respond for some time, your server was overloaded or * things like that. */ - infostream << "Server::ProcessData(): Cancelling: peer " + infostream << "Server::ProcessData(): Canceling: peer " << peer_id << " not found" << std::endl; return; } try { - if(datasize < 2) - return; - - NetworkPacket* pkt = new NetworkPacket(data, datasize, peer_id); - ToServerCommand command = (ToServerCommand) pkt->getCommand(); // Command must be handled into ToServerCommandHandler @@ -1213,7 +1205,6 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) if (toServerCommandTable[command].state == TOSERVER_STATE_NOT_CONNECTED) { handleCommand(pkt); - delete pkt; return; } @@ -1223,15 +1214,12 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) errorstream << "Server::ProcessData(): Cancelling: Peer" " serialization format invalid or not initialized." " Skipping incoming command=" << command << std::endl; - - delete pkt; return; } /* Handle commands related to client startup */ if (toServerCommandTable[command].state == TOSERVER_STATE_STARTUP) { handleCommand(pkt); - delete pkt; return; } @@ -1241,19 +1229,18 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) errorstream << "Got packet command: " << command << " for peer id " << peer_id << " but client isn't active yet. Dropping packet " << std::endl; - - delete pkt; return; } handleCommand(pkt); - delete pkt; - - } - catch(SendFailedException &e) { + } catch (SendFailedException &e) { errorstream << "Server::ProcessData(): SendFailedException: " << "what=" << e.what() << std::endl; + } catch (PacketError &e) { + actionstream << "Server::ProcessData(): PacketError: " + << "what=" << e.what() + << std::endl; } } @@ -1306,18 +1293,21 @@ Inventory* Server::getInventory(const InventoryLocation &loc) } break; default: - assert(0); + sanity_check(false); // abort break; } return NULL; } -void Server::setInventoryModified(const InventoryLocation &loc) +void Server::setInventoryModified(const InventoryLocation &loc, bool playerSend) { switch(loc.type){ case InventoryLocation::UNDEFINED: break; case InventoryLocation::PLAYER: { + if (!playerSend) + return; + Player *player = m_env->getPlayer(loc.name.c_str()); if(!player) return; @@ -1345,7 +1335,7 @@ void Server::setInventoryModified(const InventoryLocation &loc) } break; default: - assert(0); + sanity_check(false); // abort break; } } @@ -1454,7 +1444,7 @@ void Server::handlePeerChanges() break; default: - assert("Invalid peer change event received!" == 0); + FATAL_ERROR("Invalid peer change event received!"); break; } } @@ -1473,49 +1463,62 @@ void Server::SendMovement(u16 peer_id) DSTACK(__FUNCTION_NAME); std::ostringstream os(std::ios_base::binary); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_MOVEMENT, 12 * sizeof(float), peer_id); + 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"); + 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); + Send(&pkt); } void Server::SendHP(u16 peer_id, u8 hp) { DSTACK(__FUNCTION_NAME); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_HP, 1, peer_id); - *pkt << hp; - Send(pkt); + NetworkPacket pkt(TOCLIENT_HP, 1, peer_id); + pkt << hp; + Send(&pkt); } void Server::SendBreath(u16 peer_id, u16 breath) { DSTACK(__FUNCTION_NAME); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_BREATH, 2, peer_id); - *pkt << (u16) breath; - Send(pkt); + 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) +{ + DSTACK(__FUNCTION_NAME); + + NetworkPacket pkt(TOCLIENT_ACCESS_DENIED, 1, peer_id); + pkt << (u8) reason; + + if (reason == SERVER_ACCESSDENIED_CUSTOM_STRING) { + pkt << custom_reason; + } + Send(&pkt); } -void Server::SendAccessDenied(u16 peer_id,const std::wstring &reason) +void Server::SendAccessDenied_Legacy(u16 peer_id,const std::wstring &reason) { DSTACK(__FUNCTION_NAME); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_ACCESS_DENIED, 0, peer_id); - *pkt << reason; - Send(pkt); + NetworkPacket pkt(TOCLIENT_ACCESS_DENIED_LEGACY, 0, peer_id); + pkt << reason; + Send(&pkt); } void Server::SendDeathscreen(u16 peer_id,bool set_camera_point_target, @@ -1523,9 +1526,9 @@ void Server::SendDeathscreen(u16 peer_id,bool set_camera_point_target, { DSTACK(__FUNCTION_NAME); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_DEATHSCREEN, 1 + sizeof(v3f), peer_id); - *pkt << set_camera_point_target << camera_point_target; - Send(pkt); + 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, @@ -1533,7 +1536,7 @@ void Server::SendItemDef(u16 peer_id, { DSTACK(__FUNCTION_NAME); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_ITEMDEF, 0, peer_id); + NetworkPacket pkt(TOCLIENT_ITEMDEF, 0, peer_id); /* u16 command @@ -1544,13 +1547,13 @@ void Server::SendItemDef(u16 peer_id, itemdef->serialize(tmp_os, protocol_version); std::ostringstream tmp_os2(std::ios::binary); compressZlib(tmp_os.str(), tmp_os2); - pkt->putLongString(tmp_os2.str()); + pkt.putLongString(tmp_os2.str()); // Make data buffer verbosestream << "Server: Sending item definitions to id(" << peer_id - << "): size=" << pkt->getSize() << std::endl; + << "): size=" << pkt.getSize() << std::endl; - Send(pkt); + Send(&pkt); } void Server::SendNodeDef(u16 peer_id, @@ -1558,7 +1561,7 @@ void Server::SendNodeDef(u16 peer_id, { DSTACK(__FUNCTION_NAME); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_NODEDEF, 0, peer_id); + NetworkPacket pkt(TOCLIENT_NODEDEF, 0, peer_id); /* u16 command @@ -1570,13 +1573,13 @@ void Server::SendNodeDef(u16 peer_id, std::ostringstream tmp_os2(std::ios::binary); compressZlib(tmp_os.str(), tmp_os2); - pkt->putLongString(tmp_os2.str()); + pkt.putLongString(tmp_os2.str()); // Make data buffer verbosestream << "Server: Sending node definitions to id(" << peer_id - << "): size=" << pkt->getSize() << std::endl; + << "): size=" << pkt.getSize() << std::endl; - Send(pkt); + Send(&pkt); } /* @@ -1593,30 +1596,29 @@ void Server::SendInventory(PlayerSAO* playerSAO) Serialize it */ - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_INVENTORY, 0, - playerSAO->getPeerID()); + 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); + pkt.putRawString(s.c_str(), s.size()); + Send(&pkt); } void Server::SendChatMessage(u16 peer_id, const std::wstring &message) { DSTACK(__FUNCTION_NAME); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_CHAT_MESSAGE, 0, peer_id); - *pkt << message; + NetworkPacket pkt(TOCLIENT_CHAT_MESSAGE, 0, peer_id); + pkt << message; if (peer_id != PEER_ID_INEXISTENT) { - Send(pkt); + Send(&pkt); } else { - m_clients.sendToAll(0,pkt,true); + m_clients.sendToAll(0, &pkt, true); } } @@ -1625,12 +1627,12 @@ void Server::SendShowFormspecMessage(u16 peer_id, const std::string &formspec, { DSTACK(__FUNCTION_NAME); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_SHOW_FORMSPEC, 0 , peer_id); + NetworkPacket pkt(TOCLIENT_SHOW_FORMSPEC, 0 , peer_id); - pkt->putLongString(FORMSPEC_VERSION_STRING + formspec); - *pkt << formname; + pkt.putLongString(FORMSPEC_VERSION_STRING + formspec); + pkt << formname; - Send(pkt); + Send(&pkt); } // Spawns a particle on peer with peer_id @@ -1640,18 +1642,18 @@ void Server::SendSpawnParticle(u16 peer_id, v3f pos, v3f velocity, v3f accelerat { DSTACK(__FUNCTION_NAME); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_SPAWN_PARTICLE, 0, peer_id); + NetworkPacket pkt(TOCLIENT_SPAWN_PARTICLE, 0, peer_id); - *pkt << pos << velocity << acceleration << expirationtime + pkt << pos << velocity << acceleration << expirationtime << size << collisiondetection; - pkt->putLongString(texture); - *pkt << vertical; + pkt.putLongString(texture); + pkt << vertical; if (peer_id != PEER_ID_INEXISTENT) { - Send(pkt); + Send(&pkt); } else { - m_clients.sendToAll(0,pkt,true); + m_clients.sendToAll(0, &pkt, true); } } @@ -1662,21 +1664,21 @@ void Server::SendAddParticleSpawner(u16 peer_id, u16 amount, float spawntime, v3 { DSTACK(__FUNCTION_NAME); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_ADD_PARTICLESPAWNER, 0, peer_id); + NetworkPacket pkt(TOCLIENT_ADD_PARTICLESPAWNER, 0, peer_id); - *pkt << amount << spawntime << minpos << maxpos << minvel << maxvel + pkt << amount << spawntime << minpos << maxpos << minvel << maxvel << minacc << maxacc << minexptime << maxexptime << minsize << maxsize << collisiondetection; - pkt->putLongString(texture); + pkt.putLongString(texture); - *pkt << id << vertical; + pkt << id << vertical; if (peer_id != PEER_ID_INEXISTENT) { - Send(pkt); + Send(&pkt); } else { - m_clients.sendToAll(0, pkt, true); + m_clients.sendToAll(0, &pkt, true); } } @@ -1684,124 +1686,124 @@ void Server::SendDeleteParticleSpawner(u16 peer_id, u32 id) { DSTACK(__FUNCTION_NAME); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_DELETE_PARTICLESPAWNER, 2, peer_id); + NetworkPacket pkt(TOCLIENT_DELETE_PARTICLESPAWNER_LEGACY, 2, peer_id); // Ugly error in this packet - *pkt << (u16) id; + pkt << (u16) id; if (peer_id != PEER_ID_INEXISTENT) { - Send(pkt); + Send(&pkt); } else { - m_clients.sendToAll(0, pkt, true); + m_clients.sendToAll(0, &pkt, true); } } void Server::SendHUDAdd(u16 peer_id, u32 id, HudElement *form) { - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_HUDADD, 0 , peer_id); + NetworkPacket pkt(TOCLIENT_HUDADD, 0 , peer_id); - *pkt << id << (u8) form->type << form->pos << form->name << form->scale + 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); + Send(&pkt); } void Server::SendHUDRemove(u16 peer_id, u32 id) { - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_HUDRM, 4, peer_id); - *pkt << id; - Send(pkt); + 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 = new NetworkPacket(TOCLIENT_HUDCHANGE, 0, peer_id); - *pkt << id << (u8) stat; + 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; + pkt << *(v2f *) value; break; case HUD_STAT_NAME: case HUD_STAT_TEXT: - *pkt << *(std::string *) value; + pkt << *(std::string *) value; break; case HUD_STAT_WORLD_POS: - *pkt << *(v3f *) value; + pkt << *(v3f *) value; break; case HUD_STAT_SIZE: - *pkt << *(v2s32 *) value; + pkt << *(v2s32 *) value; break; case HUD_STAT_NUMBER: case HUD_STAT_ITEM: case HUD_STAT_DIR: default: - *pkt << *(u32 *) value; + pkt << *(u32 *) value; break; } - Send(pkt); + Send(&pkt); } void Server::SendHUDSetFlags(u16 peer_id, u32 flags, u32 mask) { - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_HUD_SET_FLAGS, 4 + 4, peer_id); + NetworkPacket pkt(TOCLIENT_HUD_SET_FLAGS, 4 + 4, peer_id); flags &= ~(HUD_FLAG_HEALTHBAR_VISIBLE | HUD_FLAG_BREATHBAR_VISIBLE); - *pkt << flags << mask; + pkt << flags << mask; - Send(pkt); + Send(&pkt); } void Server::SendHUDSetParam(u16 peer_id, u16 param, const std::string &value) { - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_HUD_SET_PARAM, 0, peer_id); - *pkt << param << value; - Send(pkt); + 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 = new NetworkPacket(TOCLIENT_SET_SKY, 0, peer_id); - *pkt << bgcolor << type << (u16) params.size(); + 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"); @@ -1835,39 +1841,39 @@ void Server::SendMovePlayer(u16 peer_id) Player *player = m_env->getPlayer(peer_id); assert(player); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_MOVE_PLAYER, 0, peer_id); - *pkt << player->getPosition() << player->getPitch() << player->getYaw(); + NetworkPacket pkt(TOCLIENT_MOVE_PLAYER, sizeof(v3f) + sizeof(f32) * 2, peer_id); + pkt << player->getPosition() << player->getPitch() << player->getYaw(); { v3f pos = player->getPosition(); f32 pitch = player->getPitch(); f32 yaw = player->getYaw(); - verbosestream<<"Server: Sending TOCLIENT_MOVE_PLAYER" - <<" pos=("<get("remote_media"); + Send(&pkt); } struct SendableMedia @@ -2426,21 +2438,21 @@ void Server::sendRequestedMedia(u16 peer_id, } */ - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_MEDIA, 0, peer_id); - *pkt << num_bunches << i << (u32) file_bunches[i].size(); + 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); + 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); + << " size=" << pkt.getSize() << std::endl; + Send(&pkt); } } @@ -2459,14 +2471,14 @@ void Server::sendDetachedInventory(const std::string &name, u16 peer_id) // Make data buffer std::string s = os.str(); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_DETACHED_INVENTORY, 0, peer_id); - pkt->putRawString(s.c_str(), s.size()); + NetworkPacket pkt(TOCLIENT_DETACHED_INVENTORY, 0, peer_id); + pkt.putRawString(s.c_str(), s.size()); if (peer_id != PEER_ID_INEXISTENT) { - Send(pkt); + Send(&pkt); } else { - m_clients.sendToAll(0, pkt, true); + m_clients.sendToAll(0, &pkt, true); } } @@ -2526,21 +2538,70 @@ void Server::RespawnPlayer(u16 peer_id) bool repositioned = m_script->on_respawnplayer(playersao); if(!repositioned){ - v3f pos = findSpawnPos(m_env->getServerMap()); + v3f pos = findSpawnPos(); // setPos will send the new position to client playersao->setPos(pos); } } +void Server::DenySudoAccess(u16 peer_id) +{ + DSTACK(__FUNCTION_NAME); -void Server::DenyAccess(u16 peer_id, const std::wstring &reason) + NetworkPacket pkt(TOCLIENT_DENY_SUDO_MODE, 0, peer_id); + Send(&pkt); +} + +void Server::DenyAccess(u16 peer_id, AccessDeniedCode reason, const std::string &custom_reason) { DSTACK(__FUNCTION_NAME); - SendAccessDenied(peer_id, reason); + SendAccessDenied(peer_id, reason, custom_reason); m_clients.event(peer_id, CSE_SetDenied); m_con.DisconnectPeer(peer_id); } +// 13/03/15: remove this function when protocol version 25 will become +// the minimum version for MT users, maybe in 1 year +void Server::DenyAccess_Legacy(u16 peer_id, const std::wstring &reason) +{ + DSTACK(__FUNCTION_NAME); + + SendAccessDenied_Legacy(peer_id, reason); + m_clients.event(peer_id, CSE_SetDenied); + m_con.DisconnectPeer(peer_id); +} + +void Server::acceptAuth(u16 peer_id, bool forSudoMode) +{ + DSTACK(__FUNCTION_NAME); + + if (!forSudoMode) { + RemoteClient* client = getClient(peer_id, CS_Invalid); + + NetworkPacket resp_pkt(TOCLIENT_AUTH_ACCEPT, 1 + 6 + 8 + 4, peer_id); + + // Right now, the auth mechs don't change between login and sudo mode. + u32 sudo_auth_mechs = client->allowed_auth_mechs; + client->allowed_sudo_mechs = sudo_auth_mechs; + + resp_pkt << v3f(0,0,0) << (u64) m_env->getServerMap().getSeed() + << g_settings->getFloat("dedicated_server_step") + << sudo_auth_mechs; + + Send(&resp_pkt); + m_clients.event(peer_id, CSE_AuthAccept); + } else { + NetworkPacket resp_pkt(TOCLIENT_ACCEPT_SUDO_MODE, 1 + 6 + 8 + 4, peer_id); + + // We only support SRP right now + u32 sudo_auth_mechs = AUTH_MECHANISM_FIRST_SRP; + + resp_pkt << sudo_auth_mechs; + Send(&resp_pkt); + m_clients.event(peer_id, CSE_SudoSuccess); + } +} + void Server::DeleteClient(u16 peer_id, ClientDeletionReason reason) { DSTACK(__FUNCTION_NAME); @@ -2637,8 +2698,8 @@ void Server::UpdateCrafting(Player* player) // Put the new preview in InventoryList *plist = player->inventory.getList("craftpreview"); - assert(plist); - assert(plist->getSize() >= 1); + sanity_check(plist); + sanity_check(plist->getSize() >= 1); plist->changeItem(0, preview); } @@ -2676,7 +2737,7 @@ std::wstring Server::getStatusString() std::wostringstream os(std::ios_base::binary); os<::iterator i = m_mods.begin(); + for(std::vector::const_iterator i = m_mods.begin(); i != m_mods.end(); i++){ const ModSpec &mod = *i; if(mod.name == modname) @@ -3172,23 +3233,24 @@ std::string Server::getBuiltinLuaPath() return porting::path_share + DIR_DELIM + "builtin"; } -v3f findSpawnPos(ServerMap &map) +v3f Server::findSpawnPos() { - //return v3f(50,50,50)*BS; - - v3s16 nodepos; + ServerMap &map = m_env->getServerMap(); + v3f nodeposf; + if (g_settings->getV3FNoEx("static_spawnpoint", nodeposf)) { + return nodeposf * BS; + } -#if 0 - nodepos = v2s16(0,0); - groundheight = 20; -#endif + // Default position is static_spawnpoint + // We will return it if we don't found a good place + v3s16 nodepos(nodeposf.X, nodeposf.Y, nodeposf.Z); -#if 1 s16 water_level = map.getWaterLevel(); + bool is_good = false; + // Try to find a good place a few times - for(s32 i=0; i<1000; i++) - { + for(s32 i = 0; i < 1000 && !is_good; i++) { s32 range = 1 + i; // We're going to try to throw the player to this position v2s16 nodepos2d = v2s16( @@ -3203,7 +3265,7 @@ v3f findSpawnPos(ServerMap &map) continue; nodepos = v3s16(nodepos2d.X, groundheight, nodepos2d.Y); - bool is_good = false; + s32 air_count = 0; for (s32 i = 0; i < 10; i++) { v3s16 blockpos = getNodeBlockPos(nodepos); @@ -3218,13 +3280,7 @@ v3f findSpawnPos(ServerMap &map) } nodepos.Y++; } - if(is_good){ - // Found a good place - //infostream<<"Searched through "<getServerMap()); + v3f pos = findSpawnPos(); player->setPosition(pos); // Make sure the player is saved