peer_ledger.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #pragma once
  2. #include <cstddef>
  3. #include <cstdint>
  4. #include <span>
  5. namespace sunrise::middleware::bap::activity_message::peer_ledger {
  6. /** The client returns one peer reservation it no longer needs. */
  7. inline constexpr std::uint32_t kReleaseReservationType = 14;
  8. /** The client reports that one peer is leaving. */
  9. inline constexpr std::uint32_t kPeerLeaveType = 15;
  10. /** The client reports that it cannot reach one peer. Both sides can send this type. */
  11. inline constexpr std::uint32_t kConnectivityFailureType = 37;
  12. /** The client proposes a host migration. Nothing here acts on one. */
  13. inline constexpr std::uint32_t kSpeculativeMigrationType = 48;
  14. /** Release carries two scalars and a peer key in exactly 16 bytes. */
  15. inline constexpr std::size_t kReleaseSize = 16;
  16. /** Leave carries the release fields and one trailing scalar in exactly 20 bytes. */
  17. inline constexpr std::size_t kLeaveSize = 20;
  18. /** Connectivity failure is 66 meaningful bits, padded to 9 bytes. */
  19. inline constexpr std::size_t kConnectivityFailureSize = 9;
  20. /** Migration carries a biased value and a peer key in exactly 12 bytes. */
  21. inline constexpr std::size_t kMigrationSize = 12;
  22. /** The reason selector is two bits wide. Its schema bias is not recovered, so it stays raw. */
  23. inline constexpr std::uint8_t kFailureReasonWidth = 2;
  24. /**
  25. * One returned peer reservation.
  26. * The reservation context is recovered; what the two scalars mean individually is not, so they
  27. * keep their wire order and no meaning is assigned to either.
  28. */
  29. struct ReservationRelease {
  30. std::uint32_t scalarA{};
  31. std::uint32_t scalarB{};
  32. /** Eight key bytes, low byte first, naming the peer inside the session's ledger. */
  33. std::uint64_t peerKey{};
  34. };
  35. /** One peer leave notice. It is the release body plus one trailing scalar. */
  36. struct PeerLeave {
  37. std::uint32_t scalarA{};
  38. std::uint32_t scalarB{};
  39. std::uint64_t peerKey{};
  40. std::uint32_t scalarC{};
  41. };
  42. /** One reported failure to reach a peer. */
  43. struct ConnectivityFailure {
  44. std::uint64_t peerKey{};
  45. /** Raw two-bit selector. The schema bias is unrecovered, so this is not a decoded reason. */
  46. std::uint8_t rawReason{};
  47. };
  48. /** One proposed host migration. Accepting one needs the group migration state machine. */
  49. struct MigrationProposal {
  50. /** Biased signed scalar whose meaning is not recovered. */
  51. std::int32_t scalar{};
  52. std::uint64_t peerKey{};
  53. };
  54. /**
  55. * Parses a reservation release.
  56. * @param input Activity payload after the envelope.
  57. * @param release Cleared first, then filled.
  58. * @param consumedBits Receives the bits the body used, whether or not it parsed.
  59. * @return True when the whole fixed body was present.
  60. */
  61. [[nodiscard]] bool parse_release(std::span<const std::byte> input,
  62. ReservationRelease& release,
  63. std::size_t& consumedBits) noexcept;
  64. /**
  65. * Parses a peer leave notice.
  66. * @param input Activity payload after the envelope.
  67. * @param leave Cleared first, then filled.
  68. * @param consumedBits Receives the bits the body used, whether or not it parsed.
  69. * @return True when the whole fixed body was present.
  70. */
  71. [[nodiscard]] bool
  72. parse_leave(std::span<const std::byte> input, PeerLeave& leave, std::size_t& consumedBits) noexcept;
  73. /**
  74. * Parses a connectivity failure report.
  75. * @param input Activity payload after the envelope.
  76. * @param failure Cleared first, then filled.
  77. * @param consumedBits Receives the bits the body used, whether or not it parsed.
  78. * @return True when the whole fixed body was present.
  79. */
  80. [[nodiscard]] bool parse_connectivity_failure(std::span<const std::byte> input,
  81. ConnectivityFailure& failure,
  82. std::size_t& consumedBits) noexcept;
  83. /**
  84. * Parses a speculative migration proposal.
  85. * @param input Activity payload after the envelope.
  86. * @param proposal Cleared first, then filled.
  87. * @param consumedBits Receives the bits the body used, whether or not it parsed.
  88. * @return True when the whole fixed body was present.
  89. */
  90. [[nodiscard]] bool parse_migration(std::span<const std::byte> input,
  91. MigrationProposal& proposal,
  92. std::size_t& consumedBits) noexcept;
  93. } // namespace sunrise::middleware::bap::activity_message::peer_ledger