From 382e014ebcd44a72788bea8cdcec7f64861b063f Mon Sep 17 00:00:00 2001 From: Howaner Date: Thu, 2 Oct 2014 23:50:41 +0200 Subject: Optimized chunk loader --- src/ChunkSender.cpp | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) (limited to 'src/ChunkSender.cpp') diff --git a/src/ChunkSender.cpp b/src/ChunkSender.cpp index a3151eb3f..0bdc0cf75 100644 --- a/src/ChunkSender.cpp +++ b/src/ChunkSender.cpp @@ -192,40 +192,37 @@ void cChunkSender::SendChunk(int a_ChunkX, int a_ChunkZ, cClientHandle * a_Clien ASSERT(m_World != NULL); // Ask the client if it still wants the chunk: - if (a_Client != NULL) + if ((a_Client != NULL) && !a_Client->WantsSendChunk(a_ChunkX, a_ChunkZ)) { - if (!a_Client->WantsSendChunk(a_ChunkX, a_ChunkZ)) - { - return; - } + return; } - + // If the chunk has no clients, no need to packetize it: if (!m_World->HasChunkAnyClients(a_ChunkX, a_ChunkZ)) { return; } - + // If the chunk is not valid, do nothing - whoever needs it has queued it for loading / generating if (!m_World->IsChunkValid(a_ChunkX, a_ChunkZ)) { return; } - + // If the chunk is not lighted, queue it for relighting and get notified when it's ready: if (!m_World->IsChunkLighted(a_ChunkX, a_ChunkZ)) { m_World->QueueLightChunk(a_ChunkX, a_ChunkZ, &m_Notify); return; } - + // Query and prepare chunk data: if (!m_World->GetChunkData(a_ChunkX, a_ChunkZ, *this)) { return; } cChunkDataSerializer Data(m_BlockTypes, m_BlockMetas, m_BlockLight, m_BlockSkyLight, m_BiomeMap); - + // Send: if (a_Client == NULL) { @@ -235,7 +232,7 @@ void cChunkSender::SendChunk(int a_ChunkX, int a_ChunkZ, cClientHandle * a_Clien { a_Client->SendChunkData(a_ChunkX, a_ChunkZ, Data); } - + // Send block-entity packets: for (sBlockCoords::iterator itr = m_BlockEntities.begin(); itr != m_BlockEntities.end(); ++itr) { @@ -249,7 +246,7 @@ void cChunkSender::SendChunk(int a_ChunkX, int a_ChunkZ, cClientHandle * a_Clien } } // for itr - m_Packets[] m_BlockEntities.clear(); - + // TODO: Send entity spawn packets } -- cgit v1.2.3 From b493beb3bbac05d0402a6e388a61bf446c6c00ff Mon Sep 17 00:00:00 2001 From: Howaner Date: Mon, 6 Oct 2014 21:27:53 +0200 Subject: Stream 4 chunks per tick. Added priority. --- src/ChunkSender.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'src/ChunkSender.cpp') diff --git a/src/ChunkSender.cpp b/src/ChunkSender.cpp index 0bdc0cf75..2a953db1f 100644 --- a/src/ChunkSender.cpp +++ b/src/ChunkSender.cpp @@ -91,17 +91,22 @@ void cChunkSender::ChunkReady(int a_ChunkX, int a_ChunkZ) -void cChunkSender::QueueSendChunkTo(int a_ChunkX, int a_ChunkZ, cClientHandle * a_Client) +void cChunkSender::QueueSendChunkTo(int a_ChunkX, int a_ChunkZ, eChunkPriority a_Priority, cClientHandle * a_Client) { ASSERT(a_Client != NULL); { + sSendChunk Chunk(a_ChunkX, a_ChunkZ, a_Priority, a_Client); + cCSLock Lock(m_CS); - if (std::find(m_SendChunks.begin(), m_SendChunks.end(), sSendChunk(a_ChunkX, a_ChunkZ, a_Client)) != m_SendChunks.end()) + if (std::find(m_SendChunks.begin(), m_SendChunks.end(), Chunk) != m_SendChunks.end()) { // Already queued, bail out return; } - m_SendChunks.push_back(sSendChunk(a_ChunkX, a_ChunkZ, a_Client)); + m_SendChunks.push_back(Chunk); + + // Sort the list: + m_SendChunks.sort(); } m_evtQueue.Set(); } @@ -169,7 +174,7 @@ void cChunkSender::Execute(void) sSendChunk Chunk(m_SendChunks.front()); m_SendChunks.pop_front(); Lock.Unlock(); - + SendChunk(Chunk.m_ChunkX, Chunk.m_ChunkZ, Chunk.m_Client); } Lock.Lock(); -- cgit v1.2.3 From b0988e65aadc1a9d33065cf6afefc05dbf768ef8 Mon Sep 17 00:00:00 2001 From: Howaner Date: Tue, 21 Oct 2014 17:35:23 +0200 Subject: Use two lists and 2 chunk send prioritys. --- src/ChunkSender.cpp | 48 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) (limited to 'src/ChunkSender.cpp') diff --git a/src/ChunkSender.cpp b/src/ChunkSender.cpp index 2a953db1f..680f34bce 100644 --- a/src/ChunkSender.cpp +++ b/src/ChunkSender.cpp @@ -95,18 +95,23 @@ void cChunkSender::QueueSendChunkTo(int a_ChunkX, int a_ChunkZ, eChunkPriority a { ASSERT(a_Client != NULL); { - sSendChunk Chunk(a_ChunkX, a_ChunkZ, a_Priority, a_Client); + sSendChunk Chunk(a_ChunkX, a_ChunkZ, a_Client); cCSLock Lock(m_CS); - if (std::find(m_SendChunks.begin(), m_SendChunks.end(), Chunk) != m_SendChunks.end()) + if ( + std::find(m_SendChunksLowPriority.begin(), m_SendChunksLowPriority.end(), Chunk) != m_SendChunksLowPriority.end() || + std::find(m_SendChunksHighPriority.begin(), m_SendChunksHighPriority.end(), Chunk) != m_SendChunksHighPriority.end() + ) { // Already queued, bail out return; } - m_SendChunks.push_back(Chunk); - // Sort the list: - m_SendChunks.sort(); + if (a_Priority == E_CHUNK_PRIORITY_LOW) { + m_SendChunksLowPriority.push_back(Chunk); + } else if (a_Priority == E_CHUNK_PRIORITY_HIGH) { + m_SendChunksHighPriority.push_back(Chunk); + } } m_evtQueue.Set(); } @@ -119,15 +124,23 @@ void cChunkSender::RemoveClient(cClientHandle * a_Client) { { cCSLock Lock(m_CS); - for (sSendChunkList::iterator itr = m_SendChunks.begin(); itr != m_SendChunks.end();) + for (sSendChunkList::iterator itr = m_SendChunksLowPriority.begin(); itr != m_SendChunksLowPriority.end();) { if (itr->m_Client == a_Client) { - itr = m_SendChunks.erase(itr); + itr = m_SendChunksLowPriority.erase(itr); + continue; + } + ++itr; + } // for itr - m_SendChunksLowPriority[] + for (sSendChunkList::iterator itr = m_SendChunksHighPriority.begin(); itr != m_SendChunksHighPriority.end();) + { + if (itr->m_Client == a_Client) { + itr = m_SendChunksHighPriority.erase(itr); continue; } ++itr; - } // for itr - m_SendChunks[] + } // for itr - m_SendChunksHighPriority[] m_RemoveCount++; } m_evtQueue.Set(); @@ -143,7 +156,7 @@ void cChunkSender::Execute(void) while (!m_ShouldTerminate) { cCSLock Lock(m_CS); - while (m_ChunksReady.empty() && m_SendChunks.empty()) + while (m_ChunksReady.empty() && m_SendChunksLowPriority.empty() && m_SendChunksHighPriority.empty()) { int RemoveCount = m_RemoveCount; m_RemoveCount = 0; @@ -158,8 +171,17 @@ void cChunkSender::Execute(void) return; } } // while (empty) - - if (!m_ChunksReady.empty()) + + if (!m_SendChunksHighPriority.empty()) + { + // Take one from the queue: + sSendChunk Chunk(m_SendChunksHighPriority.front()); + m_SendChunksHighPriority.pop_front(); + Lock.Unlock(); + + SendChunk(Chunk.m_ChunkX, Chunk.m_ChunkZ, Chunk.m_Client); + } + else if (!m_ChunksReady.empty()) { // Take one from the queue: cChunkCoords Coords(m_ChunksReady.front()); @@ -171,8 +193,8 @@ void cChunkSender::Execute(void) else { // Take one from the queue: - sSendChunk Chunk(m_SendChunks.front()); - m_SendChunks.pop_front(); + sSendChunk Chunk(m_SendChunksLowPriority.front()); + m_SendChunksLowPriority.pop_front(); Lock.Unlock(); SendChunk(Chunk.m_ChunkX, Chunk.m_ChunkZ, Chunk.m_Client); -- cgit v1.2.3 From 2d639675516cb563e3f7bf018ee3619970ef1edc Mon Sep 17 00:00:00 2001 From: Howaner Date: Tue, 21 Oct 2014 18:32:02 +0200 Subject: style. --- src/ChunkSender.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/ChunkSender.cpp') diff --git a/src/ChunkSender.cpp b/src/ChunkSender.cpp index 680f34bce..fdcd485a9 100644 --- a/src/ChunkSender.cpp +++ b/src/ChunkSender.cpp @@ -135,7 +135,8 @@ void cChunkSender::RemoveClient(cClientHandle * a_Client) } // for itr - m_SendChunksLowPriority[] for (sSendChunkList::iterator itr = m_SendChunksHighPriority.begin(); itr != m_SendChunksHighPriority.end();) { - if (itr->m_Client == a_Client) { + if (itr->m_Client == a_Client) + { itr = m_SendChunksHighPriority.erase(itr); continue; } -- cgit v1.2.3 From a26541a7c3ced0569098edd0aae61c097c2912f4 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Mon, 20 Oct 2014 21:55:07 +0100 Subject: En masse NULL -> nullptr replace --- src/ChunkSender.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'src/ChunkSender.cpp') diff --git a/src/ChunkSender.cpp b/src/ChunkSender.cpp index a3151eb3f..c94004780 100644 --- a/src/ChunkSender.cpp +++ b/src/ChunkSender.cpp @@ -35,9 +35,9 @@ void cNotifyChunkSender::Call(int a_ChunkX, int a_ChunkZ) cChunkSender::cChunkSender(void) : super("ChunkSender"), - m_World(NULL), + m_World(nullptr), m_RemoveCount(0), - m_Notify(NULL) + m_Notify(nullptr) { m_Notify.SetChunkSender(this); } @@ -93,7 +93,7 @@ void cChunkSender::ChunkReady(int a_ChunkX, int a_ChunkZ) void cChunkSender::QueueSendChunkTo(int a_ChunkX, int a_ChunkZ, cClientHandle * a_Client) { - ASSERT(a_Client != NULL); + ASSERT(a_Client != nullptr); { cCSLock Lock(m_CS); if (std::find(m_SendChunks.begin(), m_SendChunks.end(), sSendChunk(a_ChunkX, a_ChunkZ, a_Client)) != m_SendChunks.end()) @@ -161,7 +161,7 @@ void cChunkSender::Execute(void) m_ChunksReady.pop_front(); Lock.Unlock(); - SendChunk(Coords.m_ChunkX, Coords.m_ChunkZ, NULL); + SendChunk(Coords.m_ChunkX, Coords.m_ChunkZ, nullptr); } else { @@ -189,10 +189,10 @@ void cChunkSender::Execute(void) void cChunkSender::SendChunk(int a_ChunkX, int a_ChunkZ, cClientHandle * a_Client) { - ASSERT(m_World != NULL); + ASSERT(m_World != nullptr); // Ask the client if it still wants the chunk: - if (a_Client != NULL) + if (a_Client != nullptr) { if (!a_Client->WantsSendChunk(a_ChunkX, a_ChunkZ)) { @@ -227,7 +227,7 @@ void cChunkSender::SendChunk(int a_ChunkX, int a_ChunkZ, cClientHandle * a_Clien cChunkDataSerializer Data(m_BlockTypes, m_BlockMetas, m_BlockLight, m_BlockSkyLight, m_BiomeMap); // Send: - if (a_Client == NULL) + if (a_Client == nullptr) { m_World->BroadcastChunkData(a_ChunkX, a_ChunkZ, Data); } @@ -239,7 +239,7 @@ void cChunkSender::SendChunk(int a_ChunkX, int a_ChunkZ, cClientHandle * a_Clien // Send block-entity packets: for (sBlockCoords::iterator itr = m_BlockEntities.begin(); itr != m_BlockEntities.end(); ++itr) { - if (a_Client == NULL) + if (a_Client == nullptr) { m_World->BroadcastBlockEntity(itr->m_BlockX, itr->m_BlockY, itr->m_BlockZ); } -- cgit v1.2.3 From 9af58a81d6467829b72801169c8e80a2bab01804 Mon Sep 17 00:00:00 2001 From: Howaner Date: Thu, 23 Oct 2014 21:19:43 +0200 Subject: Use 3 priorities. --- src/ChunkSender.cpp | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) (limited to 'src/ChunkSender.cpp') diff --git a/src/ChunkSender.cpp b/src/ChunkSender.cpp index 7e2301eab..ef2be167b 100644 --- a/src/ChunkSender.cpp +++ b/src/ChunkSender.cpp @@ -100,6 +100,7 @@ void cChunkSender::QueueSendChunkTo(int a_ChunkX, int a_ChunkZ, eChunkPriority a cCSLock Lock(m_CS); if ( std::find(m_SendChunksLowPriority.begin(), m_SendChunksLowPriority.end(), Chunk) != m_SendChunksLowPriority.end() || + std::find(m_SendChunksMediumPriority.begin(), m_SendChunksMediumPriority.end(), Chunk) != m_SendChunksMediumPriority.end() || std::find(m_SendChunksHighPriority.begin(), m_SendChunksHighPriority.end(), Chunk) != m_SendChunksHighPriority.end() ) { @@ -107,10 +108,27 @@ void cChunkSender::QueueSendChunkTo(int a_ChunkX, int a_ChunkZ, eChunkPriority a return; } - if (a_Priority == E_CHUNK_PRIORITY_LOW) { - m_SendChunksLowPriority.push_back(Chunk); - } else if (a_Priority == E_CHUNK_PRIORITY_HIGH) { - m_SendChunksHighPriority.push_back(Chunk); + switch (a_Priority) + { + case E_CHUNK_PRIORITY_LOW: + { + m_SendChunksLowPriority.push_back(Chunk); + break; + } + case E_CHUNK_PRIORITY_MEDIUM: + { + m_SendChunksMediumPriority.push_back(Chunk); + break; + } + case E_CHUNK_PRIORITY_HIGH: + { + m_SendChunksHighPriority.push_back(Chunk); + break; + } + default: + { + ASSERT(!"Unknown chunk priority!"); + } } } m_evtQueue.Set(); @@ -133,6 +151,15 @@ void cChunkSender::RemoveClient(cClientHandle * a_Client) } ++itr; } // for itr - m_SendChunksLowPriority[] + for (sSendChunkList::iterator itr = m_SendChunksMediumPriority.begin(); itr != m_SendChunksMediumPriority.end();) + { + if (itr->m_Client == a_Client) + { + itr = m_SendChunksMediumPriority.erase(itr); + continue; + } + ++itr; + } // for itr - m_SendChunksMediumPriority[] for (sSendChunkList::iterator itr = m_SendChunksHighPriority.begin(); itr != m_SendChunksHighPriority.end();) { if (itr->m_Client == a_Client) @@ -191,6 +218,15 @@ void cChunkSender::Execute(void) SendChunk(Coords.m_ChunkX, Coords.m_ChunkZ, nullptr); } + else if (!m_SendChunksMediumPriority.empty()) + { + // Take one from the queue: + sSendChunk Chunk(m_SendChunksMediumPriority.front()); + m_SendChunksMediumPriority.pop_front(); + Lock.Unlock(); + + SendChunk(Chunk.m_ChunkX, Chunk.m_ChunkZ, Chunk.m_Client); + } else { // Take one from the queue: -- cgit v1.2.3 From 373b139ed63bd8ed28962d0836ee6b6ababb3045 Mon Sep 17 00:00:00 2001 From: Howaner Date: Tue, 28 Oct 2014 21:32:01 +0100 Subject: Forgotten m_SendChunksMediumPriority.empty() check. --- src/ChunkSender.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/ChunkSender.cpp') diff --git a/src/ChunkSender.cpp b/src/ChunkSender.cpp index ef2be167b..83d82884e 100644 --- a/src/ChunkSender.cpp +++ b/src/ChunkSender.cpp @@ -184,7 +184,7 @@ void cChunkSender::Execute(void) while (!m_ShouldTerminate) { cCSLock Lock(m_CS); - while (m_ChunksReady.empty() && m_SendChunksLowPriority.empty() && m_SendChunksHighPriority.empty()) + while (m_ChunksReady.empty() && m_SendChunksLowPriority.empty() && m_SendChunksMediumPriority.empty() && m_SendChunksHighPriority.empty()) { int RemoveCount = m_RemoveCount; m_RemoveCount = 0; -- cgit v1.2.3