mirror of
https://github.com/minetest/minetest.git
synced 2024-08-19 04:54:18 +02:00
Change NetworkPacket to reserve instead of resize
also make the bool serialization clearer and move the constructor to the header file
This commit is contained in:
parent
397682a5b0
commit
c0f852e016
@ -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());
|
||||
@ -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;
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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;
|
||||
@ -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;
|
||||
@ -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;
|
||||
@ -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)
|
||||
|
@ -20,19 +20,25 @@ 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();
|
||||
@ -40,13 +46,13 @@ public:
|
||||
// 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)
|
||||
@ -115,11 +121,11 @@ public:
|
||||
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)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user