Skip to main content

Packet builder

The packet builder is an easy developer friendly tools to build packets for the network SDK.
Here is a documentation on how to use the packet builder.

Get started​

You can create one packet builder instance in your project and use it multiple times to build packets.
Here is a simple example on how to create a packet with the packet builder.

main.cpp
int main() {
// Create a packet builder instance
PacketBuilder packetBuilder;
unsigned int helloMessageType = 1;
char[13] helloMessage = "Hello World!";

// Create a packet
Packet<char[13]> packet = packetBuilder
.SetPayloadType(PayloadType::kCustom)
.SetMessageType(helloMessageType)
.Build<char[13]>(helloMessage);

return 0;
}
note

In the example we specified the payload type as kCustom but it's a default value so you can skip it.

Builder steps methods​

SetPayloadType​

Set the payload type of the packet.
Be sure to respect the maximum size of the payload type. (An exception will be thrown if the payload type is too big)

PacketBuilder &SetPayloadType(PayloadType payloadType);
main.cpp
PacketBuilder packetBuilder;
packetBuilder.SetPayloadType(PayloadType::kCustom);

SetTurn​

Set the turn of the packet.
You must handle at your own a potential system of loop to avoid too big turn number.
The builder will handle header flags for you.

PacketBuilder &SetTurn(std::uint16_t turn);
main.cpp
PacketBuilder packetBuilder;
packetBuilder.SetTurn(1);

SetMessageType​

Set the message type of the packet.
It's technically a mandatory field when you construct your packet. (The default value is 0 but it's a good practice to set it).

PacketBuilder &SetMessageType(std::uint8_t messageType);
main.cpp
PacketBuilder packetBuilder;
packetBuilder.SetMessageType(1);

Builder build method​

Build a packet​

Build a packet with the specified payload.
You must specify the type of the payload you want to build in the template.
Your payload type size must respect the maximum size of the payload lenght. (An exception will be thrown if the payload type is too big)

template<typename T>
Packet<T> Build(T payload);
main.cpp
PacketBuilder packetBuilder;

Packet<std::uint8_t> packet = packetBuilder
.SetPayloadType(PayloadType::kUint8)
.SetMessageType(0)
.Build<std::uint8_t>(100);
info

The message ID is automatically incremented by the builder for each packet you build.
If you want a special ID incrementation for each user, you must for the moment have a packet builder instance for each user.

Build multiples packets​

The packet builder provide you a way to build multiples packets at once.
You must use the same build method but with a std::vector of payloads.

  template<typename T>
std::vector<Packet<T>> Build(std::vector<T> payloads);
main.cpp
PacketBuilder packetBuilder;

std::vector<std::uint8_t> payloads = {1, 2, 3, 4, 5};
std::vector<Packet<std::uint8_t>> packets = packetBuilder
.SetPayloadType(PayloadType::kUint8)
.SetMessageType(0)
.Build<std::uint8_t>(payloads);

Please, respect the maximum size of a multi packet build. You can refer to the maximum size of an offset.
The offset system will be automatically handled by the builder.
The message ID will be the same for all packets in the vector (As specified in the protocol).

warning

A vector must have at least one element.
All the packets in the vector must have the same payload type.