use of VarLong for Packet size

This commit is contained in:
2023-11-02 23:43:50 +01:00
parent 3a1b2d277c
commit 9634f2737b

View File

@@ -1,5 +1,6 @@
#include "blitz/misc/Compression.h"
#include "blitz/VarInt.h"
#include <cassert>
#include <zlib.h>
@@ -34,8 +35,8 @@ DataBuffer Compress(const DataBuffer& buffer) {
if (buffer.GetSize() < COMPRESSION_THRESHOLD) {
// Don't compress since it's a small packet
std::uint64_t compressedDataLength = 0;
std::uint64_t packetLength = buffer.GetSize() + sizeof(compressedDataLength);
VarLong compressedDataLength = 0;
std::uint64_t packetLength = compressedDataLength.GetSerializedLength() + buffer.GetSize();
packet << packetLength;
packet << compressedDataLength;
@@ -45,22 +46,22 @@ DataBuffer Compress(const DataBuffer& buffer) {
DataBuffer compressedData = Deflate(buffer.data(), buffer.GetSize());
std::uint64_t compressedDataLength = buffer.GetSize();
std::uint64_t packetLength = compressedData.GetSize() + sizeof(compressedDataLength);
VarLong uncompressedDataLength = buffer.GetSize();
std::uint64_t packetLength = uncompressedDataLength.GetSerializedLength() + compressedData.GetSize();
packet << packetLength;
packet << compressedDataLength;
packet << uncompressedDataLength;
packet.WriteSome(compressedData.data(), compressedData.GetSize());
return packet;
}
DataBuffer Decompress(DataBuffer& buffer, std::uint64_t packetLength) {
std::uint64_t uncompressedLength;
VarLong uncompressedLength;
buffer >> uncompressedLength;
std::uint64_t compressedLength = packetLength - sizeof(uncompressedLength);
std::uint64_t compressedLength = packetLength - uncompressedLength.GetSerializedLength();
if (uncompressedLength == 0) {
if (uncompressedLength.GetValue() == 0) {
// Data already uncompressed. Nothing to do
DataBuffer ret;
buffer.ReadSome(ret, compressedLength);
@@ -69,7 +70,7 @@ DataBuffer Decompress(DataBuffer& buffer, std::uint64_t packetLength) {
assert(buffer.GetReadOffset() + compressedLength <= buffer.GetSize());
return Inflate(buffer.data() + buffer.GetReadOffset(), compressedLength, uncompressedLength);
return Inflate(buffer.data() + buffer.GetReadOffset(), compressedLength, uncompressedLength.GetValue());
}
DataBuffer Decompress(DataBuffer& buffer) {