use std::io;
use wtransport::endpoint::endpoint_side::Server;
use wtransport::{ClientConfig, Endpoint, RecvStream, SendStream, ServerConfig};
pub struct WebTransport;
impl WebTransport {
pub async fn connect(
url: &str,
config: ClientConfig,
) -> io::Result<(RecvStream, SendStream)> {
let endpoint = Endpoint::client(config)?;
let conn = endpoint.connect(url).await.map_err(io::Error::other)?;
let (send, recv) = conn
.open_bi()
.await
.map_err(io::Error::other)?
.await
.map_err(io::Error::other)?;
tokio::spawn(async move {
let _endpoint = endpoint;
conn.closed().await;
});
Ok((recv, send))
}
pub fn bind(config: ServerConfig) -> io::Result<Endpoint<Server>> {
Endpoint::server(config)
}
pub async fn accept(listener: &Endpoint<Server>) -> io::Result<(RecvStream, SendStream)> {
let session_request = listener.accept().await.await.map_err(io::Error::other)?;
let conn = session_request.accept().await.map_err(io::Error::other)?;
let (send, recv) = conn.accept_bi().await.map_err(io::Error::other)?;
tokio::spawn(async move {
conn.closed().await;
});
Ok((recv, send))
}
}