palladium_actor/
envelope.rs1use crate::path::AddrHash;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11#[repr(C)]
12pub struct Envelope {
13 pub message_id: u128,
14 pub source: AddrHash,
15 pub destination: AddrHash,
16 pub type_tag: u64,
17 pub payload_len: u32,
18 pub flags: u32,
19 pub correlation_id: u64,
20}
21
22impl Envelope {
23 pub const SIZE: usize = 80;
29 pub const WIRE_SIZE: usize = 41;
31
32 pub const FLAG_RESPONSE: u32 = 1 << 0;
33 pub const FLAG_PRIORITY_MASK: u32 = 0b11 << 1;
34
35 pub fn new(source: AddrHash, destination: AddrHash, type_tag: u64, payload_len: u32) -> Self {
36 Self {
37 message_id: 0,
38 source,
39 destination,
40 type_tag,
41 payload_len,
42 flags: 0,
43 correlation_id: 0,
44 }
45 }
46
47 pub fn response(&self, payload_len: u32) -> Self {
50 let mut e = Self::new(self.destination, self.source, self.type_tag, payload_len);
51 e.message_id = self.message_id;
52 e.correlation_id = self.message_id as u64;
53 e.flags = Self::FLAG_RESPONSE | (self.flags & Self::FLAG_PRIORITY_MASK);
54 e
55 }
56
57 pub fn with_priority(mut self, priority: u8) -> Self {
58 let p = (priority.min(3) as u32) << 1;
59 self.flags = (self.flags & !Self::FLAG_PRIORITY_MASK) | p;
60 self
61 }
62
63 pub fn priority(&self) -> u8 {
64 ((self.flags & Self::FLAG_PRIORITY_MASK) >> 1) as u8
65 }
66
67 pub fn is_response(&self) -> bool {
68 (self.flags & Self::FLAG_RESPONSE) != 0
69 }
70
71 pub fn to_bytes(&self) -> [u8; Self::SIZE] {
75 let mut buf = [0u8; Self::SIZE];
76 buf[0..16].copy_from_slice(&self.message_id.to_le_bytes());
77 buf[16..32].copy_from_slice(&self.source.0.to_le_bytes());
78 buf[32..48].copy_from_slice(&self.destination.0.to_le_bytes());
79 buf[48..56].copy_from_slice(&self.type_tag.to_le_bytes());
80 buf[56..60].copy_from_slice(&self.payload_len.to_le_bytes());
81 buf[60..64].copy_from_slice(&self.flags.to_le_bytes());
82 buf[64..72].copy_from_slice(&self.correlation_id.to_le_bytes());
83 buf
85 }
86
87 pub fn from_bytes(buf: &[u8; Self::SIZE]) -> Self {
89 Self {
90 message_id: u128::from_le_bytes(buf[0..16].try_into().expect("16 bytes")),
91 source: AddrHash(u128::from_le_bytes(
92 buf[16..32].try_into().expect("16 bytes"),
93 )),
94 destination: AddrHash(u128::from_le_bytes(
95 buf[32..48].try_into().expect("16 bytes"),
96 )),
97 type_tag: u64::from_le_bytes(buf[48..56].try_into().expect("8 bytes")),
98 payload_len: u32::from_le_bytes(buf[56..60].try_into().expect("4 bytes")),
99 flags: u32::from_le_bytes(buf[60..64].try_into().expect("4 bytes")),
100 correlation_id: u64::from_le_bytes(buf[64..72].try_into().expect("8 bytes")),
101 }
102 }
103
104 pub fn to_wire(&self) -> [u8; Self::WIRE_SIZE] {
112 let mut buf = [0u8; Self::WIRE_SIZE];
113 buf[0..16].copy_from_slice(&self.destination.0.to_le_bytes());
114 buf[16..32].copy_from_slice(&self.source.0.to_le_bytes());
115 buf[32..40].copy_from_slice(&self.correlation_id.to_le_bytes());
116 buf[40] = self.priority();
117 buf
118 }
119
120 pub fn from_wire(buf: &[u8; Self::WIRE_SIZE]) -> Self {
125 let mut env = Self {
126 message_id: 0,
127 source: AddrHash(u128::from_le_bytes(
128 buf[16..32].try_into().expect("16 bytes"),
129 )),
130 destination: AddrHash(u128::from_le_bytes(
131 buf[0..16].try_into().expect("16 bytes"),
132 )),
133 type_tag: 0,
134 payload_len: 0,
135 flags: 0,
136 correlation_id: u64::from_le_bytes(buf[32..40].try_into().expect("8 bytes")),
137 };
138 env = env.with_priority(buf[40]);
139 env
140 }
141}