Skip to main content

moq_transport/message/
subscribe_update.rs

1use crate::coding::{Decode, DecodeError, Encode, EncodeError, KeyValuePairs, Location};
2
3/// Sent by the subscriber to request all future objects for the given track.
4///
5/// Objects will use the provided ID instead of the full track name, to save bytes.
6#[derive(Clone, Debug, Eq, PartialEq)]
7pub struct SubscribeUpdate {
8    /// The request ID of this request
9    pub id: u64,
10
11    /// The request ID of the SUBSCRIBE this message is updating.
12    pub subscription_request_id: u64,
13
14    /// The starting location
15    pub start_location: Location,
16    /// The end Group ID, plus 1.  A value of 0 means the subscription is open-ended.
17    pub end_group_id: u64,
18
19    /// Subscriber Priority
20    pub subscriber_priority: u8,
21
22    /// Forward Flag
23    pub forward: bool,
24
25    /// Optional parameters
26    pub params: KeyValuePairs,
27}
28
29impl Decode for SubscribeUpdate {
30    fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
31        let id = u64::decode(r)?;
32
33        let subscription_request_id = u64::decode(r)?;
34
35        let start_location = Location::decode(r)?;
36        let end_group_id = u64::decode(r)?;
37
38        let subscriber_priority = u8::decode(r)?;
39
40        let forward = bool::decode(r)?;
41
42        let params = KeyValuePairs::decode(r)?;
43
44        Ok(Self {
45            id,
46            subscription_request_id,
47            start_location,
48            end_group_id,
49            subscriber_priority,
50            forward,
51            params,
52        })
53    }
54}
55
56impl Encode for SubscribeUpdate {
57    fn encode<W: bytes::BufMut>(&self, w: &mut W) -> Result<(), EncodeError> {
58        self.id.encode(w)?;
59
60        self.subscription_request_id.encode(w)?;
61
62        self.start_location.encode(w)?;
63        self.end_group_id.encode(w)?;
64
65        self.subscriber_priority.encode(w)?;
66
67        self.forward.encode(w)?;
68
69        self.params.encode(w)?;
70
71        Ok(())
72    }
73}
74
75#[cfg(test)]
76mod tests {
77    use super::*;
78    use bytes::BytesMut;
79
80    #[test]
81    fn encode_decode() {
82        let mut buf = BytesMut::new();
83
84        // One parameter for testing
85        let mut kvps = KeyValuePairs::new();
86        kvps.set_intvalue(124, 456);
87
88        let msg = SubscribeUpdate {
89            id: 1000,
90            subscription_request_id: 924,
91            start_location: Location::new(1, 1),
92            end_group_id: 100000,
93            subscriber_priority: 127,
94            forward: true,
95            params: kvps.clone(),
96        };
97        msg.encode(&mut buf).unwrap();
98        let decoded = SubscribeUpdate::decode(&mut buf).unwrap();
99        assert_eq!(decoded, msg);
100    }
101}