Cuberite
A lightweight, fast and extensible game server for Minecraft
ChunkSender.h
Go to the documentation of this file.
1 
2 // ChunkSender.h
3 
4 // Interfaces to the cChunkSender class representing the thread that waits for chunks becoming ready (loaded / generated) and sends them to clients
5 
6 /*
7 The whole thing is a thread that runs in a loop, waiting for either:
8  "finished chunks" (ChunkReady()), or
9  "chunks to send" (QueueSendChunkTo())
10 to come to a queue.
11 And once they do, it requests the chunk data and sends it all away, either
12  broadcasting (ChunkReady), or
13  sends to a specific client (QueueSendChunkTo)
14 Chunk data is queried using the cChunkDataCallback interface.
15 It is cached inside the ChunkSender object during the query and then processed after the query ends.
16 Note that the data needs to be compressed only after the query finishes,
17 because the query callbacks run with ChunkMap's CS locked.
18 
19 A client may remove itself from all direct requests(QueueSendChunkTo()) by calling RemoveClient();
20 this ensures that the client's Send() won't be called anymore by ChunkSender.
21 Note that it may be called by world's BroadcastToChunk() if the client is still in the chunk.
22 */
23 
24 
25 
26 #pragma once
27 
28 #include "OSSupport/IsThread.h"
29 #include "ChunkDataCallback.h"
30 
31 #include <unordered_set>
32 #include <unordered_map>
33 
34 
35 
36 
37 
38 class cWorld;
39 class cClientHandle;
40 
41 
42 
43 
44 
45 // fwd:
46 class cChunkSender;
47 
48 
49 
50 
51 
53  public cIsThread,
55 {
56  typedef cIsThread super;
57 public:
58  cChunkSender(cWorld & a_World);
59  virtual ~cChunkSender() override;
60 
62  {
67 
68  };
69 
70  void Stop(void);
71 
73  void QueueSendChunkTo(int a_ChunkX, int a_ChunkZ, eChunkPriority a_Priority, cClientHandle * a_Client);
74  void QueueSendChunkTo(int a_ChunkX, int a_ChunkZ, eChunkPriority a_Priority, cChunkClientHandles a_Client);
75 
77  void RemoveClient(cClientHandle * a_Client);
78 
79 protected:
80 
81  struct sChunkQueue
82  {
85 
86  bool operator <(const sChunkQueue & a_Other) const
87  {
88  /* The Standard Priority Queue sorts from biggest to smallest
89  return true here means you are smaller than the other object, and you get pushed down.
90 
91  The priorities go from HIGH (0) to LOW (2), so a smaller priority should mean further up the list
92  therefore, return true (affirm we're "smaller", and get pushed down) only if our priority is bigger than theirs (they're more urgent)
93  */
94  return this->m_Priority > a_Other.m_Priority;
95  }
96  };
97 
99  struct sSendChunk
100  {
102  std::unordered_set<cClientHandle *> m_Clients;
104  sSendChunk(cChunkCoords a_Chunk, eChunkPriority a_Priority) :
105  m_Chunk(a_Chunk),
106  m_Priority(a_Priority)
107  {
108  }
109  };
110 
112 
114  std::priority_queue<sChunkQueue> m_SendChunks;
115  std::unordered_map<cChunkCoords, sSendChunk, cChunkCoordsHash> m_ChunkInfo;
116  cEvent m_evtQueue; // Set when anything is added to m_ChunksReady
117  cEvent m_evtRemoved; // Set when removed clients are safe to be deleted
118 
119  // Data about the chunk that is being sent:
120  // NOTE that m_BlockData[] is inherited from the cChunkDataCollector
122  std::vector<Vector3i> m_BlockEntities; // Coords of the block entities to send
123  // TODO: sEntityIDs m_Entities; // Entity-IDs of the entities to send
124 
125  // cIsThread override:
126  virtual void Execute(void) override;
127 
128  // cChunkDataCollector overrides:
129  // (Note that they are called while the ChunkMap's CS is locked - don't do heavy calculations here!)
130  virtual void BiomeData (const cChunkDef::BiomeMap * a_BiomeMap) override;
131  virtual void Entity (cEntity * a_Entity) override;
132  virtual void BlockEntity (cBlockEntity * a_Entity) override;
133 
135  void SendChunk(int a_ChunkX, int a_ChunkZ, std::unordered_set<cClientHandle *> a_Clients);
136 } ;
137 
138 
139 
std::unordered_set< cClientHandle * > m_Clients
Definition: ChunkSender.h:102
cCriticalSection m_CS
Definition: ChunkSender.h:113
cChunkSender(cWorld &a_World)
Definition: ChunkSender.cpp:62
Definition: Event.h:17
static const int Width
Definition: ChunkDef.h:134
void SendChunk(int a_ChunkX, int a_ChunkZ, std::unordered_set< cClientHandle * > a_Clients)
Sends the specified chunk to all the specified clients.
eChunkPriority m_Priority
Definition: ChunkSender.h:103
sSendChunk(cChunkCoords a_Chunk, eChunkPriority a_Priority)
Definition: ChunkSender.h:104
A simple implementation of the cChunkDataCallback interface that just copies the cChunkData.
unsigned char m_BiomeMap[cChunkDef::Width *cChunkDef::Width]
Definition: ChunkSender.h:121
cEvent m_evtRemoved
Definition: ChunkSender.h:117
eChunkPriority m_Priority
Definition: ChunkSender.h:83
bool operator<(const sChunkQueue &a_Other) const
Definition: ChunkSender.h:86
Used for sending chunks to specific clients.
Definition: ChunkSender.h:99
cWorld & m_World
Definition: ChunkSender.h:111
void Stop(void)
Definition: ChunkSender.cpp:81
std::vector< Vector3i > m_BlockEntities
Definition: ChunkSender.h:122
Definition: World.h:65
virtual void BiomeData(const cChunkDef::BiomeMap *a_BiomeMap) override
std::priority_queue< sChunkQueue > m_SendChunks
Definition: ChunkSender.h:114
std::unordered_map< cChunkCoords, sSendChunk, cChunkCoordsHash > m_ChunkInfo
Definition: ChunkSender.h:115
cIsThread super
Definition: ChunkSender.h:56
cEvent m_evtQueue
Definition: ChunkSender.h:116
void RemoveClient(cClientHandle *a_Client)
Removes the a_Client from all waiting chunk send operations.
virtual ~cChunkSender() override
Definition: ChunkSender.cpp:72
virtual void Entity(cEntity *a_Entity) override
Definition: Entity.h:73
virtual void BlockEntity(cBlockEntity *a_Entity) override
virtual void Execute(void) override
This is the main thread entrypoint.
EMCSBiome BiomeMap[Width *Width]
The type used for any biomemap operations and storage inside Cuberite, using Cuberite biomes (need no...
Definition: ChunkDef.h:147
void QueueSendChunkTo(int a_ChunkX, int a_ChunkZ, eChunkPriority a_Priority, cClientHandle *a_Client)
Queues a chunk to be sent to a specific client.
Definition: ChunkSender.cpp:92
Non-owning view of a chunk&#39;s client handles.
Definition: ChunkDef.h:103