项目作者: ssnover

项目描述 :
helles is a library providing a Unix-socket based client and server for sending JSON-based messages to a daemon application.
高级语言: Rust
项目地址: git://github.com/ssnover/helles.git
创建时间: 2020-12-12T01:36:01Z
项目社区:https://github.com/ssnover/helles

开源协议:Other

下载


helles

helles is a library providing a Unix-socket based client and server for sending JSON-based messages to a daemon application.

Usage

  1. use std::sync::atomic::{AtomicBool, Ordering};
  2. use std::sync::mpsc::TryRecvError;
  3. use std::sync::Arc;
  4. use std::time::Duration;
  5. use helles::Server;
  6. fn main() -> std::io::Result<()> {
  7. let running = Arc::new(AtomicBool::new(true));
  8. let r = running.clone();
  9. ctrlc::set_handler(move || {
  10. r.store(false, Ordering::SeqCst);
  11. })
  12. .unwrap();
  13. let (server, rx) = Server::new("/tmp/test.sock")?;
  14. let server_context = Server::start(server, running.clone());
  15. while running.load(Ordering::SeqCst) {
  16. std::thread::sleep(Duration::from_secs(1));
  17. match rx.try_recv() {
  18. Ok(cmd) => println!("Server got command: {}", cmd),
  19. Err(err) => match err {
  20. TryRecvError::Disconnected => {
  21. running.store(false, Ordering::SeqCst);
  22. }
  23. _ => {}
  24. },
  25. }
  26. }
  27. server_context.join().expect("Failed to join server thread");
  28. Ok(())
  29. }

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you shall be dual licensed as above, without any
additional terms or conditions.