blob: 620ff64736218ad49720ec5d8500e485ca0fbf0c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
use crate::interface::generic::generic_handle_connection;
use crate::CLIENT_ID_COUNTER;
use karlcommon::interfaces::unix_path;
use log::{info, warn};
use std::os::unix::net::UnixListener;
use std::thread;
pub fn run() {
if unix_path().exists() {
info!("remove old socket");
std::fs::remove_file(unix_path()).unwrap();
}
info!("binding to socket");
let listener = UnixListener::bind(unix_path()).unwrap();
info!("listening.");
loop {
let (stream, addr) = listener.accept().unwrap();
let id = CLIENT_ID_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
thread::spawn(move || {
info!("client connected: {:?}", addr);
if let Err(err) = generic_handle_connection(id, stream.try_clone().unwrap(), stream) {
warn!("client dropped: {:?} ({})", addr, err);
} else {
info!("client dropped: {:?}", addr);
}
});
}
}
|