Simple Event Driven Network Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Public Types | Public Member Functions | Friends | List of all members
SedNL::Packet Class Reference

A packet. More...

#include <Packet.hpp>

Public Types

enum  Type {
  Type::Unknown = 0x00, Type::Int8 = 0x01, Type::Int16 = 0x02, Type::Int32 = 0x03,
  Type::Int64 = 0x04, Type::UInt8 = 0x05, Type::UInt16 = 0x06, Type::UInt32 = 0x07,
  Type::UInt64 = 0x08, Type::Float = 0x10, Type::Double = 0x11, Type::String = 0x20,
  Type::Object = 0x40, Type::ArrayInt8 = 0x81, Type::ArrayInt16 = 0x82, Type::ArrayInt32 = 0x83,
  Type::ArrayInt64 = 0x84, Type::ArrayUInt8 = 0x85, Type::ArrayUInt16 = 0x86, Type::ArrayUInt32 = 0x87,
  Type::ArrayUInt64 = 0x88, Type::ArrayFloat = 0x90, Type::ArrayDouble = 0x91
 Low level binary representation of types. More...

Public Member Functions

 Packet ()
 Create an empty packet.
void swap (Packet &packet) noexcept
template<typename T >
Packetoperator<< (T dt)
 Write dt into the packet. More...
Packetoperator<< (const std::vector< char > &dt)
Packetoperator<< (const std::vector< Int8 > &dt)
Packetoperator<< (const std::vector< Int16 > &dt)
Packetoperator<< (const std::vector< Int32 > &dt)
Packetoperator<< (const std::vector< Int64 > &dt)
Packetoperator<< (const std::vector< UInt8 > &dt)
Packetoperator<< (const std::vector< UInt16 > &dt)
Packetoperator<< (const std::vector< UInt32 > &dt)
Packetoperator<< (const std::vector< UInt64 > &dt)
Packetoperator<< (const std::vector< float > &dt)
Packetoperator<< (const std::vector< double > &dt)
const ByteArray & get_data () const
 Return the computed data as a binary array. More...
bool is_valid () noexcept
 Tell if it contains valid data. More...


class PacketReader
class RingBuf
template<typename... Args>
void write_as_object (Packet &packet, Args &...args)
 Write all the argument as an object of length number_of_args(args). More...

Detailed Description

A packet.

A Packet contain all the data you want to send / you received as a binary string. The >> and << operator allow you to write to or read from a packet.

To allow reading multiple time the same packet, we use a PacketReader, which is no more than an index telling where you are in the binary string. That's why PacketReader are really cheap object with a strong API, allowing type safe reading.

Packet are used to build events, and events are what you are allowed to send through a Connection object (TCPClient is a Connection).

Here is some use cases.

//Create an empty packet
Packet packet;
//Write some stuff
packet << "Hello world";
packet << 42;
packet << 1.f << 2.f << 3.f << 4.f << 5.f;
//You can also use this syntax :
write_to_packet(packet, 3.0, 2.0, 1.0);
//You can even write array :
std::vector<int> ints = {1, 2, 3, 4, 5};
packet << ints;
//For serialised object, see SEDNL_SERIALIZABLE in Serializer.hpp.
//Then you can read from it.
PackerReader r1(packet);
PacketReader r2(packet);
std::string str1, str2;
r1 >> str1;
r2 >> str2;
// both strings now contain "Hello world".
int v;
r1 >> v; // v now contains 42
//Read all 32 bits floating number, until encoutering something else.
while (r1.next_type() == Packet::Type::Float)
float v;
r1 >> v;
std::cout << "32 bits floating number : " << v << std::endl;
//Because readers do not modify packets, we can now
//build an event from packet.
Event event("my_stuff", packet);

Definition at line 41 of file Packet.hpp.

Member Enumeration Documentation

enum SedNL::Packet::Type

Low level binary representation of types.


Reserved type.


8 bits signed integer.


16 bits signed integer.


32 bits signed integer.


64 bits signed integer.


8 bits unsigned integer.


16 bits unsigned integer.


32 bits unsigned integer.


64 bits unsigned integer.


32 bits floating number.


64 bits floating number.


A C string (from a const char* or std::string::c_str())


An user defined type (serialized object).


An array of 8 bits signed integer.


An array of 16 bits signed integer.


An array of 32 bits signed integer.


An array of 64 bits signed integer.


An array of 8 bits unsigned integer.


An array of 16 bits unsigned integer.


An array of 32 bits unsigned integer.


An array of 64 bits unsigned integer.


An array of 32 bits floating number.


An array of 64 bits floating number.

Definition at line 46 of file Packet.hpp.

Member Function Documentation

const ByteArray& SedNL::Packet::get_data ( ) const

Return the computed data as a binary array.

It's a reference to the data's internal buffer. It contains all writen data formated as Packet::Type followed by the value. Its what is send through the network.

The handle become invalid when the Packet is destructed.

Handle to internal buffer.
bool SedNL::Packet::is_valid ( )

Tell if it contains valid data.

For implementation purpose.

Do not call this function, all the packets you'll receive or send are valid.

O(n) where n is the data length.

True if the content is a valid packet, false otherwise.
template<typename T >
Packet & SedNL::Packet::operator<< ( dt)

Write dt into the packet.

If you call << on types like [unsigned] char, [unsigned] short, [unsigned] long, the matching [U]IntN operator will be called.

In case of char, signed char will be stored (but it may change in futur implementations). Both unsigned char and signed char can be read as a char.

In case of type = float, double the library assume that the system have 32, 64 bits types. Otherwise, (bigger types) it may result in wrong memory access. You don't have to worry for most of the systems where this library would actualy compile.

If this operation throw a std::bad_alloc, the packet is now in an invalid state and should't be used anymore.

You can give it serialisable object (see Serializer.hpp).

To read data from a packet, see PacketReader and PackerReader::operator>>().

A reference to this.

Definition at line 133 of file Packet.ipp.

void SedNL::Packet::swap ( Packet packet)

Swap two packets.

Swap the content of packet with the content of the current packet.

[in,out]packetPacket to swap.

Definition at line 29 of file Packet.ipp.

Friends And Related Function Documentation

template<typename... Args>
void write_as_object ( Packet packet,
Args &...  args 

Write all the argument as an object of length number_of_args(args).

This produce the same behavior as serialising an object containing the values args with Packet::operator<<().

Definition at line 110 of file Packet.ipp.

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