naia-shared 0.25.0

Common functionality shared between naia-server & naia-client crates
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! ## `EntityChannel` – Per‑Entity Demultiplexer
//!
//! This module owns the **state machine and buffering logic for a *single
//! entity*** travelling across an **unordered, reliable** transport.
//!
//! ---
//! ### 1 · What problem does it solve?
//! * Messages can arrive *out of order*
//! * Certain message kinds must obey **strict causal order _within_ the
//!   entity** (e.g. a component can’t be inserted before the entity exists).
//!
//! `EntityChannel` absorbs the raw `EntityMessage<()>` stream, re‑orders and
//! filters it, and emits **ready‑to‑apply** messages in the *only* sequence
//! the game‑logic needs to respect.
//!
//! ---
//! ### 2 · State machine
//!
//! ```text
//!                 +-----------------------------+
//!                 |   Despawned (initial)       |
//!                 +-----------------------------+
//!                     | SpawnEntity(idₛ)  ▲
//!                     v                   |
//!                 +-----------------------------+
//!                 |     Spawned                 |
//!                 +-----------------------------+
//!                     | DespawnEntity(id_d)     |
//!                     +-------------------------+
//! ```
//!
//! * **`Despawned`** – entity is not present; buffers *only* the next
//!   `SpawnEntity` plus any later auth/component messages (they will flush
//!   once the spawn occurs).
//! * **`Spawned`** – entity is live; forwards component/auth messages to the
//!   corresponding sub‑channels and drains their output immediately.
//!
//! ---
//! ### 3 · Message ingest algorithm
//! 1. **Gating by `last_epoch_id `**
//!    A message whose `id ≤ last_epoch_id ` is *by definition* older than the
//!    authoritative `SpawnEntity`; drop it to guarantee *at‑most‑once
//!    semantics*; wrap‑around itself is handled automatically by the
//!    wrap‑safe `u16` comparison helpers—no epoch reset is performed.
//! 2. **Buffered queue (`OrderedIds`)**  
//!    Messages are pushed into `buffered_messages`, ordered by the `u16`
//!    sequence with wrap‑safe comparison.  
//!    `process_messages()` iterates from the head while the next candidate is
//!    *legal* under the current FSM state.
//! 3. **Draining**  
//!    Once a message is applied, it is moved into `outgoing_messages`.  
//!    `Engine::drain_messages_into` later annotates them with the concrete
//!    entity handle and forwards them to the ECS.
//!
//! ---
//! ### 4 · Sub‑channels
//! * **`AuthChannel`** – publishes, unpublishes, and delegates authority.
//! * **`ComponentChannel`** (one per `ComponentKind`) – tracks insert/remove
//!   toggles, guaranteeing idempotency via its own `last_insert_id` guard.
//!
//! `EntityChannel` coordinates these sub‑channels but *never* peers inside
//! their logic; it merely aligns their buffers with the entity’s lifecycle
//! (e.g., flush everything ≤ `idₛ` at spawn, reset on despawn).
//!
//! ---
//! ### 5 · Key invariants
//! * **Spawn barrier** – No component/auth message can overtake the spawn
//!   that legitimises it.
//! * **Monotonic visibility** – Once a message has been emitted to
//!   `outgoing_messages`, the channel guarantees it will never retract or
//!   reorder that message.
//!
//! Together, these guarantees let higher layers treat the engine as if every
//! entity had its own perfect *ordered* stream—while the network enjoys the
//! performance of a single unordered reliable channel.

use std::{
    collections::{HashMap, HashSet},
    hash::Hash,
};

use crate::{
    sequence_less_than, world::sync::remote_component_channel::RemoteComponentChannel,
    ComponentKind, EntityAuthStatus, EntityCommand, EntityMessage, EntityMessageType, HostType,
    MessageIndex,
};

cfg_if! {
    if #[cfg(feature = "e2e_debug")] {
        use crate::world::host::host_world_manager::SubCommandId;
    }
}

/// Spawn/despawn lifecycle state of a remote entity channel.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "e2e_debug", allow(dead_code))]
pub enum EntityChannelState {
    /// Entity has not yet been spawned (or has been despawned).
    Despawned,
    /// Entity is live; component and authority messages are forwarded immediately.
    Spawned,
}

/// Per-entity demultiplexer that buffers and reorders incoming messages from an unordered reliable channel into a causal stream.
pub struct RemoteEntityChannel {
    state: EntityChannelState,
    last_epoch_id: Option<MessageIndex>,

    component_channels: HashMap<ComponentKind, RemoteComponentChannel>,
    auth_channel: AuthChannel,

    buffered_messages: OrderedIds<EntityMessage<()>>,
    incoming_messages: Vec<EntityMessage<()>>,
    outgoing_commands: Vec<EntityCommand>,
}

impl RemoteEntityChannel {
    /// Creates a fresh `RemoteEntityChannel` in the `Despawned` state for `host_type`.
    pub fn new(host_type: HostType) -> Self {
        Self {
            state: EntityChannelState::Despawned,
            last_epoch_id: None,

            component_channels: HashMap::new(),
            auth_channel: AuthChannel::new(host_type),

            buffered_messages: OrderedIds::new(),
            incoming_messages: Vec::new(),
            outgoing_commands: Vec::new(),
        }
    }

    /// Create a RemoteEntityChannel for a delegated entity (used during migration)
    ///
    /// After migration, MigrateResponse has subcommand_id=0, so the next message (SetAuthority)
    /// will have subcommand_id=1. We need to sync the receiver's next_subcommand_id accordingly.
    pub fn new_delegated(host_type: HostType) -> Self {
        let mut channel = Self::new(host_type);
        channel.configure_as_delegated();
        channel
    }

    /// Configures the auth sub-channel into the `Delegated` state, simulating `Publish → EnableDelegation` transitions.
    pub fn configure_as_delegated(&mut self) {
        // Set up the AuthChannel for a delegated entity
        // This simulates the entity having gone through Publish → EnableDelegation
        self.auth_channel.force_publish();
        self.auth_channel.force_enable_delegation();
        // Sync subcommand_id: MigrateResponse has subcommand_id=0, so next is 1
        self.auth_channel.receiver_set_next_subcommand_id(1);
    }

    /// Overrides the authority status stored in the auth sub-channel to match the global tracker after migration.
    pub fn update_auth_status(&mut self, auth_status: EntityAuthStatus) {
        self.auth_channel.force_set_auth_status(auth_status);
    }

    /// Returns the current authority status recorded in the auth sub-channel.
    pub fn auth_status(&self) -> Option<EntityAuthStatus> {
        self.auth_channel.auth_status()
    }

    /// Returns `true` if the auth sub-channel is in the Delegated state.
    pub fn is_delegated(&self) -> bool {
        self.auth_channel.is_delegated()
    }

    pub(crate) fn receive_message(&mut self, id: MessageIndex, msg: EntityMessage<()>) {
        if let Some(last_epoch_id) = self.last_epoch_id {
            if last_epoch_id == id {
                panic!("EntityChannel received a message with the same id as the last epoch id. This should not happen. Message: {:?}", msg);
            }

            if sequence_less_than(id, last_epoch_id) {
                // This message is older than the last spawn message, ignore it
                return;
            }
        }

        self.buffered_messages.push_back(id, msg);

        self.process_messages();
    }

    /// Enqueues `command` through the authority sub-channel for outbound delivery.
    pub fn send_command(&mut self, command: EntityCommand) {
        self.auth_channel.send_command(command);
        self.auth_channel
            .sender_drain_messages_into(&mut self.outgoing_commands);
    }

    pub(crate) fn drain_incoming_messages_into<E: Copy + Hash + Eq>(
        &mut self,
        entity: E,
        outgoing_events: &mut Vec<EntityMessage<E>>,
    ) {
        // Drain the entity channel and append the messages to the outgoing events
        let mut received_messages = Vec::new();
        for rmsg in std::mem::take(&mut self.incoming_messages) {
            received_messages.push(rmsg.with_entity(entity));
        }
        outgoing_events.append(&mut received_messages);
    }

    pub(crate) fn drain_outgoing_messages_into(
        &mut self,
        outgoing_commands: &mut Vec<EntityCommand>,
    ) {
        outgoing_commands.append(&mut self.outgoing_commands);
    }

    pub(crate) fn has_component_kind(&self, component_kind: &ComponentKind) -> bool {
        self.component_channels.contains_key(component_kind)
    }

    fn process_messages(&mut self) {
        loop {
            let Some((id, msg)) = self.buffered_messages.peek_front() else {
                break;
            };
            let id = *id;

            match msg.get_type() {
                EntityMessageType::Spawn => {
                    if self.state != EntityChannelState::Despawned {
                        break;
                    }

                    self.state = EntityChannelState::Spawned;
                    // Count when Spawn transitions state to Spawned
                    #[cfg(feature = "e2e_debug")]
                    {
                        extern "Rust" {
                            fn client_processed_spawn_increment();
                        }
                        // Safety: atomic counter defined by the naia-tests harness under
                        // e2e_debug; no preconditions; never active in production builds.
                        unsafe {
                            client_processed_spawn_increment();
                        }
                    }
                    self.last_epoch_id = Some(id);
                    // clear buffered messages less than or equal to the last spawn id
                    self.buffered_messages.pop_front_until_and_excluding(id);

                    self.pop_front_into_outgoing();

                    // Drain the auth channel and append the messages to the outgoing events
                    self.auth_channel.receiver_buffer_pop_front_until_and_including(id);

                    self.auth_channel.receiver_process_messages(self.state);
                    self.auth_channel.receiver_drain_messages_into(&mut self.incoming_messages);

                    // Pop buffered messages from the component channels until and excluding the spawn id
                    // Then process the messages in the component channels
                    // Then drain the messages into the outgoing messages
                    for (component_kind, component_channel) in self.component_channels.iter_mut() {
                        component_channel.buffer_pop_front_until_and_excluding(id);
                        component_channel.process_messages(self.state);
                        component_channel.drain_messages_into(component_kind, &mut self.incoming_messages);
                    }
                }
                EntityMessageType::SpawnWithComponents => {
                    if self.state != EntityChannelState::Despawned {
                        break;
                    }

                    self.state = EntityChannelState::Spawned;
                    self.last_epoch_id = Some(id);
                    // Discard stale pre-lifetime messages buffered before this id
                    self.buffered_messages.pop_front_until_and_excluding(id);

                    // Pop the SpawnWithComponents message itself
                    let (_, msg) = self.buffered_messages.pop_front().unwrap();
                    let kinds = match msg {
                        EntityMessage::SpawnWithComponents((), kinds) => kinds,
                        _ => unreachable!(),
                    };

                    // Emit synthetic Spawn event
                    self.incoming_messages.push(EntityMessage::Spawn(()));

                    // Process any pre-buffered component channels (out-of-order arrivals)
                    for (component_kind, component_channel) in self.component_channels.iter_mut() {
                        component_channel.buffer_pop_front_until_and_excluding(id);
                        component_channel.process_messages(self.state);
                        component_channel.drain_messages_into(component_kind, &mut self.incoming_messages);
                    }

                    // Accept coalesced components: mark inserted + emit InsertComponent events
                    for kind in &kinds {
                        let component_channel = self.component_channels
                            .entry(*kind)
                            .or_insert_with(RemoteComponentChannel::new);
                        component_channel.set_inserted(true, id);
                        self.incoming_messages.push(EntityMessage::InsertComponent((), *kind));
                    }

                    // Drain auth channel
                    self.auth_channel.receiver_buffer_pop_front_until_and_including(id);
                    self.auth_channel.receiver_process_messages(self.state);
                    self.auth_channel.receiver_drain_messages_into(&mut self.incoming_messages);
                }
                EntityMessageType::Despawn => {
                    if self.state != EntityChannelState::Spawned {
                        break;
                    }

                    self.state = EntityChannelState::Despawned;
                    self.last_epoch_id = Some(id);

                    self.auth_channel.reset();
                    self.component_channels.clear();

                    self.pop_front_into_outgoing();

                    // clear the buffer
                    self.buffered_messages.clear();
                }
                EntityMessageType::InsertComponent | EntityMessageType::RemoveComponent => {

                    let (id, msg) = self.buffered_messages.pop_front().unwrap();
                    let component_kind = msg.component_kind().unwrap();
                    let component_channel = self.component_channels
                        .entry(component_kind)
                        .or_insert_with(RemoteComponentChannel::new);

                    component_channel.accept_message(self.state, id, msg);
                    component_channel.drain_messages_into(&component_kind, &mut self.incoming_messages);
                }
                EntityMessageType::Publish | EntityMessageType::Unpublish |
                EntityMessageType::EnableDelegation | EntityMessageType::DisableDelegation |
                EntityMessageType::ReleaseAuthority | // NOTE: This should be possible because a client might want to release authority right after enabling delegation
                EntityMessageType::SetAuthority => {
                    let (id, msg) = self.buffered_messages.pop_front().unwrap();
                    // info!("EntityChannelReceiver::process_messages(id={}, msgType={:?})", id, msg.get_type());

                    self.auth_channel.receiver_receive_message(Some(self.state), id, msg);
                    // Only drain auth messages when entity is Spawned (spawn barrier contract)
                    if self.state == EntityChannelState::Spawned {
                        self.auth_channel.receiver_drain_messages_into(&mut self.incoming_messages);
                    }
                    // When Despawned, message stays buffered in auth_channel until Spawn arm drains it
                }
                EntityMessageType::Noop => {
                    // Drop it
                }
                msg => {
                    panic!("EntityChannel::accept_message() received an unexpected message type: {:?}", msg);
                }
            }
        }
    }

    fn pop_front_into_outgoing(&mut self) {
        let (_, msg) = self.buffered_messages.pop_front().unwrap();
        self.incoming_messages.push(msg);
    }

    #[allow(dead_code)] // used in migration unit tests
    pub(crate) fn get_state(&self) -> EntityChannelState {
        self.state
    }

    #[cfg(feature = "e2e_debug")]
    pub(crate) fn debug_auth_diagnostic(
        &self,
    ) -> (
        EntityChannelState,
        (SubCommandId, usize, Option<SubCommandId>, usize),
    ) {
        let auth_diag = self.auth_channel.receiver_debug_diagnostic();
        (self.state, auth_diag)
    }

    #[cfg(feature = "e2e_debug")]
    pub(crate) fn debug_channel_snapshot(
        &self,
    ) -> (
        EntityChannelState,
        Option<MessageIndex>,
        usize,
        Option<(MessageIndex, EntityMessageType)>,
        Option<MessageIndex>,
    ) {
        let state = self.state;
        let last_epoch_id = self.last_epoch_id;
        let buffered_len = self.buffered_messages.len();
        let head = self
            .buffered_messages
            .peek_front()
            .map(|(id, msg)| (*id, msg.get_type()));
        let spawn_id = self
            .buffered_messages
            .find_by_predicate(|msg| msg.get_type() == EntityMessageType::Spawn)
            .map(|(id, _)| id);
        (state, last_epoch_id, buffered_len, head, spawn_id)
    }

    pub(crate) fn extract_inserted_component_kinds(&self) -> HashSet<ComponentKind> {
        self.component_channels
            .iter()
            .filter(|(_, channel)| channel.is_inserted())
            .map(|(kind, _)| *kind)
            .collect()
    }

    pub(crate) fn force_drain_all_buffers(&mut self) {
        // Force-drain entity-level buffered messages
        while let Some((_, msg)) = self.buffered_messages.pop_front() {
            self.incoming_messages.push(msg);
        }

        // Force-drain all component channels
        for (_, component_channel) in self.component_channels.iter_mut() {
            component_channel.force_drain_buffers(self.state);
        }
    }

    pub(crate) fn insert_component(&mut self, component_kind: ComponentKind) {
        self.component_channels.entry(component_kind).or_insert_with(RemoteComponentChannel::new);
    }

    pub(crate) fn remove_component(&mut self, component_kind: ComponentKind) {
        self.component_channels.remove(&component_kind);
    }

    pub(crate) fn set_spawned(&mut self, epoch_id: MessageIndex) {
        if self.state != EntityChannelState::Despawned {
            panic!("Can only set spawned on despawned entity");
        }
        self.state = EntityChannelState::Spawned;
        self.last_epoch_id = Some(epoch_id);
    }

    pub(crate) fn insert_component_channel_as_inserted(
        &mut self,
        component_kind: ComponentKind,
        epoch_id: MessageIndex,
    ) {
        let mut comp_channel = RemoteComponentChannel::new();
        comp_channel.set_inserted(true, epoch_id);
        self.component_channels.insert(component_kind, comp_channel);
    }

    #[allow(dead_code)] // used in bulletproof migration unit tests
    pub(crate) fn take_incoming_events(&mut self) -> Vec<EntityMessage<()>> {
        std::mem::take(&mut self.incoming_messages)
    }
}

use crate::world::sync::auth_channel::AuthChannel;
use crate::world::sync::ordered_ids::OrderedIds;