Expand description
use lstnconn::{
Handler, ListenConnMgr, async_trait, Stream, ConnInfo, Handle,
KillSwitch, Listener
};
// Handler used to process connections
struct MyHandler { }
// Per-connection data type
#[derive(Clone)]
struct ConnectData { }
#[async_trait]
impl Handler for MyHandler {
type ConnCtx = ConnectData;
async fn connected(&self, ci: ConnInfo) -> Handle<Self::ConnCtx> {
// A connection has been established, tell listener to spawn a new task
// for running connection handler on.
Handle::Task(ConnectData {})
}
async fn run(&self, conn: Stream, cctx: &mut Self::ConnCtx) {
// .. process the server connection here ..
}
async fn disconnected(&self, cctx: Self::ConnCtx) {
// .. a connection has terminated ..
}
}
let handler = MyHandler { };
let lcm = ListenConnMgr::new(handler);
// Create a KillSwitch for terminating the listener loop
let ks = KillSwitch::new();
// Create a localhost Listener using a system-assigned port
let listener = Listener::tcp("127.0.0.1:0").unwrap();
// Run the listen/connection loop in a background task
task::spawn(lcm.run(listener, ks));Structs§
- Conn
Info - Used to pass connection-related data to the
Handler::connected()callback. - Kill
Switch - The KillSwitch is used both to signal termination and waiting for termination.
- Listen
Conn Mgr - Main listener object.
Enums§
- Handle
- Indicate whether to run the connection handler synchronously or spawn a task for it.
- Listener
- Wrapper around common protocol-specific listener specifiers.
- Sock
Addr - Abstraction around std’s
SocketAddr(for IPv4/IPv6) and tokio’s (unix local domain)SocketAddr. - Stream
- Representation of a stream acting as a server end-point (passively established connection).
Traits§
- Handler
- Listener/connections callback.