diff options
Diffstat (limited to 'src/ChunkSender.cpp')
-rw-r--r-- | src/ChunkSender.cpp | 98 |
1 files changed, 55 insertions, 43 deletions
diff --git a/src/ChunkSender.cpp b/src/ChunkSender.cpp index 1fa74977f..d98dc0ef4 100644 --- a/src/ChunkSender.cpp +++ b/src/ChunkSender.cpp @@ -1,7 +1,8 @@ // ChunkSender.cpp -// Interfaces to the cChunkSender class representing the thread that waits for chunks becoming ready (loaded / generated) and sends them to clients +// Interfaces to the cChunkSender class representing the thread that waits for chunks becoming ready (loaded / +// generated) and sends them to clients @@ -23,17 +24,22 @@ /** Callback that can be used to notify chunk sender upon another chunkcoord notification */ -class cNotifyChunkSender : - public cChunkCoordCallback +class cNotifyChunkSender : public cChunkCoordCallback { virtual void Call(cChunkCoords a_Coords, bool a_IsSuccess) override { cChunkSender & ChunkSender = m_ChunkSender; m_World.DoWithChunk( - a_Coords.m_ChunkX, a_Coords.m_ChunkZ, - [&ChunkSender] (cChunk & a_Chunk) -> bool + a_Coords.m_ChunkX, + a_Coords.m_ChunkZ, + [&ChunkSender](cChunk & a_Chunk) -> bool { - ChunkSender.QueueSendChunkTo(a_Chunk.GetPosX(), a_Chunk.GetPosZ(), cChunkSender::Priority::High, a_Chunk.GetAllClients()); + ChunkSender.QueueSendChunkTo( + a_Chunk.GetPosX(), + a_Chunk.GetPosZ(), + cChunkSender::Priority::High, + a_Chunk.GetAllClients() + ); return true; } ); @@ -43,10 +49,9 @@ class cNotifyChunkSender : cWorld & m_World; -public: - cNotifyChunkSender(cChunkSender & a_ChunkSender, cWorld & a_World): - m_ChunkSender(a_ChunkSender), - m_World(a_World) + public: + cNotifyChunkSender(cChunkSender & a_ChunkSender, cWorld & a_World) : + m_ChunkSender(a_ChunkSender), m_World(a_World) { } }; @@ -59,9 +64,7 @@ public: // cChunkSender: cChunkSender::cChunkSender(cWorld & a_World) : - Super("Chunk Sender"), - m_World(a_World), - m_Serializer(m_World.GetDimension()) + Super("Chunk Sender"), m_World(a_World), m_Serializer(m_World.GetDimension()) { } @@ -93,7 +96,7 @@ void cChunkSender::QueueSendChunkTo(int a_ChunkX, int a_ChunkZ, Priority a_Prior { ASSERT(a_Client != nullptr); { - cChunkCoords Chunk{a_ChunkX, a_ChunkZ}; + cChunkCoords Chunk {a_ChunkX, a_ChunkZ}; cCSLock Lock(m_CS); auto iter = m_ChunkInfo.find(Chunk); if (iter != m_ChunkInfo.end()) @@ -101,15 +104,15 @@ void cChunkSender::QueueSendChunkTo(int a_ChunkX, int a_ChunkZ, Priority a_Prior auto & info = iter->second; if (info.m_Priority < a_Priority) // Was the chunk's priority boosted? { - m_SendChunks.push(sChunkQueue{a_Priority, Chunk}); + m_SendChunks.push(sChunkQueue {a_Priority, Chunk}); info.m_Priority = a_Priority; } info.m_Clients.insert(a_Client->shared_from_this()); } else { - m_SendChunks.push(sChunkQueue{a_Priority, Chunk}); - auto info = sSendChunk{Chunk, a_Priority}; + m_SendChunks.push(sChunkQueue {a_Priority, Chunk}); + auto info = sSendChunk {Chunk, a_Priority}; info.m_Clients.insert(a_Client->shared_from_this()); m_ChunkInfo.emplace(Chunk, info); } @@ -121,10 +124,15 @@ void cChunkSender::QueueSendChunkTo(int a_ChunkX, int a_ChunkZ, Priority a_Prior -void cChunkSender::QueueSendChunkTo(int a_ChunkX, int a_ChunkZ, Priority a_Priority, const std::vector<cClientHandle *> & a_Clients) +void cChunkSender::QueueSendChunkTo( + int a_ChunkX, + int a_ChunkZ, + Priority a_Priority, + const std::vector<cClientHandle *> & a_Clients +) { { - cChunkCoords Chunk{a_ChunkX, a_ChunkZ}; + cChunkCoords Chunk {a_ChunkX, a_ChunkZ}; cCSLock Lock(m_CS); auto iter = m_ChunkInfo.find(Chunk); if (iter != m_ChunkInfo.end()) @@ -132,7 +140,7 @@ void cChunkSender::QueueSendChunkTo(int a_ChunkX, int a_ChunkZ, Priority a_Prior auto & info = iter->second; if (info.m_Priority < a_Priority) // Was the chunk's priority boosted? { - m_SendChunks.push(sChunkQueue{a_Priority, Chunk}); + m_SendChunks.push(sChunkQueue {a_Priority, Chunk}); info.m_Priority = a_Priority; } for (const auto & Client : a_Clients) @@ -142,8 +150,8 @@ void cChunkSender::QueueSendChunkTo(int a_ChunkX, int a_ChunkZ, Priority a_Prior } else { - m_SendChunks.push(sChunkQueue{a_Priority, Chunk}); - auto info = sSendChunk{Chunk, a_Priority}; + m_SendChunks.push(sChunkQueue {a_Priority, Chunk}); + auto info = sSendChunk {Chunk, a_Priority}; for (const auto & Client : a_Clients) { info.m_Clients.insert(Client->shared_from_this()); @@ -251,29 +259,33 @@ void cChunkSender::SendChunk(int a_ChunkX, int a_ChunkZ, const WeakClients & a_C // Send entity packets: for (const auto EntityID : m_EntityIDs) { - m_World.DoWithEntityByID(EntityID, [Client](cEntity & a_Entity) - { - /* - // DEBUG: - LOGD("cChunkSender: Entity #%d (%s) at [%f, %f, %f] spawning for player \"%s\"", - a_Entity.GetUniqueID(), a_Entity.GetClass(), - a_Entity.GetPosition().x, a_Entity.GetPosition().y, a_Entity.GetPosition().z, - Client->GetUsername().c_str() - ); - */ - - /* This check looks highly suspect. - Its purpose is to check the client still has a valid player object associated, - since the player destroys itself when the client is destroyed. - It's done within the world lock to ensure correctness. - A better way involves fixing chunk sending (GH #3696) to obviate calling SpawnOn from this thread in the first place. */ - if (!Client->IsDestroyed()) + m_World.DoWithEntityByID( + EntityID, + [Client](cEntity & a_Entity) { - a_Entity.SpawnOn(*Client); + /* + // DEBUG: + LOGD("cChunkSender: Entity #%d (%s) at [%f, %f, %f] spawning for player \"%s\"", + a_Entity.GetUniqueID(), a_Entity.GetClass(), + a_Entity.GetPosition().x, a_Entity.GetPosition().y, a_Entity.GetPosition().z, + Client->GetUsername().c_str() + ); + */ + + /* This check looks highly suspect. + Its purpose is to check the client still has a valid player object associated, + since the player destroys itself when the client is destroyed. + It's done within the world lock to ensure correctness. + A better way involves fixing chunk sending (GH #3696) to obviate calling SpawnOn from this thread in + the first place. */ + if (!Client->IsDestroyed()) + { + a_Entity.SpawnOn(*Client); + } + + return true; } - - return true; - }); + ); } } |