2023-10-16 04:05:40 +00:00
|
|
|
use std::{sync::Arc, time};
|
2023-06-08 07:01:34 +00:00
|
|
|
|
|
|
|
use anyhow::Context;
|
|
|
|
|
2023-06-16 18:38:19 +00:00
|
|
|
use tokio::task::JoinSet;
|
|
|
|
|
2023-10-16 04:05:40 +00:00
|
|
|
use crate::{Config, Origin, Session, Tls};
|
2023-09-15 19:06:28 +00:00
|
|
|
|
2023-10-16 04:05:40 +00:00
|
|
|
pub struct Quic {
|
2023-10-12 04:09:32 +00:00
|
|
|
quic: quinn::Endpoint,
|
2023-06-08 07:01:34 +00:00
|
|
|
|
2023-07-20 01:04:45 +00:00
|
|
|
// The active connections.
|
|
|
|
conns: JoinSet<anyhow::Result<()>>,
|
2023-06-08 07:01:34 +00:00
|
|
|
|
2023-09-15 19:06:28 +00:00
|
|
|
// The map of active broadcasts by path.
|
2023-10-12 04:09:32 +00:00
|
|
|
origin: Origin,
|
2023-06-08 07:01:34 +00:00
|
|
|
}
|
|
|
|
|
2023-10-16 04:05:40 +00:00
|
|
|
impl Quic {
|
|
|
|
// Create a QUIC endpoint that can be used for both clients and servers.
|
|
|
|
pub async fn new(config: Config, tls: Tls) -> anyhow::Result<Self> {
|
2023-10-30 13:54:27 +00:00
|
|
|
let mut client_config = tls.client.clone();
|
|
|
|
let mut server_config = tls.server.clone();
|
2023-10-12 04:09:32 +00:00
|
|
|
client_config.alpn_protocols = vec![webtransport_quinn::ALPN.to_vec()];
|
|
|
|
server_config.alpn_protocols = vec![webtransport_quinn::ALPN.to_vec()];
|
2023-06-08 17:08:18 +00:00
|
|
|
|
|
|
|
// Enable BBR congestion control
|
|
|
|
// TODO validate the implementation
|
2023-06-08 07:01:34 +00:00
|
|
|
let mut transport_config = quinn::TransportConfig::default();
|
2023-10-12 04:09:32 +00:00
|
|
|
transport_config.max_idle_timeout(Some(time::Duration::from_secs(10).try_into().unwrap()));
|
|
|
|
transport_config.keep_alive_interval(Some(time::Duration::from_secs(4))); // TODO make this smarter
|
2023-09-15 19:06:28 +00:00
|
|
|
transport_config.congestion_controller_factory(Arc::new(quinn::congestion::BbrConfig::default()));
|
2023-10-20 03:04:55 +00:00
|
|
|
transport_config.mtu_discovery_config(None); // Disable MTU discovery
|
2023-10-12 04:09:32 +00:00
|
|
|
let transport_config = Arc::new(transport_config);
|
|
|
|
|
|
|
|
let mut client_config = quinn::ClientConfig::new(Arc::new(client_config));
|
|
|
|
let mut server_config = quinn::ServerConfig::with_crypto(Arc::new(server_config));
|
|
|
|
server_config.transport_config(transport_config.clone());
|
|
|
|
client_config.transport_config(transport_config);
|
|
|
|
|
|
|
|
// There's a bit more boilerplate to make a generic endpoint.
|
|
|
|
let runtime = quinn::default_runtime().context("no async runtime")?;
|
|
|
|
let endpoint_config = quinn::EndpointConfig::default();
|
|
|
|
let socket = std::net::UdpSocket::bind(config.listen).context("failed to bind UDP socket")?;
|
|
|
|
|
|
|
|
// Create the generic QUIC endpoint.
|
|
|
|
let mut quic = quinn::Endpoint::new(endpoint_config, Some(server_config), socket, runtime)
|
|
|
|
.context("failed to create QUIC endpoint")?;
|
|
|
|
quic.set_default_client_config(client_config);
|
|
|
|
|
|
|
|
let api = config.api.map(|url| {
|
|
|
|
log::info!("using moq-api: url={}", url);
|
|
|
|
moq_api::Client::new(url)
|
|
|
|
});
|
|
|
|
|
2023-10-17 05:50:17 +00:00
|
|
|
if let Some(ref node) = config.api_node {
|
2023-10-12 04:09:32 +00:00
|
|
|
log::info!("advertising origin: url={}", node);
|
|
|
|
}
|
2023-06-08 07:01:34 +00:00
|
|
|
|
2023-10-17 05:50:17 +00:00
|
|
|
let origin = Origin::new(api, config.api_node, quic.clone());
|
2023-07-20 01:04:45 +00:00
|
|
|
let conns = JoinSet::new();
|
2023-06-08 07:01:34 +00:00
|
|
|
|
2023-10-12 04:09:32 +00:00
|
|
|
Ok(Self { quic, origin, conns })
|
2023-06-08 07:01:34 +00:00
|
|
|
}
|
|
|
|
|
2023-10-16 04:05:40 +00:00
|
|
|
pub async fn serve(mut self) -> anyhow::Result<()> {
|
2023-10-12 04:09:32 +00:00
|
|
|
log::info!("listening on {}", self.quic.local_addr()?);
|
2023-09-19 05:37:49 +00:00
|
|
|
|
2023-06-08 07:01:34 +00:00
|
|
|
loop {
|
2023-06-16 18:38:19 +00:00
|
|
|
tokio::select! {
|
2023-10-12 04:09:32 +00:00
|
|
|
res = self.quic.accept() => {
|
2023-07-20 01:04:45 +00:00
|
|
|
let conn = res.context("failed to accept QUIC connection")?;
|
2023-10-12 04:09:32 +00:00
|
|
|
let mut session = Session::new(self.origin.clone());
|
2023-09-15 19:06:28 +00:00
|
|
|
self.conns.spawn(async move { session.run(conn).await });
|
2023-06-16 18:38:19 +00:00
|
|
|
},
|
2023-07-20 01:04:45 +00:00
|
|
|
res = self.conns.join_next(), if !self.conns.is_empty() => {
|
2023-06-16 18:38:19 +00:00
|
|
|
let res = res.expect("no tasks").expect("task aborted");
|
|
|
|
if let Err(err) = res {
|
2023-09-15 19:06:28 +00:00
|
|
|
log::warn!("connection terminated: {:?}", err);
|
2023-06-16 18:38:19 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2023-06-08 07:01:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|