Change: [Network] Transfer command data as serialized byte stream without fixed structure.
The data will be transmitted as the length followed by the serialized data. This allows the command data to be different for every command type in the future.
This commit is contained in:
@@ -185,6 +185,17 @@ void Packet::Send_string(const std::string_view data)
|
||||
this->buffer.emplace_back('\0');
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy a sized byte buffer into the packet.
|
||||
* @param data The data to send.
|
||||
*/
|
||||
void Packet::Send_buffer(const std::vector<byte> &data)
|
||||
{
|
||||
assert(this->CanWriteToPacket(sizeof(uint16) + data.size()));
|
||||
this->Send_uint16((uint16)data.size());
|
||||
this->buffer.insert(this->buffer.end(), data.begin(), data.end());
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
@@ -366,6 +377,23 @@ uint64 Packet::Recv_uint64()
|
||||
return n;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract a sized byte buffer from the packet.
|
||||
* @return The extracted buffer.
|
||||
*/
|
||||
std::vector<byte> Packet::Recv_buffer()
|
||||
{
|
||||
uint16 size = this->Recv_uint16();
|
||||
if (size == 0 || !this->CanReadFromPacket(size, true)) return {};
|
||||
|
||||
std::vector<byte> data;
|
||||
while (size-- > 0) {
|
||||
data.push_back(this->buffer[this->pos++]);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads characters (bytes) from the packet until it finds a '\0', or reaches a
|
||||
* maximum of \c length characters.
|
||||
|
Reference in New Issue
Block a user