]> git.lizzy.rs Git - minetest.git/blobdiff - src/client.cpp
Fixed minetest reliable udp implementation (compatible to old clients)
[minetest.git] / src / client.cpp
index b3030991b826f0f3a15053a1c43e6c0047697b36..1c7ecf3f36225ef0446a33d508e54140778795ba 100644 (file)
@@ -51,15 +51,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "util/pointedthing.h"
 #include "version.h"
 
-#if USE_CURL
-#include <curl/curl.h>
-#endif
-
-static std::string getMediaCacheDir()
-{
-       return porting::path_user + DIR_DELIM + "cache" + DIR_DELIM + "media";
-}
-
 /*
        QueuedMeshUpdate
 */
@@ -295,6 +286,20 @@ Client::Client(
        }
 }
 
+void Client::Stop()
+{
+       //request all client managed threads to stop
+       m_mesh_update_thread.Stop();
+}
+
+bool Client::isShutdown()
+{
+
+       if (!m_mesh_update_thread.IsRunning()) return true;
+
+       return false;
+}
+
 Client::~Client()
 {
        {
@@ -305,7 +310,7 @@ Client::~Client()
        m_mesh_update_thread.Stop();
        m_mesh_update_thread.Wait();
        while(!m_mesh_update_thread.m_queue_out.empty()) {
-               MeshUpdateResult r = m_mesh_update_thread.m_queue_out.pop_front();
+               MeshUpdateResult r = m_mesh_update_thread.m_queue_out.pop_frontNoEx();
                delete r.mesh;
        }
 
@@ -533,7 +538,7 @@ void Client::step(float dtime)
                        writeU16(&data[53], CLIENT_PROTOCOL_VERSION_MAX);
 
                        // Send as unreliable
-                       Send(0, data, false);
+                       Send(1, data, false);
                }
 
                // Not connected, return
@@ -592,7 +597,7 @@ void Client::step(float dtime)
                                        writeV3S16(&reply[2+1+6*k], *j);
                                        k++;
                                }
-                               m_con.Send(PEER_ID_SERVER, 1, reply, true);
+                               m_con.Send(PEER_ID_SERVER, 2, reply, true);
 
                                if(i == deleted_blocks.end())
                                        break;
@@ -701,7 +706,7 @@ void Client::step(float dtime)
                while(!m_mesh_update_thread.m_queue_out.empty())
                {
                        num_processed_meshes++;
-                       MeshUpdateResult r = m_mesh_update_thread.m_queue_out.pop_front();
+                       MeshUpdateResult r = m_mesh_update_thread.m_queue_out.pop_frontNoEx();
                        MapBlock *block = m_env.getMap().getBlockNoCreateNoEx(r.p);
                        if(block)
                        {
@@ -740,7 +745,7 @@ void Client::step(float dtime)
                                reply[2] = 1;
                                writeV3S16(&reply[3], r.p);
                                // Send as reliable
-                               m_con.Send(PEER_ID_SERVER, 1, reply, true);
+                               m_con.Send(PEER_ID_SERVER, 2, reply, true);
                        }
                }
                if(num_processed_meshes > 0)
@@ -835,7 +840,7 @@ void Client::step(float dtime)
                        std::string s = os.str();
                        SharedBuffer<u8> data((u8*)s.c_str(), s.size());
                        // Send as reliable
-                       Send(0, data, true);
+                       Send(1, data, true);
                }
        }
 }
@@ -902,30 +907,12 @@ bool Client::loadMedia(const std::string &data, const std::string &filename)
        name = removeStringEnd(filename, model_ext);
        if(name != "")
        {
-               verbosestream<<"Client: Storing model into Irrlicht: "
+               verbosestream<<"Client: Storing model into memory: "
                                <<"\""<<filename<<"\""<<std::endl;
-               scene::ISceneManager *smgr = m_device->getSceneManager();
-
-               //check if mesh was already cached
-               scene::IAnimatedMesh *mesh =
-                       smgr->getMeshCache()->getMeshByName(filename.c_str());
-
-               if (mesh != NULL) {
-                       errorstream << "Multiple models with name: " << filename.c_str() <<
-                                       " found replacing previous model!" << std::endl;
-
-                       smgr->getMeshCache()->removeMesh(mesh);
-                       mesh = 0;
-               }
-
-               io::IFileSystem *irrfs = m_device->getFileSystem();
-               io::IReadFile *rfile = irrfs->createMemoryReadFile(
-                               *data_rw, data_rw.getSize(), filename.c_str());
-               assert(rfile);
-               
-               mesh = smgr->getMesh(rfile);
-               smgr->getMeshCache()->addMesh(filename.c_str(), mesh);
-               rfile->drop();
+               if(m_mesh_data.count(filename))
+                       errorstream<<"Multiple models with name \""<<filename.c_str()
+                                       <<"\" found; replacing previous model"<<std::endl;
+               m_mesh_data[filename] = data;
                return true;
        }
 
@@ -970,7 +957,7 @@ void Client::request_media(const std::list<std::string> &file_requests)
        std::string s = os.str();
        SharedBuffer<u8> data((u8*)s.c_str(), s.size());
        // Send as reliable
-       Send(0, data, true);
+       Send(1, data, true);
        infostream<<"Client: Sending media request list to server ("
                        <<file_requests.size()<<" files)"<<std::endl;
 }
@@ -983,7 +970,7 @@ void Client::received_media()
        std::string s = os.str();
        SharedBuffer<u8> data((u8*)s.c_str(), s.size());
        // Send as reliable
-       Send(0, data, true);
+       Send(1, data, true);
        infostream<<"Client: Notifying server that we received all media"
                        <<std::endl;
 }
@@ -2845,3 +2832,31 @@ MtEventManager* Client::getEventManager()
        return m_event;
 }
 
+scene::IAnimatedMesh* Client::getMesh(const std::string &filename)
+{
+       std::map<std::string, std::string>::const_iterator i =
+                       m_mesh_data.find(filename);
+       if(i == m_mesh_data.end()){
+               errorstream<<"Client::getMesh(): Mesh not found: \""<<filename<<"\""
+                               <<std::endl;
+               return NULL;
+       }
+       const std::string &data = i->second;
+       scene::ISceneManager *smgr = m_device->getSceneManager();
+
+       // Create the mesh, remove it from cache and return it
+       // This allows unique vertex colors and other properties for each instance
+       Buffer<char> data_rw(data.c_str(), data.size()); // Const-incorrect Irrlicht
+       io::IFileSystem *irrfs = m_device->getFileSystem();
+       io::IReadFile *rfile = irrfs->createMemoryReadFile(
+                       *data_rw, data_rw.getSize(), filename.c_str());
+       assert(rfile);
+       scene::IAnimatedMesh *mesh = smgr->getMesh(rfile);
+       rfile->drop();
+       // NOTE: By playing with Irrlicht refcounts, maybe we could cache a bunch
+       // of uniquely named instances and re-use them
+       mesh->grab();
+       smgr->getMeshCache()->removeMesh(mesh);
+       return mesh;
+}
+