# 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)
| The next couple of functions make sure we can send uint8, uint16, uint32 and uint64 endian-safe over the network.
void | Send_uint8 (uint8 data)
void | Send_uint16 (uint16 data)
void | Send_uint32 (uint32 data)
void | Send_uint64 (uint64 data)
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)
| Receiving commands Again, the next couple of functions are endian-safe see the comment before Send_bool for more info.
bool | Recv_bool ()
uint8 | Recv_uint8 ()
uint16 | Recv_uint16 ()
uint32 | Recv_uint32 ()
uint64 | Recv_uint64 ()
void | Recv_string (char *buffer, size_t size, bool allow_newlines=false)
| 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.

Definition at line 34 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 | ) |

Receiving commands Again, the next couple of functions are endian-safe see the comment before Send_bool for more info.

Is it safe to read from the packet, i.e. didn't we run over the buffer ?

Definition at line 147 of file packet.cpp.

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

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

The next couple of functions make sure we can send uint8, uint16, uint32 and uint64 endian-safe over the network.

The least significant bytes are sent first.

So 0x01234567 would be sent as 67 45 23 01.

A bool is sent as a uint8 where zero means false and non-zero means true.

Definition at line 85 of file packet.cpp.

Referenced by NetworkUDPSocketHandler::SendNetworkGameInfo().

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 130 of file packet.cpp.

References buffer, SEND_MTU, and size.

Referenced by DEF_UDP_RECEIVE_COMMAND(), NetworkGameSocketHandler::SendCommand(), and NetworkUDPSocketHandler::SendNetworkGameInfo().

## Field Documentation

byte* Packet::buffer

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

Definition at line 46 of file packet.h.

Referenced by TCPListenHandler< ServerNetworkGameSocketHandler, PACKET_SERVER_FULL, PACKET_SERVER_BANNED >::AcceptClient(), PacketReader::AddPacket(), Packet(), PrepareToSend(), ReadRawPacketSize(), NetworkTCPSocketHandler::ReceivePacket(), NetworkUDPSocketHandler::ReceivePackets(), Send_string(), NetworkUDPSocketHandler::SendPacket(), NetworkTCPSocketHandler::SendPacket(), NetworkTCPSocketHandler::SendPackets(), PacketWriter::Write(), and ~Packet().

NetworkSocketHandler* Packet::cs`[private]`

Socket we're associated with.

Definition at line 50 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 36 of file packet.h.

Referenced by PacketWriter::AppendQueue(), NetworkTCPSocketHandler::CloseConnection(), Packet(), PrepareToSend(), ReadRawPacketSize(), NetworkTCPSocketHandler::SendPacket(), NetworkTCPSocketHandler::SendPackets(), and ServerNetworkGameSocketHandler::~ServerNetworkGameSocketHandler().

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 42 of file packet.h.

Referenced by TCPListenHandler< ServerNetworkGameSocketHandler, PACKET_SERVER_FULL, PACKET_SERVER_BANNED >::AcceptClient(), PacketReader::AddPacket(), CanReadFromPacket(), Packet(), PrepareToSend(), ReadRawPacketSize(), ServerNetworkGameSocketHandler::ReceivePacket(), NetworkTCPSocketHandler::ReceivePacket(), NetworkUDPSocketHandler::ReceivePackets(), Send_string(), 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