Encodes a DestinationEntry as a 7-element MsgPack array: [timestamp, hops, expires, received_from, random_blobs, receiving_interface, announce_packet] random_blobs is itself an array of length-prefixed binary blobs, tail-trimmed to PERSIST_RANDOM_BLOBS to bound on-disk size. Adding new fields in the future is a matter of bumping the outer array length and appending; old decoders see the extra ele
| 29 | // is a matter of bumping the outer array length and appending; old decoders see |
| 30 | // the extra elements as trailing array members they can ignore. |
| 31 | /*static*/ std::vector<uint8_t> microStore::Codec<DestinationEntry>::encode(const DestinationEntry& entry) { |
| 32 | |
| 33 | // If invalid/empty entry then return empty |
| 34 | if (!entry) return {}; |
| 35 | |
| 36 | MsgPack::Packer p; |
| 37 | p.packArraySize(7); |
| 38 | |
| 39 | // timestamp |
| 40 | p.packFloat64(entry._timestamp); |
| 41 | |
| 42 | // hops |
| 43 | p.serialize(entry._hops); |
| 44 | |
| 45 | // expires |
| 46 | p.packFloat64(entry._expires); |
| 47 | |
| 48 | // received_from |
| 49 | p.packBinary(entry._received_from.data(), entry._received_from.size()); |
| 50 | |
| 51 | // random_blobs -- write only the tail-newest PERSIST_RANDOM_BLOBS entries |
| 52 | const size_t persist_cap = Type::Transport::PERSIST_RANDOM_BLOBS; |
| 53 | const size_t total_blobs = entry._random_blobs.size(); |
| 54 | const size_t persist_n = (total_blobs > persist_cap) ? persist_cap : total_blobs; |
| 55 | const size_t start_idx = total_blobs - persist_n; |
| 56 | p.packArraySize(persist_n); |
| 57 | for (size_t i = start_idx; i < total_blobs; i++) { |
| 58 | const auto& blob = entry._random_blobs[i]; |
| 59 | p.packBinary(blob.data(), blob.size()); |
| 60 | } |
| 61 | |
| 62 | // receiving_interface (hash only; the live Interface is re-bound on decode) |
| 63 | Bytes interface_hash(entry._receiving_interface.get_hash()); |
| 64 | p.packBinary(interface_hash.data(), interface_hash.size()); |
| 65 | |
| 66 | // announce_packet (raw bytes, including header) |
| 67 | const Bytes& raw = entry._announce_packet.raw(); |
| 68 | p.packBinary(raw.data(), raw.size()); |
| 69 | |
| 70 | return std::vector<uint8_t>(p.data(), p.data() + p.size()); |
| 71 | } |
| 72 | |
| 73 | /*static*/ bool microStore::Codec<DestinationEntry>::decode(const std::vector<uint8_t>& data, DestinationEntry& entry) |
| 74 | { |