Skip to main content

atomr_remote/
pdu.rs

1//! Akka protocol data units. akka.net: `Remote/Transport/AkkaPduCodec.cs`.
2//!
3//! Every byte that crosses an association is one of these PDUs:
4//!
5//! * `Associate`  — handshake, initiated by the connecting side.
6//! * `Disassociate` — graceful or quarantine teardown.
7//! * `Heartbeat` — liveness ping when the writer is otherwise idle.
8//! * `Payload` — a user / system `RemoteEnvelope`.
9//! * `Ack` — sliding-window acknowledgement.
10//!
11//! Wire format: bincode-serialized `AkkaPdu` framed by the transport
12//! (length-prefix u32 big-endian).
13
14use serde::{Deserialize, Serialize};
15
16use atomr_core::actor::Address;
17
18use crate::envelope::RemoteEnvelope;
19
20/// One frame on the wire.
21#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
22pub enum AkkaPdu {
23    Associate(AssociateInfo),
24    Disassociate(DisassociateReason),
25    Heartbeat,
26    Payload(RemoteEnvelope),
27    Ack(AckInfo),
28}
29
30/// Carried in the initial `Associate` PDU. The receiving side validates
31/// the cookie (if any) and uses `origin` + `uid` to identify the peer.
32#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
33pub struct AssociateInfo {
34    pub origin: Address,
35    pub uid: u64,
36    pub cookie: Option<String>,
37    pub protocol_version: u32,
38}
39
40/// Why a peer is disassociating. `Quarantined` is permanent until the
41/// quarantine window expires.
42#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
43pub enum DisassociateReason {
44    /// Normal shutdown — the peer is cooperating.
45    Normal,
46    /// The peer rejected our handshake (bad cookie, mismatched protocol).
47    HandshakeFailure(String),
48    /// We detected a UID change and are quarantining the old incarnation.
49    Quarantined,
50    /// Catch-all error.
51    Other(String),
52}
53
54/// Sliding-window ack. `cumulative_ack` is the highest `seq_no` we have
55/// successfully delivered; `nacks` is the set of explicitly missing seq
56/// numbers below that watermark which the sender should resend.
57#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
58pub struct AckInfo {
59    pub cumulative_ack: u64,
60    pub nacks: Vec<u64>,
61}
62
63/// Wire protocol version. Bump only on backward-incompatible changes.
64pub const PROTOCOL_VERSION: u32 = 1;