12 #include "../../stdafx.h"
13 #include "../../string_func.h"
17 #include "../../safeguards.h"
33 assert(
cs !=
nullptr);
36 this->
buffer.resize(initial_read_size);
51 size_t size = EncodedLengthOfPacketSize();
57 this->
buffer.resize(size, 0);
75 size_t offset = EncodedLengthOfPacketSize();
77 size_t message_offset = offset + mac_size;
82 this->
buffer.shrink_to_fit();
92 return this->
Size() + bytes_to_write <= this->
limit;
123 this->
buffer.emplace_back(data);
133 this->
buffer.emplace_back(
GB(data, 0, 8));
134 this->
buffer.emplace_back(
GB(data, 8, 8));
144 this->
buffer.emplace_back(
GB(data, 0, 8));
145 this->
buffer.emplace_back(
GB(data, 8, 8));
146 this->
buffer.emplace_back(
GB(data, 16, 8));
147 this->
buffer.emplace_back(
GB(data, 24, 8));
157 this->
buffer.emplace_back(
GB(data, 0, 8));
158 this->
buffer.emplace_back(
GB(data, 8, 8));
159 this->
buffer.emplace_back(
GB(data, 16, 8));
160 this->
buffer.emplace_back(
GB(data, 24, 8));
161 this->
buffer.emplace_back(
GB(data, 32, 8));
162 this->
buffer.emplace_back(
GB(data, 40, 8));
163 this->
buffer.emplace_back(
GB(data, 48, 8));
164 this->
buffer.emplace_back(
GB(data, 56, 8));
175 this->
buffer.insert(this->
buffer.end(), data.begin(), data.end());
176 this->
buffer.emplace_back(
'\0');
187 this->
buffer.insert(this->
buffer.end(), data.begin(), data.end());
199 size_t amount = std::min<size_t>(span.size(), this->limit - this->Size());
200 this->
buffer.insert(this->
buffer.end(), span.data(), span.data() + amount);
201 return span.subspan(amount);
225 if (this->
pos + bytes_to_read > this->
Size()) {
226 if (close_connection) this->
cs->NetworkSocketHandler::MarkClosed();
240 return this->
pos >= EncodedLengthOfPacketSize();
252 return this->
buffer.size();
261 size_t size =
static_cast<size_t>(this->
buffer[0]);
262 size +=
static_cast<size_t>(this->
buffer[1]) << 8;
267 if (size < EncodedLengthOfPacketSize() + EncodedLengthOfPacketType() || size > this->
limit)
return false;
269 this->
buffer.resize(size);
270 this->
pos =
static_cast<PacketSize>(EncodedLengthOfPacketSize());
281 this->
pos =
static_cast<PacketSize>(EncodedLengthOfPacketSize());
286 if (this->
buffer.size() <=
pos + mac_size)
return false;
289 this->pos +=
static_cast<PacketSize>(mac_size);
299 assert(this->
Size() >= EncodedLengthOfPacketSize() + EncodedLengthOfPacketType());
300 size_t offset = EncodedLengthOfPacketSize();
339 n += (uint16_t)this->
buffer[this->
pos++] << 8;
354 n += (uint32_t)this->
buffer[this->
pos++] << 8;
355 n += (uint32_t)this->
buffer[this->
pos++] << 16;
356 n += (uint32_t)this->
buffer[this->
pos++] << 24;
371 n += (uint64_t)this->
buffer[this->
pos++] << 8;
372 n += (uint64_t)this->
buffer[this->
pos++] << 16;
373 n += (uint64_t)this->
buffer[this->
pos++] << 24;
374 n += (uint64_t)this->
buffer[this->
pos++] << 32;
375 n += (uint64_t)this->
buffer[this->
pos++] << 40;
376 n += (uint64_t)this->
buffer[this->
pos++] << 48;
377 n += (uint64_t)this->
buffer[this->
pos++] << 56;
390 std::vector<uint8_t> data;
392 data.push_back(this->
buffer[this->
pos++]);
405 auto tranfer_to_span = [](std::span<uint8_t> destination,
const char *source,
size_t amount) {
406 size_t to_copy = std::min(amount, destination.size());
407 std::copy(source, source + to_copy, destination.data());
433 while (--length > 0 && (character = this->
Recv_uint8()) !=
'\0') str.push_back(character);
449 return this->
Size() - this->
pos;
constexpr static debug_inline 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.
uint8_t valid
Bits indicating what variable is valid (for each bit, 0 is invalid, 1 is valid).
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(T &dst, const char *str, const char *last, StringValidationSettings settings)
Copies the valid (UTF-8) characters from str up to last to the dst.
StringValidationSettings
Settings for the string validation.
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_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.
ssize_t TransferOut(F transfer_function, D destination, Args &&... args)
Transfer data from the packet to the given function.
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.
std::string Recv_string(size_t length, StringValidationSettings settings=SVS_REPLACE_WITH_QUESTION_MARK)
Reads characters (bytes) from the packet until it finds a '\0', or reaches a maximum of length charac...
void Send_string(const std::string_view data)
Sends a string over the network.
void Send_uint64(uint64_t data)
Package a 64 bits integer in the packet.