Catch SendFailedException when replying back in Connection::Receive()
[oweals/minetest.git] / src / environment.cpp
index 99dc6d62be495172ff12400e954e2e09f786fcc9..25e65d2be667f0db661faa13e1743cfb28ca6cc0 100644 (file)
@@ -684,7 +684,7 @@ void ServerEnvironment::step(float dtime)
                Handle players
        */
        {
-               ScopeProfiler sp(g_profiler, "SEnv: handle players avg", SPT_LOWPASS);
+               ScopeProfiler sp(g_profiler, "SEnv: handle players avg", SPT_AVG);
                for(core::list<Player*>::Iterator i = m_players.begin();
                                i != m_players.end(); i++)
                {
@@ -726,7 +726,7 @@ void ServerEnvironment::step(float dtime)
        */
        if(m_active_blocks_management_interval.step(dtime, 2.0))
        {
-               ScopeProfiler sp(g_profiler, "SEnv: manage act. block list avg", SPT_LOWPASS);
+               ScopeProfiler sp(g_profiler, "SEnv: manage act. block list avg /2s", SPT_AVG);
                /*
                        Get player block positions
                */
@@ -803,7 +803,7 @@ void ServerEnvironment::step(float dtime)
        */
        if(m_active_blocks_nodemetadata_interval.step(dtime, 1.0))
        {
-               ScopeProfiler sp(g_profiler, "SEnv: mess in act. blocks avg", SPT_LOWPASS);
+               ScopeProfiler sp(g_profiler, "SEnv: mess in act. blocks avg /1s", SPT_AVG);
                
                float dtime = 1.0;
 
@@ -842,7 +842,7 @@ void ServerEnvironment::step(float dtime)
        
        if(m_active_blocks_test_interval.step(dtime, 10.0))
        {
-               ScopeProfiler sp(g_profiler, "SEnv: modify in blocks avg", SPT_LOWPASS);
+               ScopeProfiler sp(g_profiler, "SEnv: modify in blocks avg /10s", SPT_AVG);
                //float dtime = 10.0;
                
                for(core::map<v3s16, bool>::Iterator
@@ -1045,8 +1045,10 @@ void ServerEnvironment::step(float dtime)
                Step active objects
        */
        {
-               ScopeProfiler sp(g_profiler, "SEnv: step act. objs avg", SPT_LOWPASS);
+               ScopeProfiler sp(g_profiler, "SEnv: step act. objs avg", SPT_AVG);
                //TimeTaker timer("Step active objects");
+
+               g_profiler->avg("SEnv: num of objects", m_active_objects.size());
                
                // This helps the objects to send data at the same time
                bool send_recommended = false;
@@ -1086,7 +1088,7 @@ void ServerEnvironment::step(float dtime)
        */
        if(m_object_management_interval.step(dtime, 0.5))
        {
-               ScopeProfiler sp(g_profiler, "SEnv: remove removed objs avg", SPT_LOWPASS);
+               ScopeProfiler sp(g_profiler, "SEnv: remove removed objs avg /.5s", SPT_AVG);
                /*
                        Remove objects that satisfy (m_removed && m_known_by_count==0)
                */
@@ -1341,21 +1343,24 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
                bool set_changed)
 {
        assert(object);
-       if(object->getId() == 0)
-       {
+       if(object->getId() == 0){
                u16 new_id = getFreeServerActiveObjectId(m_active_objects);
                if(new_id == 0)
                {
-                       infostream<<"ServerEnvironment::addActiveObjectRaw(): "
+                       errorstream<<"ServerEnvironment::addActiveObjectRaw(): "
                                        <<"no free ids available"<<std::endl;
                        delete object;
                        return 0;
                }
                object->setId(new_id);
        }
+       else{
+               verbosestream<<"ServerEnvironment::addActiveObjectRaw(): "
+                               <<"supplied with id "<<object->getId()<<std::endl;
+       }
        if(isFreeServerActiveObjectId(object->getId(), m_active_objects) == false)
        {
-               infostream<<"ServerEnvironment::addActiveObjectRaw(): "
+               errorstream<<"ServerEnvironment::addActiveObjectRaw(): "
                                <<"id is not free ("<<object->getId()<<")"<<std::endl;
                delete object;
                return 0;
@@ -1364,7 +1369,12 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
                        <<"added (id="<<object->getId()<<")"<<std::endl;*/
                        
        m_active_objects.insert(object->getId(), object);
-
+  
+       verbosestream<<"ServerEnvironment::addActiveObjectRaw(): "
+                       <<"Added id="<<object->getId()<<"; there are now "
+                       <<m_active_objects.size()<<" active objects."
+                       <<std::endl;
+       
        // Add static object to active static list of the block
        v3f objectpos = object->getBasePosition();
        std::string staticdata = object->getStaticData();
@@ -1382,8 +1392,9 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
                        block->setChangedFlag();
        }
        else{
-               infostream<<"ServerEnv: Could not find a block for "
-                               <<"storing newly added static active object"<<std::endl;
+               errorstream<<"ServerEnvironment::addActiveObjectRaw(): "
+                               <<"could not find block for storing id="<<object->getId()
+                               <<" statically"<<std::endl;
        }
 
        return object->getId();
@@ -1448,6 +1459,40 @@ void ServerEnvironment::removeRemovedObjects()
        }
 }
 
+static void print_hexdump(std::ostream &o, const std::string &data)
+{
+       const int linelength = 16;
+       for(int l=0; ; l++){
+               int i0 = linelength * l;
+               bool at_end = false;
+               int thislinelength = linelength;
+               if(i0 + thislinelength > (int)data.size()){
+                       thislinelength = data.size() - i0;
+                       at_end = true;
+               }
+               for(int di=0; di<linelength; di++){
+                       int i = i0 + di;
+                       char buf[4];
+                       if(di<thislinelength)
+                               snprintf(buf, 4, "%.2x ", data[i]);
+                       else
+                               snprintf(buf, 4, "   ");
+                       o<<buf;
+               }
+               o<<" ";
+               for(int di=0; di<thislinelength; di++){
+                       int i = i0 + di;
+                       if(data[i] >= 32)
+                               o<<data[i];
+                       else
+                               o<<".";
+               }
+               o<<std::endl;
+               if(at_end)
+                       break;
+       }
+}
+
 /*
        Convert stored objects from blocks near the players to active.
 */
@@ -1458,6 +1503,18 @@ void ServerEnvironment::activateObjects(MapBlock *block)
        // Ignore if no stored objects (to not set changed flag)
        if(block->m_static_objects.m_stored.size() == 0)
                return;
+       verbosestream<<"ServerEnvironment::activateObjects(): "
+                       <<"activating objects of block "<<PP(block->getPos())
+                       <<" ("<<block->m_static_objects.m_stored.size()
+                       <<" objects)"<<std::endl;
+       bool large_amount = (block->m_static_objects.m_stored.size() >= 50);
+       if(large_amount){
+               errorstream<<"suspiciously large amount of objects detected: "
+                               <<block->m_static_objects.m_stored.size()<<" in "
+                               <<PP(block->getPos())
+                               <<"; not activating."<<std::endl;
+               return;
+       }
        // A list for objects that couldn't be converted to static for some
        // reason. They will be stored back.
        core::list<StaticObject> new_stored;
@@ -1475,9 +1532,18 @@ void ServerEnvironment::activateObjects(MapBlock *block)
                // If couldn't create object, store static data back.
                if(obj==NULL)
                {
+                       errorstream<<"ServerEnvironment::activateObjects(): "
+                                       <<"failed to create active object from static object "
+                                       <<"in block "<<PP(s_obj.pos/BS)
+                                       <<" type="<<(int)s_obj.type<<" data:"<<std::endl;
+                       print_hexdump(verbosestream, s_obj.data);
+                       
                        new_stored.push_back(s_obj);
                        continue;
                }
+               verbosestream<<"ServerEnvironment::activateObjects(): "
+                               <<"activated static object pos="<<PP(s_obj.pos/BS)
+                               <<" type="<<(int)s_obj.type<<std::endl;
                // This will also add the object to the active static list
                addActiveObjectRaw(obj, false);
                //u16 id = addActiveObjectRaw(obj, false);
@@ -1534,6 +1600,10 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
                if(m_active_blocks.contains(blockpos_o))
                        continue;
 
+               verbosestream<<"ServerEnvironment::deactivateFarObjects(): "
+                               <<"deactivating object id="<<id<<" on inactive block "
+                               <<PP(blockpos_o)<<std::endl;
+
                /*
                        Update the static data
                */
@@ -1549,7 +1619,9 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
                                block->m_static_objects.remove(id);
                                oldblock = block;
                        }
+                       obj->m_static_exists = false;
                }
+
                // Create new static object
                std::string staticdata = obj->getStaticData();
                StaticObject s_obj(obj->getType(), objectpos, staticdata);
@@ -1571,15 +1643,23 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
 
                if(block)
                {
-                       block->m_static_objects.insert(0, s_obj);
-                       block->setChangedFlag();
-                       obj->m_static_exists = true;
-                       obj->m_static_block = block->getPos();
+                       if(block->m_static_objects.m_stored.size() >= 50){
+                               errorstream<<"ServerEnv: Trying to store id="<<obj->getId()
+                                               <<" statically but block "<<PP(blockpos)
+                                               <<" already contains over 50 objects."
+                                               <<" Forcing delete."<<std::endl;
+                               force_delete = true;
+                       } else {
+                               block->m_static_objects.insert(0, s_obj);
+                               block->setChangedFlag();
+                               obj->m_static_exists = true;
+                               obj->m_static_block = block->getPos();
+                       }
                }
                else{
-                       infostream<<"ServerEnv: Could not find or generate "
-                                       <<"a block for storing static object"<<std::endl;
-                       obj->m_static_exists = false;
+                       errorstream<<"ServerEnv: Could not find or generate "
+                                       <<"a block for storing id="<<obj->getId()
+                                       <<" statically"<<std::endl;
                        continue;
                }
 
@@ -1591,12 +1671,17 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
                // If known by some client, don't delete.
                if(obj->m_known_by_count > 0 && force_delete == false)
                {
+                       verbosestream<<"ServerEnvironment::deactivateFarObjects(): "
+                                       <<"object id="<<id<<" is known by clients"
+                                       <<"; not deleting yet"<<std::endl;
+
                        obj->m_pending_deactivation = true;
                        continue;
                }
                
-               /*infostream<<"Server: Stored static data. Deleting object."
-                               <<std::endl;*/
+               verbosestream<<"ServerEnvironment::deactivateFarObjects(): "
+                               <<"object id="<<id<<" is not known by clients"
+                               <<"; deleting"<<std::endl;
                // Delete active object
                delete obj;
                // Id to be removed from m_active_objects