Skip to content

Commit

Permalink
Change NetworkPacket to reserve instead of resize
Browse files Browse the repository at this point in the history
also make the bool serialization clearer and move the constructor
to the header file
  • Loading branch information
sfan5 committed Jan 27, 2024
1 parent 397682a commit c0f852e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 36 deletions.
35 changes: 8 additions & 27 deletions src/network/networkpacket.cpp
Expand Up @@ -23,27 +23,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/serialize.h"
#include "networkprotocol.h"

NetworkPacket::NetworkPacket(u16 command, u32 datasize, session_t peer_id):
m_datasize(datasize), m_command(command), m_peer_id(peer_id)
{
m_data.resize(m_datasize);
}

NetworkPacket::NetworkPacket(u16 command, u32 datasize):
m_datasize(datasize), m_command(command)
{
m_data.resize(m_datasize);
}

NetworkPacket::~NetworkPacket()
{
m_data.clear();
}

void NetworkPacket::checkReadOffset(u32 from_offset, u32 field_size)
void NetworkPacket::checkReadOffset(u32 from_offset, u32 field_size) const
{
if (from_offset + field_size > m_datasize) {
std::stringstream ss;
std::ostringstream ss;
ss << "Reading outside packet (offset: " <<
from_offset << ", packet size: " << getSize() << ")";
throw PacketError(ss.str());
Expand All @@ -56,6 +39,7 @@ void NetworkPacket::putRawPacket(const u8 *data, u32 datasize, session_t peer_id
// This is not permitted
assert(m_command == 0);

assert(datasize >= 2);
m_datasize = datasize - 2;
m_peer_id = peer_id;

Expand All @@ -76,7 +60,7 @@ void NetworkPacket::clear()
m_peer_id = 0;
}

const char* NetworkPacket::getString(u32 from_offset)
const char* NetworkPacket::getString(u32 from_offset) const
{
checkReadOffset(from_offset, 0);

Expand All @@ -85,10 +69,7 @@ const char* NetworkPacket::getString(u32 from_offset)

void NetworkPacket::putRawString(const char* src, u32 len)
{
if (m_read_offset + len > m_datasize) {
m_datasize = m_read_offset + len;
m_data.resize(m_datasize);
}
checkDataSize(len);

if (len == 0)
return;
Expand Down Expand Up @@ -279,7 +260,7 @@ NetworkPacket& NetworkPacket::operator<<(bool src)
{
checkDataSize(1);

writeU8(&m_data[m_read_offset], src);
writeU8(&m_data[m_read_offset], src ? 1 : 0);

m_read_offset += 1;
return *this;
Expand Down Expand Up @@ -329,7 +310,7 @@ NetworkPacket& NetworkPacket::operator>>(bool& dst)
{
checkReadOffset(m_read_offset, 1);

dst = readU8(&m_data[m_read_offset]);
dst = readU8(&m_data[m_read_offset]) != 0;

m_read_offset += 1;
return *this;
Expand Down Expand Up @@ -360,7 +341,7 @@ u8* NetworkPacket::getU8Ptr(u32 from_offset)

checkReadOffset(from_offset, 1);

return (u8*)&m_data[from_offset];
return &m_data[from_offset];
}

NetworkPacket& NetworkPacket::operator>>(u16& dst)
Expand Down
24 changes: 15 additions & 9 deletions src/network/networkpacket.h
Expand Up @@ -20,33 +20,39 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once

#include "util/pointer.h"
#include "util/numeric.h"
#include "networkprotocol.h"
#include <SColor.h>

class NetworkPacket
{

public:
NetworkPacket(u16 command, u32 datasize, session_t peer_id);
NetworkPacket(u16 command, u32 datasize);
NetworkPacket(u16 command, u32 preallocate, session_t peer_id) :
m_command(command), m_peer_id(peer_id)
{
m_data.reserve(preallocate);
}
NetworkPacket(u16 command, u32 preallocate) :
m_command(command)
{
m_data.reserve(preallocate);
}
NetworkPacket() = default;

~NetworkPacket();
~NetworkPacket() = default;

void putRawPacket(const u8 *data, u32 datasize, session_t peer_id);
void clear();

// Getters
u32 getSize() const { return m_datasize; }
session_t getPeerId() const { return m_peer_id; }
u16 getCommand() { return m_command; }
u16 getCommand() const { return m_command; }
u32 getRemainingBytes() const { return m_datasize - m_read_offset; }
const char *getRemainingString() { return getString(m_read_offset); }

// Returns a c-string without copying.
// A better name for this would be getRawString()
const char *getString(u32 from_offset);
const char *getString(u32 from_offset) const;
// major difference to putCString(): doesn't write len into the buffer
void putRawString(const char *src, u32 len);
void putRawString(const std::string &src)
Expand Down Expand Up @@ -115,11 +121,11 @@ class NetworkPacket
NetworkPacket &operator<<(video::SColor src);

// Temp, we remove SharedBuffer when migration finished
// ^ this comment has been here for 4 years
// ^ this comment has been here for 7 years
Buffer<u8> oldForgePacket();

private:
void checkReadOffset(u32 from_offset, u32 field_size);
void checkReadOffset(u32 from_offset, u32 field_size) const;

inline void checkDataSize(u32 field_size)
{
Expand Down

0 comments on commit c0f852e

Please sign in to comment.