Utility class to build blocks of data to transfer over the network. More...
#include <SFML/Network/Packet.hpp>
Public Member Functions | |
Packet ()=default | |
Default constructor. | |
virtual | ~Packet ()=default |
Virtual destructor. | |
Packet (const Packet &)=default | |
Copy constructor. | |
Packet & | operator= (const Packet &)=default |
Copy assignment. | |
Packet (Packet &&) noexcept=default | |
Move constructor. | |
Packet & | operator= (Packet &&) noexcept=default |
Move assignment. | |
void | append (const void *data, std::size_t sizeInBytes) |
Append data to the end of the packet. | |
std::size_t | getReadPosition () const |
Get the current reading position in the packet. | |
void | clear () |
Clear the packet. | |
const void * | 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 bool () const | |
Test the validity of the packet, for reading. | |
Packet & | operator>> (bool &data) |
Overload of operator>> to read data from the packet. | |
Packet & | operator>> (std::int8_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 & | operator>> (std::uint8_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 & | operator>> (std::int16_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 & | operator>> (std::uint16_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 & | operator>> (std::int32_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 & | operator>> (std::uint32_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 & | operator>> (std::int64_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 & | operator>> (std::uint64_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 & | 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 & | 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 & | 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 & | 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 & | 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 & | 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. | |
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. | |
Packet & | operator<< (bool data) |
Overload of operator<< to write data into the packet. | |
Packet & | operator<< (std::int8_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 & | operator<< (std::uint8_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 & | operator<< (std::int16_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 & | operator<< (std::uint16_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 & | operator<< (std::int32_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 & | operator<< (std::uint32_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 & | operator<< (std::int64_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 & | operator<< (std::uint64_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 & | 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 & | 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 & | 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 & | 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 & | 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 & | 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. | |
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. | |
Protected Member Functions | |
virtual const void * | onSend (std::size_t &size) |
Called before the packet is sent over the network. | |
virtual void | onReceive (const void *data, std::size_t size) |
Called after the packet is received over the network. | |
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 transferring data over the network:
- data is interpreted correctly according to the endianness
- the bounds of the packet are preserved (one send == one receive)
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 std::int32_t
, 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:
bool
- fixed-size integer types (
int[8|16|32]_t
,uint[8|16|32]_t
) - floating point numbers (
float
,double
) - string types (
char*
,wchar_t*
,std::string
,std::wstring
,sf::String
)
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:
- See also
sf::TcpSocket
,sf::UdpSocket
Definition at line 48 of file Packet.hpp.
Constructor & Destructor Documentation
◆ Packet() [1/3]
|
default |
Default constructor.
Creates an empty packet.
◆ ~Packet()
|
virtualdefault |
Virtual destructor.
◆ Packet() [2/3]
|
default |
Copy constructor.
◆ Packet() [3/3]
|
defaultnoexcept |
Move constructor.
Member Function Documentation
◆ append()
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
-
getReadPosition
◆ clear()
void sf::Packet::clear | ( | ) |
◆ endOfPacket()
|
nodiscard |
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
◆ getData()
|
nodiscard |
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 a nullptr
if the packet is empty.
- Returns
- Pointer to the data
- See also
getDataSize
◆ getDataSize()
|
nodiscard |
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
◆ getReadPosition()
|
nodiscard |
Get the current reading position in the packet.
The next read operation will read data from this position
- Returns
- The byte offset of the current read position
- See also
append
◆ onReceive()
|
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.
- Parameters
-
data Pointer to the received bytes size Number of bytes
- See also
onSend
◆ onSend()
|
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.
- Parameters
-
size Variable to fill with the size of data to send
- Returns
- Pointer to the array of bytes to send
- See also
onReceive
◆ operator bool()
|
explicit |
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.
- Returns
true
if last data extraction from packet was successful
- See also
endOfPacket
◆ operator<<() [1/16]
Packet & sf::Packet::operator<< | ( | bool | data | ) |
Overload of operator<<
to write data into the packet.
◆ operator<<() [2/16]
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.
◆ operator<<() [3/16]
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.
◆ operator<<() [4/16]
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.
◆ operator<<() [5/16]
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
◆ operator<<() [6/16]
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.
◆ operator<<() [7/16]
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.
◆ operator<<() [8/16]
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.
◆ operator<<() [9/16]
Packet & sf::Packet::operator<< | ( | std::int16_t | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
◆ operator<<() [10/16]
Packet & sf::Packet::operator<< | ( | std::int32_t | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
◆ operator<<() [11/16]
Packet & sf::Packet::operator<< | ( | std::int64_t | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
◆ operator<<() [12/16]
Packet & sf::Packet::operator<< | ( | std::int8_t | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
◆ operator<<() [13/16]
Packet & sf::Packet::operator<< | ( | std::uint16_t | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
◆ operator<<() [14/16]
Packet & sf::Packet::operator<< | ( | std::uint32_t | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
◆ operator<<() [15/16]
Packet & sf::Packet::operator<< | ( | std::uint64_t | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
◆ operator<<() [16/16]
Packet & sf::Packet::operator<< | ( | std::uint8_t | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
◆ operator=() [1/2]
◆ operator=() [2/2]
◆ operator>>() [1/16]
Packet & sf::Packet::operator>> | ( | bool & | data | ) |
Overload of operator>>
to read data from the packet.
◆ operator>>() [2/16]
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.
◆ operator>>() [3/16]
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.
◆ operator>>() [4/16]
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.
◆ operator>>() [5/16]
Packet & sf::Packet::operator>> | ( | std::int16_t & | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
◆ operator>>() [6/16]
Packet & sf::Packet::operator>> | ( | std::int32_t & | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
◆ operator>>() [7/16]
Packet & sf::Packet::operator>> | ( | std::int64_t & | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
◆ operator>>() [8/16]
Packet & sf::Packet::operator>> | ( | std::int8_t & | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
◆ operator>>() [9/16]
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.
◆ operator>>() [10/16]
Packet & sf::Packet::operator>> | ( | std::uint16_t & | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
◆ operator>>() [11/16]
Packet & sf::Packet::operator>> | ( | std::uint32_t & | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
◆ operator>>() [12/16]
Packet & sf::Packet::operator>> | ( | std::uint64_t & | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
◆ operator>>() [13/16]
Packet & sf::Packet::operator>> | ( | std::uint8_t & | data | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
◆ operator>>() [14/16]
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.
◆ operator>>() [15/16]
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
◆ operator>>() [16/16]
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.
Friends And Related Symbol Documentation
◆ TcpSocket
|
friend |
Definition at line 368 of file Packet.hpp.
◆ UdpSocket
|
friend |
Definition at line 369 of file Packet.hpp.
The documentation for this class was generated from the following file: