//! Unix-socket listener for the `--daemon` process. //! //! Flow: `IpcServer::bind_unix` opens/binds a Unix socket at a well-known //! path (clearing any stale file left by a crashed prior daemon) → //! `accept` blocks for the next client and wraps it as a `Connection` //! (see `conn.rs`) for framed request/response traffic. use std::os::unix::net::UnixListener; use anyhow::Result; use super::conn::Connection; /// Server-side handle for the `--daemon` process: listens on a Unix /// socket and hands out `Connection`s to accepted clients. pub struct IpcServer { listener: UnixListener, } impl IpcServer { /// Bind a new Unix-socket listener at `path`. /// /// Why: removes any stale socket file at `path` first, since a prior /// crashed daemon can leave one behind and `UnixListener::bind` fails /// on an existing path. pub fn bind_unix(path: &str) -> Result { let _ = std::fs::remove_file(path); let listener = UnixListener::bind(path)?; Ok(IpcServer { listener }) } /// Block until a client connects, then wrap it as a `Connection`. pub fn accept(&self) -> Result { let (stream, _addr) = self.listener.accept()?; Ok(Connection::from_stream(stream)) } }