pub struct Endpoint<Side> { /* private fields */ }Expand description
Entrypoint for creating client or server connections.
A single endpoint can be used to accept or connect multiple connections. Each endpoint internally binds an UDP socket.
§Server
Use Endpoint::server for creating a server-side endpoint.
Afterwards use the method Endpoint::accept for awaiting on incoming session request.
use wtransport::Endpoint;
let server = Endpoint::server(config)?;
loop {
let incoming_session = server.accept().await;
// Spawn task that handles client incoming session...
}§Client
Use Endpoint::client for creating a client-side endpoint and use Endpoint::connect
to connect to a server specifying the URL.
use wtransport::ClientConfig;
use wtransport::Endpoint;
let connection = Endpoint::client(ClientConfig::default())?
.connect("https://localhost:4433")
.await?;Implementations§
Source§impl<Side> Endpoint<Side>
impl<Side> Endpoint<Side>
Sourcepub fn close(&self, error_code: VarInt, reason: &[u8])
pub fn close(&self, error_code: VarInt, reason: &[u8])
Closes all of this endpoint’s connections immediately and cease accepting new connections.
Sourcepub async fn wait_idle(&self)
pub async fn wait_idle(&self)
Waits for all connections on the endpoint to be cleanly shut down.
Sourcepub fn local_addr(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
Gets the local SocketAddr the underlying socket is bound to.
Sourcepub fn open_connections(&self) -> usize
pub fn open_connections(&self) -> usize
Get the number of connections that are currently open.
Source§impl Endpoint<Server>
impl Endpoint<Server>
Sourcepub fn server(server_config: ServerConfig) -> Result<Self>
pub fn server(server_config: ServerConfig) -> Result<Self>
Constructs a server endpoint.
Sourcepub async fn accept(&self) -> IncomingSession
pub async fn accept(&self) -> IncomingSession
Get the next incoming connection attempt from a client.
Sourcepub fn reload_config(
&self,
server_config: ServerConfig,
rebind: bool,
) -> Result<()>
pub fn reload_config( &self, server_config: ServerConfig, rebind: bool, ) -> Result<()>
Reloads the server configuration.
Useful for e.g. refreshing TLS certificates without disrupting existing connections.
§Arguments
server_config- The new configuration for the server.rebind- A boolean indicating whether the server should rebind its socket. Iftrue, the server will bind to a new socket with the provided configuration. Iffalse, the bind address configuration will be ignored.
Source§impl Endpoint<Client>
impl Endpoint<Client>
Sourcepub fn client(client_config: ClientConfig) -> Result<Self>
pub fn client(client_config: ClientConfig) -> Result<Self>
Constructs a client endpoint.
Sourcepub async fn connect<O>(
&self,
options: O,
) -> Result<Connection, ConnectingError>where
O: IntoConnectOptions,
pub async fn connect<O>(
&self,
options: O,
) -> Result<Connection, ConnectingError>where
O: IntoConnectOptions,
Establishes a WebTransport connection to a specified URL.
This method initiates a WebTransport connection to the specified URL. It validates the URL, and performs necessary steps to establish a secure connection.
§Arguments
options- Connection options specifying the URL and additional headers. It can be simply an URL string representing the WebTransport endpoint to connect to. It must have anhttpsscheme. The URL can specify either an IP address or a hostname. When specifying a hostname, the method will internally perform DNS resolution, configured withClientConfigBuilder::dns_resolver.
§Examples
Connect using a URL with a hostname (DNS resolution is performed):
let url = "https://example.com:4433/webtransport";
let connection = endpoint.connect(url).await?;Connect using a URL with an IP address:
let url = "https://127.0.0.1:4343/webtransport";
let connection = endpoint.connect(url).await?;Connect adding an additional header:
let options = ConnectOptions::builder("https://example.com:4433/webtransport")
.add_header("Authorization", "AuthToken")
.build();
let connection = endpoint.connect(options).await?;