66 // end of HeaderOffsets
99template<
typename MsgType>
114 : m_ptr(ptr), m_count(count) {}
121 auto result = MsgType::Decode(m_ptr, MsgType::kWireSize);
122 return result.value_or(MsgType{});
128 m_ptr += MsgType::kWireSize;
146 if (m_count == 0 && other.m_count == 0)
return true;
147 return m_ptr == other.m_ptr && m_count == other.m_count;
152 return !(*
this == other);
156 const uint8_t* m_ptr;
189 template<
typename MsgType>
202 template<
typename MsgType>
241template<
typename MsgType,
typename Storage = std::vector<MsgType>>
244 "Storage must be a valid packet storage container (vector-like interface)");
255template<
typename MsgType, std::
size_t Capacity = 64>
259template<
typename MsgType>
290 std::optional<PacketView>
view;
305uint32_t
ComputeCrc32(
const uint8_t* data, std::size_t length);
321template<
typename MsgType,
typename Storage>
323 std::size_t capacity, std::size_t& bytesWritten) {
331 if (capacity < required) {
344 for (
const auto& msg : packet.
messages) {
345 if (!msg.Encode(&output[offset], MsgType::kWireSize)) {
348 offset += MsgType::kWireSize;
355 bytesWritten = required;
371template<
typename MsgType,
typename Storage>
377 output.resize(required);
378 std::size_t bytesWritten = 0;
382 output.resize(bytesWritten);
396template<
typename MsgType>
408 auto msg = MsgType::Decode(ptr, MsgType::kWireSize);
413 ptr += MsgType::kWireSize;
430template<
typename MsgType,
typename Storage>
442 auto msg = MsgType::Decode(ptr, MsgType::kWireSize);
447 ptr += MsgType::kWireSize;
489template<
typename MsgType>
Forward iterator for zero-copy message access from packet payload.
std::ptrdiff_t difference_type
bool operator==(const MessageIterator &other) const
Equality comparison (end iterators compare equal).
MessageIterator & operator++()
Advance to the next message (pre-increment).
MessageIterator(const uint8_t *ptr, std::size_t count)
Construct iterator at a position in the payload.
std::forward_iterator_tag iterator_category
bool operator!=(const MessageIterator &other) const
Inequality comparison.
MsgType operator*() const
Decode and return the current message.
MessageIterator operator++(int)
Advance to the next message (post-increment).
constexpr uint8_t kProtocolMinor
Current protocol minor version.
constexpr std::size_t kMaxMessagesPerPacket
Maximum number of messages allowed in a single packet.
constexpr uint8_t kFlagClearQueue
Packet flag: Clear command queue before processing this packet.
constexpr uint8_t kProtocolMajor
Current protocol major version.
constexpr std::size_t kChecksumSize
Size of CRC32 checksum in bytes.
constexpr std::size_t kHeaderSize
Size of packet header in bytes.
void StoreU16(uint16_t v, uint8_t *p)
void StoreU32(uint32_t v, uint8_t *p)
std::optional< TypedPacket< MsgType, Storage > > DecodeTypedPacketAs(const PacketView &view)
Decode messages from a PacketView with custom storage type.
constexpr std::size_t kHeaderMsgCountIndex
std::optional< TypedPacket< MsgType > > DecodeTypedPacket(const PacketView &view)
Decode messages from a PacketView into a typed packet.
bool EncodeTypedPacket(const TypedPacket< MsgType, Storage > &packet, uint8_t *output, std::size_t capacity, std::size_t &bytesWritten)
Encode a typed packet to a pre-allocated buffer.
DecodeViewResult DecodePacketViewAs(const uint8_t *data, std::size_t length)
Type-safe packet view decode using compile-time message type.
constexpr bool IsValidPacketStorage_v
DecodeViewResult DecodePacketView(const uint8_t *data, std::size_t length)
Decode a packet using the global message type registry.
constexpr std::size_t kHeaderMsgTypeIndex
void ReserveIfPossible(Container &container, std::size_t capacity)
Helper to reserve capacity (no-op for static storage).
uint32_t ComputeCrc32(const uint8_t *data, std::size_t length)
Compute CRC32 checksum for data integrity verification.
constexpr uint8_t kProtocolMinorV3
PacketError
Error codes returned by packet decoding operations.
@ UnsupportedVersion
Protocol version mismatch.
@ HandshakeRequired
Connection requires handshake first.
@ ChecksumMismatch
CRC32 validation failed.
@ TooManyMessages
Message count exceeds kMaxMessagesPerPacket.
@ Truncated
Buffer ends before expected packet length.
@ SchemaMismatch
Client/server schema hash mismatch.
@ InvalidFloat
NaN or Inf detected in float field.
@ UnknownMessageType
Message type ID not in registry.
@ None
No error - packet decoded successfully.
@ TooSmall
Buffer too small to contain header.
DecodeViewResult DecodePacketViewWithSize(const uint8_t *data, std::size_t length, std::size_t wireSize)
Decode a packet with explicitly provided message wire size.
constexpr std::size_t kHeaderSizeV3
constexpr uint8_t kProtocolMajorV3
Fixed-capacity vector with stack allocation (no heap).
Result of decoding a packet from raw bytes.
std::optional< PacketView > view
Decoded view (valid if error == None)
std::size_t bytesConsumed
Bytes consumed from input buffer.
PacketError error
Error code if decode failed.
Zero-copy view into a decoded packet buffer.
MessageIterator< MsgType > end_as() const
Get end iterator for type-safe message iteration.
const uint8_t * GetPayload() const
Get raw pointer to payload for manual parsing.
PacketHeader header
Parsed header information.
std::size_t GetPayloadSize() const
Calculate total payload size in bytes.
const uint8_t * payloadStart
Pointer to first message in buffer.
MessageTypeId GetMessageType() const
Get the message type ID for this packet.
MessageIterator< MsgType > begin_as() const
Get type-safe iterator to the first message.
Generic packet containing messages of a specific type.