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.
| Packet (PacketType type)
| Creates a packet to send.
| ~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.
void | Send_uint8 (uint8 data)
| Package a 8 bits integer in the packet.
void | Send_uint16 (uint16 data)
| Package a 16 bits integer in the packet.
void | Send_uint32 (uint32 data)
| Package a 32 bits integer in the packet.
void | Send_uint64 (uint64 data)
| Package a 64 bits integer in the packet.
void | Send_string (const char *data)
| Sends a string over the network.
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.
bool | Recv_bool ()
| Read a boolean from the packet.
uint8 | Recv_uint8 ()
| Read a 8 bits integer from the packet.
uint16 | Recv_uint16 ()
| Read a 16 bits integer from the packet.
uint32 | Recv_uint32 ()
| Read a 32 bits integer from the packet.
uint64 | Recv_uint64 ()
| Read a 64 bits integer from the packet.
void | Recv_string (char *buffer, size_t size, StringValidationSettings settings=SVS_REPLACE_WITH_QUESTION_MARK)
| Reads a string till it finds a '' in the stream.
## Data Fields
Packet * | next
| The next packet.
PacketSize | size
| The size of the whole packet for received packets.
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
NetworkSocketHandler * | cs
| 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 ''. 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 44 of file packet.h.

## Constructor & Destructor Documentation

Packet::Packet | ( | NetworkSocketHandler * | cs | ) |

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

Parameters:

  | cs | the socket handler associated with the socket we are reading from

Definition at line 25 of file packet.cpp.

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

Packet::Packet | ( | PacketType | type | ) |

Creates a packet to send.

Parameters:

  | type | of the packet to send

Definition at line 40 of file packet.cpp.

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

## Member Function Documentation

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_read | The amount of bytes we want to try to read.

Returns:
  True if that is safe, otherwise false.

Definition at line 171 of file packet.cpp.

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

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

bool Packet::Recv_bool | ( |  | ) |

Read a boolean from the packet.

Returns:
  The read data.

Definition at line 210 of file packet.cpp.

References Recv_uint8().

Referenced by ClientNetworkGameSocketHandler::Receive_SERVER_CHAT(), ClientNetworkGameSocketHandler::Receive_SERVER_COMMAND(), ClientNetworkGameSocketHandler::Receive_SERVER_COMPANY_INFO(), and NetworkUDPSocketHandler::ReceiveNetworkGameInfo().

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

Reads a string till it finds a '' in the stream.

Parameters:

  | buffer | The buffer to put the data into.
  | size | The size of the buffer.
  | settings | The string validation settings.

Definition at line 288 of file packet.cpp.

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

Referenced by ClientNetworkGameSocketHandler::Receive_SERVER_CHAT(), ClientNetworkGameSocketHandler::Receive_SERVER_CLIENT_INFO(), ClientNetworkGameSocketHandler::Receive_SERVER_COMPANY_INFO(), ClientNetworkGameSocketHandler::Receive_SERVER_NEED_COMPANY_PASSWORD(), ClientNetworkUDPSocketHandler::Receive_SERVER_NEWGRFS(), ClientNetworkGameSocketHandler::Receive_SERVER_RCON(), ClientNetworkGameSocketHandler::Receive_SERVER_WELCOME(), NetworkGameSocketHandler::ReceiveCommand(), and NetworkUDPSocketHandler::ReceiveNetworkGameInfo().

uint16 Packet::Recv_uint16 | ( |  | ) |

Read a 16 bits integer from the packet.

Returns:
  The read data.

Definition at line 233 of file packet.cpp.

References buffer, CanReadFromPacket(), and pos.

Referenced by ClientNetworkUDPSocketHandler::Receive_MASTER_RESPONSE_LIST(), ClientNetworkGameSocketHandler::Receive_SERVER_COMPANY_INFO(), ClientNetworkGameSocketHandler::Receive_SERVER_COMPANY_UPDATE(), ClientNetworkGameSocketHandler::Receive_SERVER_RCON(), and NetworkUDPSocketHandler::ReceiveNetworkGameInfo().

uint32 Packet::Recv_uint32 | ( |  | ) |

Read a 32 bits integer from the packet.

Returns:
  The read data.

Definition at line 248 of file packet.cpp.

References buffer, CanReadFromPacket(), and pos.

Referenced by ClientNetworkUDPSocketHandler::Receive_MASTER_RESPONSE_LIST(), ClientNetworkGameSocketHandler::Receive_SERVER_CHAT(), ClientNetworkGameSocketHandler::Receive_SERVER_CLIENT_INFO(), ClientNetworkGameSocketHandler::Receive_SERVER_COMMAND(), ClientNetworkGameSocketHandler::Receive_SERVER_COMPANY_INFO(), ClientNetworkGameSocketHandler::Receive_SERVER_ERROR_QUIT(), ClientNetworkGameSocketHandler::Receive_SERVER_FRAME(), ClientNetworkGameSocketHandler::Receive_SERVER_JOIN(), ClientNetworkGameSocketHandler::Receive_SERVER_MAP_BEGIN(), ClientNetworkGameSocketHandler::Receive_SERVER_MAP_SIZE(), ClientNetworkGameSocketHandler::Receive_SERVER_MOVE(), ClientNetworkGameSocketHandler::Receive_SERVER_NEED_COMPANY_PASSWORD(), ClientNetworkGameSocketHandler::Receive_SERVER_QUIT(), ClientNetworkGameSocketHandler::Receive_SERVER_SYNC(), ClientNetworkGameSocketHandler::Receive_SERVER_WELCOME(), NetworkGameSocketHandler::ReceiveCommand(), NetworkSocketHandler::ReceiveGRFIdentifier(), and NetworkUDPSocketHandler::ReceiveNetworkGameInfo().

uint64 Packet::Recv_uint64 | ( |  | ) |

Read a 64 bits integer from the packet.

Returns:
  The read data.

Definition at line 265 of file packet.cpp.

References buffer, CanReadFromPacket(), and pos.

Referenced by MasterNetworkUDPSocketHandler::Receive_MASTER_SESSION_KEY(), ClientNetworkGameSocketHandler::Receive_SERVER_CHAT(), and ClientNetworkGameSocketHandler::Receive_SERVER_COMPANY_INFO().

uint8 Packet::Recv_uint8 | ( |  | ) |

Read a 8 bits integer from the packet.

Returns:
  The read data.

Definition at line 219 of file packet.cpp.

References buffer, CanReadFromPacket(), and pos.

Referenced by NetworkGameSocketHandler::HandlePacket(), NetworkContentSocketHandler::HandlePacket(), NetworkAdminSocketHandler::HandlePacket(), NetworkUDPSocketHandler::HandleUDPPacket(), ServerNetworkUDPSocketHandler::Receive_CLIENT_GET_NEWGRFS(), ClientNetworkUDPSocketHandler::Receive_MASTER_RESPONSE_LIST(), ClientNetworkGameSocketHandler::Receive_SERVER_CHAT(), ClientNetworkGameSocketHandler::Receive_SERVER_CHECK_NEWGRFS(), ClientNetworkGameSocketHandler::Receive_SERVER_CLIENT_INFO(), ClientNetworkGameSocketHandler::Receive_SERVER_COMPANY_INFO(), ClientNetworkGameSocketHandler::Receive_SERVER_CONFIG_UPDATE(), ClientNetworkGameSocketHandler::Receive_SERVER_ERROR(), ClientNetworkGameSocketHandler::Receive_SERVER_ERROR_QUIT(), ClientNetworkGameSocketHandler::Receive_SERVER_FRAME(), ClientNetworkGameSocketHandler::Receive_SERVER_MOVE(), ClientNetworkUDPSocketHandler::Receive_SERVER_NEWGRFS(), ClientNetworkGameSocketHandler::Receive_SERVER_WAIT(), NetworkGameSocketHandler::ReceiveCommand(), NetworkSocketHandler::ReceiveGRFIdentifier(), NetworkUDPSocketHandler::ReceiveNetworkGameInfo(), and Recv_bool().

void Packet::Send_bool | ( | bool | data | ) |

Package a boolean in the packet.

Parameters:

  | data | The data to send.

Definition at line 89 of file packet.cpp.

References Send_uint8().

Referenced by ServerNetworkGameSocketHandler::SendChat(), ServerNetworkAdminSocketHandler::SendCmdNames(), ServerNetworkGameSocketHandler::SendCommand(), ServerNetworkGameSocketHandler::SendCompanyInfo(), ServerNetworkAdminSocketHandler::SendCompanyInfo(), ServerNetworkAdminSocketHandler::SendCompanyUpdate(), NetworkUDPSocketHandler::SendNetworkGameInfo(), ServerNetworkAdminSocketHandler::SendProtocol(), and ServerNetworkAdminSocketHandler::SendWelcome().

void Packet::Send_string | ( | const char * | data | ) |

Sends a string over the network.

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

Parameters:

  | data | The string to send

Definition at line 150 of file packet.cpp.

References buffer, SEND_MTU, and size.

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

void Packet::Send_uint16 | ( | uint16 | data | ) |

Package a 16 bits integer in the packet.

Parameters:

  | data | The data to send.

Definition at line 108 of file packet.cpp.

References buffer, GB(), SEND_MTU, and size.

Referenced by ClientNetworkContentSocketHandler::DownloadSelectedContentFallback(), NetworkUDPAdvertiseThread(), NetworkUDPRemoveAdvertiseThread(), ClientNetworkContentSocketHandler::RequestContentList(), ServerNetworkAdminSocketHandler::SendCmdLogging(), ServerNetworkAdminSocketHandler::SendCmdNames(), ServerNetworkAdminSocketHandler::SendCompanyEconomy(), ServerNetworkAdminSocketHandler::SendCompanyStats(), ServerNetworkGameSocketHandler::SendCompanyUpdate(), NetworkUDPSocketHandler::SendNetworkGameInfo(), ServerNetworkAdminSocketHandler::SendProtocol(), ServerNetworkAdminSocketHandler::SendRcon(), ServerNetworkGameSocketHandler::SendRConResult(), and ServerNetworkAdminSocketHandler::SendWelcome().

void Packet::Send_uint32 | ( | uint32 | data | ) |

Package a 32 bits integer in the packet.

Parameters:

  | data | The data to send.

Definition at line 119 of file packet.cpp.

References buffer, GB(), SEND_MTU, and size.

Referenced by ClientNetworkContentSocketHandler::DownloadSelectedContentFallback(), PacketWriter::Finish(), ClientNetworkContentSocketHandler::RequestContentList(), ClientNetworkGameSocketHandler::SendAck(), ServerNetworkGameSocketHandler::SendChat(), ClientNetworkGameSocketHandler::SendChat(), ServerNetworkAdminSocketHandler::SendChat(), ServerNetworkAdminSocketHandler::SendClientError(), ServerNetworkGameSocketHandler::SendClientInfo(), ServerNetworkAdminSocketHandler::SendClientInfo(), ServerNetworkAdminSocketHandler::SendClientJoin(), ServerNetworkAdminSocketHandler::SendClientQuit(), ServerNetworkAdminSocketHandler::SendClientUpdate(), ServerNetworkAdminSocketHandler::SendCmdLogging(), ServerNetworkGameSocketHandler::SendCommand(), NetworkGameSocketHandler::SendCommand(), ServerNetworkAdminSocketHandler::SendCompanyInfo(), ServerNetworkAdminSocketHandler::SendDate(), ServerNetworkGameSocketHandler::SendErrorQuit(), ServerNetworkGameSocketHandler::SendFrame(), NetworkSocketHandler::SendGRFIdentifier(), ServerNetworkGameSocketHandler::SendJoin(), ClientNetworkGameSocketHandler::SendJoin(), ServerNetworkGameSocketHandler::SendMap(), ServerNetworkGameSocketHandler::SendMove(), ServerNetworkGameSocketHandler::SendNeedCompanyPassword(), NetworkUDPSocketHandler::SendNetworkGameInfo(), ServerNetworkAdminSocketHandler::SendPong(), ServerNetworkGameSocketHandler::SendQuit(), ServerNetworkGameSocketHandler::SendSync(), ServerNetworkGameSocketHandler::SendWelcome(), and ServerNetworkAdminSocketHandler::SendWelcome().

void Packet::Send_uint64 | ( | uint64 | data | ) |

Package a 64 bits integer in the packet.

Parameters:

  | data | The data to send.

Definition at line 132 of file packet.cpp.

References buffer, GB(), SEND_MTU, and size.

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

void Packet::Send_uint8 | ( | uint8 | data | ) |

Package a 8 bits integer in the packet.

Parameters:

  | data | The data to send.

Definition at line 98 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(), ServerNetworkGameSocketHandler::SendChat(), ClientNetworkGameSocketHandler::SendChat(), ServerNetworkAdminSocketHandler::SendChat(), ServerNetworkAdminSocketHandler::SendClientError(), ServerNetworkGameSocketHandler::SendClientInfo(), ServerNetworkAdminSocketHandler::SendClientInfo(), ServerNetworkAdminSocketHandler::SendClientUpdate(), ServerNetworkAdminSocketHandler::SendCmdLogging(), NetworkGameSocketHandler::SendCommand(), ServerNetworkAdminSocketHandler::SendCompanyEconomy(), ServerNetworkGameSocketHandler::SendCompanyInfo(), ServerNetworkAdminSocketHandler::SendCompanyInfo(), ServerNetworkAdminSocketHandler::SendCompanyNew(), ServerNetworkAdminSocketHandler::SendCompanyRemove(), ServerNetworkAdminSocketHandler::SendCompanyStats(), ServerNetworkAdminSocketHandler::SendCompanyUpdate(), ServerNetworkGameSocketHandler::SendConfigUpdate(), ServerNetworkGameSocketHandler::SendError(), ClientNetworkGameSocketHandler::SendError(), ServerNetworkAdminSocketHandler::SendError(), ServerNetworkGameSocketHandler::SendErrorQuit(), ServerNetworkGameSocketHandler::SendFrame(), NetworkSocketHandler::SendGRFIdentifier(), ClientNetworkGameSocketHandler::SendJoin(), ServerNetworkGameSocketHandler::SendMove(), ClientNetworkGameSocketHandler::SendMove(), NetworkUDPSocketHandler::SendNetworkGameInfo(), ServerNetworkGameSocketHandler::SendNewGRFCheck(), ServerNetworkAdminSocketHandler::SendProtocol(), ServerNetworkGameSocketHandler::SendWait(), and ServerNetworkAdminSocketHandler::SendWelcome().

## Field Documentation

byte* Packet::buffer

The buffer of this packet, of basically variable length up to SEND_MTU.

Definition at line 56 of file packet.h.

Referenced by TCPListenHandler< ServerNetworkGameSocketHandler, PACKET_SERVER_FULL, PACKET_SERVER_BANNED >::AcceptClient(), Packet(), PrepareToSend(), ReadRawPacketSize(), NetworkTCPSocketHandler::ReceivePacket(), NetworkUDPSocketHandler::ReceivePackets(), Recv_uint16(), Recv_uint32(), Recv_uint64(), Recv_uint8(), Send_string(), Send_uint16(), Send_uint32(), Send_uint64(), Send_uint8(), ServerNetworkGameSocketHandler::SendMap(), NetworkUDPSocketHandler::SendPacket(), NetworkTCPSocketHandler::SendPacket(), NetworkTCPSocketHandler::SendPackets(), PacketWriter::Write(), and ~Packet().

NetworkSocketHandler* Packet::cs`[private]`

Socket we're associated with.

Definition at line 60 of file packet.h.

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

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(), PrepareToSend(), ReadRawPacketSize(), NetworkTCPSocketHandler::SendPacket(), NetworkTCPSocketHandler::SendPackets(), and PacketWriter::~PacketWriter().

PacketSize Packet::size

The size of the whole packet for received packets.

For packets that will be sent, the value is filled in just before the actual transmission.

Definition at line 52 of file packet.h.

Referenced by TCPListenHandler< ServerNetworkGameSocketHandler, PACKET_SERVER_FULL, PACKET_SERVER_BANNED >::AcceptClient(), CanReadFromPacket(), Packet(), PrepareToSend(), ReadRawPacketSize(), ServerNetworkUDPSocketHandler::Receive_CLIENT_DETAIL_INFO(), ClientNetworkGameSocketHandler::Receive_SERVER_FRAME(), NetworkTCPSocketHandler::ReceivePacket(), NetworkUDPSocketHandler::ReceivePackets(), Send_string(), Send_uint16(), Send_uint32(), Send_uint64(), Send_uint8(), ServerNetworkAdminSocketHandler::SendCmdNames(), NetworkUDPSocketHandler::SendPacket(), NetworkTCPSocketHandler::SendPacket(), NetworkTCPSocketHandler::SendPackets(), and PacketWriter::Write().

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

- src/network/core/packet.h
- src/network/core/packet.cpp