3#include <cereal/archives/portable_binary.hpp>
4#include <cereal/types/map.hpp>
5#include <cereal/types/vector.hpp>
6#include <cereal/types/string.hpp>
24 return static_cast<uint32_t
>(subsystem_type) << 16;
36 return static_cast<PacketID>(subsystem_type & 0xFFFF);
42 return (
static_cast<UniquePacketID>(subsystem_id) << 16) | packet_id;
88 template <
class Archive>
112 template <
typename PacketType>
139 std::ostringstream oss(std::ios::out | std::ios::binary);
140 cereal::PortableBinaryOutputArchive oa(oss);
141 oa << static_cast<const PacketType &>(*
this);
142 std::string
const &s = oss.str();
158 const auto char_view = buffer.
as<
char>();
159 const std::string s(char_view, buffer.size());
160 std::istringstream iss(s, std::ios::in | std::ios::binary);
161 cereal::PortableBinaryInputArchive ia(iss);
162 std::unique_ptr<PacketType> derived_packet = std::make_unique<PacketType>();
163 ia >> *derived_packet;
164 return derived_packet;
169 template <
typename T>
173 std::is_base_of_v<DerivedPacket<T>, T>;
174 std::same_as<std::decay_t<
decltype(T::static_unique_id)>, UniquePacketID>;
175 std::same_as<std::decay_t<
decltype(T::time_to_live)>,
float>;
A templated class representing a derived packet from the base Packet class.
static std::unique_ptr< Packet > deserialize(const ByteView buffer)
Deserialize a byte view into a unique pointer of the specified packet type.
void serialize_to_bytearray(ByteArray &buffer) const override
Serialize the derived packet data into a ByteArray.
DerivedPacket()
Constructor for the DerivedPacket class.
virtual ~DerivedPacket()=default
Virtual destructor for the DerivedPacket class.
Base class for all packets.
static Measurer< std::chrono::steady_clock > measurer
Measurer for packet timestamps.
float timestamp() const noexcept
Get the timestamp when the packet was received.
bool expired() const noexcept
Check if the packet has expired based on its time-to-live.
Packet(Packet &&)=default
void serialize(Archive &, const unsigned int)
Packet(const UniquePacketID type, const float time_to_live)
Constructor for Packet class.
const UniquePacketID type
Unique packet ID.
virtual const char * packet_name() const =0
virtual ~Packet()=default
Virtual destructor for Packet class.
Packet & operator=(Packet &&)=default
virtual void serialize_to_bytearray(ByteArray &buffer) const =0
Serialize the packet into a ByteArray.
friend class cereal::access
const float timestamp_
Timestamp when the packet was received.
const float time_to_live
Time-to-live for the packet.
float get_packet_time_alive() const noexcept
Get the time elapsed since the packet was received.
Concept to check if a given type satisfies the requirements of being a packet.
This is the main namespace for the Mal Packet Weaver library.
constexpr PacketSubsystemID UniquePacketIDToPacketSubsystemID(UniquePacketID subsystem_type) noexcept
Extract PacketSubsystemID from a UniquePacketID.
constexpr uint32_t PacketSubsystemIDToUint32(PacketSubsystemID subsystem_type) noexcept
Convert a PacketSubsystemID to a uint32_t value.
uint16_t PacketSubsystemID
Type alias for packet subsystem IDs.
constexpr UniquePacketID CreatePacketID(PacketSubsystemID subsystem_id, PacketID packet_id) noexcept
Create a UniquePacketID from subsystem and packet IDs.
std::function< std::unique_ptr< Packet >(const ByteView)> PacketDeserializeFunc
Type alias for packet deserialization function.
constexpr PacketID UniquePacketIDToPacketID(UniquePacketID subsystem_type) noexcept
Extract PacketID from a UniquePacketID.
uint32_t UniquePacketID
Unique identifier for a packet, combining subsystem and packet IDs.
uint16_t PacketID
Type alias for packet IDs.