internal.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. #pragma once
  2. #include <array>
  3. #include <cstddef>
  4. #include <cstdint>
  5. #include <span>
  6. #include "../../client/network/consumer.h"
  7. #include "../../core/threading/srw_lock.h"
  8. #include "../../middleware/bap/activity_message/activity_patch_epoch_parser.h"
  9. #include "../../middleware/bap/activity_message/replicate_membership.h"
  10. #include "../../middleware/bap/activity_message/sensor_auth_update.h"
  11. #include "../../middleware/bap/frame.h"
  12. #include "../../middleware/content/packages/tables/scenario_reader.h"
  13. #include "../../state/activity/bubble_authority/definition.h"
  14. #include "../../state/activity/definition.h"
  15. #include "../../state/activity_sdk/runtime.h"
  16. #include "../../state/build_data/scenarios/definition.h"
  17. #include "../../state/gameplay/external/squad_entity_retirement.h"
  18. #include "../../state/runtime/runtime.h"
  19. #include "../activity/host_runtime.h"
  20. #include "activity_authority_query_owner.h"
  21. #include "activity_authority_reset_owner.h"
  22. #include "encrypted/queuez/definition.h"
  23. #include "runtime.h"
  24. namespace sunrise::server::bap {
  25. /** One session per transport peer slot, so a connection id indexes this array directly. */
  26. inline constexpr std::size_t kSessionCount = client::network::kBapConnectionCount;
  27. /**
  28. * A delivered activity frame defers the next silence-prevention write.
  29. * This is the host's own period. It is not the floor handed to the client, which is a separate
  30. * value, and no bulk body may be scheduled on it.
  31. */
  32. inline constexpr std::uint64_t kActivityKeepaliveIntervalMs = 2'000;
  33. /** Counts matching authenticated links while the caller already owns the BAP lock. */
  34. [[nodiscard]] std::size_t
  35. activity_link_count_locked(const state::activity::SessionBinding& binding) noexcept;
  36. /** Fixed scratch storage owned by the lock, kept off the Client thread's stack. */
  37. struct Scratch {
  38. std::array<std::byte, client::network::kBapFrameCapacity> plaintext{};
  39. std::array<std::byte, client::network::kBapFrameCapacity> responseBody{};
  40. std::array<std::byte, client::network::kBapFrameCapacity> responsePayload{};
  41. std::array<std::byte, client::network::kBapFrameCapacity> sealed{};
  42. std::array<std::byte, client::network::kBapFrameCapacity> framed{};
  43. /** Roster groups the outbound body's slot spans point into, top-level and per-bubble alike. */
  44. std::array<state::build_data::scenarios::RosterGroup,
  45. middleware::bap::activity_message::sensor_auth_update::kPublishedGroupCapacity>
  46. rosterGroups{};
  47. /** Exact typed auth bodies the outbound snapshot span points into. */
  48. std::array<middleware::bap::activity_message::sensor_auth_update::AuthOverride,
  49. middleware::bap::activity_message::sensor_auth_update::kAuthOverrideCapacity>
  50. rosterAuthOverrides{};
  51. std::array<middleware::bap::activity_message::sensor_auth_update::SenseOverride,
  52. middleware::bap::activity_message::sensor_auth_update::kAuthOverrideCapacity>
  53. rosterSenseOverrides{};
  54. /** SDK-authored scene inputs staged before their exact msg-5 targets are installed. */
  55. std::array<state::activity_sdk::AuthoredSceneSeed,
  56. middleware::bap::activity_message::sensor_auth_update::kAuthOverrideCapacity>
  57. rosterSceneSeeds{};
  58. /** Per-bubble sub-blocks the outbound body's field-1 span points into. */
  59. std::array<middleware::bap::activity_message::sensor_auth_update::BubbleSubBlock,
  60. state::build_data::scenarios::kBubbleCapacity>
  61. rosterSubBlocks{};
  62. /** Keys each sub-block carries, which its own span points into. */
  63. std::array<
  64. std::array<std::uint32_t,
  65. middleware::bap::activity_message::sensor_auth_update::kBubbleKeyCapacity>,
  66. state::build_data::scenarios::kBubbleCapacity>
  67. rosterSubBlockKeys{};
  68. };
  69. /** One registry key and its package object tag copied from an exact msg-5 roster snapshot. */
  70. struct RosterDecodeEntry final {
  71. std::uint32_t registryKey{};
  72. std::uint32_t objectTag{};
  73. };
  74. /** Complete msg-5 roster identity map retained for one exact ActivityClient generation. */
  75. struct RosterDecodeMap final {
  76. std::array<RosterDecodeEntry,
  77. middleware::bap::activity_message::sensor_auth_update::kPublishedGroupCapacity>
  78. entries{};
  79. std::uint64_t bindingGeneration{};
  80. std::uint16_t count{};
  81. bool valid{};
  82. };
  83. /**
  84. * One published group's registration identity and the revision last sent beside its key.
  85. * The wire carries one revision byte per key. The client tears down and rebuilds a group's
  86. * objects only when that byte moves, so the byte stays put while the group's identity holds.
  87. */
  88. struct RosterGroupLease {
  89. std::uint32_t key{};
  90. std::uint32_t identityFold{};
  91. std::uint8_t sequence{};
  92. bool used{};
  93. };
  94. /** Lease slots mirror the roster's published-group capacity. */
  95. inline constexpr std::size_t kRosterGroupLeaseCapacity =
  96. middleware::bap::activity_message::sensor_auth_update::kPublishedGroupCapacity;
  97. /**
  98. * What one staged roster body owes State, and the counters to put back if it is discarded.
  99. * A bubble is offered once, and each group revision may rebuild that registry key. Counters and
  100. * staged per-group revisions may move only once the frame reaches the caller.
  101. */
  102. struct RosterPublication {
  103. state::activity::bubble_authority::Grant grant{};
  104. state::gameplay::squad_entity_retirement::RetirementPlan entityRetirement{};
  105. /** Epochs remain staged until both retirement and roster frames reach the caller. */
  106. std::uint8_t retirementPriorEpoch{};
  107. std::uint8_t retirementBaseEpoch{};
  108. std::uint8_t retirementEpoch{};
  109. bool priorRosterOwedForEpoch{};
  110. /** Exact decode identities carried by this staged complete roster snapshot. */
  111. RosterDecodeMap decodeMap{};
  112. /** Exact typed body carried by this staged roster, if any. */
  113. activity::host::PendingScriptableOverride scriptableOverride{};
  114. /** ActivityClient generation that staged this grant and its roster counters. */
  115. std::uint64_t bindingGeneration{};
  116. /** Group leases as they stood before this body, put back if it never reaches the caller. */
  117. std::array<RosterGroupLease, kRosterGroupLeaseCapacity> priorLeases{};
  118. std::uint8_t priorSends{};
  119. std::uint8_t priorState{};
  120. /** Region epoch and the bubble it was stamped for, put back with the leases above. */
  121. std::uint8_t priorRegionEpoch{};
  122. std::int32_t priorRegionBubble{-1};
  123. /** Generated squad-group revision carried by this staged body. */
  124. std::uint8_t squadStateSequence{};
  125. /** Activity Host state revision carried by this body. */
  126. std::uint64_t hostStateRevision{};
  127. /** SDK selected-state roster lease revision carried by this body. */
  128. std::uint64_t missionSeedRevision{};
  129. /** Type-17 lifetime state carried for that revision. */
  130. std::uint8_t hostLifetimeState{};
  131. /** Exact authored region that owns the staged state-local group. */
  132. std::int32_t stateLocalRegion{-1};
  133. /** Set when the staged body carried a bubble grant that State has not recorded yet. */
  134. bool hasGrant{};
  135. /** Set when this body carries a pending Activity Host state revision. */
  136. bool hasHostState{};
  137. /** Set when this body carries a not-yet-published SDK selected-state roster lease revision. */
  138. bool hasMissionSeedRevision{};
  139. /** Set when this body carries the pending typed body above. */
  140. bool hasScriptableOverride{};
  141. /** Set when the delivered body merges into this binding's retained squad Auth set. */
  142. bool activatesSquadOverride{};
  143. /** Set when the staged squad body carries its own per-group revision. */
  144. bool hasSquadStateSequence{};
  145. /** Set while a roster body is staged and its outcome is undecided. */
  146. bool staged{};
  147. };
  148. /** Compact retained squad body; shared target fields and the generated group live on its group. */
  149. struct RetainedSquadAuth {
  150. std::array<std::byte, middleware::bap::activity_message::squad_auth::kMaximumRetainedByteCount>
  151. body{};
  152. std::uint32_t generation{};
  153. std::uint16_t rosterSlotOffset{};
  154. std::uint16_t slotIndex{};
  155. std::uint16_t bitCount{};
  156. std::uint16_t byteCount{};
  157. /** Dense retained-group index that owns this body. */
  158. std::uint8_t groupIndex{};
  159. };
  160. /** One generated registry key and the squad Auth bodies that target its slots. */
  161. struct RetainedSquadGroup {
  162. /** Fields shared by this group's retained slots; only slot offset and index vary. */
  163. activity::host::ScriptableTarget scopeTarget{};
  164. /** Exact generated SDK group shared by this group's retained slots. */
  165. state::build_data::scenarios::RosterGroup stateLocalRosterGroup{};
  166. std::int32_t region{-1};
  167. std::uint16_t authCount{};
  168. /** Last delivered revision of the generated state-local roster group. */
  169. std::uint8_t stateSequence{};
  170. /** Region epoch the revision was armed under; a newer one re-arms the group. */
  171. std::uint8_t regionEpoch{};
  172. };
  173. /** All generated squad groups retained by one exact ActivityClient binding. */
  174. struct SquadOverrideLease {
  175. std::array<RetainedSquadAuth,
  176. middleware::bap::activity_message::sensor_auth_update::kAuthOverrideCapacity>
  177. authBodies{};
  178. std::array<RetainedSquadGroup,
  179. middleware::bap::activity_message::sensor_auth_update::kPublishedGroupCapacity>
  180. groups{};
  181. std::uint64_t bindingGeneration{};
  182. std::uint16_t authCount{};
  183. std::uint16_t groupCount{};
  184. bool active{};
  185. };
  186. /** Off-by-default generated selected-state roster plan pinned to one exact ActivityClient
  187. * generation. */
  188. struct MissionSeedLease {
  189. ActivityMissionSeedPlan plan{};
  190. std::uint64_t bindingGeneration{};
  191. std::uint64_t revision{};
  192. std::uint64_t publishedRevision{};
  193. /**
  194. * Every authored region this lease has selected, in selection order. The peer's registered set
  195. * only grows: a group is torn down by a changed state byte, never by being left out, and every
  196. * message must seed its sync records. So each publication carries the union of these regions.
  197. */
  198. std::array<std::uint32_t, middleware::content::packages::tables::kSliceSetIndexFactor>
  199. registeredRegions{};
  200. std::uint8_t registeredRegionCount{};
  201. bool configured{};
  202. /**
  203. * Set once the complete selected-state set has been published. It only ratchets: shrinking
  204. * the published set back to the transition subset would re-register the shared groups.
  205. */
  206. bool fullSetPublished{};
  207. /**
  208. * The plan the previous selection held, kept while a region change waits for the client to
  209. * arrive. Publications in that window answer this plan's region: the client is tearing its
  210. * old world down, and registering the new region's groups into it races the teardown.
  211. */
  212. ActivityMissionSeedPlan previousPlan{};
  213. /** True from a region-changing selection until the client's post-arrival solicited answer. */
  214. bool regionArrivalPending{};
  215. /** Set when a mission script selected the plan. An adopted default plan is not a selection. */
  216. bool scriptSelected{};
  217. };
  218. static_assert(middleware::bap::activity_message::sensor_auth_update::kAuthOverrideCapacity
  219. == state::build_data::scenarios::kRosterSlotCapacity);
  220. /** ActivityClient role owned by one authenticated BAP link. */
  221. enum class ActivityClientRole : std::uint8_t {
  222. none,
  223. privateCurrent,
  224. publicTarget,
  225. };
  226. /** Exact activity-session generations owned by one BAP link. */
  227. struct ActivityClientBinding {
  228. /** Target/current session that every activity envelope on this link names. */
  229. state::activity::SessionBinding session{};
  230. /** Same as session for private links; advertised source for public targets. */
  231. state::activity::SessionBinding source{};
  232. std::uint64_t groupSessionId{};
  233. std::uint64_t hostGeneration{};
  234. /** Changes on every bind and rejoin, even when the session id stays the same. */
  235. std::uint64_t bindingGeneration{};
  236. /** Epoch this host authored in the accepted join result. */
  237. std::uint8_t replicationEpoch{};
  238. /** Private: last citizen region. Public: immutable region captured by the host binding. */
  239. std::int32_t advertisedRegion{-1};
  240. /** True when the last advertisement named a private region's own Bubble Host. */
  241. bool advertisedPrivate{};
  242. ActivityClientRole role{ActivityClientRole::none};
  243. };
  244. /** Patch epoch tied to the exact ActivityClient binding that received it. */
  245. struct BoundPatchEpoch {
  246. middleware::bap::activity_message::patch_epoch::PatchEpoch value{};
  247. std::uint64_t bindingGeneration{};
  248. bool seen{};
  249. };
  250. /** Host rows one membership body's directory holds against replacement. */
  251. struct AdvertisementRetains {
  252. std::array<std::uint64_t,
  253. middleware::bap::activity_message::replicate_membership::kCitizenCapacity>
  254. hostGenerations{};
  255. std::uint8_t count{};
  256. };
  257. /** Host-session retains staged by one membership body until its frame is published. */
  258. struct AdvertisementPublication {
  259. AdvertisementRetains retains{};
  260. bool staged{};
  261. };
  262. /** One staged msg-19 host output, committed only after its complete frame reaches the caller. */
  263. struct IncidentPublication {
  264. std::uint64_t bindingGeneration{};
  265. std::uint64_t revision{};
  266. bool staged{};
  267. };
  268. /** One generation-bound activity message 44 request. */
  269. struct ReplicationEpochPublication {
  270. std::uint64_t bindingGeneration{};
  271. std::uint8_t generation{};
  272. bool pending{};
  273. bool staged{};
  274. };
  275. /** Which inventory one world reward lands in. */
  276. enum class WorldRewardKind : std::uint8_t {
  277. item,
  278. profileItem,
  279. };
  280. /** One reward earned in world, held until a Family-4 peer can publish it. */
  281. struct WorldRewardRequest {
  282. std::int32_t quantity{};
  283. std::uint16_t itemDefinitionIndex{};
  284. WorldRewardKind kind{};
  285. };
  286. /** Packed size of one world reward row: the queue is sized in whole rows of this width. */
  287. inline constexpr std::size_t kWorldRewardRequestSize = 8;
  288. static_assert(sizeof(WorldRewardRequest) == kWorldRewardRequestSize);
  289. /** One flyout runs for seconds, so the queue only has to cover a burst inside one activity. */
  290. inline constexpr std::size_t kWorldRewardQueueCapacity = 64;
  291. /** Mutable transport state owned by one BAP connection. */
  292. struct Session {
  293. std::uint64_t activityAdvertisementHostGeneration{};
  294. std::uint64_t acquisitionPresentationUntilTick{};
  295. std::array<encrypted::queuez::AcquisitionPresentationRow,
  296. encrypted::queuez::kAcquisitionPresentationRowCapacity>
  297. acquisitionPresentationRows{};
  298. std::uint32_t id{};
  299. std::uint32_t activityRosterGroups{};
  300. std::int32_t pendingSeasonalExperienceAmount{};
  301. std::uint32_t pendingSeasonalExperienceMutationSerial{};
  302. bool authenticated{};
  303. /** Owes one family-five snapshot for the unlock overrides an artifact change moved. */
  304. bool artifactRefreshArmed{};
  305. bool artifactFamily4RefreshArmed{};
  306. std::uint64_t artifactFamily4RefreshDueTick{};
  307. state::ArtifactResetResult artifactResetRefresh{};
  308. std::size_t artifactResetRefreshCursor{};
  309. std::uint8_t acquisitionPresentationRowCount{};
  310. std::array<std::byte, state::kBapNonceSize> sendNonce{};
  311. std::array<std::byte, state::kBapNonceSize> receiveNonce{};
  312. /** This connection's own AES-GCM key. Sharing one across links reuses key and nonce pairs. */
  313. std::array<std::byte, state::kAesKeySize> sessionKey{};
  314. /** Opaque State handle taken only after the server hello authenticates. */
  315. state::matchmaking::ContextHandle matchmakingContext{};
  316. /** Exact private or public ActivityClient generation owned by this connection. */
  317. ActivityClientBinding activity{};
  318. /** Tick count after which the activity link owes its next keepalive write. */
  319. std::uint64_t activityKeepaliveDueTick{};
  320. /** Client member key from the join request. It seeds the membership id. */
  321. std::uint64_t activityMemberKey{};
  322. /** Binding generation whose entity-slot join committed, including a zero member key. */
  323. std::uint64_t activityJoinGeneration{};
  324. /**
  325. * Character the join request named, or zero when it carried none.
  326. * The roster's participation key must be the character the client signed in on. The client
  327. * binds its player by matching that value.
  328. */
  329. std::uint64_t activityCharacterSoid{};
  330. /** Tick count after which the activity link owes its next roster update. */
  331. std::uint64_t activityRosterDueTick{};
  332. /**
  333. * Binding generation whose membership body this link has already delivered.
  334. * The client sets its membership flag once and never clears it, and never acknowledges a body
  335. * on a public-target link, so this is a one-shot per binding. Latched on delivery, not encode.
  336. */
  337. std::uint64_t activityMembershipSentGeneration{};
  338. /** Binding generation whose client-authored type-23 identity has committed. */
  339. std::uint64_t activityClientIdentitySeenGeneration{};
  340. /**
  341. * Binding generation whose committed type-23 identity reached the client in membership.
  342. * The initial roster must not consume its object-rebuild revisions before this latch.
  343. */
  344. std::uint64_t activityClientIdentityPublishedGeneration{};
  345. /**
  346. * Set when the join burst staged a membership body, and read once the frame is published.
  347. * The generation it belongs to does not exist until publication, so the latch cannot be written
  348. * at staging time.
  349. */
  350. bool activityJoinMembershipStaged{};
  351. /**
  352. * Tick count until which the client is loading, so the roster runs at its faster cadence.
  353. * A join and a transition-token change are the only two things that open it.
  354. */
  355. std::uint64_t activityTransitionUntilTick{};
  356. /** The client's own patch epoch, scoped to the binding that received message 52. */
  357. BoundPatchEpoch activityPatchEpoch{};
  358. /** Replication epoch held until its encrypted frame reaches the transport caller. */
  359. ReplicationEpochPublication activityReplicationEpoch{};
  360. /** Per published group key: its registration identity and the revision the client holds. */
  361. std::array<RosterGroupLease, kRosterGroupLeaseCapacity> activityRosterGroupLeases{};
  362. /**
  363. * A roster answer was refused for want of the client's patch epoch, and is still owed.
  364. * The epoch arriving is what makes that answer possible, so message 52 discharges it.
  365. */
  366. bool activityRosterOwedForEpoch{};
  367. /**
  368. * Region-rebuild epoch, folded into every group's identity. It advances once per region
  369. * transition so every group's revision byte moves and the client re-registers the groups for
  370. * the region it entered. Without that move, object intents into the new bubble are refused.
  371. */
  372. std::uint8_t activityRosterRegionEpoch{};
  373. /** Bubble the epoch was stamped for; a change in it is the region transition. */
  374. std::int32_t activityRosterRegionBubble{-1};
  375. /** One-shot first-roster flag; the mission seed's adoption guard reads it. */
  376. std::uint8_t activityRosterSends{};
  377. /** Group-revision allocator; each value is spent on one group registration change. */
  378. std::uint8_t activityRosterState{};
  379. /** Latest Activity Host revision staged into this connection's transport output. */
  380. std::uint64_t activityHostStateRevision{};
  381. /** One bounded authority-mask readback owned by this exact ActivityClient generation. */
  382. authority_query::Owner activityAuthorityQuery{};
  383. /** One bounded authority-mask reset owned by this exact ActivityClient generation. */
  384. authority_reset::Owner activityAuthorityReset{};
  385. /** Operator incident held until the complete encrypted frame reaches the transport caller. */
  386. IncidentPublication activityIncidentStaged{};
  387. /** Earliest tick at which a refused incident frame may be rebuilt. */
  388. std::uint64_t activityIncidentRetryDueTick{};
  389. /** Host rows retained by the last delivered host directory. */
  390. AdvertisementRetains activityAdvertisementHeld{};
  391. /** Host rows retained by a staged membership body until publication is known. */
  392. AdvertisementPublication activityAdvertisementStaged{};
  393. /**
  394. * Reason code of the last logged roster outcome.
  395. * The push runs every second, so a refusal is logged only when the reason changes. One flag
  396. * for every reason hides the second failure behind the first.
  397. */
  398. std::uint8_t activityRosterReason{};
  399. /** What one staged roster body owes, and what to put back if it never reaches the caller. */
  400. RosterPublication activityRosterStaged{};
  401. /** Last complete msg-5 roster known to have reached this exact connection generation. */
  402. RosterDecodeMap activityRosterDecode{};
  403. /** Delivered squad Auth bodies, all re-emitted so phase-2 reset cannot clear any slot. */
  404. SquadOverrideLease activitySquadOverride{};
  405. /** Generated selected-state roster lease. Disabled until an operator enables its SDK row. */
  406. MissionSeedLease activityMissionSeed{};
  407. /** Queuez versions and residents published only through this authenticated peer. */
  408. encrypted::queuez::SessionState queuez{};
  409. /** Tick count after which the owed Family-4 re-push may go out. */
  410. std::uint64_t family4RepushDueTick{};
  411. /** Root the owed re-push must use. */
  412. std::uint64_t family4RepushRoot{};
  413. /** True while one Family-4 re-push is still owed to this peer. */
  414. bool family4RepushArmed{};
  415. /** Tick count after which the owed banner re-push may go out. */
  416. std::uint64_t bannerRepushDueTick{};
  417. /** Root the owed banner re-push must use. */
  418. std::uint64_t bannerRepushRoot{};
  419. /** True while one banner re-push is still owed to this peer. */
  420. bool bannerRepushArmed{};
  421. /** Root the last family-two subscribe was answered against, and the re-push must reuse. */
  422. std::uint64_t socialRosterRepushRoot{};
  423. /** True while one family-two re-push is still owed to this peer. */
  424. bool socialRosterRepushArmed{};
  425. /** Latest shared-account generation this peer has received. */
  426. std::uint64_t accountGeneration{};
  427. /** Newest shared-account generation owed as a full cross-peer refresh. */
  428. std::uint64_t accountResyncGeneration{};
  429. /** Set by encrypted processing only after one account mutation commits and is copied out. */
  430. bool accountMutationPublished{};
  431. /** True while another peer's account mutation still needs a full local refresh. */
  432. bool accountResyncArmed{};
  433. /**
  434. * Tick count after which the owed ability-icon refresh may go out. A subclass selection
  435. * invalidates the published ability buckets and the rebuild runs off the Client
  436. * content-extraction pump, so the inline refresh can carry empty ones; this one re-derives.
  437. */
  438. std::uint64_t abilityRefreshDueTick{};
  439. /** True while one ability-icon refresh is still owed to this peer. */
  440. bool abilityRefreshArmed{};
  441. /**
  442. * True while the launch cinematic hold has parked the account player-record for this peer.
  443. * Set when the family-0 park push is armed on a loading launch, cleared when arrival arms the
  444. * family-4 completion that resolves it. Gates the two phases so each fires once per launch.
  445. */
  446. bool cinematicHeld{};
  447. };
  448. /** Guards the session table; hold it for the whole event and never re-enter it from a route. */
  449. [[nodiscard]] core::threading::SrwLock& session_lock() noexcept;
  450. /** @return Every session slot, open or not. Caller owns the session lock. */
  451. [[nodiscard]] std::span<Session> sessions() noexcept;
  452. /** @return True while any authenticated peer holds a Family-4 subscription. */
  453. [[nodiscard]] bool has_active_family4_peer() noexcept;
  454. /**
  455. * Finds one exact authenticated ActivityClient while the caller owns the session lock.
  456. * @param binding Exact Activity Host generation selected by the caller.
  457. * @param count Receives how many links own the binding.
  458. * @return The link when exactly one owns the binding, else null.
  459. */
  460. [[nodiscard]] const Session*
  461. unique_activity_link_locked(const state::activity::SessionBinding& binding,
  462. std::size_t& count) noexcept;
  463. /** @return Region index this connection's msg-5 builder selects. Caller owns the session lock. */
  464. [[nodiscard]] std::int32_t selected_region_index_locked(const Session& session) noexcept;
  465. /** Loads the scenario layout one lock-held ActivityClient owns. @return False when it has none. */
  466. [[nodiscard]] bool
  467. session_scenario_layout(const Session& session,
  468. state::build_data::scenarios::Definition& output) noexcept;
  469. /** Commits every queued world reward with no presentation and empties the queue. */
  470. void drain_world_rewards() noexcept;
  471. /** Arms every other Family-4 peer after the origin publishes a complete account mutation. */
  472. void arm_account_resync_elsewhere(Session& origin) noexcept;
  473. /** Arms every Family-4 peer, including the origin, for a full account resync. */
  474. void arm_account_resync_everywhere() noexcept;
  475. /** Holds this peer's full Family-4 refreshes until its acquisition flyout has finished. */
  476. void arm_acquisition_presentation_hold(Session& session) noexcept;
  477. /** Queues one character item for normal acquisition feedback. */
  478. [[nodiscard]] bool arm_world_item_acquisition(std::uint16_t itemDefinitionIndex) noexcept;
  479. /** Queues one profile material for normal acquisition feedback. */
  480. [[nodiscard]] bool arm_world_profile_item_acquisition(std::uint16_t itemDefinitionIndex,
  481. std::int32_t quantity) noexcept;
  482. /** Reads the oldest queued world reward without removing it. */
  483. [[nodiscard]] bool current_world_reward(WorldRewardRequest& request) noexcept;
  484. /** Removes the world reward returned by current_world_reward. */
  485. void complete_world_reward() noexcept;
  486. /** Commits the queued reward with no flyout once its presentation cannot be built. */
  487. void settle_world_reward() noexcept;
  488. /** Queues one transient XP item update so the native HUD presents a seasonal XP gain. */
  489. [[nodiscard]] bool arm_seasonal_experience_presentation(std::int32_t amount) noexcept;
  490. /**
  491. * Finds one unambiguous registry identity in a committed connection-local roster map.
  492. * @param map Last complete msg-5 map delivered on one BAP connection.
  493. * @param expectedBindingGeneration Exact ActivityClient generation routing the client message.
  494. * @param registryKey Client-reported registry key from that roster.
  495. * @return The unique entry, or null for an invalid/stale map, absent key, or duplicate key.
  496. */
  497. [[nodiscard]] const RosterDecodeEntry*
  498. find_roster_decode_entry(const RosterDecodeMap& map,
  499. std::uint64_t expectedBindingGeneration,
  500. std::uint32_t registryKey) noexcept;
  501. namespace plaintext {
  502. /**
  503. * Handles plaintext bootstrap services, arms encryption after service 25, and routes the rest.
  504. * @param session Auth and nonce state owned by the connection.
  505. * @param scratch Transform buffers owned by the lock, kept off the Client thread stack.
  506. * @param outer Parsed outer frame carrying the service id and its body.
  507. * @param response Whole-frame storage owned by the caller.
  508. * @param written Gets the encoded response size in bytes.
  509. * @return True when the service owes no reply, or its response is encoded.
  510. */
  511. [[nodiscard]] bool consume(Session& session,
  512. Scratch& scratch,
  513. const middleware::bap::OuterFrame& outer,
  514. std::span<std::byte> response,
  515. std::size_t& written) noexcept;
  516. } // namespace plaintext
  517. namespace encrypted {
  518. /**
  519. * Authenticates and routes one encrypted post-bootstrap service frame.
  520. * @param session Auth and nonce state owned by the connection.
  521. * @param scratch Transform buffers owned by the lock, kept off the Client thread stack.
  522. * @param outer Validated encrypted outer frame.
  523. * @param response Whole-frame storage owned by the caller.
  524. * @param written Gets the encoded response size in bytes.
  525. * @return True when routing works, any response fits, State commits and the nonce is published.
  526. */
  527. [[nodiscard]] bool consume(Session& session,
  528. Scratch& scratch,
  529. const middleware::bap::OuterFrame& outer,
  530. std::span<std::byte> response,
  531. std::size_t& written) noexcept;
  532. /**
  533. * Sends the owed Family-4 re-push once its delay has passed.
  534. * @param session Auth, nonce and queuez state owned by the connection.
  535. * @param scratch Transform buffers owned by the lock, kept off the Client thread stack.
  536. * @param response Whole-frame storage owned by the caller.
  537. * @param written Gets the encoded notification size in bytes.
  538. * @param touchesScratch Set before any scratch buffer is used.
  539. * @return True when a whole Family-4 notification is published.
  540. */
  541. [[nodiscard]] bool consume_deferred(Session& session,
  542. Scratch& scratch,
  543. std::span<std::byte> response,
  544. std::size_t& written,
  545. bool& touchesScratch) noexcept;
  546. } // namespace encrypted
  547. } // namespace sunrise::server::bap