use std::str::FromStr; use protwrap::tokio::server::{ listener::{async_trait, KillSwitch, Listener}, Stream }; use lstnconn::{ConnInfo, Handle, Handler, ListenConnMgr}; /// Type used to store application-specific information with each connection. #[derive(Clone)] struct ConnCtx {} struct MyConnHandler {} #[async_trait] impl Handler for MyConnHandler { type ConnCtx = ConnCtx; async fn connected(&self, ci: ConnInfo) -> Handle { println!("Connected! id={}, sa={:?}", ci.id.val(), ci.sa); // Spawn a new task to handle the connection Handle::Task(ConnCtx {}) } async fn run(&self, _conn: Stream, _cctx: &mut Self::ConnCtx) { println!("Running!"); } async fn disconnected(&self, _cctx: Self::ConnCtx) { println!("Disconnected!"); } } #[tokio::main] async fn main() { let ks = KillSwitch::new(); let ks2 = ks.clone(); let jh = tokio::task::spawn(async move { let listener = Listener::from_str("127.0.0.1:8080").unwrap(); let handler = MyConnHandler {}; let lcm = ListenConnMgr::new(handler); lcm.run(listener, ks2).await; }); tokio::task::spawn(async { tokio::time::sleep(std::time::Duration::from_millis(500)).await; let _conn = tokio::net::TcpStream::connect("127.0.0.1:8080").await; }); tokio::task::spawn(async { tokio::time::sleep(std::time::Duration::from_millis(250)).await; let _conn = tokio::net::TcpStream::connect("127.0.0.1:8080").await; }); tokio::time::sleep(std::time::Duration::from_millis(1000)).await; ks.trigger(); ks.finalize().await.unwrap(); let _ = jh.await; } // vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :