OpenTTD Source  1.11.2
Packet Struct Reference

Internal entity of a packet. More...

#include <packet.h>

Public Member Functions

 Packet (NetworkSocketHandler *cs)
 Create a packet that is used to read from a network socket. More...
 
 Packet (PacketType type)
 Creates a packet to send. More...
 
 ~Packet ()
 Free the buffer of this packet.
 
void PrepareToSend ()
 Writes the packet size from the raw packet from packet->size.
 
void Send_bool (bool data)
 Package a boolean in the packet. More...
 
void Send_uint8 (uint8 data)
 Package a 8 bits integer in the packet. More...
 
void Send_uint16 (uint16 data)
 Package a 16 bits integer in the packet. More...
 
void Send_uint32 (uint32 data)
 Package a 32 bits integer in the packet. More...
 
void Send_uint64 (uint64 data)
 Package a 64 bits integer in the packet. More...
 
void Send_string (const char *data)
 Sends a string over the network. More...
 
void ReadRawPacketSize ()
 Reads the packet size from the raw packet and stores it in the packet->size.
 
void PrepareToRead ()
 Prepares the packet so it can be read.
 
bool CanReadFromPacket (uint bytes_to_read)
 Is it safe to read from the packet, i.e. More...
 
bool Recv_bool ()
 Read a boolean from the packet. More...
 
uint8 Recv_uint8 ()
 Read a 8 bits integer from the packet. More...
 
uint16 Recv_uint16 ()
 Read a 16 bits integer from the packet. More...
 
uint32 Recv_uint32 ()
 Read a 32 bits integer from the packet. More...
 
uint64 Recv_uint64 ()
 Read a 64 bits integer from the packet. More...
 
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. More...
 

Data Fields

Packetnext
 The next packet. More...
 
PacketSize size
 The size of the whole packet for received packets. More...
 
PacketSize pos
 The current read/write position in the packet.
 
byte * buffer
 The buffer of this packet, of basically variable length up to SEND_MTU.
 

Private Attributes

NetworkSocketHandlercs
 Socket we're associated with.
 

Detailed Description

Internal entity of a packet.

As everything is sent as a packet, all network communication will need to call the functions that populate the packet. Every packet can be at most SEND_MTU bytes. Overflowing this limit will give an assertion when sending (i.e. writing) the packet. Reading past the size of the packet when receiving will return all 0 values and "" in case of the string.

— Points of attention —

  • all > 1 byte integral values are written in little endian, unless specified otherwise. Thus, 0x01234567 would be sent as {0x67, 0x45, 0x23, 0x01}.
  • all sent strings are of variable length and terminated by a '\0'. Thus, the length of the strings is not sent.
  • years that are leap years in the 'days since X' to 'date' calculations: (year % 4 == 0) and ((year % 100 != 0) or (year % 400 == 0))

Definition at line 40 of file packet.h.

Constructor & Destructor Documentation

◆ Packet() [1/2]

Packet::Packet ( NetworkSocketHandler cs)

Create a packet that is used to read from a network socket.

Parameters
csthe socket handler associated with the socket we are reading from

Definition at line 23 of file packet.cpp.

References buffer, cs, next, pos, SEND_MTU, and size.

◆ Packet() [2/2]

Packet::Packet ( PacketType  type)

Creates a packet to send.

Parameters
typeof the packet to send

Definition at line 38 of file packet.cpp.

References buffer, cs, next, pos, SEND_MTU, and size.

Member Function Documentation

◆ CanReadFromPacket()

bool Packet::CanReadFromPacket ( uint  bytes_to_read)

Is it safe to read from the packet, i.e.

didn't we run over the buffer ?

Parameters
bytes_to_readThe amount of bytes we want to try to read.
Returns
True if that is safe, otherwise false.

Definition at line 169 of file packet.cpp.

References cs, NetworkSocketHandler::HasClientQuit(), pos, and size.

Referenced by Recv_uint16(), Recv_uint32(), Recv_uint64(), and Recv_uint8().

◆ Recv_bool()

bool Packet::Recv_bool ( )

◆ Recv_string()

◆ Recv_uint16()

◆ Recv_uint32()

◆ Recv_uint64()

uint64 Packet::Recv_uint64 ( )

◆ Recv_uint8()

◆ Send_bool()

◆ Send_string()

void Packet::Send_string ( const char *  data)

Sends a string over the network.

It sends out the string + '\0'. No size-byte or something.

Parameters
dataThe string to send

Definition at line 148 of file packet.cpp.

References buffer, SEND_MTU, and size.

Referenced by NetworkUDPAdvertiseThread(), ServerNetworkUDPSocketHandler::Receive_CLIENT_GET_NEWGRFS(), ServerNetworkAdminSocketHandler::SendChat(), ClientNetworkGameSocketHandler::SendChat(), ServerNetworkGameSocketHandler::SendChat(), ServerNetworkAdminSocketHandler::SendClientInfo(), ServerNetworkGameSocketHandler::SendClientInfo(), ServerNetworkAdminSocketHandler::SendClientUpdate(), ServerNetworkAdminSocketHandler::SendCmdLogging(), ServerNetworkAdminSocketHandler::SendCmdNames(), NetworkGameSocketHandler::SendCommand(), ServerNetworkGameSocketHandler::SendCompanyInfo(), ClientNetworkGameSocketHandler::SendCompanyPassword(), ServerNetworkAdminSocketHandler::SendConsole(), ServerNetworkGameSocketHandler::SendError(), ClientNetworkGameSocketHandler::SendGamePassword(), ServerNetworkAdminSocketHandler::SendGameScript(), ClientNetworkGameSocketHandler::SendMove(), ServerNetworkGameSocketHandler::SendNeedCompanyPassword(), ServerNetworkAdminSocketHandler::SendRcon(), ClientNetworkGameSocketHandler::SendRCon(), ServerNetworkAdminSocketHandler::SendRconEnd(), ServerNetworkGameSocketHandler::SendRConResult(), ClientNetworkGameSocketHandler::SendSetName(), ClientNetworkGameSocketHandler::SendSetPassword(), ServerNetworkAdminSocketHandler::SendWelcome(), ServerNetworkGameSocketHandler::SendWelcome(), and SerializeNetworkGameInfo().

◆ Send_uint16()

◆ Send_uint32()

◆ Send_uint64()

void Packet::Send_uint64 ( uint64  data)

◆ Send_uint8()

void Packet::Send_uint8 ( uint8  data)

Package a 8 bits integer in the packet.

Parameters
dataThe data to send.

Definition at line 96 of file packet.cpp.

References buffer, SEND_MTU, and size.

Referenced by NetworkUDPAdvertiseThread(), NetworkUDPQueryMasterServer(), NetworkUDPRemoveAdvertiseThread(), ServerNetworkUDPSocketHandler::Receive_CLIENT_DETAIL_INFO(), ServerNetworkUDPSocketHandler::Receive_CLIENT_GET_NEWGRFS(), ClientNetworkUDPSocketHandler::Receive_SERVER_RESPONSE(), ClientNetworkContentSocketHandler::RequestContentList(), Send_bool(), ClientNetworkGameSocketHandler::SendAck(), ServerNetworkAdminSocketHandler::SendChat(), ClientNetworkGameSocketHandler::SendChat(), ServerNetworkGameSocketHandler::SendChat(), ServerNetworkAdminSocketHandler::SendClientError(), ServerNetworkAdminSocketHandler::SendClientInfo(), ServerNetworkGameSocketHandler::SendClientInfo(), ServerNetworkAdminSocketHandler::SendClientUpdate(), ServerNetworkAdminSocketHandler::SendCmdLogging(), NetworkGameSocketHandler::SendCommand(), ServerNetworkAdminSocketHandler::SendCompanyEconomy(), ServerNetworkGameSocketHandler::SendCompanyInfo(), ServerNetworkAdminSocketHandler::SendCompanyNew(), ServerNetworkAdminSocketHandler::SendCompanyRemove(), ServerNetworkAdminSocketHandler::SendCompanyStats(), ServerNetworkGameSocketHandler::SendConfigUpdate(), ServerNetworkAdminSocketHandler::SendError(), ClientNetworkGameSocketHandler::SendError(), ServerNetworkGameSocketHandler::SendError(), ServerNetworkGameSocketHandler::SendErrorQuit(), ServerNetworkGameSocketHandler::SendMove(), ClientNetworkGameSocketHandler::SendMove(), ServerNetworkGameSocketHandler::SendNewGRFCheck(), ServerNetworkAdminSocketHandler::SendProtocol(), ServerNetworkGameSocketHandler::SendWait(), ServerNetworkAdminSocketHandler::SendWelcome(), SerializeGRFIdentifier(), and SerializeNetworkGameInfo().

Field Documentation

◆ next

◆ size


The documentation for this struct was generated from the following files: