Serialisation: Adjust method names/types

This commit is contained in:
Jonathan G Rennison
2023-06-14 18:16:23 +01:00
parent 4b807e091a
commit 11a3dc287b
8 changed files with 38 additions and 30 deletions

View File

@@ -62,7 +62,7 @@ struct CommandAuxiliarySerialised : public CommandAuxiliaryBase {
virtual std::optional<span<const uint8>> GetDeserialisationSrc() const override { return span<const uint8>(this->serialised_data.data(), this->serialised_data.size()); } virtual std::optional<span<const uint8>> GetDeserialisationSrc() const override { return span<const uint8>(this->serialised_data.data(), this->serialised_data.size()); }
virtual void Serialise(CommandSerialisationBuffer &buffer) const override { buffer.Send_binary((const char *)this->serialised_data.data(), this->serialised_data.size()); } virtual void Serialise(CommandSerialisationBuffer &buffer) const override { buffer.Send_binary(this->serialised_data.data(), this->serialised_data.size()); }
}; };
template <typename T> template <typename T>

View File

@@ -119,7 +119,7 @@ void BufferSend_string(std::vector<byte> &buffer, size_t limit, const std::strin
* @param end The end 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. * @return The number of bytes that were added to this packet.
*/ */
size_t BufferSend_bytes(std::vector<byte> &buffer, size_t limit, const byte *begin, const byte *end) size_t BufferSend_binary_until_full(std::vector<byte> &buffer, size_t limit, const byte *begin, const byte *end)
{ {
size_t amount = std::min<size_t>(end - begin, limit - buffer.size()); size_t amount = std::min<size_t>(end - begin, limit - buffer.size());
buffer.insert(buffer.end(), begin, begin + amount); buffer.insert(buffer.end(), begin, begin + amount);
@@ -130,7 +130,7 @@ size_t BufferSend_bytes(std::vector<byte> &buffer, size_t limit, const byte *beg
* Sends a binary data over the network. * Sends a binary data over the network.
* @param data The data to send * @param data The data to send
*/ */
void BufferSend_binary(std::vector<byte> &buffer, size_t limit, const char *data, const size_t size) void BufferSend_binary(std::vector<byte> &buffer, size_t limit, const byte *data, const size_t size)
{ {
assert(data != nullptr); assert(data != nullptr);
assert(BufferCanWriteToPacket(buffer, limit, size)); assert(BufferCanWriteToPacket(buffer, limit, size));

View File

@@ -16,6 +16,7 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <limits>
void BufferSend_bool (std::vector<byte> &buffer, size_t limit, bool data); void BufferSend_bool (std::vector<byte> &buffer, size_t limit, bool data);
void BufferSend_uint8 (std::vector<byte> &buffer, size_t limit, uint8 data); void BufferSend_uint8 (std::vector<byte> &buffer, size_t limit, uint8 data);
@@ -23,8 +24,8 @@ void BufferSend_uint16(std::vector<byte> &buffer, size_t limit, uint16 data);
void BufferSend_uint32(std::vector<byte> &buffer, size_t limit, uint32 data); void BufferSend_uint32(std::vector<byte> &buffer, size_t limit, uint32 data);
void BufferSend_uint64(std::vector<byte> &buffer, size_t limit, uint64 data); void BufferSend_uint64(std::vector<byte> &buffer, size_t limit, uint64 data);
void BufferSend_string(std::vector<byte> &buffer, size_t limit, const std::string_view data); void BufferSend_string(std::vector<byte> &buffer, size_t limit, const std::string_view data);
size_t BufferSend_bytes (std::vector<byte> &buffer, size_t limit, const byte *begin, const byte *end); size_t BufferSend_binary_until_full(std::vector<byte> &buffer, size_t limit, const byte *begin, const byte *end);
void BufferSend_binary(std::vector<byte> &buffer, size_t limit, const char *data, const size_t size); void BufferSend_binary(std::vector<byte> &buffer, size_t limit, const byte *data, const size_t size);
void BufferSend_buffer(std::vector<byte> &buffer, size_t limit, const byte *data, const size_t size); void BufferSend_buffer(std::vector<byte> &buffer, size_t limit, const byte *data, const size_t size);
template <typename T> template <typename T>
@@ -65,18 +66,23 @@ struct BufferSerialisationHelper {
BufferSend_string(self->GetSerialisationBuffer(), self->GetSerialisationLimit(), data); BufferSend_string(self->GetSerialisationBuffer(), self->GetSerialisationLimit(), data);
} }
size_t Send_bytes(const byte *begin, const byte *end) size_t Send_binary_until_full(const byte *begin, const byte *end)
{ {
T *self = static_cast<T *>(this); T *self = static_cast<T *>(this);
return BufferSend_bytes(self->GetSerialisationBuffer(), self->GetSerialisationLimit(), begin, end); return BufferSend_binary_until_full(self->GetSerialisationBuffer(), self->GetSerialisationLimit(), begin, end);
} }
void Send_binary(const char *data, const size_t size) void Send_binary(const byte *data, const size_t size)
{ {
T *self = static_cast<T *>(this); T *self = static_cast<T *>(this);
BufferSend_binary(self->GetSerialisationBuffer(), self->GetSerialisationLimit(), data, size); BufferSend_binary(self->GetSerialisationBuffer(), self->GetSerialisationLimit(), data, size);
} }
void Send_binary(const byte *data, const byte *end)
{
this->Send_binary(data, end - data);
}
void Send_buffer(const byte *data, const size_t size) void Send_buffer(const byte *data, const size_t size)
{ {
T *self = static_cast<T *>(this); T *self = static_cast<T *>(this);
@@ -247,7 +253,7 @@ public:
* @param buffer The buffer to put the data into. * @param buffer The buffer to put the data into.
* @param size The size of the data. * @param size The size of the data.
*/ */
void Recv_binary(char *buffer, size_t size) void Recv_binary(byte *buffer, size_t size)
{ {
if (!this->CanRecvBytes(size, true)) return; if (!this->CanRecvBytes(size, true)) return;
@@ -262,14 +268,16 @@ public:
* @param buffer The buffer to put the data into. * @param buffer The buffer to put the data into.
* @param size The size of the data. * @param size The size of the data.
*/ */
void Recv_binary(std::string &buffer, size_t size) std::vector<uint8> Recv_binary(size_t size)
{ {
if (!this->CanRecvBytes(size, true)) return; if (!this->CanRecvBytes(size, true)) return {};
auto &pos = static_cast<T *>(this)->GetDeserialisationPosition(); auto &pos = static_cast<T *>(this)->GetDeserialisationPosition();
buffer.assign((const char *) &this->GetBuffer()[pos], size); std::vector<uint8> buffer { &this->GetBuffer()[pos], &this->GetBuffer()[pos + size] };
pos += (decltype(pos)) size; pos += (decltype(pos)) size;
return buffer;
} }
/** /**
@@ -290,4 +298,13 @@ public:
} }
}; };
struct BufferSerialiser : public BufferSerialisationHelper<BufferSerialiser> {
std::vector<byte> &buffer;
BufferSerialiser(std::vector<byte> &buffer) : buffer(buffer) {}
std::vector<byte> &GetSerialisationBuffer() { return this->buffer; }
size_t GetSerialisationLimit() const { return std::numeric_limits<size_t>::max(); }
};
#endif /* SERIALISATION_HPP */ #endif /* SERIALISATION_HPP */

View File

@@ -97,7 +97,7 @@ void NetworkUDPSocketHandler::SendPacket(Packet *p, NetworkAddress *recv, bool a
frag.Send_uint8 (current_frag); frag.Send_uint8 (current_frag);
frag.Send_uint8 (frag_count); frag.Send_uint8 (frag_count);
frag.Send_uint16 (payload_size); frag.Send_uint16 (payload_size);
frag.Send_binary((const char *) p->GetBufferData() + offset, payload_size); frag.Send_binary(p->GetBufferData() + offset, payload_size);
current_frag++; current_frag++;
offset += payload_size; offset += payload_size;
this->SendPacket(&frag, recv, all, broadcast, short_mtu); this->SendPacket(&frag, recv, all, broadcast, short_mtu);
@@ -239,7 +239,7 @@ void NetworkUDPSocketHandler::Receive_EX_MULTI(Packet *p, NetworkAddress *client
Packet merged(this, SHRT_MAX, 0); Packet merged(this, SHRT_MAX, 0);
merged.ReserveBuffer(total_payload); merged.ReserveBuffer(total_payload);
for (auto &frag : fs.fragments) { for (auto &frag : fs.fragments) {
merged.Send_binary(frag.data(), frag.size()); merged.Send_binary((const byte *)frag.data(), frag.size());
} }
merged.ParsePacketSize(); merged.ParsePacketSize();
merged.PrepareToRead(); merged.PrepareToRead();

View File

@@ -223,15 +223,6 @@ std::string GenerateCompanyPasswordHash(const std::string &password, const std::
return hashed_password.str(); return hashed_password.str();
} }
struct HashBuffer : public BufferSerialisationHelper<HashBuffer> {
std::vector<byte> &buffer;
HashBuffer(std::vector<byte> &buffer) : buffer(buffer) {}
std::vector<byte> &GetSerialisationBuffer() { return this->buffer; }
size_t GetSerialisationLimit() const { return UINT32_MAX; }
};
/** /**
* Hash the given password using server ID and game seed. * Hash the given password using server ID and game seed.
* @param password Password to hash. * @param password Password to hash.
@@ -245,7 +236,7 @@ std::vector<uint8> GenerateGeneralPasswordHash(const std::string &password, cons
std::vector<byte> data; std::vector<byte> data;
data.reserve(password.size() + password_server_id.size() + 6); data.reserve(password.size() + password_server_id.size() + 6);
HashBuffer buffer(data); BufferSerialiser buffer(data);
/* key field */ /* key field */
buffer.Send_uint64(password_game_seed); buffer.Send_uint64(password_game_seed);

View File

@@ -557,7 +557,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::SendDesyncLog(const std::strin
Packet *p = new Packet(PACKET_CLIENT_DESYNC_LOG, SHRT_MAX); Packet *p = new Packet(PACKET_CLIENT_DESYNC_LOG, SHRT_MAX);
size_t size = std::min<size_t>(log.size() - offset, SHRT_MAX - 2 - p->Size()); size_t size = std::min<size_t>(log.size() - offset, SHRT_MAX - 2 - p->Size());
p->Send_uint16((uint16)size); p->Send_uint16((uint16)size);
p->Send_binary(log.data() + offset, size); p->Send_binary((const byte *)(log.data() + offset), size);
my_client->SendPacket(p); my_client->SendPacket(p);
offset += size; offset += size;
@@ -1175,7 +1175,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_DESYNC_LOG(Pack
{ {
uint size = p->Recv_uint16(); uint size = p->Recv_uint16();
this->server_desync_log.resize(this->server_desync_log.size() + size); this->server_desync_log.resize(this->server_desync_log.size() + size);
p->Recv_binary(this->server_desync_log.data() + this->server_desync_log.size() - size, size); p->Recv_binary((byte *)(this->server_desync_log.data() + this->server_desync_log.size() - size), size);
DEBUG(net, 2, "Received %u bytes of server desync log", size); DEBUG(net, 2, "Received %u bytes of server desync log", size);
return NETWORK_RECV_STATUS_OKAY; return NETWORK_RECV_STATUS_OKAY;
} }

View File

@@ -343,7 +343,7 @@ const char *NetworkGameSocketHandler::ReceiveCommand(Packet *p, CommandPacket *c
CommandAuxiliarySerialised *aux_data = new CommandAuxiliarySerialised(); CommandAuxiliarySerialised *aux_data = new CommandAuxiliarySerialised();
cp->aux_data.reset(aux_data); cp->aux_data.reset(aux_data);
aux_data->serialised_data.resize(aux_data_size); aux_data->serialised_data.resize(aux_data_size);
p->Recv_binary((char *)(aux_data->serialised_data.data()), aux_data_size); p->Recv_binary((aux_data->serialised_data.data()), aux_data_size);
} }
return nullptr; return nullptr;

View File

@@ -160,7 +160,7 @@ struct PacketWriter : SaveFilter {
byte *bufe = buf + size; byte *bufe = buf + size;
while (buf != bufe) { while (buf != bufe) {
size_t written = this->current->Send_bytes(buf, bufe); size_t written = this->current->Send_binary_until_full(buf, bufe);
buf += written; buf += written;
if (!this->current->CanWriteToPacket(1)) { if (!this->current->CanWriteToPacket(1)) {
@@ -452,7 +452,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendDesyncLog(const std::strin
Packet *p = new Packet(PACKET_SERVER_DESYNC_LOG, SHRT_MAX); Packet *p = new Packet(PACKET_SERVER_DESYNC_LOG, SHRT_MAX);
size_t size = std::min<size_t>(log.size() - offset, SHRT_MAX - 2 - p->Size()); size_t size = std::min<size_t>(log.size() - offset, SHRT_MAX - 2 - p->Size());
p->Send_uint16(static_cast<uint16>(size)); p->Send_uint16(static_cast<uint16>(size));
p->Send_binary(log.data() + offset, size); p->Send_binary((const byte *)(log.data() + offset), size);
this->SendPacket(p); this->SendPacket(p);
offset += size; offset += size;
@@ -1246,7 +1246,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_DESYNC_LOG(Pack
{ {
uint size = p->Recv_uint16(); uint size = p->Recv_uint16();
this->desync_log.resize(this->desync_log.size() + size); this->desync_log.resize(this->desync_log.size() + size);
p->Recv_binary(this->desync_log.data() + this->desync_log.size() - size, size); p->Recv_binary((byte *)(this->desync_log.data() + this->desync_log.size() - size), size);
DEBUG(net, 2, "Received %u bytes of client desync log", size); DEBUG(net, 2, "Received %u bytes of client desync log", size);
this->receive_limit += p->Size(); this->receive_limit += p->Size();
return NETWORK_RECV_STATUS_OKAY; return NETWORK_RECV_STATUS_OKAY;