Skip to content

Commit

Permalink
fix: content length calculation in Packet class
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterLaplace committed Nov 11, 2024
1 parent d13481e commit 0964b4b
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions Flakkari/Protocol/Packet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "Components.hpp"
#include "Events.hpp"
#include "Header.hpp"
#include "Logger/Logger.hpp"

#include <cstring>

Expand Down Expand Up @@ -87,7 +88,7 @@ template <typename Id> struct Packet {

const byte *dataBytes = reinterpret_cast<const byte *>(&data);
packet.payload.insert(packet.payload.end(), dataBytes, dataBytes + sizeof(data));
packet.header._contentLength += (ushort) packet.payload.size() + (ushort) sizeof(data);
packet.header._contentLength += (ushort) sizeof(data);
return packet;
}

Expand Down Expand Up @@ -118,25 +119,18 @@ template <typename Id> struct Packet {
return packet;
}

/**
* @brief Inject a string into the packet.
*
* @param str The string to inject.
*/
void injectString(std::string str)
{
int intValue = (int) str.size();
const byte *dataBytes = reinterpret_cast<const byte *>(&intValue);
payload.insert(payload.end(), dataBytes, dataBytes + sizeof(intValue));
payload += str;
header._contentLength += (ushort) payload.size() + (ushort) sizeof(intValue);
}

std::string extractString()
{
std::string str;
int intValue;
std::memcpy(&intValue, payload.data(), sizeof(intValue));
payload.erase(payload.begin(), payload.begin() + sizeof(intValue));
str = std::string((const char *) payload.data(), intValue);
payload.erase(payload.begin(), payload.begin() + intValue);
header._contentLength -= (ushort) sizeof(intValue) + (ushort) intValue;
return str;
header._contentLength += (ushort) str.size() + (ushort) sizeof(intValue);
}

/**
Expand Down

0 comments on commit 0964b4b

Please sign in to comment.