sf::Packet Class Reference
[Network module]

Utility class to build blocks of data to transfer over the network. More...

#include <Packet.hpp>

List of all members.

Public Member Functions

 Packet ()
 Default constructor.
virtual ~Packet ()
 Virtual destructor.
void Append (const void *data, std::size_t sizeInBytes)
 Append data to the end of the packet.
void Clear ()
 Clear the packet.
const char * GetData () const
 Get a pointer to the data contained in the packet.
std::size_t GetDataSize () const
 Get the size of the data contained in the packet.
bool EndOfPacket () const
 Tell if the reading position has reached the end of the packet.
 operator BoolType () const
 Test the validity of the packet, for reading.
Packetoperator>> (bool &data)
 Overloads of operator >> to read data from the packet.
Packetoperator>> (Int8 &data)
Packetoperator>> (Uint8 &data)
Packetoperator>> (Int16 &data)
Packetoperator>> (Uint16 &data)
Packetoperator>> (Int32 &data)
Packetoperator>> (Uint32 &data)
Packetoperator>> (float &data)
Packetoperator>> (double &data)
Packetoperator>> (char *data)
Packetoperator>> (std::string &data)
Packetoperator>> (wchar_t *data)
Packetoperator>> (std::wstring &data)
Packetoperator>> (String &data)
Packetoperator<< (bool data)
 Overloads of operator << to write data into the packet.
Packetoperator<< (Int8 data)
Packetoperator<< (Uint8 data)
Packetoperator<< (Int16 data)
Packetoperator<< (Uint16 data)
Packetoperator<< (Int32 data)
Packetoperator<< (Uint32 data)
Packetoperator<< (float data)
Packetoperator<< (double data)
Packetoperator<< (const char *data)
Packetoperator<< (const std::string &data)
Packetoperator<< (const wchar_t *data)
Packetoperator<< (const std::wstring &data)
Packetoperator<< (const String &data)

Friends

class TcpSocket
class UdpSocket

Detailed Description

Utility class to build blocks of data to transfer over the network.

Packets provide a safe and easy way to serialize data, in order to send it over the network using sockets (sf::TcpSocket, sf::UdpSocket).

Packets solve 2 fundamental problems that arise when transfering data over the network:

The sf::Packet class provides both input and output modes. It is designed to follow the behaviour of standard C++ streams, using operators >> and << to extract and insert data.

It is recommended to use only fixed-size types (like sf::Int32, etc.), to avoid possible differences between the sender and the receiver. Indeed, the native C++ types may have different sizes on two platforms and your data may be corrupted if that happens.

Usage example:

 sf::Uint32 x = 24;
 std::string s = "hello";
 double d = 5.89;

 // Group the variables to send into a packet
 sf::Packet packet;
 packet << x << s << d;

 // Send it over the network (socket is a valid sf::TcpSocket)
 socket.Send(packet);

 -----------------------------------------------------------------

 // Receive the packet at the other end
 sf::Packet packet;
 socket.Receive(packet);

 // Extract the variables contained in the packet
 sf::Uint32 x;
 std::string s;
 double d;
 if (packet >> x >> s >> d)
 {
     // Data extracted successfully...
 }

Packets have built-in operator >> and << overloads for standard types:

Like standard streams, it is also possible to define your own overloads of operators >> and << in order to handle your custom types.

 struct MyStruct
 {
     float       number;
     sf::Int8    integer;
     std::string str;
 };

 sf::Packet& operator <<(sf::Packet& packet, const MyStruct& m)
 {
     return packet << m.number << m.integer << m.str;
 }

 sf::Packet& operator >>(sf::Packet& packet, MyStruct& m)
 {
     return packet >> m.number >> m.integer >> m.str;
 }

Packets also provide an extra feature that allows to apply custom transformations to the data before it is sent, and after it is received. This is typically used to handle automatic compression or encryption of the data. This is achieved by inheriting from sf::Packet, and overriding the OnSend and OnReceive functions.

Here is an example:

 class ZipPacket : public sf::Packet
 {
     virtual const char* OnSend(std::size_t& size)
     {
         const char* srcData = GetData();
         std::size_t srcSize = GetDataSize();

         return MySuperZipFunction(srcData, srcSize, &size);
     }

     virtual void OnReceive(const char* data, std::size_t size)
     {
         std::size_t dstSize;
         const char* dstData = MySuperUnzipFunction(data, size, &dstSize);

         Append(dstData, dstSize);
     }
 };

 // Use like regular packets:
 ZipPacket packet;
 packet << x << s << d;
 ...
See also:
sf::TcpSocket, sf::UdpSocket

Definition at line 47 of file Packet.hpp.


Constructor & Destructor Documentation

sf::Packet::Packet (  ) 

Default constructor.

Creates an empty packet.

virtual sf::Packet::~Packet (  )  [virtual]

Virtual destructor.


Member Function Documentation

void sf::Packet::Append ( const void *  data,
std::size_t  sizeInBytes 
)

Append data to the end of the packet.

Parameters:
data Pointer to the sequence of bytes to append
sizeInBytes Number of bytes to append
See also:
Clear
void sf::Packet::Clear (  ) 

Clear the packet.

After calling Clear, the packet is empty.

See also:
Append
bool sf::Packet::EndOfPacket (  )  const

Tell if the reading position has reached the end of the packet.

This function is useful to know if there is some data left to be read, without actually reading it.

Returns:
True if all data was read, false otherwise
See also:
operator bool
const char* sf::Packet::GetData (  )  const

Get a pointer to the data contained in the packet.

Warning: the returned pointer may become invalid after you append data to the packet, therefore it should never be stored. The return pointer is NULL if the packet is empty.

Returns:
Pointer to the data
See also:
GetDataSize
std::size_t sf::Packet::GetDataSize (  )  const

Get the size of the data contained in the packet.

This function returns the number of bytes pointed to by what GetData returns.

Returns:
Data size, in bytes
See also:
GetData
sf::Packet::operator BoolType (  )  const

Test the validity of the packet, for reading.

This operator allows to test the packet as a boolean variable, to check if a reading operation was successful.

A packet will be in an invalid state if it has no more data to read.

This behaviour is the same as standard C++ streams.

Usage example:

 float x;
 packet >> x;
 if (packet)
 {
    // ok, x was extracted successfully
 }

 // -- or --

 float x;
 if (packet >> x)
 {
    // ok, x was extracted successfully
 }

Don't focus on the return type, it's equivalent to bool but it disallows unwanted implicit conversions to integer or pointer types.

Returns:
True if last data extraction from packet was successful
See also:
EndOfPacket
Packet& sf::Packet::operator<< ( bool  data  ) 

Overloads of operator << to write data into the packet.

Packet& sf::Packet::operator>> ( bool &  data  ) 

Overloads of operator >> to read data from the packet.


The documentation for this class was generated from the following file: