Class Packet#

Class Documentation#

class Packet#

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

Public Functions

constexpr Packet() noexcept = default#

Constructor. Creates an empty packet.

virtual constexpr ~Packet() = default#

Destructor.

constexpr Packet(const Packet&) noexcept = default#

Copy constructor.

constexpr Packet &operator=(const Packet&) noexcept = default#

Copy assignment operator.

constexpr Packet(Packet&&) noexcept = default#

Move constructor.

constexpr Packet &operator=(Packet&&) noexcept = default#

Move assignment operator.

constexpr void Append(const void *data, usize 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.

constexpr usize GetReadPosition() const noexcept#

Get the current reading position in the packet. The next read operation will read data from this position.

Returns:

The bytes offset of the current read position.

constexpr void Clear() noexcept#

Clear the packet. After calling Clear, the packet is empty.

constexpr const void *GetData() const#

Get a pointer to the data contained in the packet.

The return pointer is nullptr if the packet is empty.

Warning

The returned pointer may become invalid after you append data to the packet, therefore it should never be stored.

Returns:

Pointer to the data.

constexpr usize GetDataSize() const noexcept#

Get the size of the data contained in the packet.

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

Returns:

Data size, in bytes.

constexpr bool EndOfPacket() const noexcept#

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.

explicit constexpr operator bool() const noexcept#

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.

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.

constexpr Packet &operator>>(bool &data)#

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

constexpr Packet &operator>>(i8 &data)#

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

constexpr Packet &operator>>(u8 &data)#

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

constexpr Packet &operator>>(i16 &data)#

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

constexpr Packet &operator>>(u16 &data)#

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

constexpr Packet &operator>>(i32 &data)#

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

constexpr Packet &operator>>(u32 &data)#

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

constexpr Packet &operator>>(i64 &data)#

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

constexpr Packet &operator>>(u64 &data)#

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

constexpr Packet &operator>>(f32 &data)#

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

constexpr Packet &operator>>(f64 &data)#

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

constexpr Packet &operator>>(char *data)#

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

constexpr Packet &operator>>(std::string &data)#

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

constexpr Packet &operator>>(wchar_t *data)#

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

constexpr Packet &operator>>(std::wstring &data)#

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

constexpr Packet &operator<<(bool data)#

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

constexpr Packet &operator<<(i8 data)#

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

constexpr Packet &operator<<(u8 data)#

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

constexpr Packet &operator<<(i16 data)#

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

constexpr Packet &operator<<(u16 data)#

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

constexpr Packet &operator<<(i32 data)#

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

constexpr Packet &operator<<(u32 data)#

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

constexpr Packet &operator<<(i64 data)#

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

constexpr Packet &operator<<(u64 data)#

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

constexpr Packet &operator<<(f32 data)#

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

constexpr Packet &operator<<(f64 data)#

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

constexpr Packet &operator<<(std::string_view data)#

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

constexpr Packet &operator<<(std::wstring_view data)#

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

constexpr bool operator==(const Packet &right) const noexcept = delete#

Disallow comparisons between packets.

constexpr bool operator!=(const Packet &right) const noexcept = delete#

Disallow comparisons between packets.

Protected Functions

virtual const void *OnSend(usize &size)#

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.

virtual constexpr void OnReceive(const void *data, usize size)#

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.

Friends

friend class TCPSocket
friend class UDPSocket
friend class TCPSocketIPv6
friend class UDPSocketIPv6