2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// ChunkSender.cpp
|
|
|
|
|
|
|
|
// Interfaces to the cChunkSender class representing the thread that waits for chunks becoming ready (loaded / generated) and sends them to clients
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
#include "ChunkSender.h"
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "World.h"
|
2013-05-28 16:36:35 -04:00
|
|
|
#include "BlockEntities/BlockEntity.h"
|
2012-09-23 17:37:46 -04:00
|
|
|
#include "Protocol/ChunkDataSerializer.h"
|
2014-09-26 13:13:19 -04:00
|
|
|
#include "ClientHandle.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-17 16:15:34 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2012-06-14 09:06:06 -04:00
|
|
|
// cNotifyChunkSender:
|
|
|
|
|
2015-05-30 06:11:17 -04:00
|
|
|
|
|
|
|
/// Callback that can be used to notify chunk sender upon another chunkcoord notification
|
|
|
|
class cNotifyChunkSender :
|
|
|
|
public cChunkCoordCallback
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2015-05-30 06:11:17 -04:00
|
|
|
virtual void Call(int a_ChunkX, int a_ChunkZ) override
|
|
|
|
{
|
|
|
|
m_ChunkSender->ChunkReady(a_ChunkX, a_ChunkZ);
|
|
|
|
}
|
|
|
|
|
|
|
|
cChunkSender * m_ChunkSender;
|
|
|
|
public:
|
|
|
|
cNotifyChunkSender(cChunkSender * a_ChunkSender) : m_ChunkSender(a_ChunkSender) {}
|
|
|
|
};
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-17 16:15:34 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2012-06-14 09:06:06 -04:00
|
|
|
// cChunkSender:
|
|
|
|
|
|
|
|
cChunkSender::cChunkSender(void) :
|
|
|
|
super("ChunkSender"),
|
2014-10-20 16:55:07 -04:00
|
|
|
m_World(nullptr),
|
2015-05-30 06:11:17 -04:00
|
|
|
m_RemoveCount(0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cChunkSender::~cChunkSender()
|
|
|
|
{
|
|
|
|
Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cChunkSender::Start(cWorld * a_World)
|
|
|
|
{
|
2012-10-25 15:20:29 -04:00
|
|
|
m_ShouldTerminate = false;
|
2012-06-14 09:06:06 -04:00
|
|
|
m_World = a_World;
|
|
|
|
return super::Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cChunkSender::Stop(void)
|
|
|
|
{
|
|
|
|
m_ShouldTerminate = true;
|
|
|
|
m_evtQueue.Set();
|
|
|
|
Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-13 17:02:10 -04:00
|
|
|
void cChunkSender::ChunkReady(int a_ChunkX, int a_ChunkZ)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
// This is probably never gonna be called twice for the same chunk, and if it is, we don't mind, so we don't check
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CS);
|
2014-08-28 05:36:35 -04:00
|
|
|
m_ChunksReady.push_back(cChunkCoords(a_ChunkX, a_ChunkZ));
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
m_evtQueue.Set();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-10-06 15:27:53 -04:00
|
|
|
void cChunkSender::QueueSendChunkTo(int a_ChunkX, int a_ChunkZ, eChunkPriority a_Priority, cClientHandle * a_Client)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-10-20 16:55:07 -04:00
|
|
|
ASSERT(a_Client != nullptr);
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-10-21 11:35:23 -04:00
|
|
|
sSendChunk Chunk(a_ChunkX, a_ChunkZ, a_Client);
|
2014-10-06 15:27:53 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
cCSLock Lock(m_CS);
|
2014-10-21 11:35:23 -04:00
|
|
|
if (
|
|
|
|
std::find(m_SendChunksLowPriority.begin(), m_SendChunksLowPriority.end(), Chunk) != m_SendChunksLowPriority.end() ||
|
2014-10-23 15:19:43 -04:00
|
|
|
std::find(m_SendChunksMediumPriority.begin(), m_SendChunksMediumPriority.end(), Chunk) != m_SendChunksMediumPriority.end() ||
|
2014-10-21 11:35:23 -04:00
|
|
|
std::find(m_SendChunksHighPriority.begin(), m_SendChunksHighPriority.end(), Chunk) != m_SendChunksHighPriority.end()
|
|
|
|
)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
// Already queued, bail out
|
|
|
|
return;
|
|
|
|
}
|
2014-10-06 15:27:53 -04:00
|
|
|
|
2014-10-23 15:19:43 -04:00
|
|
|
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;
|
|
|
|
}
|
2014-10-21 11:35:23 -04:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
m_evtQueue.Set();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cChunkSender::RemoveClient(cClientHandle * a_Client)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CS);
|
2014-10-21 11:35:23 -04:00
|
|
|
for (sSendChunkList::iterator itr = m_SendChunksLowPriority.begin(); itr != m_SendChunksLowPriority.end();)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
if (itr->m_Client == a_Client)
|
|
|
|
{
|
2014-10-21 11:35:23 -04:00
|
|
|
itr = m_SendChunksLowPriority.erase(itr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++itr;
|
|
|
|
} // for itr - m_SendChunksLowPriority[]
|
2014-10-23 15:19:43 -04:00
|
|
|
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[]
|
2014-10-21 11:35:23 -04:00
|
|
|
for (sSendChunkList::iterator itr = m_SendChunksHighPriority.begin(); itr != m_SendChunksHighPriority.end();)
|
|
|
|
{
|
2014-10-21 12:32:02 -04:00
|
|
|
if (itr->m_Client == a_Client)
|
|
|
|
{
|
2014-10-21 11:35:23 -04:00
|
|
|
itr = m_SendChunksHighPriority.erase(itr);
|
2012-06-14 09:06:06 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++itr;
|
2014-10-21 11:35:23 -04:00
|
|
|
} // for itr - m_SendChunksHighPriority[]
|
2012-06-14 09:06:06 -04:00
|
|
|
m_RemoveCount++;
|
|
|
|
}
|
|
|
|
m_evtQueue.Set();
|
|
|
|
m_evtRemoved.Wait(); // Wait for removal confirmation
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cChunkSender::Execute(void)
|
|
|
|
{
|
|
|
|
while (!m_ShouldTerminate)
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CS);
|
2014-10-28 16:32:01 -04:00
|
|
|
while (m_ChunksReady.empty() && m_SendChunksLowPriority.empty() && m_SendChunksMediumPriority.empty() && m_SendChunksHighPriority.empty())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
int RemoveCount = m_RemoveCount;
|
|
|
|
m_RemoveCount = 0;
|
|
|
|
cCSUnlock Unlock(Lock);
|
|
|
|
for (int i = 0; i < RemoveCount; i++)
|
|
|
|
{
|
|
|
|
m_evtRemoved.Set(); // Notify that the removed clients are safe to be deleted
|
|
|
|
}
|
|
|
|
m_evtQueue.Wait();
|
|
|
|
if (m_ShouldTerminate)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} // while (empty)
|
2014-10-21 11:35:23 -04:00
|
|
|
|
|
|
|
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())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
// Take one from the queue:
|
|
|
|
cChunkCoords Coords(m_ChunksReady.front());
|
|
|
|
m_ChunksReady.pop_front();
|
|
|
|
Lock.Unlock();
|
|
|
|
|
2014-10-20 16:55:07 -04:00
|
|
|
SendChunk(Coords.m_ChunkX, Coords.m_ChunkZ, nullptr);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2014-10-23 15:19:43 -04:00
|
|
|
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);
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Take one from the queue:
|
2014-10-21 11:35:23 -04:00
|
|
|
sSendChunk Chunk(m_SendChunksLowPriority.front());
|
|
|
|
m_SendChunksLowPriority.pop_front();
|
2012-06-14 09:06:06 -04:00
|
|
|
Lock.Unlock();
|
2014-10-06 15:27:53 -04:00
|
|
|
|
2014-08-28 05:36:35 -04:00
|
|
|
SendChunk(Chunk.m_ChunkX, Chunk.m_ChunkZ, Chunk.m_Client);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
Lock.Lock();
|
|
|
|
int RemoveCount = m_RemoveCount;
|
|
|
|
m_RemoveCount = 0;
|
|
|
|
Lock.Unlock();
|
|
|
|
for (int i = 0; i < RemoveCount; i++)
|
|
|
|
{
|
|
|
|
m_evtRemoved.Set(); // Notify that the removed clients are safe to be deleted
|
|
|
|
}
|
|
|
|
} // while (!mShouldTerminate)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-28 05:36:35 -04:00
|
|
|
void cChunkSender::SendChunk(int a_ChunkX, int a_ChunkZ, cClientHandle * a_Client)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-10-20 16:55:07 -04:00
|
|
|
ASSERT(m_World != nullptr);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// Ask the client if it still wants the chunk:
|
2014-10-23 08:59:42 -04:00
|
|
|
if ((a_Client != nullptr) && !a_Client->WantsSendChunk(a_ChunkX, a_ChunkZ))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-10-02 17:50:41 -04:00
|
|
|
return;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2014-10-02 17:50:41 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// If the chunk has no clients, no need to packetize it:
|
2013-04-13 17:02:10 -04:00
|
|
|
if (!m_World->HasChunkAnyClients(a_ChunkX, a_ChunkZ))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-10-02 17:50:41 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// If the chunk is not valid, do nothing - whoever needs it has queued it for loading / generating
|
2013-04-13 17:02:10 -04:00
|
|
|
if (!m_World->IsChunkValid(a_ChunkX, a_ChunkZ))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-10-02 17:50:41 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// 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))
|
|
|
|
{
|
2015-05-30 06:11:17 -04:00
|
|
|
m_World->QueueLightChunk(a_ChunkX, a_ChunkZ, cpp14::make_unique<cNotifyChunkSender>(this));
|
2012-06-14 09:06:06 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-10-02 17:50:41 -04:00
|
|
|
|
2012-08-26 17:01:07 -04:00
|
|
|
// Query and prepare chunk data:
|
2013-04-13 17:02:10 -04:00
|
|
|
if (!m_World->GetChunkData(a_ChunkX, a_ChunkZ, *this))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2012-08-26 17:01:07 -04:00
|
|
|
cChunkDataSerializer Data(m_BlockTypes, m_BlockMetas, m_BlockLight, m_BlockSkyLight, m_BiomeMap);
|
2014-10-02 17:50:41 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// Send:
|
2014-10-20 16:55:07 -04:00
|
|
|
if (a_Client == nullptr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-26 17:01:07 -04:00
|
|
|
m_World->BroadcastChunkData(a_ChunkX, a_ChunkZ, Data);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-26 17:01:07 -04:00
|
|
|
a_Client->SendChunkData(a_ChunkX, a_ChunkZ, Data);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2014-10-02 17:50:41 -04:00
|
|
|
|
2012-08-24 03:58:26 -04:00
|
|
|
// Send block-entity packets:
|
|
|
|
for (sBlockCoords::iterator itr = m_BlockEntities.begin(); itr != m_BlockEntities.end(); ++itr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-10-20 16:55:07 -04:00
|
|
|
if (a_Client == nullptr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-24 03:58:26 -04:00
|
|
|
m_World->BroadcastBlockEntity(itr->m_BlockX, itr->m_BlockY, itr->m_BlockZ);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-24 03:58:26 -04:00
|
|
|
m_World->SendBlockEntity(itr->m_BlockX, itr->m_BlockY, itr->m_BlockZ, *a_Client);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
} // for itr - m_Packets[]
|
2012-08-24 03:58:26 -04:00
|
|
|
m_BlockEntities.clear();
|
2014-10-02 17:50:41 -04:00
|
|
|
|
2012-08-24 03:58:26 -04:00
|
|
|
// TODO: Send entity spawn packets
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cChunkSender::BlockEntity(cBlockEntity * a_Entity)
|
|
|
|
{
|
2012-08-24 03:58:26 -04:00
|
|
|
m_BlockEntities.push_back(sBlockCoord(a_Entity->GetPosX(), a_Entity->GetPosY(), a_Entity->GetPosZ()));
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-12-21 10:00:31 -05:00
|
|
|
void cChunkSender::Entity(cEntity *)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
// Nothing needed yet, perhaps in the future when we save entities into chunks we'd like to send them upon load, too ;)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cChunkSender::BiomeData(const cChunkDef::BiomeMap * a_BiomeMap)
|
|
|
|
{
|
2013-12-20 10:01:34 -05:00
|
|
|
for (size_t i = 0; i < ARRAYCOUNT(m_BiomeMap); i++)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
if ((*a_BiomeMap)[i] < 255)
|
|
|
|
{
|
|
|
|
// Normal MC biome, copy as-is:
|
2015-05-24 07:56:56 -04:00
|
|
|
m_BiomeMap[i] = static_cast<unsigned char>((*a_BiomeMap)[i]);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// TODO: MCS-specific biome, need to map to some basic MC biome:
|
|
|
|
ASSERT(!"Unimplemented MCS-specific biome");
|
|
|
|
}
|
|
|
|
} // for i - m_BiomeMap[]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|