|
OpenTTD Source
14.0-beta3
|
Go to the documentation of this file.
12 #include "../../stdafx.h"
13 #include "../../string_func.h"
17 #include "../../safeguards.h"
33 assert(
cs !=
nullptr);
36 this->
buffer.resize(initial_read_size);
60 assert(this->
cs ==
nullptr);
66 this->
buffer.shrink_to_fit();
76 return this->
Size() + bytes_to_write <= this->
limit;
107 this->
buffer.emplace_back(data);
117 this->
buffer.emplace_back(
GB(data, 0, 8));
118 this->
buffer.emplace_back(
GB(data, 8, 8));
128 this->
buffer.emplace_back(
GB(data, 0, 8));
129 this->
buffer.emplace_back(
GB(data, 8, 8));
130 this->
buffer.emplace_back(
GB(data, 16, 8));
131 this->
buffer.emplace_back(
GB(data, 24, 8));
141 this->
buffer.emplace_back(
GB(data, 0, 8));
142 this->
buffer.emplace_back(
GB(data, 8, 8));
143 this->
buffer.emplace_back(
GB(data, 16, 8));
144 this->
buffer.emplace_back(
GB(data, 24, 8));
145 this->
buffer.emplace_back(
GB(data, 32, 8));
146 this->
buffer.emplace_back(
GB(data, 40, 8));
147 this->
buffer.emplace_back(
GB(data, 48, 8));
148 this->
buffer.emplace_back(
GB(data, 56, 8));
159 this->
buffer.insert(this->
buffer.end(), data.begin(), data.end());
160 this->
buffer.emplace_back(
'\0');
171 this->
buffer.insert(this->
buffer.end(), data.begin(), data.end());
184 size_t amount = std::min<size_t>(end - begin, this->
limit - this->
Size());
185 this->
buffer.insert(this->
buffer.end(), begin, begin + amount);
210 if (this->
pos + bytes_to_read > this->
Size()) {
211 if (close_connection) this->
cs->NetworkSocketHandler::MarkClosed();
237 return this->
buffer.size();
246 assert(this->
cs !=
nullptr);
247 size_t size = (size_t)this->
buffer[0];
248 size += (size_t)this->
buffer[1] << 8;
255 this->
buffer.resize(size);
313 n += (uint16_t)this->
buffer[this->
pos++] << 8;
328 n += (uint32_t)this->
buffer[this->
pos++] << 8;
329 n += (uint32_t)this->
buffer[this->
pos++] << 16;
330 n += (uint32_t)this->
buffer[this->
pos++] << 24;
345 n += (uint64_t)this->
buffer[this->
pos++] << 8;
346 n += (uint64_t)this->
buffer[this->
pos++] << 16;
347 n += (uint64_t)this->
buffer[this->
pos++] << 24;
348 n += (uint64_t)this->
buffer[this->
pos++] << 32;
349 n += (uint64_t)this->
buffer[this->
pos++] << 40;
350 n += (uint64_t)this->
buffer[this->
pos++] << 48;
351 n += (uint64_t)this->
buffer[this->
pos++] << 56;
364 std::vector<byte> data;
366 data.push_back(this->
buffer[this->
pos++]);
391 while (--length > 0 && (character = this->
Recv_uint8()) !=
'\0') str.push_back(character);
407 return this->
Size() - this->
pos;
uint64_t Recv_uint64()
Read a 64 bits integer from the packet.
void Send_uint64(uint64_t data)
Package a 64 bits integer in the packet.
void PrepareToSend()
Writes the packet size from the raw packet from packet->size.
size_t Size() const
Get the number of bytes in the packet.
SocketHandler for all network sockets in OpenTTD.
size_t Send_bytes(const byte *begin, const byte *end)
Send as many of the bytes as possible in the packet.
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.
bool HasPacketSizeData() const
Check whether the packet, given the position of the "write" pointer, has read enough of the packet to...
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.
uint8_t PacketType
Identifier for the packet.
bool ParsePacketSize()
Reads the packet size from the raw packet and stores it in the packet->size.
bool CanReadFromPacket(size_t bytes_to_read, bool close_connection=false)
Is it safe to read from 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...
Packet(NetworkSocketHandler *cs, size_t limit, size_t initial_read_size=sizeof(PacketSize))
Create a packet that is used to read from a network socket.
void Send_uint32(uint32_t data)
Package a 32 bits integer in the packet.
void Send_uint8(uint8_t data)
Package a 8 bits integer in the packet.
uint32_t Recv_uint32()
Read a 32 bits integer from the packet.
void PrepareToRead()
Prepares the packet so it can be read.
void Send_buffer(const std::vector< byte > &data)
Copy a sized byte buffer into the packet.
fluid_settings_t * settings
FluidSynth settings handle.
std::vector< byte > Recv_buffer()
Extract a sized byte buffer from the packet.
uint16_t PacketSize
Size of the whole packet.
StringValidationSettings
Settings for the string validation.
void Send_bool(bool data)
Package a boolean in the packet.
size_t limit
The limit for the packet size.
bool HasClientQuit() const
Whether the current client connected to the socket has quit.
PacketType GetPacketType() const
Get the PacketType from this packet.
bool CanWriteToPacket(size_t bytes_to_write)
Is it safe to write to the packet, i.e.
bool Recv_bool()
Read a boolean from the packet.
NetworkSocketHandler * cs
Socket we're associated with.
void Send_string(const std::string_view data)
Sends a string over the network.
std::vector< byte > buffer
The buffer of this packet.
uint16_t Recv_uint16()
Read a 16 bits integer from the packet.
size_t RemainingBytesToTransfer() const
Get the amount of bytes that are still available for the Transfer functions.
void Send_uint16(uint16_t data)
Package a 16 bits integer in the packet.
uint8_t Recv_uint8()
Read a 8 bits integer from the packet.
PacketSize pos
The current read/write position in the packet.