AntHocNet 2.6.1
Paper-faithful ant-colony ad hoc routing: the shared core and its adapters
Loading...
Searching...
No Matches
ant_message_codec.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT OR GPL-2.0-only
2// Copyright (C) 2026 Daniel Henrique Joppi
3
4/**
5 * Canonical, simulator-agnostic wire format for an AntMessage.
6 *
7 * Both adapters reuse this so an NS-2 and an NS-3 node would interpret the
8 * same bytes identically (and so the round-trip is unit-testable without a
9 * simulator). Encoding is little-endian, length-prefixed for the variable
10 * arrays. The NS-2 PacketHeaderClass and the NS-3 ns3::Header both delegate
11 * their (de)serialization to these functions.
12 */
13#ifndef ANTHOCNET_CORE_ANT_MESSAGE_CODEC_H
14#define ANTHOCNET_CORE_ANT_MESSAGE_CODEC_H
15
16#include <cstddef>
17#include <cstdint>
18#include <vector>
19
21
22namespace anthocnet {
23namespace core {
24namespace codec {
25
26/// On-wire format version, written at offset 0 of every frame and checked
27/// before any other field (ADR-0006). Bump on any layout/semantic change.
28/// Both adapter headers must serialize this same value.
29/// 0x01: version byte + bounds/enum enforcement (item 12).
30/// 0x02: + AntMessage::broadcastBudget field, AntType::LinkFail (item 05).
31/// 0x03: backward-ant slim — prevHop/hops/pathTime/pheromone off the wire,
32/// reconstructed locally from `history` (item 02, ADR-0009).
33/// 0x04: + optional trailing kinematics section on hellos (#573, ADR-0025):
34/// tag kKinematicsTag, then x y z vx vy vz as float32. Absent unless
35/// Config::shareKinematics is on, so default frames keep their length.
36constexpr std::uint8_t kWireVersion = 0x04;
37
38/// First byte of a hello's kinematics section ('K').
39constexpr std::uint8_t kKinematicsTag = 0x4B;
40/// Tag + six float32 values.
41constexpr std::size_t kKinematicsSize = 1 + 6 * 4;
42
43/// Protocol bounds enforced on *decode* of untrusted input, mirroring the
44/// Config defaults so the codec stays standalone (golden rule #5). A frame
45/// declaring more elements than these is rejected, not just buffer-checked.
46constexpr std::uint16_t kMaxVisitedOnWire = 100; ///< == Config::maxPathLength
47constexpr std::uint16_t kMaxHistoryOnWire = 100; ///< retraced path ≤ path length
48constexpr std::uint16_t kMaxHelloOnWire = 64; ///< generous bound on adverts
49
50/// Number of bytes serialize() will produce for `msg`.
51std::size_t serializedSize(const AntMessage& msg);
52
53/// Encode `msg` into a fresh byte buffer.
54std::vector<std::uint8_t> serialize(const AntMessage& msg);
55
56/// Encode `msg` into `out` (resized to serializedSize(msg)).
57void serialize(const AntMessage& msg, std::vector<std::uint8_t>& out);
58
59/// Decode `bytes` into `msg`. Returns false if the buffer is truncated or
60/// internally inconsistent; on failure `msg` is left in an unspecified state.
61bool deserialize(const std::uint8_t* bytes, std::size_t len, AntMessage& msg);
62
63inline bool deserialize(const std::vector<std::uint8_t>& bytes, AntMessage& msg) {
64 return deserialize(bytes.data(), bytes.size(), msg);
65}
66
67} // namespace codec
68} // namespace core
69} // namespace anthocnet
70
71#endif // ANTHOCNET_CORE_ANT_MESSAGE_CODEC_H
std::size_t serializedSize(const AntMessage &msg)
Number of bytes serialize() will produce for msg.
std::vector< std::uint8_t > serialize(const AntMessage &msg)
Encode msg into a fresh byte buffer.
bool deserialize(const std::uint8_t *bytes, std::size_t len, AntMessage &msg)
Decode bytes into msg.
constexpr std::size_t kKinematicsSize
Tag + six float32 values.
constexpr std::uint8_t kKinematicsTag
First byte of a hello's kinematics section ('K').
constexpr std::uint8_t kWireVersion
On-wire format version, written at offset 0 of every frame and checked before any other field (ADR-00...
constexpr std::uint16_t kMaxHelloOnWire
generous bound on adverts
constexpr std::uint16_t kMaxHistoryOnWire
retraced path ≤ path length
constexpr std::uint16_t kMaxVisitedOnWire
Protocol bounds enforced on decode of untrusted input, mirroring the Config defaults so the codec sta...
AntHistoryTracker: (src, seqNum) duplicate detection.
Definition ant_history.h:24
Complete, copyable description of an ant packet.
Definition ant_message.h:48