10#include "../../stdafx.h"
11#include "../../string_func.h"
15#include "../../safeguards.h"
31 assert(
cs !=
nullptr);
34 this->
buffer.resize(initial_read_size);
49 size_t size = EncodedLengthOfPacketSize();
55 this->
buffer.resize(size, 0);
73 size_t offset = EncodedLengthOfPacketSize();
75 size_t message_offset = offset + mac_size;
80 this->
buffer.shrink_to_fit();
90 return this->
Size() + bytes_to_write <= this->
limit;
121 this->
buffer.emplace_back(data);
131 this->
buffer.emplace_back(
GB(data, 0, 8));
132 this->
buffer.emplace_back(
GB(data, 8, 8));
142 this->
buffer.emplace_back(
GB(data, 0, 8));
143 this->
buffer.emplace_back(
GB(data, 8, 8));
144 this->
buffer.emplace_back(
GB(data, 16, 8));
145 this->
buffer.emplace_back(
GB(data, 24, 8));
155 this->
buffer.emplace_back(
GB(data, 0, 8));
156 this->
buffer.emplace_back(
GB(data, 8, 8));
157 this->
buffer.emplace_back(
GB(data, 16, 8));
158 this->
buffer.emplace_back(
GB(data, 24, 8));
159 this->
buffer.emplace_back(
GB(data, 32, 8));
160 this->
buffer.emplace_back(
GB(data, 40, 8));
161 this->
buffer.emplace_back(
GB(data, 48, 8));
162 this->
buffer.emplace_back(
GB(data, 56, 8));
173 this->
buffer.insert(this->
buffer.end(), data.begin(), data.end());
174 this->
buffer.emplace_back(
'\0');
185 this->
buffer.insert(this->
buffer.end(), data.begin(), data.end());
197 size_t amount = std::min<size_t>(span.size(), this->limit - this->Size());
198 this->
buffer.insert(this->
buffer.end(), span.data(), span.data() + amount);
199 return span.subspan(amount);
223 if (this->
pos + bytes_to_read > this->
Size()) {
224 if (close_connection) this->
cs->NetworkSocketHandler::MarkClosed();
238 return this->
pos >= EncodedLengthOfPacketSize();
250 return this->
buffer.size();
259 size_t size =
static_cast<size_t>(this->
buffer[0]);
260 size +=
static_cast<size_t>(this->
buffer[1]) << 8;
265 if (size < EncodedLengthOfPacketSize() + EncodedLengthOfPacketType() || size > this->
limit)
return false;
267 this->
buffer.resize(size);
268 this->
pos =
static_cast<PacketSize>(EncodedLengthOfPacketSize());
279 this->
pos =
static_cast<PacketSize>(EncodedLengthOfPacketSize());
284 if (this->
buffer.size() <=
pos + mac_size)
return false;
287 this->pos +=
static_cast<PacketSize>(mac_size);
297 assert(this->
Size() >= EncodedLengthOfPacketSize() + EncodedLengthOfPacketType());
298 size_t offset = EncodedLengthOfPacketSize();
337 n += (uint16_t)this->
buffer[this->
pos++] << 8;
352 n += (uint32_t)this->
buffer[this->
pos++] << 8;
353 n += (uint32_t)this->
buffer[this->
pos++] << 16;
354 n += (uint32_t)this->
buffer[this->
pos++] << 24;
369 n += (uint64_t)this->
buffer[this->
pos++] << 8;
370 n += (uint64_t)this->
buffer[this->
pos++] << 16;
371 n += (uint64_t)this->
buffer[this->
pos++] << 24;
372 n += (uint64_t)this->
buffer[this->
pos++] << 32;
373 n += (uint64_t)this->
buffer[this->
pos++] << 40;
374 n += (uint64_t)this->
buffer[this->
pos++] << 48;
375 n += (uint64_t)this->
buffer[this->
pos++] << 56;
388 std::vector<uint8_t> data;
390 data.push_back(this->
buffer[this->
pos++]);
403 auto transfer_to_span = [&destination](std::span<const uint8_t> source) {
404 auto to_copy = source.subspan(0, destination.size());
405 std::ranges::copy(to_copy, destination.begin());
406 return to_copy.size();
431 while (--length > 0 && (character = this->
Recv_uint8()) !=
'\0') str.push_back(character);
447 return this->
Size() - this->
pos;
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
SocketHandler for all network sockets in OpenTTD.
bool HasClientQuit() const
Whether the current client connected to the socket has quit.
std::unique_ptr< class NetworkEncryptionHandler > send_encryption_handler
The handler for encrypting sent packets.
std::unique_ptr< class NetworkEncryptionHandler > receive_encryption_handler
The handler for decrypting received packets.
fluid_settings_t * settings
FluidSynth settings handle.
Basic functions to create, fill and read packets.
uint8_t PacketType
Identifier for the packet.
uint16_t PacketSize
Size of the whole packet.
static void StrMakeValid(Builder &builder, StringConsumer &consumer, StringValidationSettings settings)
Copies the valid (UTF-8) characters from consumer to the builder.
size_t Size() const
Get the number of bytes in the packet.
uint16_t Recv_uint16()
Read a 16 bits integer from the packet.
size_t Recv_bytes(std::span< uint8_t > span)
Extract at most the length of the span bytes from the packet into the span.
uint64_t Recv_uint64()
Read a 64 bits integer from the packet.
bool Recv_bool()
Read a boolean from the packet.
uint32_t Recv_uint32()
Read a 32 bits integer from the packet.
NetworkSocketHandler * cs
Socket we're associated with.
void Send_string(std::string_view data)
Sends a string over the network.
std::string Recv_string(size_t length, StringValidationSettings settings=StringValidationSetting::ReplaceWithQuestionMark)
Reads characters (bytes) from the packet until it finds a '\0', or reaches a maximum of length charac...
void Send_bool(bool data)
Package a boolean in the packet.
bool PrepareToRead()
Prepares the packet so it can be read.
PacketType GetPacketType() const
Get the PacketType from this packet.
PacketSize pos
The current read/write position in the packet.
std::span< const uint8_t > Send_bytes(const std::span< const uint8_t > span)
Send as many of the bytes as possible in the packet.
size_t limit
The limit for the packet size.
bool HasPacketSizeData() const
Check whether the packet, given the position of the "write" pointer, has read enough of the packet to...
uint8_t Recv_uint8()
Read a 8 bits integer from the packet.
bool ParsePacketSize()
Reads the packet size from the raw packet and stores it in the packet->size.
std::vector< uint8_t > buffer
The buffer of this packet.
void PrepareToSend()
Writes the packet size from the raw packet from packet->size.
void Send_uint8(uint8_t data)
Package a 8 bits integer in the packet.
size_t RemainingBytesToTransfer() const
Get the amount of bytes that are still available for the Transfer functions.
void Send_uint32(uint32_t data)
Package a 32 bits integer in the packet.
void Send_buffer(const std::vector< uint8_t > &data)
Copy a sized byte buffer into the packet.
std::vector< uint8_t > Recv_buffer()
Extract a sized byte buffer from the packet.
Packet(NetworkSocketHandler *cs, size_t limit, size_t initial_read_size=EncodedLengthOfPacketSize())
Create a packet that is used to read from a network socket.
void Send_uint16(uint16_t data)
Package a 16 bits integer in the packet.
bool CanReadFromPacket(size_t bytes_to_read, bool close_connection=false)
Is it safe to read from the packet, i.e.
bool CanWriteToPacket(size_t bytes_to_write)
Is it safe to write to the packet, i.e.
void Send_uint64(uint64_t data)
Package a 64 bits integer in the packet.
ssize_t TransferOut(F transfer_function)
Transfer data from the packet to the given function.