# 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.
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 [SEND_MTU]
| The buffer of this packet.

## Private Attributes

NetworkSocketHandler * | cs

## 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 next, pos, and size.

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

Creates a packet to send.

Parameters:

  | type | of the packet to send

Definition at line 39 of file packet.cpp.

References buffer, next, pos, 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 137 of file packet.cpp.

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

Referenced by NetworkUDPSocketHandler::Send_NetworkGameInfo().

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

References buffer, and size.

Referenced by DEF_UDP_RECEIVE_COMMAND(), NetworkClientSocket::Send_Command(), and NetworkUDPSocketHandler::Send_NetworkGameInfo().

## Field Documentation

Packet* Packet::next

The next packet.

Used for queueing packets before sending.

Definition at line 36 of file packet.h.

Referenced by NetworkTCPSocketHandler::CloseConnection(), Packet(), PrepareToSend(), ReadRawPacketSize(), NetworkTCPSocketHandler::Send_Packet(), and NetworkTCPSocketHandler::Send_Packets().

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

Referenced by CanReadFromPacket(), Packet(), PrepareToSend(), ReadRawPacketSize(), NetworkUDPSocketHandler::ReceivePackets(), NetworkTCPSocketHandler::Recv_Packet(), NetworkTCPSocketHandler::Send_Packets(), Send_string(), and NetworkUDPSocketHandler::SendPacket().

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

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