Skip to main content

rustorb_rtps/
receiver.rs

1//! The RTPS message receiver (DDSI-RTPS 2.5, 8.3.4): walks the
2//! submessages of a message and keeps the receiver state that tells
3//! entity submessages where they come from and who they are for.
4
5use crate::Octets;
6use crate::message::{Header, Message, Submessages};
7use crate::submessage::Submessage;
8use crate::types::{GuidPrefix, Time};
9
10/// An entity submessage with the receiver state that applies to it.
11#[derive(Clone, Debug, PartialEq, Eq)]
12pub struct Received {
13    /// `sourceGuidPrefix`: the participant the submessage comes from.
14    pub source_prefix: GuidPrefix,
15    /// `timestamp` set by the last INFO_TS, if any.
16    pub timestamp: Option<Time>,
17    /// The submessage: one of DATA, DATA_FRAG, HEARTBEAT, HEARTBEAT_FRAG,
18    /// GAP, ACKNACK, NACK_FRAG or an unknown one.
19    pub submessage: Submessage,
20}
21
22/// Reads a message for the participant `local` and returns its entity
23/// submessages.
24///
25/// The interpreter submessages update the state as in 8.3.4.1: INFO_SRC
26/// sets the source, INFO_DST sets the destination and INFO_TS the time.
27/// Entity submessages whose destination is neither `local` nor unknown
28/// are left out. Reading stops at the first invalid submessage, and the
29/// submessages before it are kept (8.3.4.1 rule 3 and 8.3.4.2). `None`
30/// means the header itself is invalid.
31pub fn receive(bytes: &[u8], local: GuidPrefix) -> Option<(Header, Vec<Received>)> {
32    let (header, submessages) = Message::parse(bytes).ok()?;
33    Some(walk(header, submessages, local))
34}
35
36/// Like [`receive`], with the payloads of DATA and DATA_FRAG as slices of
37/// `bytes` instead of copies.
38pub fn receive_shared(bytes: &Octets, local: GuidPrefix) -> Option<(Header, Vec<Received>)> {
39    let (header, submessages) = Message::parse_shared(bytes).ok()?;
40    Some(walk(header, submessages, local))
41}
42
43fn walk(
44    header: Header,
45    submessages: Submessages<'_>,
46    local: GuidPrefix,
47) -> (Header, Vec<Received>) {
48    let mut source = header.guid_prefix;
49    let mut dest_ok = true;
50    let mut timestamp = None;
51    let mut out = Vec::new();
52    for sub in submessages {
53        let Ok(sub) = sub else { break };
54        match sub {
55            Submessage::InfoSource(s) => {
56                source = s.guid_prefix;
57                // INFO_SRC also clears the time (8.3.8.10 "Change in
58                // state of Receiver").
59                timestamp = None;
60            }
61            Submessage::InfoDestination(d) => {
62                dest_ok = d.guid_prefix == GuidPrefix::UNKNOWN || d.guid_prefix == local;
63            }
64            Submessage::InfoTimestamp(t) => timestamp = t.timestamp,
65            Submessage::InfoReply(_)
66            | Submessage::InfoReplyIp4(_)
67            | Submessage::Pad(_)
68            | Submessage::HeaderExtension(_) => {}
69            other => {
70                if dest_ok {
71                    out.push(Received {
72                        source_prefix: source,
73                        timestamp,
74                        submessage: other,
75                    });
76                }
77            }
78        }
79    }
80    (header, out)
81}
82
83#[cfg(test)]
84mod tests {
85    use super::*;
86    use crate::submessage::{Gap, InfoDestination, InfoSource, InfoTimestamp};
87    use crate::types::{EntityId, ProtocolVersion, SequenceNumber, SequenceNumberSet, VendorId};
88    use crate::wire::Endianness;
89
90    fn gap(n: i64) -> Submessage {
91        Submessage::Gap(Gap {
92            reader_id: EntityId::UNKNOWN,
93            writer_id: EntityId::UNKNOWN,
94            gap_start: SequenceNumber(n),
95            gap_list: SequenceNumberSet::new(SequenceNumber(n + 1), 0).unwrap(),
96            group_info: None,
97            filtered_count: None,
98        })
99    }
100
101    #[test]
102    fn state() {
103        let local = GuidPrefix([1; 12]);
104        let mut m = Message::new(Header::new(VendorId([1, 1]), GuidPrefix([2; 12])));
105        let ts = Time {
106            seconds: 5,
107            fraction: 0,
108        };
109        m.submessages.push(gap(1));
110        m.submessages.push(Submessage::InfoTimestamp(InfoTimestamp {
111            timestamp: Some(ts),
112        }));
113        m.submessages
114            .push(Submessage::InfoDestination(InfoDestination {
115                guid_prefix: GuidPrefix([3; 12]),
116            }));
117        m.submessages.push(gap(2));
118        m.submessages
119            .push(Submessage::InfoDestination(InfoDestination {
120                guid_prefix: local,
121            }));
122        m.submessages.push(gap(3));
123        m.submessages.push(Submessage::InfoSource(InfoSource {
124            protocol_version: ProtocolVersion::CURRENT,
125            vendor_id: VendorId([1, 1]),
126            guid_prefix: GuidPrefix([4; 12]),
127        }));
128        m.submessages.push(gap(4));
129        let bytes = m.encode(Endianness::Little).unwrap();
130        let (h, r) = receive(&bytes, local).unwrap();
131        assert_eq!(h.guid_prefix, GuidPrefix([2; 12]));
132        assert_eq!(r.len(), 3);
133        assert_eq!(r[0].submessage, gap(1));
134        assert_eq!(r[0].timestamp, None);
135        assert_eq!(r[1].submessage, gap(3));
136        assert_eq!(r[1].timestamp, Some(ts));
137        assert_eq!(r[1].source_prefix, GuidPrefix([2; 12]));
138        assert_eq!(r[2].submessage, gap(4));
139        assert_eq!(r[2].source_prefix, GuidPrefix([4; 12]));
140        assert_eq!(r[2].timestamp, None);
141        assert!(receive(b"RTPX", local).is_none());
142    }
143}