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