moq_transfork/message/
subscribe.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use std::time;

use crate::{
	coding::{Decode, DecodeError, Encode},
	message::group,
	Path,
};

/// Sent by the subscriber to request all future objects for the given track.
///
/// Objects will use the provided ID instead of the full track name, to save bytes.
#[derive(Clone, Debug)]
pub struct Subscribe {
	pub id: u64,
	pub path: Path,
	pub priority: i8,

	pub group_order: group::GroupOrder,
	pub group_expires: time::Duration,
	pub group_min: Option<u64>,
	pub group_max: Option<u64>,
}

impl Decode for Subscribe {
	fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
		let id = u64::decode_more(r, 6)?;
		let path = Path::decode_more(r, 5)?;
		let priority = i8::decode_more(r, 4)?;

		let group_order = group::GroupOrder::decode_more(r, 3)?;
		let group_expires = time::Duration::decode_more(r, 2)?;
		let group_min = match u64::decode_more(r, 1)? {
			0 => None,
			n => Some(n - 1),
		};
		let group_max = match u64::decode(r)? {
			0 => None,
			n => Some(n - 1),
		};

		Ok(Self {
			id,
			path,
			priority,

			group_order,
			group_expires,
			group_min,
			group_max,
		})
	}
}

impl Encode for Subscribe {
	fn encode<W: bytes::BufMut>(&self, w: &mut W) {
		self.id.encode(w);
		self.path.encode(w);
		self.priority.encode(w);

		self.group_order.encode(w);
		self.group_expires.encode(w);
		self.group_min.map(|v| v + 1).unwrap_or(0).encode(w);
		self.group_max.map(|v| v + 1).unwrap_or(0).encode(w);
	}
}

#[derive(Clone, Debug)]
pub struct SubscribeUpdate {
	pub priority: u64,

	pub group_order: group::GroupOrder,
	pub group_expires: time::Duration,
	pub group_min: Option<u64>,
	pub group_max: Option<u64>,
}

impl Decode for SubscribeUpdate {
	fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
		let priority = u64::decode_more(r, 4)?;
		let group_order = group::GroupOrder::decode_more(r, 3)?;
		let group_expires = time::Duration::decode_more(r, 2)?;
		let group_min = match u64::decode_more(r, 1)? {
			0 => None,
			n => Some(n - 1),
		};
		let group_max = match u64::decode(r)? {
			0 => None,
			n => Some(n - 1),
		};

		Ok(Self {
			priority,
			group_order,
			group_expires,
			group_min,
			group_max,
		})
	}
}

impl Encode for SubscribeUpdate {
	fn encode<W: bytes::BufMut>(&self, w: &mut W) {
		self.priority.encode(w);
		self.group_order.encode(w);
		self.group_min.map(|v| v + 1).unwrap_or(0).encode(w);
		self.group_max.map(|v| v + 1).unwrap_or(0).encode(w);
	}
}