Skip to main content

atomr_remote/
envelope.rs

1//! Remote message envelope. akka.net: `Remote/MessageSerializer.cs` +
2//! `Remote/RemoteEnvelope.cs`.
3
4use serde::{Deserialize, Serialize};
5
6/// A `RemoteEnvelope` is the unit of payload delivery — one user message
7/// (or system control) bound for one recipient. The envelope itself is
8/// always serialized via bincode regardless of which `serializer_id` is
9/// used for the inner `payload`.
10#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
11pub struct RemoteEnvelope {
12    /// Full path string of the recipient, including address scheme.
13    pub recipient_path: String,
14    /// Optional sender path for `tell_with_sender` / ask routing.
15    pub sender_path: Option<String>,
16    /// UID of the sending `ActorSystem` (lets the receiver detect a peer
17    /// restart and discard stale state).
18    pub sender_uid: u64,
19    /// Recipient's expected `ActorSystem` UID; receiver drops envelopes
20    /// whose `recipient_uid` does not match the local UID, which avoids
21    /// surprise message delivery to a freshly-restarted system.
22    /// `0` means "any UID" (best-effort delivery).
23    pub recipient_uid: u64,
24    /// Monotonic sequence number assigned by the sending Endpoint. Used
25    /// for ack'd delivery and duplicate suppression.
26    pub seq_no: u64,
27    /// Serializer identifier (looked up in the receiving system's
28    /// `SerializerRegistry`).
29    pub serializer_id: u32,
30    /// Type manifest — usually `std::any::type_name::<M>()`.
31    pub manifest: String,
32    /// `true` for `RemoteSystemMsg` (Stop/Watch/Unwatch/Terminated),
33    /// `false` for user messages.
34    pub system: bool,
35    /// Serialized payload bytes.
36    pub payload: Vec<u8>,
37}
38
39impl RemoteEnvelope {
40    #[allow(clippy::too_many_arguments)]
41    pub fn user(
42        recipient: impl Into<String>,
43        sender: Option<String>,
44        sender_uid: u64,
45        recipient_uid: u64,
46        seq_no: u64,
47        serializer_id: u32,
48        manifest: impl Into<String>,
49        payload: Vec<u8>,
50    ) -> Self {
51        Self {
52            recipient_path: recipient.into(),
53            sender_path: sender,
54            sender_uid,
55            recipient_uid,
56            seq_no,
57            serializer_id,
58            manifest: manifest.into(),
59            system: false,
60            payload,
61        }
62    }
63
64    pub fn system_msg(
65        recipient: impl Into<String>,
66        sender_uid: u64,
67        recipient_uid: u64,
68        seq_no: u64,
69        manifest: impl Into<String>,
70        payload: Vec<u8>,
71    ) -> Self {
72        Self {
73            recipient_path: recipient.into(),
74            sender_path: None,
75            sender_uid,
76            recipient_uid,
77            seq_no,
78            serializer_id: crate::serialization::SYSTEM_SERIALIZER_ID,
79            manifest: manifest.into(),
80            system: true,
81            payload,
82        }
83    }
84}