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)
 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

Packetnext
 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

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.

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

Socket we're associated with.

Definition at line 50 of file packet.h.

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


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