added cntrl_c exit handling
This commit is contained in:
parent
8d09c6142c
commit
e9c546f559
1 changed files with 22 additions and 8 deletions
24
src/main.rs
24
src/main.rs
|
@ -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);
|
||||||
|
|
||||||
|
ctrlc::set_handler(move || {
|
||||||
|
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);
|
println!("{}", message);
|
||||||
|
},
|
||||||
|
Err(_) => {continue},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
listener_flag.set(false);
|
println!("ding");
|
||||||
|
listener_can_run_flag.set(false);
|
||||||
listener_join_handle.join().unwrap();
|
listener_join_handle.join().unwrap();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue