Expand description
Library for abstracting over TCP server sockets, UNIX server sockets, inetd-like mode.
ListenerAddress is like SocketAddr and Listener is like TcpListener, but with more flexibility.
let addr1 : ListenerAddress = "127.0.0.1:8087".parse().unwrap();
let addr2 : ListenerAddress = "/path/to/socket".parse().unwrap();
let addr3 : ListenerAddress = "@abstract_linux_address".parse().unwrap();
let system_options : SystemOptions = Default::default();
let user_options : UserOptions = Default::default();
let mut l = Listener::bind(&addr1, &system_options, &user_options).await.unwrap();
while let Ok((conn, addr)) = l.accept().await {
// ...
}There is special integration with clap:
use clap::Parser;
#[derive(Parser)]
/// Demo application for tokio-listener
struct Args {
#[clap(flatten)]
listener: tokio_listener::ListenerAddressPositional,
}
let args = Args::parse();
let listener = args.listener.bind().await.unwrap();
let app = axum06::Router::new().route("/", axum06::routing::get(|| async { "Hello, world\n" }));
axum06::Server::builder(listener).serve(app.into_make_service()).await;See project README for details and more examples.
§Feature flags
clap— Enablederive(Parser)forUserOptionsand other Clap helpersuser_facing_default(enabled by default) — Subset of default features that add features supposed to be accessed by end userserde— Enableserde::Serializeandserde::Deserializeimplementations forUserOptionsandListenerAddressand some other types.hyper014— Enablehyper(v0.14)::server::accept::Acceptimplementation forListeneraxum07— Enable tokio-listener-adaptedservefunction from Axum 0.7 (and related types)inetd— Enable inetd (stdin/stdout) modeunix— Enable UNIX socket modeunix_path_tools— Enable tools such as unlink/chmod/chown for UNIX path sockets inUserOptionssd_listen— Enable receiving pre-bound sockets from inherited file descriptor (e.g. from systemd). Disabling this should make the crateunsafe-free.socket_options— Enable socket options such as receive or send buffer sizes or keepalives inUserOptionstonic010— Enabletonic(v0.10)::transport::server::Connectedimplementation forConnectiontonic011— Enabletonic(v0.11)::transport::server::Connectedimplementation forConnectiontonic012— Enabletonic(v0.12)::transport::server::Connectedimplementation forConnectiontokio-util(enabled by default) — Enabletokio_util::net::Listenerimplementation forListener.multi-listener— EnableListener::bind_multipleandsd-listen:*(if combined withsd_listenfeature)
Disabling default features bring tokio-listener close to usual TcpListener.
Modules§
- axum07
- Analogue of
axum::servemodule, but for tokio-listener.
Structs§
- Connection
- Accepted connection, which can be a TCP socket, AF_UNIX stream socket or a stdin/stdout pair.
- Listener
- Configured TCP.
AF_UNIXor other stream socket acceptor. - Listener
AddressL Flag - Clap helper to provide optional listener address a named argument
--listen-addressor-l. - Listener
Address Positional - Clap helper to require listener address as a required positional argument
listen_address, forclap(flatten)-ing into your primary options struct. - System
Options - Listener options that are supposed to be hard coded in the code (not configurable by user)
- TcpKeepalive
Params - Value of
--tcp-keepaliveoption. - User
Options - User options that supplement listening address.
Enums§
- Accept
Error tokio-listener-specific accept errors, to be packed instd::io::Error::other.- Bind
Error tokio-listener-specific bind errors, to be packed instd::io::Error::other.- Listener
Address - Abstraction over socket address that instructs in which way and at what address (if any) [
Listener] should listen for incoming stream connections. - Some
Socket Addr - Some form of accepted connection’s address.
Variant depends on variant used in [
ListenerAddress]. - Some
Socket Addr Clonable - Other representation of
SomeSocketAddrwith Arc-wrapped Unix addresses to enable cloning - Unix
Chmod Variant - Value of
--unix-listen-chmodoption which allows changing DAC file access mode for UNIX path socket