/// Types for the BLK_BOX NOW pub protocol #ifndef _BBNOW_PUB_H #define _BBNOW_PUB_H #include #include #include #include #include "bb_expantions.hpp" /////////////////////////////////////// // (1) DISCOVERY // ///////////////////////////////////// /// The data associated with the `DISCOVERY` type. struct BlkBoxNowPubDiscoveryData { BlkBoxExpantion expantion_type{}; }; /////////////////////////////////////// // PUB PACKET // ///////////////////////////////////// /// The packet type for `BBNowPubPacket`s. enum class BBNowPubPacketType: uint16_t { NONE = 0, DISCOVERY = 1, MAX = 1, }; /// The data field for the packet union BlkBoxNowPubPacketData { BlkBoxNowPubDiscoveryData discovery; }; struct BBNowPubPacket { const static uint32_t PUB_MAGIC_NUMBER = 0x6262'6E70; // DISCOVERY packet size const static size_t DISCOVERY_PACKET_SIZE = sizeof(PUB_MAGIC_NUMBER) + sizeof(BBNowPubPacketType) + sizeof(BlkBoxNowPubDiscoveryData); static_assert(DISCOVERY_PACKET_SIZE == 8, "Discovery packet should be 8 bytes"); /// The type of packet this is. BBNowPubPacketType packet_type{}; /// The data held by the packet. BlkBoxNowPubPacketData packet_data{}; /// The size of the serialized packet (in bytes). size_t serialize_size(); /// Serializes the packet to a vector. std::vector serialize(); /// Serializes the packet into the given vector. /// /// Returns the number of bytes written into the buffer. size_t serialize_into(std::vector& buf); /// Deserializes a packet from the given bytes. static std::optional deserialize(std::span bytes); /// Constructs an empty packet. BBNowPubPacket() = default; /// Constructs a new `Discovery` type packet with the given data. static BBNowPubPacket new_discovery(BlkBoxNowPubDiscoveryData data); }; #endif /* _BBNOW_PUB_H */