- sf
- Packet
Utility class to build blocks of data to transfer over the network. More...
#include <Packet.hpp>
Public Member Functions | |
Packet () | |
Default constructor. More... | |
virtual | ~Packet () |
Virtual destructor. More... | |
void | append (const void *data, std::size_t sizeInBytes) |
Append data to the end of the packet. More... | |
void | clear () |
Clear the packet. More... | |
const void * | getData () const |
Get a pointer to the data contained in the packet. More... | |
std::size_t | getDataSize () const |
Get the size of the data contained in the packet. More... | |
bool | endOfPacket () const |
Tell if the reading position has reached the end of the packet. More... | |
operator BoolType () const | |
Test the validity of the packet, for reading. More... | |
Packet & | operator>> (bool &data) |
Overload of operator >> to read data from the packet. More... | |
Packet & | operator>> (Int8 &data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator>> (Uint8 &data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator>> (Int16 &data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator>> (Uint16 &data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator>> (Int32 &data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator>> (Uint32 &data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator>> (Int64 &data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator>> (Uint64 &data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator>> (float &data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator>> (double &data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator>> (char *data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator>> (std::string &data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator>> (wchar_t *data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator>> (std::wstring &data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator>> (String &data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator<< (bool data) |
Overload of operator << to write data into the packet. More... | |
Packet & | operator<< (Int8 data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator<< (Uint8 data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator<< (Int16 data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator<< (Uint16 data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator<< (Int32 data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator<< (Uint32 data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator<< (Int64 data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator<< (Uint64 data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator<< (float data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator<< (double data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator<< (const char *data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator<< (const std::string &data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator<< (const wchar_t *data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator<< (const std::wstring &data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Packet & | operator<< (const String &data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Protected Member Functions | |
virtual const void * | onSend (std::size_t &size) |
Called before the packet is sent over the network. More... | |
virtual void | onReceive (const void *data, std::size_t size) |
Called after the packet is received over the network. More... | |
Friends | |
class | TcpSocket |
class | UdpSocket |
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 transferring data over the network:
The sf::Packet class provides both input and output modes. It is designed to follow the behavior 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:
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.
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:
Definition at line 47 of file Packet.hpp.
sf::Packet::Packet | ( | ) |
Default constructor.
Creates an empty packet.
|
virtual |
Virtual destructor.
void sf::Packet::append | ( | const void * | data, |
std::size_t | sizeInBytes | ||
) |
Append data to the end of the packet.
data | Pointer to the sequence of bytes to append |
sizeInBytes | Number of bytes to append |
void sf::Packet::clear | ( | ) |
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.
const void* 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.
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.
|
protectedvirtual |
Called after the packet is received over the network.
This function can be defined by derived classes to transform the data after it is received; this can be used for decompression, decryption, etc. The function receives a pointer to the received data, and must fill the packet with the transformed bytes. The default implementation fills the packet directly without transforming the data.
data | Pointer to the received bytes |
size | Number of bytes |
|
protectedvirtual |
Called before the packet is sent over the network.
This function can be defined by derived classes to transform the data before it is sent; this can be used for compression, encryption, etc. The function must return a pointer to the modified data, as well as the number of bytes pointed. The default implementation provides the packet's data without transforming it.
size | Variable to fill with the size of data to send |
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 behavior is the same as standard C++ streams.
Usage example:
Don't focus on the return type, it's equivalent to bool but it disallows unwanted implicit conversions to integer or pointer types.
Packet& sf::Packet::operator<< | ( | bool | data | ) |
Overload of operator << to write data into the packet.
Packet& sf::Packet::operator<< | ( | Int8 | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator<< | ( | Uint8 | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator<< | ( | Int16 | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator<< | ( | Uint16 | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator<< | ( | Int32 | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator<< | ( | Uint32 | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator<< | ( | Int64 | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator<< | ( | Uint64 | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator<< | ( | float | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator<< | ( | double | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator<< | ( | const char * | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator<< | ( | const std::string & | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator<< | ( | const wchar_t * | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator<< | ( | const std::wstring & | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator>> | ( | bool & | data | ) |
Overload of operator >> to read data from the packet.
Packet& sf::Packet::operator>> | ( | Int8 & | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator>> | ( | Uint8 & | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator>> | ( | Int16 & | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator>> | ( | Uint16 & | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator>> | ( | Int32 & | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator>> | ( | Uint32 & | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator>> | ( | Int64 & | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator>> | ( | Uint64 & | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator>> | ( | float & | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator>> | ( | double & | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator>> | ( | char * | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator>> | ( | std::string & | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator>> | ( | wchar_t * | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Packet& sf::Packet::operator>> | ( | std::wstring & | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.