added cntrl_c exit handling

This commit is contained in:
Nickiel12 2021-12-18 18:22:14 -08:00
parent 8d09c6142c
commit e9c546f559

View file

@ -1,6 +1,7 @@
use std::sync::mpsc; use std::{sync::mpsc, time::Duration};
use modules::socket_handler::Socket; use modules::socket_handler::Socket;
use workctl::sync_flag;
mod tests; mod tests;
mod modules; mod modules;
@ -12,11 +13,24 @@ const SERVER_ADDRESS: &str = "10.0.0.168:5000";
fn main() { fn main() {
let socket_listener = Socket::make_listener(SERVER_ADDRESS); let socket_listener = Socket::make_listener(SERVER_ADDRESS);
let (from_socket_tx, from_socket_rx) = mpsc::channel::<String>(); let (from_socket_tx, from_socket_rx) = mpsc::channel::<String>();
let (mut listener_flag, listener_join_handle) = Socket::handle_connections(socket_listener, from_socket_tx); let (mut listener_can_run_flag, listener_join_handle) = Socket::handle_connections(socket_listener, from_socket_tx);
let message = from_socket_rx.recv().unwrap(); let (mut control_c_flag_tx, control_c_called_flag_rx) = sync_flag::new_syncflag(false);
println!("{}", message);
ctrlc::set_handler(move || {
listener_flag.set(false); control_c_flag_tx.set(true);
}).expect("control C handler failed!");
while !control_c_called_flag_rx.get() {
match from_socket_rx.recv_timeout(Duration::from_millis(100)) {
Ok(message) => {
println!("{}", message);
},
Err(_) => {continue},
}
}
println!("ding");
listener_can_run_flag.set(false);
listener_join_handle.join().unwrap(); listener_join_handle.join().unwrap();
} }