moq-net 0.1.17

The networking layer for Media over QUIC: real-time pub/sub with built-in caching, fan-out, and prioritization.
Documentation
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
use crate::{
	ALPN_14, ALPN_15, ALPN_16, ALPN_17, ALPN_18, ALPN_19, ALPN_LITE, ALPN_LITE_03, ALPN_LITE_04, ALPN_LITE_05_WIP,
	Error, NEGOTIATED, OriginConsumer, OriginProducer, Session, StatsHandle, Version, Versions,
	coding::{Decode, Encode, Stream},
	ietf, lite, setup,
};

/// A MoQ server session builder.
#[derive(Default, Clone)]
pub struct Server {
	publish: Option<OriginConsumer>,
	consume: Option<OriginProducer>,
	stats: StatsHandle,
	versions: Versions,
}

impl Server {
	pub fn new() -> Self {
		Default::default()
	}

	pub fn with_publish(mut self, publish: impl Into<Option<OriginConsumer>>) -> Self {
		self.publish = publish.into();
		self
	}

	pub fn with_consume(mut self, consume: impl Into<Option<OriginProducer>>) -> Self {
		self.consume = consume.into();
		self
	}

	/// Attach a tier-scoped [`StatsHandle`]. Per-broadcast and per-subscription
	/// counters will be bumped through this handle for the lifetime of the session.
	/// Pass [`StatsHandle::default`] (a no-op handle) to opt out.
	pub fn with_stats(mut self, stats: StatsHandle) -> Self {
		self.stats = stats;
		self
	}

	/// Set both publish and consume from an `OriginProducer`.
	///
	/// This is equivalent to calling `with_publish(origin.consume())` and `with_consume(origin)`.
	pub fn with_origin(self, origin: OriginProducer) -> Self {
		let consumer = origin.consume();
		self.with_publish(consumer).with_consume(origin)
	}

	pub fn with_versions(mut self, versions: Versions) -> Self {
		self.versions = versions;
		self
	}

	/// Perform the MoQ handshake as a server, returning the established [`Session`].
	///
	/// Convenience wrapper over [`accept_request`](Self::accept_request) that completes
	/// the handshake immediately. Use `accept_request` when you need to inspect the
	/// client's advertised path before deciding what to serve.
	pub async fn accept<S: web_transport_trait::Session>(&self, session: S) -> Result<Session, Error> {
		self.accept_request(session).await?.ok().await
	}

	/// Begin the MoQ handshake, pausing once the client's request path is known so the
	/// caller can authorize or scope before serving.
	///
	/// Reads the client's SETUP (the in-band path lives there on URL-less transports),
	/// then returns a [`Request`]: inspect [`path`](Request::path), set the origins to
	/// serve, and call [`ok`](Request::ok) or [`close`](Request::close). Starting the
	/// session is deferred to `ok()`, so origins set on the `Request` take effect.
	///
	/// The path is surfaced for moq-lite-05; it is `None` on versions with no in-band
	/// request path (lite 01-04, and the IETF drafts in this build).
	pub async fn accept_request<S: web_transport_trait::Session>(&self, session: S) -> Result<Request<S>, Error> {
		// Regimes without an in-band path defer to `ok()` without surfacing one.
		let deferred = |handshake| Request {
			server: self.clone(),
			path: None,
			handshake,
		};

		let (encoding, supported) = match session.protocol() {
			Some(ALPN_19) => {
				self.versions
					.select(Version::Ietf(ietf::Version::Draft19))
					.ok_or(Error::Version)?;
				return Ok(deferred(Handshake::IetfModern {
					session,
					version: ietf::Version::Draft19,
				}));
			}
			Some(ALPN_18) => {
				self.versions
					.select(Version::Ietf(ietf::Version::Draft18))
					.ok_or(Error::Version)?;
				return Ok(deferred(Handshake::IetfModern {
					session,
					version: ietf::Version::Draft18,
				}));
			}
			Some(ALPN_17) => {
				self.versions
					.select(Version::Ietf(ietf::Version::Draft17))
					.ok_or(Error::Version)?;
				return Ok(deferred(Handshake::IetfModern {
					session,
					version: ietf::Version::Draft17,
				}));
			}
			Some(ALPN_16) => {
				let v = self
					.versions
					.select(Version::Ietf(ietf::Version::Draft16))
					.ok_or(Error::Version)?;
				(v, v.into())
			}
			Some(ALPN_15) => {
				let v = self
					.versions
					.select(Version::Ietf(ietf::Version::Draft15))
					.ok_or(Error::Version)?;
				(v, v.into())
			}
			Some(ALPN_14) => {
				let v = self
					.versions
					.select(Version::Ietf(ietf::Version::Draft14))
					.ok_or(Error::Version)?;
				(v, v.into())
			}
			Some(ALPN_LITE_05_WIP) => {
				self.versions
					.select(Version::Lite(lite::Version::Lite05Wip))
					.ok_or(Error::Version)?;

				// Gate on the client's SETUP: read it before serving so the caller can
				// scope by the advertised path.
				let client_setup = lite::accept_setup(&session, lite::Version::Lite05Wip).await?;
				return Ok(Request {
					server: self.clone(),
					path: client_setup.path,
					handshake: Handshake::Lite05 { session },
				});
			}
			Some(ALPN_LITE_04) => {
				self.versions
					.select(Version::Lite(lite::Version::Lite04))
					.ok_or(Error::Version)?;
				return Ok(deferred(Handshake::LiteBare {
					session,
					version: lite::Version::Lite04,
				}));
			}
			Some(ALPN_LITE_03) => {
				self.versions
					.select(Version::Lite(lite::Version::Lite03))
					.ok_or(Error::Version)?;
				return Ok(deferred(Handshake::LiteBare {
					session,
					version: lite::Version::Lite03,
				}));
			}
			Some(ALPN_LITE) | None => {
				let supported = self.versions.filter(&NEGOTIATED.into()).ok_or(Error::Version)?;
				(Version::Ietf(ietf::Version::Draft14), supported)
			}
			Some(p) => return Err(Error::UnknownAlpn(p.to_string())),
		};

		// Legacy bidi SETUP exchange (IETF 14-16, lite 01/02). Read the client's SETUP
		// to choose the version; `ok()` sends the server SETUP and starts the session.
		let mut stream = Stream::accept(&session, encoding).await?;
		let mut client: setup::Client = stream.reader.decode().await?;

		let version = client
			.versions
			.iter()
			.flat_map(|v| Version::try_from(*v).ok())
			.find(|v| supported.contains(v))
			.ok_or(Error::Version)?;

		// Pull the max request ID out now (IETF only) so `ok()` doesn't re-decode the
		// consumed parameters.
		let request_id_max = match version {
			Version::Ietf(v) => {
				let params = ietf::Parameters::decode(&mut client.parameters, v)?;
				params
					.get_varint(ietf::ParameterVarInt::MaxRequestId)
					.map(ietf::RequestId)
			}
			Version::Lite(_) => None,
		};

		Ok(deferred(Handshake::Legacy {
			session,
			stream,
			version,
			request_id_max,
		}))
	}
}

/// A paused server-side handshake.
///
/// Returned by [`Server::accept_request`] once the client's advertised
/// [`path`](Self::path) is known but before the session is granted anything. Set the
/// origins to serve, then call [`ok`](Self::ok) to complete the handshake, or
/// [`close`](Self::close) to reject it. Modeled on the WebTransport `Request`.
pub struct Request<S: web_transport_trait::Session> {
	server: Server,
	path: Option<String>,
	handshake: Handshake<S>,
}

/// The handshake state captured at the pause point. Every variant defers its session
/// start to [`Request::ok`] so origins set on the `Request` still apply.
enum Handshake<S: web_transport_trait::Session> {
	/// Modern IETF (17/18): SETUP is exchanged in the background by the session.
	IetfModern { session: S, version: ietf::Version },
	/// moq-lite 03/04: no Setup stream.
	LiteBare { session: S, version: lite::Version },
	/// moq-lite 05+: the client's Setup stream has already been read.
	Lite05 { session: S },
	/// Legacy IETF (14-16) and lite 01/02: the client SETUP has been read off the bidi
	/// stream but the server SETUP hasn't been sent. `ok()` finishes it.
	Legacy {
		session: S,
		stream: Stream<S, Version>,
		version: Version,
		request_id_max: Option<ietf::RequestId>,
	},
}

impl<S: web_transport_trait::Session> Request<S> {
	/// The request path the client advertised in its SETUP, if any.
	///
	/// Populated for moq-lite-05; `None` on versions without an in-band request path.
	/// See the note on [`Server::accept_request`].
	pub fn path(&self) -> Option<&str> {
		self.path.as_deref()
	}

	/// Publish to the connected client. Overrides any value from the [`Server`]
	/// builder; typically set after inspecting [`path`](Self::path).
	pub fn with_publish(mut self, publish: impl Into<Option<OriginConsumer>>) -> Self {
		self.server.publish = publish.into();
		self
	}

	/// Subscribe to the connected client. Overrides any value from the [`Server`] builder.
	pub fn with_consume(mut self, consume: impl Into<Option<OriginProducer>>) -> Self {
		self.server.consume = consume.into();
		self
	}

	/// Set the tier-scoped stats handle. Overrides any value from the [`Server`] builder.
	pub fn with_stats(mut self, stats: StatsHandle) -> Self {
		self.server.stats = stats;
		self
	}

	/// Accept the session, completing the handshake.
	pub async fn ok(self) -> Result<Session, Error> {
		let server = self.server;

		// Warn here, not in `accept_request`: callers attach origins on the Request
		// (after inspecting the path), so checking earlier gives false positives.
		if server.publish.is_none() && server.consume.is_none() {
			tracing::warn!("not publishing or consuming anything");
		}

		let (session, mut stream, version, request_id_max) = match self.handshake {
			Handshake::IetfModern { session, version } => {
				ietf::start(
					session.clone(),
					None,
					None,
					false,
					server.publish,
					server.consume,
					server.stats,
					version,
				)?;
				tracing::debug!(?version, "connected");
				return Ok(Session::new(session, version.into(), None));
			}
			Handshake::LiteBare { session, version } => {
				let recv_bw = lite::start(
					session.clone(),
					None,
					server.publish,
					server.consume,
					server.stats,
					version,
					lite::Setup::default(),
				)?;
				return Ok(Session::new(session, version.into(), recv_bw));
			}
			Handshake::Lite05 { session } => {
				// A server never advertises a request path.
				let recv_bw = lite::start(
					session.clone(),
					None,
					server.publish,
					server.consume,
					server.stats,
					lite::Version::Lite05Wip,
					lite::Setup::default(),
				)?;
				return Ok(Session::new(session, lite::Version::Lite05Wip.into(), recv_bw));
			}
			Handshake::Legacy {
				session,
				stream,
				version,
				request_id_max,
			} => (session, stream, version, request_id_max),
		};

		// Encode parameters using the version-appropriate type.
		let parameters = match version {
			Version::Ietf(v) => {
				let mut parameters = ietf::Parameters::default();
				parameters.set_varint(ietf::ParameterVarInt::MaxRequestId, u32::MAX as u64);
				parameters.set_bytes(ietf::ParameterBytes::Implementation, b"moq-lite-rs".to_vec());
				parameters.encode_bytes(v)?
			}
			Version::Lite(v) => lite::Parameters::default().encode_bytes(v)?,
		};

		let server_setup = setup::Server {
			version: version.into(),
			parameters,
		};
		stream.writer.encode(&server_setup).await?;

		let recv_bw = match version {
			Version::Lite(v) => {
				let stream = stream.with_version(v);
				// Pre-lite-05: no Setup stream, so nothing to advertise.
				lite::start(
					session.clone(),
					Some(stream),
					server.publish,
					server.consume,
					server.stats,
					v,
					lite::Setup::default(),
				)?
			}
			Version::Ietf(v) => {
				let stream = stream.with_version(v);
				ietf::start(
					session.clone(),
					Some(stream),
					request_id_max,
					false,
					server.publish,
					server.consume,
					server.stats,
					v,
				)?;
				None
			}
		};

		Ok(Session::new(session, version, recv_bw))
	}

	/// Reject the session, closing the transport with `err`'s wire code.
	pub fn close(self, err: Error) {
		let session = match self.handshake {
			Handshake::IetfModern { session, .. } => session,
			Handshake::LiteBare { session, .. } => session,
			Handshake::Lite05 { session } => session,
			Handshake::Legacy { session, .. } => session,
		};
		session.close(err.to_code(), &err.to_string());
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use std::{
		collections::VecDeque,
		sync::{Arc, Mutex},
	};

	use bytes::Bytes;

	#[derive(Debug, Clone, Default)]
	struct FakeError;
	impl std::fmt::Display for FakeError {
		fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
			write!(f, "fake transport error")
		}
	}
	impl std::error::Error for FakeError {}
	impl web_transport_trait::Error for FakeError {
		fn session_error(&self) -> Option<(u32, String)> {
			Some((0, "closed".to_string()))
		}
	}

	/// A session that replays a queue of unidirectional streams (each a `Vec<u8>`) in
	/// order from `accept_uni`; everything else is inert.
	#[derive(Clone)]
	struct FakeSession {
		protocol: Option<&'static str>,
		uni: Arc<Mutex<VecDeque<Vec<u8>>>>,
	}

	impl FakeSession {
		fn new(protocol: &'static str, uni: impl IntoIterator<Item = Vec<u8>>) -> Self {
			Self {
				protocol: Some(protocol),
				uni: Arc::new(Mutex::new(uni.into_iter().collect())),
			}
		}
	}

	impl web_transport_trait::Session for FakeSession {
		type SendStream = FakeSend;
		type RecvStream = FakeRecv;
		type Error = FakeError;

		async fn accept_uni(&self) -> Result<Self::RecvStream, Self::Error> {
			// Drop the guard before any await so the future stays Send.
			let data = self.uni.lock().unwrap().pop_front();
			match data {
				Some(data) => Ok(FakeRecv { data: data.into() }),
				None => std::future::pending().await,
			}
		}
		async fn accept_bi(&self) -> Result<(Self::SendStream, Self::RecvStream), Self::Error> {
			std::future::pending().await
		}
		async fn open_bi(&self) -> Result<(Self::SendStream, Self::RecvStream), Self::Error> {
			std::future::pending().await
		}
		async fn open_uni(&self) -> Result<Self::SendStream, Self::Error> {
			std::future::pending().await
		}
		fn send_datagram(&self, _payload: Bytes) -> Result<(), Self::Error> {
			Ok(())
		}
		async fn recv_datagram(&self) -> Result<Bytes, Self::Error> {
			std::future::pending().await
		}
		fn max_datagram_size(&self) -> usize {
			1200
		}
		fn protocol(&self) -> Option<&str> {
			self.protocol
		}
		fn close(&self, _code: u32, _reason: &str) {}
		async fn closed(&self) -> Self::Error {
			std::future::pending().await
		}
	}

	#[derive(Clone, Default)]
	struct FakeSend;
	impl web_transport_trait::SendStream for FakeSend {
		type Error = FakeError;
		async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
			Ok(buf.len())
		}
		fn set_priority(&mut self, _order: u8) {}
		fn finish(&mut self) -> Result<(), Self::Error> {
			Ok(())
		}
		fn reset(&mut self, _code: u32) {}
		async fn closed(&mut self) -> Result<(), Self::Error> {
			Ok(())
		}
	}

	struct FakeRecv {
		data: VecDeque<u8>,
	}
	impl web_transport_trait::RecvStream for FakeRecv {
		type Error = FakeError;
		async fn read(&mut self, dst: &mut [u8]) -> Result<Option<usize>, Self::Error> {
			if self.data.is_empty() {
				return Ok(None);
			}
			let size = dst.len().min(self.data.len());
			for slot in dst.iter_mut().take(size) {
				*slot = self.data.pop_front().unwrap();
			}
			Ok(Some(size))
		}
		fn stop(&mut self, _code: u32) {}
		async fn closed(&mut self) -> Result<(), Self::Error> {
			Ok(())
		}
	}

	/// Encode a lite-05 Setup stream: the `DataType::Setup` tag then the SETUP message.
	fn lite05_setup(path: Option<&str>) -> Vec<u8> {
		let v = lite::Version::Lite05Wip;
		let mut buf = Vec::new();
		lite::DataType::Setup.encode(&mut buf, v).unwrap();
		lite::Setup {
			path: path.map(str::to_string),
		}
		.encode(&mut buf, v)
		.unwrap();
		buf
	}

	/// Encode a lite-05 Group uni stream header (just the `DataType::Group` tag).
	fn lite05_group() -> Vec<u8> {
		let mut buf = Vec::new();
		lite::DataType::Group
			.encode(&mut buf, lite::Version::Lite05Wip)
			.unwrap();
		buf
	}

	#[tokio::test(start_paused = true)]
	async fn accept_request_reads_lite05_path() {
		let session = FakeSession::new(ALPN_LITE_05_WIP, [lite05_setup(Some("/team/room"))]);
		let request = Server::new()
			.with_versions(Version::Lite(lite::Version::Lite05Wip).into())
			.accept_request(session)
			.await
			.unwrap();
		assert_eq!(request.path(), Some("/team/room"));
	}

	#[tokio::test(start_paused = true)]
	async fn accept_request_lite05_without_path_is_none() {
		let session = FakeSession::new(ALPN_LITE_05_WIP, [lite05_setup(None)]);
		let request = Server::new()
			.with_versions(Version::Lite(lite::Version::Lite05Wip).into())
			.accept_request(session)
			.await
			.unwrap();
		assert_eq!(request.path(), None);
	}

	#[tokio::test(start_paused = true)]
	async fn accept_request_skips_uni_stream_before_setup() {
		// A Group racing ahead of the SETUP is reset and skipped; the gate keeps
		// reading until it finds the SETUP.
		let session = FakeSession::new(ALPN_LITE_05_WIP, [lite05_group(), lite05_setup(Some("/team/room"))]);
		let request = Server::new()
			.with_versions(Version::Lite(lite::Version::Lite05Wip).into())
			.accept_request(session)
			.await
			.unwrap();
		assert_eq!(request.path(), Some("/team/room"));
	}
}