Skip to main content

de_mls/ds/
transport.rs

1//! Transport-agnostic envelopes + delivery service interface.
2use crate::ds::DeliveryServiceError;
3/// A transport-agnostic packet that should be sent to the network.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct OutboundPacket {
6    pub payload: Vec<u8>,
7    pub subtopic: String,
8    pub group_id: String,
9    /// Application instance identifier. Transported as the `meta` field on the
10    /// wire (Waku message JSON). Used for self-message filtering.
11    pub app_id: Vec<u8>,
12}
13
14impl OutboundPacket {
15    pub fn new(payload: Vec<u8>, subtopic: &str, group_id: &str, app_id: &[u8]) -> Self {
16        Self {
17            payload,
18            subtopic: subtopic.to_string(),
19            group_id: group_id.to_string(),
20            app_id: app_id.to_vec(),
21        }
22    }
23}
24
25/// A transport-agnostic packet delivered from the network into the application.
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct InboundPacket {
28    pub payload: Vec<u8>,
29    pub subtopic: String,
30    pub group_id: String,
31    /// Transport-provided app instance id / metadata (used for self-message filtering).
32    pub app_id: Vec<u8>,
33    pub timestamp: i64,
34}
35
36impl InboundPacket {
37    pub fn new(
38        payload: Vec<u8>,
39        subtopic: &str,
40        group_id: &str,
41        app_id: Vec<u8>,
42        timestamp: i64,
43    ) -> Self {
44        Self {
45            payload,
46            subtopic: subtopic.to_string(),
47            group_id: group_id.to_string(),
48            app_id,
49            timestamp,
50        }
51    }
52}
53
54pub trait DeliveryService: Send + Sync + 'static {
55    /// Send a packet to the network and return a transport message id (if available).
56    fn send(&self, pkt: OutboundPacket) -> Result<String, DeliveryServiceError>;
57
58    /// Subscribe to inbound packets.
59    ///
60    /// Each call creates a new channel and registers its sender internally.
61    /// Senders are pruned when the corresponding receiver is dropped, but only
62    /// during the next inbound event dispatch. Avoid calling this in a loop
63    /// without dropping previous receivers.
64    fn subscribe(&self) -> std::sync::mpsc::Receiver<InboundPacket>;
65}