OpenTTD Source
12.0-beta2
|
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);
61 while (*queue !=
nullptr) queue = &(*queue)->
next;
85 assert(this->
cs ==
nullptr && this->
next ==
nullptr);
91 this->
buffer.shrink_to_fit();
101 return this->
Size() + bytes_to_write <= this->
limit;
132 this->
buffer.emplace_back(data);
142 this->
buffer.emplace_back(
GB(data, 0, 8));
143 this->
buffer.emplace_back(
GB(data, 8, 8));
153 this->
buffer.emplace_back(
GB(data, 0, 8));
154 this->
buffer.emplace_back(
GB(data, 8, 8));
155 this->
buffer.emplace_back(
GB(data, 16, 8));
156 this->
buffer.emplace_back(
GB(data, 24, 8));
166 this->
buffer.emplace_back(
GB(data, 0, 8));
167 this->
buffer.emplace_back(
GB(data, 8, 8));
168 this->
buffer.emplace_back(
GB(data, 16, 8));
169 this->
buffer.emplace_back(
GB(data, 24, 8));
170 this->
buffer.emplace_back(
GB(data, 32, 8));
171 this->
buffer.emplace_back(
GB(data, 40, 8));
172 this->
buffer.emplace_back(
GB(data, 48, 8));
173 this->
buffer.emplace_back(
GB(data, 56, 8));
184 this->
buffer.insert(this->
buffer.end(), data.begin(), data.end());
185 this->
buffer.emplace_back(
'\0');
198 size_t amount = std::min<size_t>(end - begin, this->
limit - this->
Size());
199 this->
buffer.insert(this->
buffer.end(), begin, begin + amount);
224 if (this->
pos + bytes_to_read > this->
Size()) {
225 if (close_connection) this->
cs->NetworkSocketHandler::MarkClosed();
251 return this->
buffer.size();
260 assert(this->
cs !=
nullptr && this->
next ==
nullptr);
261 size_t size = (size_t)this->
buffer[0];
262 size += (size_t)this->
buffer[1] << 8;
269 this->
buffer.resize(size);
327 n += (uint16)this->
buffer[this->
pos++] << 8;
342 n += (uint32)this->
buffer[this->
pos++] << 8;
343 n += (uint32)this->
buffer[this->
pos++] << 16;
344 n += (uint32)this->
buffer[this->
pos++] << 24;
359 n += (uint64)this->
buffer[this->
pos++] << 8;
360 n += (uint64)this->
buffer[this->
pos++] << 16;
361 n += (uint64)this->
buffer[this->
pos++] << 24;
362 n += (uint64)this->
buffer[this->
pos++] << 32;
363 n += (uint64)this->
buffer[this->
pos++] << 40;
364 n += (uint64)this->
buffer[this->
pos++] << 48;
365 n += (uint64)this->
buffer[this->
pos++] << 56;
388 while (--length > 0 && (character = this->
Recv_uint8()) !=
'\0') str.push_back(character);
404 return this->
Size() - this->
pos;
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.
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
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.
bool HasPacketSizeData() const
Check whether the packet, given the position of the "write" pointer, has read enough of the packet to...
bool ParsePacketSize()
Reads the packet size from the raw packet and stores it in the packet->size.
void Send_uint8(uint8 data)
Package a 8 bits integer in the packet.
uint8 PacketType
Identifier for the packet.
void Send_uint32(uint32 data)
Package a 32 bits integer in the packet.
static void AddToQueue(Packet **queue, Packet *packet)
Add the given Packet to the end of the queue of packets.
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...
static Packet * PopFromQueue(Packet **queue)
Pop the packet from the begin of the queue and set the begin of the queue to the second element in th...
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.
uint32 Recv_uint32()
Read a 32 bits integer from the packet.
void PrepareToRead()
Prepares the packet so it can be read.
void Send_uint16(uint16 data)
Package a 16 bits integer in the packet.
fluid_settings_t * settings
FluidSynth settings handle.
Packet * next
The next packet.
Internal entity of a 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.
std::string StrMakeValid(const std::string &str, StringValidationSettings settings)
Scans the string for invalid characters and replaces then with a question mark '?' (if not ignored).
PacketType GetPacketType() const
Get the PacketType from this packet.
uint16 PacketSize
Size of the whole 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.
void Send_uint64(uint64 data)
Package a 64 bits integer in the packet.
NetworkSocketHandler * cs
Socket we're associated with.
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
void Send_string(const std::string_view data)
Sends a string over the network.
uint64 Recv_uint64()
Read a 64 bits integer from the packet.
std::vector< byte > buffer
The buffer of this packet.
size_t RemainingBytesToTransfer() const
Get the amount of bytes that are still available for the Transfer functions.
uint16 Recv_uint16()
Read a 16 bits integer from the packet.
PacketSize pos
The current read/write position in the packet.