16 #include "../../stdafx.h" 17 #include "../../string_func.h" 21 #include "../../safeguards.h" 67 assert(this->
cs == NULL && this->
next == NULL);
154 assert(data != NULL);
157 while ((this->
buffer[this->
size++] = *data++) !=
'\0') {}
179 if (this->
pos + bytes_to_read > this->
size) {
180 this->
cs->NetworkSocketHandler::CloseConnection();
192 assert(this->
cs != NULL && this->
next == NULL);
242 n += (uint16)this->
buffer[this->
pos++] << 8;
257 n += (uint32)this->
buffer[this->
pos++] << 8;
258 n += (uint32)this->
buffer[this->
pos++] << 16;
259 n += (uint32)this->
buffer[this->
pos++] << 24;
274 n += (uint64)this->
buffer[this->
pos++] << 8;
275 n += (uint64)this->
buffer[this->
pos++] << 16;
276 n += (uint64)this->
buffer[this->
pos++] << 24;
277 n += (uint64)this->
buffer[this->
pos++] << 32;
278 n += (uint64)this->
buffer[this->
pos++] << 40;
279 n += (uint64)this->
buffer[this->
pos++] << 48;
280 n += (uint64)this->
buffer[this->
pos++] << 56;
294 const char *last = buffer + size - 1;
300 while (--size > 0 && pos < this->size && (*buffer++ = this->buffer[pos++]) !=
'\0') {}
302 if (size == 0 || pos == this->size) {
306 while (pos < this->size && this->buffer[pos] !=
'\0') pos++;
bool HasClientQuit() const
Whether the current client connected to the socket has quit.
uint32 Recv_uint32()
Read a 32 bits integer from the packet.
PacketSize pos
The current read/write position in the packet.
void PrepareToSend()
Writes the packet size from the raw packet from packet->size.
void Send_string(const char *data)
Sends a string over the network.
uint8 PacketType
Identifier for the packet.
void Send_uint8(uint8 data)
Package a 8 bits integer in the packet.
void Send_uint32(uint32 data)
Package a 32 bits integer in the packet.
bool CanReadFromPacket(uint bytes_to_read)
Is it safe to read from the packet, i.e.
StringValidationSettings
Settings for the string validation.
void PrepareToRead()
Prepares the packet so it can be read.
void Send_uint64(uint64 data)
Package a 64 bits integer in the packet.
byte * buffer
The buffer of this packet, of basically variable length up to SEND_MTU.
void str_validate(char *str, const char *last, StringValidationSettings settings)
Scans the string for valid characters and if it finds invalid ones, replaces them with a question mar...
void Send_uint16(uint16 data)
Package a 16 bits integer in the packet.
~Packet()
Free the buffer of this packet.
PacketSize size
The size of the whole packet for received packets.
bool Recv_bool()
Read a boolean from the packet.
Packet * next
The next packet.
NetworkSocketHandler * cs
Socket we're associated with.
Packet(NetworkSocketHandler *cs)
Create a packet that is used to read from a network socket.
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
uint64 Recv_uint64()
Read a 64 bits integer from the packet.
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
static const uint16 SEND_MTU
Number of bytes we can pack in a single packet.
Basic functions to create, fill and read packets.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
void ReadRawPacketSize()
Reads the packet size from the raw packet and stores it in the packet->size.
uint16 Recv_uint16()
Read a 16 bits integer from the packet.
void Send_bool(bool data)
Package a boolean in the packet.
uint16 PacketSize
Size of the whole packet.
SocketHandler for all network sockets in OpenTTD.
void Recv_string(char *buffer, size_t size, StringValidationSettings settings=SVS_REPLACE_WITH_QUESTION_MARK)
Reads a string till it finds a '\0' in the stream.