OpenTTD
Public Member Functions | Data Fields | Private Attributes
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. More...
 

Private Attributes

NetworkSocketHandlercs
 Socket we're associated with. More...
 

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 —

Definition at line 44 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 27 of file packet.cpp.

◆ Packet() [2/2]

Packet::Packet ( PacketType  type)

Creates a packet to send.

Parameters
typeof the packet to send

Definition at line 42 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 173 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()

void Packet::Recv_string ( char *  buffer,
size_t  size,
StringValidationSettings  settings = SVS_REPLACE_WITH_QUESTION_MARK 
)

◆ Recv_uint16()

uint16 Packet::Recv_uint16 ( )

◆ Recv_uint32()

uint32 Packet::Recv_uint32 ( )

◆ Recv_uint64()

uint64 Packet::Recv_uint64 ( )

◆ Recv_uint8()

uint8 Packet::Recv_uint8 ( )

◆ Send_bool()

void Packet::Send_bool ( bool  data)

◆ Send_string()

void Packet::Send_string ( const char *  data)

◆ Send_uint16()

void Packet::Send_uint16 ( uint16  data)

◆ Send_uint32()

void Packet::Send_uint32 ( uint32  data)

◆ Send_uint64()

void Packet::Send_uint64 ( uint64  data)

Package a 64 bits integer in the packet.

Parameters
dataThe data to send.

Definition at line 134 of file packet.cpp.

Referenced by NetworkUDPAdvertiseThread(), ServerNetworkAdminSocketHandler::SendChat(), ClientNetworkGameSocketHandler::SendChat(), and ServerNetworkGameSocketHandler::SendChat().

◆ Send_uint8()

void Packet::Send_uint8 ( uint8  data)

Package a 8 bits integer in the packet.

Parameters
dataThe data to send.

Definition at line 100 of file packet.cpp.

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::SendCompanyNew(), ServerNetworkAdminSocketHandler::SendCompanyRemove(), ServerNetworkGameSocketHandler::SendConfigUpdate(), ServerNetworkAdminSocketHandler::SendError(), ClientNetworkGameSocketHandler::SendError(), ServerNetworkGameSocketHandler::SendError(), ServerNetworkGameSocketHandler::SendErrorQuit(), NetworkSocketHandler::SendGRFIdentifier(), ServerNetworkGameSocketHandler::SendMove(), ClientNetworkGameSocketHandler::SendMove(), NetworkUDPSocketHandler::SendNetworkGameInfo(), ServerNetworkGameSocketHandler::SendNewGRFCheck(), ServerNetworkAdminSocketHandler::SendProtocol(), and ServerNetworkGameSocketHandler::SendWait().

Field Documentation

◆ buffer

byte* Packet::buffer

◆ cs

NetworkSocketHandler* Packet::cs
private

Socket we're associated with.

Definition at line 60 of file packet.h.

Referenced by CanReadFromPacket(), Packet(), and Recv_string().

◆ next

Packet* Packet::next

The next packet.

Used for queueing packets before sending.

Definition at line 46 of file packet.h.

Referenced by PacketWriter::AppendQueue(), NetworkTCPSocketHandler::CloseConnection(), Packet(), PacketWriter::PopPacket(), NetworkTCPSocketHandler::SendPackets(), and PacketWriter::~PacketWriter().

◆ size

PacketSize Packet::size

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