Skip to content

Commit

Permalink
fix: some things
Browse files Browse the repository at this point in the history
  • Loading branch information
dudantas committed Sep 30, 2024
1 parent fdb5623 commit b77cbb2
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/server/network/message/networkmessage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ class NetworkMessage {
std::array<unsigned char, sizeof(T)> tempBuffer;
// Copy data from the buffer to the temporary array
std::span<const unsigned char> sourceSpan(buffer.data() + info.position, sizeof(T));
auto it = std::ranges::copy(sourceSpan, tempBuffer.begin());
g_logger().trace("First value copied from sourceSpan: {}, second value copied from sourceSpan: {}", *it.in, *it.out);
std::ranges::copy(sourceSpan, tempBuffer.begin());
// Update the read position in the buffer
info.position += sizeof(T);
// Convert the byte array to type T using std::bit_cast and return the result
Expand All @@ -69,21 +68,19 @@ class NetworkMessage {

template <typename T>
void add(T value, std::source_location location = std::source_location::current()) {
// Check if there is enough space to add the value to the buffer
static_assert(!std::is_same_v<T, double>, "Error: get<double>() is not allowed. Use addDouble() instead.");
static_assert(std::is_trivially_copyable_v<T>, "Type T must be trivially copyable");

if (!canAdd(sizeof(T))) {
g_logger().error("Cannot add value of size {}, buffer overflow. Called at line '{}:{}' in '{}'", sizeof(T), buffer.size(), location.line(), location.column(), location.function_name());
g_logger().error("Cannot add value of size '{}', buffer size: '{}' overflow. Called at line '{}:{}' in '{}'", sizeof(T), buffer.size(), location.line(), location.column(), location.function_name());
return;
}

// Verify if the buffer has enough space to accommodate the value
if (info.position + sizeof(T) > buffer.size()) {
g_logger().error("Buffer overflow detected, current position: {}, value size: {}, buffer size: {}. Called at line '{}:{}' in '{}'", info.position, sizeof(T), buffer.size(), location.line(), location.column(), location.function_name());
g_logger().error("Buffer overflow detected, current position: '{}', value size: '{}', buffer size: '{}'. Called at line '{}:{}' in '{}'", info.position, sizeof(T), buffer.size(), location.line(), location.column(), location.function_name());
return;
}

// Ensure that T is trivially copyable
static_assert(std::is_trivially_copyable_v<T>, "Type T must be trivially copyable");

// Convert the value to an array of unsigned char using std::bit_cast
auto byteArray = std::bit_cast<std::array<unsigned char, sizeof(T)>>(value);

Expand All @@ -98,7 +95,6 @@ class NetworkMessage {

g_logger().trace("[{}] called at line '{}:{}' in '{}'", __FUNCTION__, location.line(), location.column(), location.function_name());

// Copy the bytes into the buffer at the correct position
std::ranges::copy(byteSpan, buffer.begin() + info.position);

info.position += sizeof(T);
Expand Down

0 comments on commit b77cbb2

Please sign in to comment.