aboutsummaryrefslogtreecommitdiff
path: root/src/message.rs
blob: d918c2935ee81bf2a816c91af6429dc4d9273f8d (plain) (blame)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
use std::fmt;
use std::marker::PhantomData;
use std::sync::Arc;

use bytes::Bytes;
use serde::{Deserialize, Serialize};

use futures::stream::{Stream, StreamExt};

use crate::error::*;
use crate::util::*;

/// Priority of a request (click to read more about priorities).
///
/// This priority value is used to priorize messages
/// in the send queue of the client, and their responses in the send queue of the
/// server. Lower values mean higher priority.
///
/// This mechanism is usefull for messages bigger than the maximum chunk size
/// (set at `0x4000` bytes), such as large file transfers.
/// In such case, all of the messages in the send queue with the highest priority
/// will take turns to send individual chunks, in a round-robin fashion.
/// Once all highest priority messages are sent successfully, the messages with
/// the next highest priority will begin being sent in the same way.
///
/// The same priority value is given to a request and to its associated response.
pub type RequestPriority = u8;

/// Priority class: high
pub const PRIO_HIGH: RequestPriority = 0x20;
/// Priority class: normal
pub const PRIO_NORMAL: RequestPriority = 0x40;
/// Priority class: background
pub const PRIO_BACKGROUND: RequestPriority = 0x80;
/// Priority: primary among given class
pub const PRIO_PRIMARY: RequestPriority = 0x00;
/// Priority: secondary among given class (ex: `PRIO_HIGH | PRIO_SECONDARY`)
pub const PRIO_SECONDARY: RequestPriority = 0x01;

// ----

/// This trait should be implemented by all messages your application
/// wants to handle
pub trait Message: Serialize + for<'de> Deserialize<'de> + Send + Sync {
	type Response: Serialize + for<'de> Deserialize<'de> + Send + Sync;
}

pub struct Req<M: Message> {
	pub(crate) _phantom: PhantomData<M>,
	pub(crate) msg: Arc<M>,
	pub(crate) msg_ser: Option<Bytes>,
	pub(crate) body: BodyData,
}

pub struct Resp<M: Message> {
	pub(crate) _phantom: PhantomData<M>,
	pub(crate) msg: M::Response,
	pub(crate) body: BodyData,
}

pub(crate) enum BodyData {
	None,
	Fixed(Bytes),
	Stream(ByteStream),
}

impl BodyData {
	pub fn into_stream(self) -> Option<ByteStream> {
		match self {
			BodyData::None => None,
			BodyData::Fixed(b) => Some(Box::pin(futures::stream::once(async move { Ok(b) }))),
			BodyData::Stream(s) => Some(s),
		}
	}
}

// ----

impl<M: Message> Req<M> {
	pub fn msg(&self) -> &M {
		&self.msg
	}

	pub fn with_fixed_body(self, b: Bytes) -> Self {
		Self {
			body: BodyData::Fixed(b),
			..self
		}
	}

	pub fn with_streaming_body(self, b: ByteStream) -> Self {
		Self {
			body: BodyData::Stream(b),
			..self
		}
	}
}

pub trait IntoReq<M: Message> {
	fn into_req(self) -> Result<Req<M>, rmp_serde::encode::Error>;
	fn into_req_local(self) -> Req<M>;
}

impl<M: Message> IntoReq<M> for M {
	fn into_req(self) -> Result<Req<M>, rmp_serde::encode::Error> {
		let msg_ser = rmp_to_vec_all_named(&self)?;
		Ok(Req {
			_phantom: Default::default(),
			msg: Arc::new(self),
			msg_ser: Some(Bytes::from(msg_ser)),
			body: BodyData::None,
		})
	}
	fn into_req_local(self) -> Req<M> {
		Req {
			_phantom: Default::default(),
			msg: Arc::new(self),
			msg_ser: None,
			body: BodyData::None,
		}
	}
}

impl<M: Message> IntoReq<M> for Req<M> {
	fn into_req(self) -> Result<Req<M>, rmp_serde::encode::Error> {
		Ok(self)
	}
	fn into_req_local(self) -> Req<M> {
		self
	}
}

impl<M: Message> Clone for Req<M> {
	fn clone(&self) -> Self {
		let body = match &self.body {
			BodyData::None => BodyData::None,
			BodyData::Fixed(b) => BodyData::Fixed(b.clone()),
			BodyData::Stream(_) => panic!("Cannot clone a Req<_> with a stream body"),
		};
		Self {
			_phantom: Default::default(),
			msg: self.msg.clone(),
			msg_ser: self.msg_ser.clone(),
			body,
		}
	}
}

impl<M> fmt::Debug for Req<M>
where
	M: Message + fmt::Debug,
{
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
		write!(f, "Req[{:?}", self.msg)?;
		match &self.body {
			BodyData::None => write!(f, "]"),
			BodyData::Fixed(b) => write!(f, "; body={}]", b.len()),
			BodyData::Stream(_) => write!(f, "; body=stream]"),
		}
	}
}

impl<M> fmt::Debug for Resp<M>
where
	M: Message,
	<M as Message>::Response: fmt::Debug,
{
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
		write!(f, "Resp[{:?}", self.msg)?;
		match &self.body {
			BodyData::None => write!(f, "]"),
			BodyData::Fixed(b) => write!(f, "; body={}]", b.len()),
			BodyData::Stream(_) => write!(f, "; body=stream]"),
		}
	}
}

impl<M: Message> Resp<M> {
	pub fn new(v: M::Response) -> Self {
		Resp {
			_phantom: Default::default(),
			msg: v,
			body: BodyData::None,
		}
	}

	pub fn with_fixed_body(self, b: Bytes) -> Self {
		Self {
			body: BodyData::Fixed(b),
			..self
		}
	}

	pub fn with_streaming_body(self, b: ByteStream) -> Self {
		Self {
			body: BodyData::Stream(b),
			..self
		}
	}

	pub fn msg(&self) -> &M::Response {
		&self.msg
	}

	pub fn into_msg(self) -> M::Response {
		self.msg
	}
}

// ---- ----

pub(crate) struct QueryMessage<'a> {
	pub(crate) prio: RequestPriority,
	pub(crate) path: &'a [u8],
	pub(crate) telemetry_id: Option<Vec<u8>>,
	pub(crate) body: &'a [u8],
}

/// QueryMessage encoding:
/// - priority: u8
/// - path length: u8
/// - path: [u8; path length]
/// - telemetry id length: u8
/// - telemetry id: [u8; telemetry id length]
/// - body [u8; ..]
impl<'a> QueryMessage<'a> {
	pub(crate) fn encode(self) -> Vec<u8> {
		let tel_len = match &self.telemetry_id {
			Some(t) => t.len(),
			None => 0,
		};

		let mut ret = Vec::with_capacity(10 + self.path.len() + tel_len + self.body.len());

		ret.push(self.prio);

		ret.push(self.path.len() as u8);
		ret.extend_from_slice(self.path);

		if let Some(t) = self.telemetry_id {
			ret.push(t.len() as u8);
			ret.extend(t);
		} else {
			ret.push(0u8);
		}

		ret.extend_from_slice(self.body);

		ret
	}

	pub(crate) fn decode(bytes: &'a [u8]) -> Result<Self, Error> {
		if bytes.len() < 3 {
			return Err(Error::Message("Invalid protocol message".into()));
		}

		let path_length = bytes[1] as usize;
		if bytes.len() < 3 + path_length {
			return Err(Error::Message("Invalid protocol message".into()));
		}

		let telemetry_id_len = bytes[2 + path_length] as usize;
		if bytes.len() < 3 + path_length + telemetry_id_len {
			return Err(Error::Message("Invalid protocol message".into()));
		}

		let path = &bytes[2..2 + path_length];
		let telemetry_id = if telemetry_id_len > 0 {
			Some(bytes[3 + path_length..3 + path_length + telemetry_id_len].to_vec())
		} else {
			None
		};

		let body = &bytes[3 + path_length + telemetry_id_len..];

		Ok(Self {
			prio: bytes[0],
			path,
			telemetry_id,
			body,
		})
	}
}

// ---- ----

pub(crate) struct Framing {
	direct: Vec<u8>,
	stream: Option<ByteStream>,
}

impl Framing {
	pub fn new(direct: Vec<u8>, stream: Option<ByteStream>) -> Self {
		assert!(direct.len() <= u32::MAX as usize);
		Framing { direct, stream }
	}

	pub fn into_stream(self) -> ByteStream {
		use futures::stream;
		let len = self.direct.len() as u32;
		// required because otherwise the borrow-checker complains
		let Framing { direct, stream } = self;

		let res = stream::once(async move { Ok(u32::to_be_bytes(len).to_vec().into()) })
			.chain(stream::once(async move { Ok(direct.into()) }));

		if let Some(stream) = stream {
			Box::pin(res.chain(stream))
		} else {
			Box::pin(res)
		}
	}

	pub async fn from_stream<S: Stream<Item = Packet> + Unpin + Send + 'static>(
		mut stream: S,
	) -> Result<Self, Error> {
		let mut packet = stream
			.next()
			.await
			.ok_or(Error::Framing)?
			.map_err(|_| Error::Framing)?;
		if packet.len() < 4 {
			return Err(Error::Framing);
		}

		let mut len = [0; 4];
		len.copy_from_slice(&packet[..4]);
		let len = u32::from_be_bytes(len);
		packet = packet.slice(4..);

		let mut buffer = Vec::new();
		let len = len as usize;
		loop {
			let max_cp = std::cmp::min(len - buffer.len(), packet.len());

			buffer.extend_from_slice(&packet[..max_cp]);
			if buffer.len() == len {
				packet = packet.slice(max_cp..);
				break;
			}
			packet = stream
				.next()
				.await
				.ok_or(Error::Framing)?
				.map_err(|_| Error::Framing)?;
		}

		let stream: ByteStream = if packet.is_empty() {
			Box::pin(stream)
		} else {
			Box::pin(futures::stream::once(async move { Ok(packet) }).chain(stream))
		};

		Ok(Framing {
			direct: buffer,
			stream: Some(stream),
		})
	}

	pub fn into_parts(self) -> (Vec<u8>, ByteStream) {
		let Framing { direct, stream } = self;
		(direct, stream.unwrap_or(Box::pin(futures::stream::empty())))
	}
}