established_packet.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. #include "established_packet.h"
  2. #include <array>
  3. #include "../../encoding/bit_raw.h"
  4. #include "peer_container.h"
  5. namespace sunrise::middleware::gameplay::peer {
  6. namespace {
  7. namespace bits = encoding::bits;
  8. /** An established packet carries marker 0. */
  9. constexpr std::uint64_t kEstablishedMarker = 0;
  10. /** Width of the marker and of the fragmented flag. */
  11. constexpr std::uint8_t kFlagWidth = 1;
  12. /** The connection sequence guard is two bits. */
  13. constexpr std::uint8_t kSequenceGuardWidth = 2;
  14. /** The outbound packet head is published as ten low bits. */
  15. constexpr std::uint8_t kOutboundHeadWidth = 10;
  16. /** The distance from the head back to the event cursor is seven bits. */
  17. constexpr std::uint8_t kCursorWidth = 7;
  18. /** The acknowledged base is seven bits, matching the 128-entry ring. */
  19. constexpr std::uint8_t kAckBaseWidth = 7;
  20. /** Every status form is selected by a two-bit prefix, one of which extends to three. */
  21. constexpr std::uint8_t kStatusPrefixWidth = 2;
  22. /** The extending bit of the three-bit prefixes. */
  23. constexpr std::uint8_t kStatusExtensionWidth = 1;
  24. /** Prefix of the empty-ring form. */
  25. constexpr std::uint64_t kStatusEmpty = 0;
  26. /** Prefix of the single-difference form. */
  27. constexpr std::uint64_t kStatusSingle = 1;
  28. /** Prefix of the eight-entry explicit form. */
  29. constexpr std::uint64_t kStatusExplicit = 2;
  30. /** Prefix shared by the long-span and uninitialized forms. */
  31. constexpr std::uint64_t kStatusExtended = 3;
  32. /** Extension bit selecting the long-span form. */
  33. constexpr std::uint64_t kStatusLongSpan = 0;
  34. /** Width of the long-span count. */
  35. constexpr std::uint8_t kStatusCountWidth = 7;
  36. /** Width of the index in the single-difference form. */
  37. constexpr std::uint8_t kSingleIndexWidth = 3;
  38. /** The acknowledgement delay is ten bits. */
  39. constexpr std::uint8_t kDelayWidth = 10;
  40. /** Longest status run the long-span form can name. */
  41. constexpr std::uint64_t kMaximumStatusCount = 128;
  42. /** The message sequence selector is two bits. */
  43. constexpr std::uint8_t kSelectorWidth = 2;
  44. /** Selector ending the record list. */
  45. constexpr std::uint64_t kSelectorEnd = 0;
  46. /** Selector introducing an absolute low-13 sequence. */
  47. constexpr std::uint64_t kSelectorAbsolute = 1;
  48. /** Selector introducing a four-bit delta above the previous sequence. */
  49. constexpr std::uint64_t kSelectorDelta = 2;
  50. /** Width of an absolute message sequence. */
  51. constexpr std::uint8_t kAbsoluteSequenceWidth = 13;
  52. /** Message sequences unwrap modulo 8,192. */
  53. constexpr std::uint16_t kSequenceModulus = 8192;
  54. /** Width of the delta form. */
  55. constexpr std::uint8_t kDeltaWidth = 4;
  56. /** Width of the queue-specific short length for the 32-byte queue. */
  57. constexpr std::uint8_t kLargeShortLengthWidth = 8;
  58. /** Width of the queue-specific short length for the 6-byte queue. */
  59. constexpr std::uint8_t kSmallShortLengthWidth = 6;
  60. /** Bits in one byte. */
  61. constexpr std::uint16_t kByteBits = 8;
  62. /** The reassembled message begins with a 6-bit registry id. */
  63. constexpr std::uint8_t kMessageIdWidth = 6;
  64. /** The declared decoded size after it is 18 bits. */
  65. constexpr std::uint8_t kMessageSizeWidth = 18;
  66. /**
  67. * Reads one ternary packet status.
  68. * @param reader Open reader.
  69. * @param received Receives true for the two codes that mean received.
  70. * @return True when a complete code was present.
  71. */
  72. [[nodiscard]] bool read_status(bits::Reader& reader, bool& received) noexcept {
  73. std::uint64_t first = 0;
  74. if (!reader.read(kFlagWidth, first)) {
  75. return false;
  76. }
  77. if (first == 0) {
  78. // Code 0 is status 1, the ordinary received case.
  79. received = true;
  80. return true;
  81. }
  82. std::uint64_t second = 0;
  83. if (!reader.read(kFlagWidth, second)) {
  84. return false;
  85. }
  86. // Code 10 is status 0, unresolved. Code 11 is status 2, received out of order.
  87. received = second != 0;
  88. return true;
  89. }
  90. /** Writes one ternary packet status. @param received Selects status 1 or status 0. */
  91. [[nodiscard]] bool write_status(bits::Writer& writer, bool received) noexcept {
  92. if (received) {
  93. return writer.write(0, kFlagWidth);
  94. }
  95. return writer.write(1, kFlagWidth) && writer.write(0, kFlagWidth);
  96. }
  97. /**
  98. * Reads the acknowledgement handler payload.
  99. * @param reader Open reader positioned after the packet head.
  100. * @param output Receives the peer's acknowledgement state.
  101. * @return True when the selected status form was complete.
  102. */
  103. [[nodiscard]] bool read_ack(bits::Reader& reader, AckState& output) noexcept {
  104. std::uint64_t present = 0;
  105. std::uint64_t head = 0;
  106. std::uint64_t cursor = 0;
  107. if (!reader.read(kFlagWidth, present) || !reader.read(kOutboundHeadWidth, head)
  108. || !reader.read(kCursorWidth, cursor)) {
  109. return false;
  110. }
  111. output.outboundHeadPresent = present != 0;
  112. output.outboundHead = static_cast<std::uint16_t>(head);
  113. output.headMinusCursor = static_cast<std::uint8_t>(cursor);
  114. std::uint64_t base = 0;
  115. std::uint64_t prefix = 0;
  116. if (!reader.read(kAckBaseWidth, base) || !reader.read(kStatusPrefixWidth, prefix)) {
  117. return false;
  118. }
  119. output.receiveHead = static_cast<std::uint16_t>(base);
  120. output.received = {};
  121. output.reportedCount = 0;
  122. if (prefix == kStatusExtended) {
  123. std::uint64_t extension = 0;
  124. if (!reader.read(kStatusExtensionWidth, extension)) {
  125. return false;
  126. }
  127. if (extension != kStatusLongSpan) {
  128. // The uninitialized form ends the payload and carries no delay.
  129. output.ringInitialized = false;
  130. return true;
  131. }
  132. std::uint64_t count = 0;
  133. // The field is the number of statuses, not one less than it.
  134. if (!reader.read(kStatusCountWidth, count) || count > kMaximumStatusCount) {
  135. return false;
  136. }
  137. for (std::uint64_t index = 0; index < count; ++index) {
  138. bool received = false;
  139. if (!read_status(reader, received)) {
  140. return false;
  141. }
  142. if (index < output.received.size()) {
  143. output.received[index] = received;
  144. output.reportedCount = static_cast<std::uint8_t>(index + 1);
  145. }
  146. }
  147. } else if (prefix == kStatusExplicit) {
  148. for (bool& entry : output.received) {
  149. if (!read_status(reader, entry)) {
  150. return false;
  151. }
  152. }
  153. output.reportedCount = static_cast<std::uint8_t>(output.received.size());
  154. } else if (prefix == kStatusSingle) {
  155. std::uint64_t outOfOrder = 0;
  156. std::uint64_t index = 0;
  157. if (!reader.read(kFlagWidth, outOfOrder) || !reader.read(kSingleIndexWidth, index)) {
  158. return false;
  159. }
  160. // The named entry is the last one and the only one that is not an ordinary receive.
  161. output.received.fill(true);
  162. output.received[static_cast<std::size_t>(index)] = outOfOrder != 0;
  163. output.reportedCount = static_cast<std::uint8_t>(index + 1);
  164. } else {
  165. std::uint64_t ringState = 0;
  166. if (!reader.read(kFlagWidth, ringState)) {
  167. return false;
  168. }
  169. }
  170. std::uint64_t delay = 0;
  171. if (!reader.read(kDelayWidth, delay)) {
  172. return false;
  173. }
  174. output.delay = static_cast<std::uint16_t>(delay);
  175. output.ringInitialized = true;
  176. return true;
  177. }
  178. /**
  179. * Reads one reliable queue payload.
  180. * @param reader Open reader positioned at the queue payload.
  181. * @param fragmentBytes Fixed fragment size of this queue.
  182. * @param shortLengthWidth Width of this queue's short-fragment length field.
  183. * @param output Receives every record the packet carried.
  184. * @return True when the record list was well formed.
  185. */
  186. [[nodiscard]] bool read_queue(bits::Reader& reader,
  187. std::size_t fragmentBytes,
  188. std::uint8_t shortLengthWidth,
  189. QueueRecords& output) noexcept {
  190. output.count = 0;
  191. std::uint64_t recordsPresent = 0;
  192. if (!reader.read(kFlagWidth, recordsPresent)) {
  193. return false;
  194. }
  195. if (recordsPresent == 0) {
  196. return true;
  197. }
  198. std::uint16_t previous = 0;
  199. bool hasPrevious = false;
  200. for (;;) {
  201. std::uint64_t selector = 0;
  202. if (!reader.read(kSelectorWidth, selector)) {
  203. return false;
  204. }
  205. if (selector == kSelectorEnd) {
  206. return true;
  207. }
  208. if (output.count >= output.records.size()) {
  209. return false;
  210. }
  211. QueueRecord& record = output.records[output.count];
  212. record = {};
  213. if (selector == kSelectorAbsolute) {
  214. std::uint64_t sequence = 0;
  215. if (!reader.read(kAbsoluteSequenceWidth, sequence)) {
  216. return false;
  217. }
  218. record.sequence = static_cast<std::uint16_t>(sequence);
  219. } else if (selector == kSelectorDelta) {
  220. std::uint64_t delta = 0;
  221. if (!hasPrevious || !reader.read(kDeltaWidth, delta)) {
  222. return false;
  223. }
  224. record.sequence = static_cast<std::uint16_t>((previous + 1 + delta) % kSequenceModulus);
  225. } else {
  226. if (!hasPrevious) {
  227. return false;
  228. }
  229. record.sequence = static_cast<std::uint16_t>((previous + 1) % kSequenceModulus);
  230. }
  231. previous = record.sequence;
  232. hasPrevious = true;
  233. std::uint64_t isShort = 0;
  234. if (!reader.read(kFlagWidth, isShort)) {
  235. return false;
  236. }
  237. record.shortFragment = isShort != 0;
  238. if (record.shortFragment) {
  239. std::uint64_t secondary = 0;
  240. std::uint64_t lengthMinusOne = 0;
  241. if (!reader.read(kFlagWidth, secondary)
  242. || !reader.read(shortLengthWidth, lengthMinusOne)) {
  243. return false;
  244. }
  245. record.bitCount = static_cast<std::uint16_t>(lengthMinusOne + 1);
  246. } else {
  247. record.bitCount = static_cast<std::uint16_t>(fragmentBytes * kByteBits);
  248. }
  249. if (record.bitCount > record.bytes.size() * kByteBits) {
  250. return false;
  251. }
  252. // Fragment bits are read as whole bytes plus a remainder, so the tail keeps its padding.
  253. const std::size_t wholeBytes = record.bitCount / kByteBits;
  254. const std::uint8_t remainder = static_cast<std::uint8_t>(record.bitCount % kByteBits);
  255. if (!bits::read_raw(reader, std::span<std::byte>(record.bytes.data(), wholeBytes))) {
  256. return false;
  257. }
  258. if (remainder != 0) {
  259. std::uint64_t tail = 0;
  260. if (!reader.read(remainder, tail)) {
  261. return false;
  262. }
  263. record.bytes[wholeBytes] = static_cast<std::byte>(tail << (kByteBits - remainder));
  264. }
  265. ++output.count;
  266. }
  267. }
  268. } // namespace
  269. /** Decodes one established packet up to and including both reliable queues. */
  270. bool decode_established(std::span<const std::byte> payload,
  271. bool expectExternal,
  272. EstablishedPacket& output) noexcept {
  273. bits::Reader reader(payload);
  274. std::uint64_t marker = 0;
  275. std::uint64_t fragmented = 0;
  276. std::uint64_t guard = 0;
  277. if (!reader.read(kFlagWidth, marker) || marker != kEstablishedMarker
  278. || !reader.read(kFlagWidth, fragmented) || fragmented != 0
  279. || !reader.read(kSequenceGuardWidth, guard)) {
  280. return false;
  281. }
  282. output = {};
  283. output.connectionSequenceLow2 = static_cast<std::uint8_t>(guard);
  284. if (!read_ack(reader, output.ack)
  285. || !read_queue(reader, kLargeFragmentBytes, kLargeShortLengthWidth, output.large)
  286. || !read_queue(reader, kSmallFragmentBytes, kSmallShortLengthWidth, output.small)) {
  287. return false;
  288. }
  289. // The sentinel handler writes no bits, so the external body starts here when one exists.
  290. output.hasExternal = expectExternal;
  291. output.externalBitOffset = payload.size() * kByteBits - reader.remaining_bits();
  292. return true;
  293. }
  294. /** Reports whether one acknowledgement covers a packet this host sent. */
  295. bool acknowledgement_covers(const AckState& ack, std::uint16_t sentSequence) noexcept {
  296. if (!ack.ringInitialized) {
  297. return false;
  298. }
  299. const auto base = static_cast<std::uint16_t>(ack.receiveHead % kPacketRingSize);
  300. const auto sent = static_cast<std::uint16_t>(sentSequence % kPacketRingSize);
  301. const auto distance = static_cast<std::uint16_t>((base - sent) % kPacketRingSize);
  302. if (distance >= kPacketRingSize / 2) {
  303. // The base is behind the packet, so the peer has not reached it yet.
  304. return false;
  305. }
  306. if (distance == 0) {
  307. // The base is the newest packet the peer holds, and it carries no status entry.
  308. return true;
  309. }
  310. if (distance <= ack.reportedCount) {
  311. return ack.received[distance - 1U];
  312. }
  313. // Older than every entry the peer named, so it has left the peer's window.
  314. return true;
  315. }
  316. /** Writes the packet head and the acknowledgement handler payload. */
  317. bool write_head_and_ack(bits::Writer& writer,
  318. std::uint8_t connectionSequenceLow2,
  319. const AckState& ack) noexcept {
  320. if (!writer.write(kEstablishedMarker, kFlagWidth) || !writer.write(0, kFlagWidth)
  321. || !writer.write(connectionSequenceLow2, kSequenceGuardWidth)) {
  322. return false;
  323. }
  324. if (!writer.write(ack.outboundHeadPresent ? 1U : 0U, kFlagWidth)
  325. || !writer.write(ack.outboundHead, kOutboundHeadWidth)
  326. || !writer.write(ack.headMinusCursor, kCursorWidth)) {
  327. return false;
  328. }
  329. if (!ack.ringInitialized) {
  330. // Nothing has been received yet, so the uninitialized form ends the payload here.
  331. return writer.write(0, kAckBaseWidth) && writer.write(kStatusExtended, kStatusPrefixWidth)
  332. && writer.write(1, kStatusExtensionWidth);
  333. }
  334. if (!writer.write(ack.receiveHead, kAckBaseWidth)
  335. || !writer.write(kStatusExplicit, kStatusPrefixWidth)) {
  336. return false;
  337. }
  338. // Eight explicit statuses say exactly which packets arrived, without claiming any others.
  339. for (const bool received : ack.received) {
  340. if (!write_status(writer, received)) {
  341. return false;
  342. }
  343. }
  344. return writer.write(ack.delay, kDelayWidth);
  345. }
  346. /** Writes one reliable queue that carries no records. */
  347. bool write_empty_queue(bits::Writer& writer) noexcept {
  348. return writer.write(0, kFlagWidth);
  349. }
  350. /** Writes one reliable queue and every fragment it owes. */
  351. bool write_queue(bits::Writer& writer, const state::gameplay::OutboundQueue& queue) noexcept {
  352. if (queue.count == 0) {
  353. return write_empty_queue(writer);
  354. }
  355. if (!writer.write(1, kFlagWidth)) {
  356. return false;
  357. }
  358. for (std::size_t index = 0; index < queue.count; ++index) {
  359. const state::gameplay::OutboundFragment& fragment = queue.fragments[index];
  360. if (!writer.write(kSelectorAbsolute, kSelectorWidth)
  361. || !writer.write(fragment.sequence, kAbsoluteSequenceWidth)
  362. || !writer.write(fragment.shortFragment ? 1U : 0U, kFlagWidth)) {
  363. return false;
  364. }
  365. if (fragment.shortFragment) {
  366. // The secondary flag has no meaning for a fragment this host writes and stays clear.
  367. if (!writer.write(0, kFlagWidth)
  368. || !writer.write(fragment.bitCount - 1U, kLargeShortLengthWidth)) {
  369. return false;
  370. }
  371. }
  372. const std::size_t wholeBytes = fragment.bitCount / kByteBits;
  373. const auto remainder = static_cast<std::uint8_t>(fragment.bitCount % kByteBits);
  374. if (!bits::write_raw(writer, {fragment.bytes.data(), wholeBytes})) {
  375. return false;
  376. }
  377. if (remainder != 0) {
  378. const auto tail = std::to_integer<std::uint64_t>(fragment.bytes[wholeBytes]);
  379. if (!writer.write(tail >> (kByteBits - remainder), remainder)) {
  380. return false;
  381. }
  382. }
  383. }
  384. return writer.write(kSelectorEnd, kSelectorWidth);
  385. }
  386. /** Splits one reliable message into fragments and appends them to a send queue. */
  387. bool enqueue_message(state::gameplay::OutboundQueue& queue,
  388. std::uint8_t id,
  389. std::uint32_t declaredSize,
  390. std::span<const std::byte> body,
  391. std::size_t bodyBits) noexcept {
  392. if (id > kMaximumMessageId) {
  393. return false;
  394. }
  395. // The inner header precedes the body, so the message is staged once and then split.
  396. std::array<std::byte, state::gameplay::kReassemblyCapacity> staged{};
  397. bits::Writer writer(staged);
  398. if (!writer.write(id, kMessageIdWidth) || !writer.write(declaredSize, kMessageSizeWidth)) {
  399. return false;
  400. }
  401. bits::Reader reader(body);
  402. std::size_t remaining = bodyBits;
  403. while (remaining != 0) {
  404. const auto width = static_cast<std::uint8_t>(remaining < kByteBits ? remaining : kByteBits);
  405. std::uint64_t value = 0;
  406. if (!reader.read(width, value) || !writer.write(value, width)) {
  407. return false;
  408. }
  409. remaining -= width;
  410. }
  411. std::size_t stagedBytes = 0;
  412. if (!writer.finish(stagedBytes)) {
  413. return false;
  414. }
  415. const std::size_t totalBits = writer.bit_count();
  416. // Every fragment but the last carries the queue's whole fixed size.
  417. constexpr std::size_t kFragmentBits = kLargeFragmentBytes * kByteBits;
  418. std::size_t consumed = 0;
  419. bits::Reader source({staged.data(), stagedBytes});
  420. // A half-enqueued message can never be reassembled, so any failure below restores the queue.
  421. const std::size_t entryCount = queue.count;
  422. const std::uint16_t entrySequence = queue.nextSequence;
  423. const auto restore = [&queue, entryCount, entrySequence]() noexcept {
  424. for (std::size_t index = entryCount; index < queue.count; ++index) {
  425. queue.fragments[index] = {};
  426. }
  427. queue.count = entryCount;
  428. queue.nextSequence = entrySequence;
  429. };
  430. while (consumed < totalBits) {
  431. if (queue.count >= queue.fragments.size()) {
  432. restore();
  433. return false;
  434. }
  435. const std::size_t take =
  436. (totalBits - consumed) < kFragmentBits ? totalBits - consumed : kFragmentBits;
  437. // Only a short fragment closes a run. A full last fragment still needs one appended.
  438. const bool last = consumed + take >= totalBits;
  439. state::gameplay::OutboundFragment& fragment = queue.fragments[queue.count];
  440. fragment = {};
  441. bits::Writer chunk(fragment.bytes);
  442. std::size_t written = 0;
  443. std::size_t pending = take;
  444. while (pending != 0) {
  445. const auto width = static_cast<std::uint8_t>(pending < kByteBits ? pending : kByteBits);
  446. std::uint64_t value = 0;
  447. if (!source.read(width, value) || !chunk.write(value, width)) {
  448. restore();
  449. return false;
  450. }
  451. pending -= width;
  452. }
  453. if (!chunk.finish(written)) {
  454. restore();
  455. return false;
  456. }
  457. fragment.sequence = queue.nextSequence;
  458. fragment.bitCount = static_cast<std::uint16_t>(take);
  459. fragment.shortFragment = last && take < kFragmentBits;
  460. fragment.occupied = true;
  461. queue.nextSequence = static_cast<std::uint16_t>((queue.nextSequence + 1)
  462. % state::gameplay::kMessageSequenceModulus);
  463. ++queue.count;
  464. consumed += take;
  465. if (last && !fragment.shortFragment) {
  466. // An exact multiple of the fragment size still needs a closing short fragment.
  467. if (queue.count >= queue.fragments.size()) {
  468. restore();
  469. return false;
  470. }
  471. state::gameplay::OutboundFragment& terminator = queue.fragments[queue.count];
  472. terminator = {};
  473. terminator.sequence = queue.nextSequence;
  474. terminator.bitCount = 1;
  475. terminator.shortFragment = true;
  476. terminator.occupied = true;
  477. queue.nextSequence = static_cast<std::uint16_t>(
  478. (queue.nextSequence + 1) % state::gameplay::kMessageSequenceModulus);
  479. ++queue.count;
  480. }
  481. }
  482. return true;
  483. }
  484. /** Writes the filler trailer that ends every packet. */
  485. bool write_absent_filler(bits::Writer& writer) noexcept {
  486. // Two bits close a packet: the extended-presence bit, then the external-body present bit.
  487. // The reader consumes both, so both must be written even though both are zero.
  488. return writer.write(0, kFlagWidth) && writer.write(0, kFlagWidth);
  489. }
  490. } // namespace sunrise::middleware::gameplay::peer