karyon_net/transport.rs
1use std::sync::Arc;
2
3use async_trait::async_trait;
4
5use crate::{ByteStream, Endpoint, Result};
6
7/// `Arc` reference to a `Transport`.
8pub type TransportRef = Arc<dyn Transport>;
9
10/// A user supplied way to dial and listen, e.g. in-memory pipes, sockets
11/// from another runtime, or a WASI host's sockets. Crates on karyon_net use
12/// it for every endpoint it `supports`, and their built-in transports for
13/// the others. Pair it with a custom endpoint scheme (`mem://node-a`) or
14/// take over a built-in one (`tcp://`, `tls://`, `quic://`).
15///
16/// The returned streams are used as is: no TLS or other layer is added.
17#[async_trait]
18pub trait Transport: Send + Sync {
19 /// Whether this transport handles `endpoint`.
20 fn supports(&self, endpoint: &Endpoint) -> bool;
21
22 /// Opens a connection to `endpoint`.
23 async fn dial(&self, endpoint: &Endpoint) -> Result<Box<dyn ByteStream>>;
24
25 /// Starts listening on `endpoint`.
26 async fn listen(&self, endpoint: &Endpoint) -> Result<Box<dyn TransportListener>>;
27}
28
29/// Accepts the connections of a `Transport` listener.
30#[async_trait]
31pub trait TransportListener: Send + Sync {
32 /// Waits for the next connection. Keep it cheap: it runs in the
33 /// accept loop, so a slow accept holds back the next connections.
34 async fn accept(&self) -> Result<Box<dyn ByteStream>>;
35
36 /// The endpoint the listener is bound to.
37 fn local_endpoint(&self) -> Result<Endpoint>;
38}