use crate::{handle_packet, CLIENT_ID_COUNTER}; use karlcommon::{ interfaces::unix_path, version, ClientboundPacket, ProtoError, ServerboundPacket, }; use log::{debug, error, info, warn}; use std::io; use std::io::{BufRead, BufReader, ErrorKind, Write}; use std::os::unix::net::{UnixListener, UnixStream}; use std::thread; pub fn run() { if unix_path().exists() { info!("remove old socket"); std::fs::remove_file(unix_path()).unwrap(); } 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) = handle_connection(id, stream) { warn!("client dropped: {:?} ({})", addr, err); } else { info!("client dropped: {:?}", addr); } }); } } fn handle_connection(id: u32, mut stream: UnixStream) -> io::Result<()> { let mut reader = BufReader::new(stream.try_clone()?); let (responder, responses) = crossbeam_channel::unbounded(); responder .send(ClientboundPacket::Handshake { version: version!(), }) .unwrap(); thread::spawn(move || { for m in responses { debug!("{id} -> {m:?}"); match stream .write_fmt(format_args!("{}\n", serde_json::to_string(&m).unwrap())) .map_err(|e| e.kind()) { Ok(_) => (), Err(ErrorKind::BrokenPipe) => break, Err(e) => error!("network error: {:?}", e), } } }); { let mut buf = String::new(); loop { if reader.read_line(&mut buf)? == 0 { break Ok(()); }; match serde_json::from_str::(buf.as_str()) { Ok(packet) => { debug!("{id} <- {packet:?}"); handle_packet(id, packet, responder.clone()); } Err(err) => { warn!("client error: {:?}", &err); responder .send(ClientboundPacket::Error(ProtoError::FormatError(format!( "{}", &err )))) .map_err(|_| io::Error::from(ErrorKind::InvalidInput))? } } buf.clear(); } } }