[−][src]Crate linebased
Drop-in TCP command server
Provide a callback that is passed commands from clients and handle them synchronously.
tokio
is used internally so multiple clients may be active.
Examples
use linebased::Server; #[tokio::main] async fn main() { // Create a server with the default config and a // handler that only knows the "version" command let mut server = Server::new(Default::default(), |query| { match query { "version" => String::from("0.1.0"), _ => String::from("unknown command"), } }).unwrap(); server.run().await.unwrap(); }
Running a server in the background is also possible, just spawn the future
returned by Server::run
. Request a handle ! from the server so that you
may shut it down gracefully.
use linebased::Server; use std::thread; #[tokio::main] async fn main() { let mut server = Server::new(Default::default(), |query| { match query { "version" => String::from("0.1.0"), _ => String::from("unknown command"), } }).unwrap(); let handle = server.handle(); let fut = tokio::spawn(async move { server.run().await }); // Time passes handle.shutdown(); fut.await.expect("failed to spawn future").expect("Error from linebased::Server::run"); }
Structs
Config | Server configuration |
Handle | Handle for the server |
Server | The linebased TCP server |
Enums
Error | Error from linebased crate |
Type Definitions
Result | Results from linebased crate |