Skip to main content

moq_transport_iroh/
lib.rs

1//! MoQ sessions over iroh connections.
2//!
3//! Everything needed to turn an iroh connection into a [`moq_net::Session`],
4//! and nothing about nodes, routes or publications:
5//!
6//! - [`Transport`] implements moq-net's poll transport on an iroh connection.
7//!   [`Transport::connect`] dials a peer, and [`Transport::new`] wraps a
8//!   connection that is already open, accepted or dialed. Both check the
9//!   negotiated ALPN. Hand the transport to [`moq_net::Client::connect`] or
10//!   [`moq_net::Server::handshake`].
11//! - [`SendGroup`] is moq-net's send group on iroh: it packs a group's
12//!   priority and its streams' orders into iroh's stream urgency, so
13//!   equal-priority subscriptions share the bytes.
14//! - [`ALPN`], [`alpns`] and [`HANDSHAKE_TIMEOUT`] are the protocol
15//!   identifiers to dial and mount a handler with, and the bound on a
16//!   session's setup.
17//!
18//! MoQ runs on raw QUIC. Raw QUIC has no request URI, so the request path and
19//! any token travel in the MoQ SETUP.
20//!
21//! ```no_run
22//! use iroh::EndpointId;
23//! use moq_transport_iroh::Transport;
24//!
25//! # async fn run(endpoint: iroh::Endpoint, peer: EndpointId) -> Result<(), Box<dyn std::error::Error>> {
26//! let transport = Transport::connect(&endpoint, peer).await?;
27//! let (session, driver) = moq_net::Client::new()
28//!     .connect(moq_net::time::Instant::now(), transport)
29//!     .await?;
30//! # drop((session, driver));
31//! # Ok(())
32//! # }
33//! ```
34
35#![forbid(unsafe_code)]
36
37mod error;
38mod group;
39mod stream;
40mod transport;
41
42use std::time::Duration;
43
44pub use self::{
45    error::{Error, TransportError},
46    group::SendGroup,
47    stream::{RecvStream, SendStream},
48    transport::Transport,
49};
50
51/// How long a connection may take to open its MoQ session, dialed or accepted.
52///
53/// Without a bound, a peer that never sends its setup holds a task for as long
54/// as the connection lives.
55pub const HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(10);
56
57/// The newest MoQ version this build speaks.
58///
59/// A dial offers only this ALPN. A server accepts every ALPN in [`alpns`], so
60/// a peer that offers an older version still connects.
61pub const ALPN: &[u8] = moq_net::ALPNS[0].as_bytes();
62
63/// Returns every ALPN a server accepts, newest MoQ version first.
64///
65/// Mount a protocol handler under all of them.
66#[must_use]
67pub fn alpns() -> Vec<&'static [u8]> {
68    moq_net::ALPNS.iter().map(|alpn| alpn.as_bytes()).collect()
69}