From 00f8c3a2251833f2d16ec89861a4377029cb2401 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sun, 19 Jul 2020 16:29:49 +0100 Subject: Implement "caching" in ChunkDataSerializer + When sending a chunk to multiple clients, group them by protocol version and send the same data --- src/Protocol/ChunkDataSerializer.h | 47 ++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 22 deletions(-) (limited to 'src/Protocol/ChunkDataSerializer.h') diff --git a/src/Protocol/ChunkDataSerializer.h b/src/Protocol/ChunkDataSerializer.h index 2670a0705..69e0a7689 100644 --- a/src/Protocol/ChunkDataSerializer.h +++ b/src/Protocol/ChunkDataSerializer.h @@ -7,6 +7,12 @@ +class cByteBuffer; + + + + + /** Serializes one chunk's data to (possibly multiple) protocol versions. Caches the serialized data for as long as this object lives, so that the same data can be sent to other clients using the same protocol. */ @@ -14,26 +20,31 @@ class cChunkDataSerializer { public: - enum - { - RELEASE_1_8_0 = 47, - RELEASE_1_9_0 = 107, - RELEASE_1_9_4 = 110, - RELEASE_1_13 = 393, - } ; - cChunkDataSerializer( - const cChunkData & a_Data, - const unsigned char * a_BiomeData, - const eDimension a_Dimension + int a_ChunkX, + int a_ChunkZ, + const cChunkData & a_Data, + const unsigned char * a_BiomeData, + const eDimension a_Dimension ); - /** Serializes the contained chunk data into the specified protocol version. */ - const AString & Serialize(int a_Version, int a_ChunkX, int a_ChunkZ); + /** For each client, serializes the chunk into their protocol version and sends it. */ + void SendToClients(const std::unordered_set & a_SendTo); protected: - using Serializations = std::map; + void Serialize47 (const std::vector & a_SendTo); // Release 1.8 + void Serialize107(const std::vector & a_SendTo); // Release 1.9 + void Serialize110(const std::vector & a_SendTo); // Release 1.9.4 + + template + void Serialize393And401(const std::vector & a_SendTo); // Release 1.13 - 1.13.1 + + /** Finalises the data, compresses it if required, and delivers it to all clients. */ + void CompressAndSend(cByteBuffer & a_Packet, const std::vector & a_SendTo); + + /** The coordinates of the chunk to serialise. */ + int m_ChunkX, m_ChunkZ; /** The data read from the chunk, to be serialized. */ const cChunkData & m_Data; @@ -43,14 +54,6 @@ protected: /** The dimension where the chunk resides. */ const eDimension m_Dimension; - - /** The per-protocol serialized data, cached for reuse for other clients. */ - Serializations m_Serializations; - - void Serialize47 (AString & a_Data, int a_ChunkX, int a_ChunkZ); // Release 1.8 - void Serialize107(AString & a_Data, int a_ChunkX, int a_ChunkZ); // Release 1.9 - void Serialize110(AString & a_Data, int a_ChunkX, int a_ChunkZ); // Release 1.9.4 - void Serialize393(AString & a_Data, int a_ChunkX, int a_ChunkZ); // Release 1.13 } ; -- cgit v1.2.3