Factor out packet serialisation functions into new common file

This commit is contained in:
Jonathan G Rennison
2022-12-07 18:10:55 +00:00
parent 24b3e70494
commit c29b395357
5 changed files with 222 additions and 124 deletions

View File

@@ -85,121 +85,6 @@ bool Packet::CanWriteToPacket(size_t bytes_to_write)
return this->Size() + bytes_to_write <= this->limit;
}
/*
* The next couple of functions make sure we can send
* uint8, uint16, uint32 and uint64 endian-safe
* over the network. The least significant bytes are
* sent first.
*
* So 0x01234567 would be sent as 67 45 23 01.
*
* A bool is sent as a uint8 where zero means false
* and non-zero means true.
*/
/**
* Package a boolean in the packet.
* @param data The data to send.
*/
void Packet::Send_bool(bool data)
{
this->Send_uint8(data ? 1 : 0);
}
/**
* Package a 8 bits integer in the packet.
* @param data The data to send.
*/
void Packet::Send_uint8(uint8 data)
{
assert(this->CanWriteToPacket(sizeof(data)));
this->buffer.emplace_back(data);
}
/**
* Package a 16 bits integer in the packet.
* @param data The data to send.
*/
void Packet::Send_uint16(uint16 data)
{
assert(this->CanWriteToPacket(sizeof(data)));
this->buffer.insert(this->buffer.end(), {
(uint8)GB(data, 0, 8),
(uint8)GB(data, 8, 8),
});
}
/**
* Package a 32 bits integer in the packet.
* @param data The data to send.
*/
void Packet::Send_uint32(uint32 data)
{
assert(this->CanWriteToPacket(sizeof(data)));
this->buffer.insert(this->buffer.end(), {
(uint8)GB(data, 0, 8),
(uint8)GB(data, 8, 8),
(uint8)GB(data, 16, 8),
(uint8)GB(data, 24, 8),
});
}
/**
* Package a 64 bits integer in the packet.
* @param data The data to send.
*/
void Packet::Send_uint64(uint64 data)
{
assert(this->CanWriteToPacket(sizeof(data)));
this->buffer.insert(this->buffer.end(), {
(uint8)GB(data, 0, 8),
(uint8)GB(data, 8, 8),
(uint8)GB(data, 16, 8),
(uint8)GB(data, 24, 8),
(uint8)GB(data, 32, 8),
(uint8)GB(data, 40, 8),
(uint8)GB(data, 48, 8),
(uint8)GB(data, 56, 8),
});
}
/**
* Sends a string over the network. It sends out
* the string + '\0'. No size-byte or something.
* @param data The string to send
*/
void Packet::Send_string(const std::string_view data)
{
assert(this->CanWriteToPacket(data.size() + 1));
this->buffer.insert(this->buffer.end(), data.begin(), data.end());
this->buffer.emplace_back('\0');
}
/**
* Send as many of the bytes as possible in the packet. This can mean
* that it is possible that not all bytes are sent. To cope with this
* the function returns the amount of bytes that were actually sent.
* @param begin The begin of the buffer to send.
* @param end The end of the buffer to send.
* @return The number of bytes that were added to this packet.
*/
size_t Packet::Send_bytes(const byte *begin, const byte *end)
{
size_t amount = std::min<size_t>(end - begin, this->limit - this->Size());
this->buffer.insert(this->buffer.end(), begin, begin + amount);
return amount;
}
/**
* Sends a binary data over the network.
* @param data The data to send
*/
void Packet::Send_binary(const char *data, const size_t size)
{
assert(data != nullptr);
assert(this->CanWriteToPacket(size));
this->buffer.insert(this->buffer.end(), data, data + size);
}
/*

View File

@@ -16,6 +16,7 @@
#include "config.h"
#include "core.h"
#include "../../string_type.h"
#include "../../core/serialisation.hpp"
#include <string>
#include <functional>
#include <limits>
@@ -42,7 +43,7 @@ typedef uint8 PacketType; ///< Identifier for the packet
* - years that are leap years in the 'days since X' to 'date' calculations:
* (year % 4 == 0) and ((year % 100 != 0) or (year % 400 == 0))
*/
struct Packet {
struct Packet : public BufferSerialisationHelper<Packet> {
private:
/** The current read/write position in the packet */
PacketSize pos;
@@ -63,15 +64,10 @@ public:
/* Sending/writing of packets */
void PrepareToSend();
std::vector<byte> &GetSerialisationBuffer() { return this->buffer; }
size_t GetSerialisationLimit() const { return this->limit; }
bool CanWriteToPacket(size_t bytes_to_write);
void Send_bool (bool data);
void Send_uint8 (uint8 data);
void Send_uint16(uint16 data);
void Send_uint32(uint32 data);
void Send_uint64(uint64 data);
void Send_string(const std::string_view data);
size_t Send_bytes (const byte *begin, const byte *end);
void Send_binary(const char *data, const size_t size);
/* Reading/receiving of packets */
size_t ReadRawPacketSize() const;