moq_transfork/message/
subscribe.rs

1use crate::{
2	coding::{Decode, DecodeError, Encode},
3	message::group,
4};
5
6/// Sent by the subscriber to request all future objects for the given track.
7///
8/// Objects will use the provided ID instead of the full track name, to save bytes.
9#[derive(Clone, Debug)]
10pub struct Subscribe {
11	pub id: u64,
12	pub path: String,
13	pub priority: i8,
14
15	pub group_order: group::GroupOrder,
16	pub group_min: Option<u64>,
17	pub group_max: Option<u64>,
18}
19
20impl Decode for Subscribe {
21	fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
22		let id = u64::decode(r)?;
23		let path = String::decode(r)?;
24		let priority = i8::decode(r)?;
25
26		let group_order = group::GroupOrder::decode(r)?;
27		let group_min = match u64::decode(r)? {
28			0 => None,
29			n => Some(n - 1),
30		};
31		let group_max = match u64::decode(r)? {
32			0 => None,
33			n => Some(n - 1),
34		};
35
36		Ok(Self {
37			id,
38			path,
39			priority,
40
41			group_order,
42			group_min,
43			group_max,
44		})
45	}
46}
47
48impl Encode for Subscribe {
49	fn encode<W: bytes::BufMut>(&self, w: &mut W) {
50		self.id.encode(w);
51		self.path.encode(w);
52		self.priority.encode(w);
53
54		self.group_order.encode(w);
55		self.group_min.map(|v| v + 1).unwrap_or(0).encode(w);
56		self.group_max.map(|v| v + 1).unwrap_or(0).encode(w);
57	}
58}
59
60#[derive(Clone, Debug)]
61pub struct SubscribeUpdate {
62	pub priority: u64,
63
64	pub group_order: group::GroupOrder,
65	pub group_min: Option<u64>,
66	pub group_max: Option<u64>,
67}
68
69impl Decode for SubscribeUpdate {
70	fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
71		let priority = u64::decode(r)?;
72		let group_order = group::GroupOrder::decode(r)?;
73		let group_min = match u64::decode(r)? {
74			0 => None,
75			n => Some(n - 1),
76		};
77		let group_max = match u64::decode(r)? {
78			0 => None,
79			n => Some(n - 1),
80		};
81
82		Ok(Self {
83			priority,
84			group_order,
85			group_min,
86			group_max,
87		})
88	}
89}
90
91impl Encode for SubscribeUpdate {
92	fn encode<W: bytes::BufMut>(&self, w: &mut W) {
93		self.priority.encode(w);
94		self.group_order.encode(w);
95		self.group_min.map(|v| v + 1).unwrap_or(0).encode(w);
96		self.group_max.map(|v| v + 1).unwrap_or(0).encode(w);
97	}
98}