Skip to content

Commit

Permalink
[CompressedPacket] Fixed #97. [gamekit] Updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed May 13, 2020
1 parent ca8b52b commit 58944ea
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
2 changes: 1 addition & 1 deletion external/gamekit
Submodule gamekit updated 1 files
+4 −1 CMakeLists.txt
66 changes: 63 additions & 3 deletions source/common/network/CompressedPacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,63 @@
*/
#include <cassert>

#include <gk/core/Debug.hpp>

#include "CompressedPacket.hpp"

// Note: This class was implemented thanks to this SFML forum topic:
// https://en.sfml-dev.org/forums/index.php?topic=14344.0

#include <iostream>
#include <vector>
#include <iomanip>
#include <numeric>

template<typename byte_type = std::uint8_t, typename container_type = std::vector<std::vector<byte_type>>>
container_type arrange_bytes(const byte_type* buffer, const std::size_t size, const std::size_t w = 16) {
return std::accumulate(buffer, buffer + size, container_type{{}}, [w](auto& acc, const byte_type byte) {
if(acc.back().size() >= w) {
acc.push_back({});
}
acc.back().push_back(byte);
return acc;
});
}

std::string init_text_row(const int offset) {
std::ostringstream ost{};
ost << std::hex << std::setfill('0') << std::setw(8) << offset;
return ost.str();
}

template<typename byte_type = std::uint8_t>
std::string format_row(const std::vector<byte_type>& bytes, const int offset) {
auto init = init_text_row(offset);
return std::accumulate(bytes.begin(), bytes.end(), init, [](auto& acc, const auto& byte) {
std::ostringstream ost{};
ost << ' ' << std::hex << std::setfill('0') << static_cast<unsigned>(byte);
return acc + ost.str();
});
}

template<typename byte_type = std::uint8_t, typename container_type = std::vector<std::vector<byte_type>>>
std::string format_bytes(const container_type& bytes) {
struct memory {
std::string data = {};
int offset = 0;
};
return std::accumulate(bytes.begin(), bytes.end(), memory{}, [](auto& acc, const auto& row) {
acc.data += format_row(row, acc.offset) + '\n';
acc.offset += row.size();
return acc;
}).data;
}

template<typename byte_type = std::uint8_t>
std::string hexdump(const byte_type* buffer, std::size_t size) {
return format_bytes(arrange_bytes(buffer, size));
}

const void* CompressedPacket::onSend(std::size_t& size) {
// We only support data with a maximum size of
// an unsigned short (so the size can be sent
Expand All @@ -57,7 +109,11 @@ const void* CompressedPacket::onSend(std::size_t& size) {
m_compressionBuffer[1] = (srcSize >> 8) & 0xFF;

// Compress the data into the rest of the buffer
compress(m_compressionBuffer.data() + 2, &dstSize, srcData, srcSize);
int result = compress(m_compressionBuffer.data() + 2, &dstSize, srcData, srcSize);
if (result != Z_OK)
gkError() << "Failed to compress packet";

// hexdump(srcData, srcSize);

// Set the size to the compressed size plus
// two bytes for the size marker
Expand All @@ -79,10 +135,14 @@ void CompressedPacket::onReceive(const void* data, std::size_t size) {
m_compressionBuffer.resize(uncompressedSize);

// Declare a variable for the destination size
uLong dstSize;
uLong dstSize = uncompressedSize;

// Uncompress the data (remove the first two bytes)
uncompress(m_compressionBuffer.data(), &dstSize, (srcData + 2), size - 2);
int result = uncompress(m_compressionBuffer.data(), &dstSize, (srcData + 2), size - 2);
if (result != Z_OK)
gkError() << "Failed to uncompress packet";

// hexdump(m_compressionBuffer.data(), dstSize);

// Assert that the uncompressed size is the same as the
// size we were sent for the buffer
Expand Down

0 comments on commit 58944ea

Please sign in to comment.