27 lines
682 B
Rust
27 lines
682 B
Rust
//! IPC server — binds a Unix socket and accepts incoming client connections.
|
|
|
|
use std::os::unix::net::UnixListener;
|
|
use std::path::Path;
|
|
|
|
/// A Unix-socket IPC server.
|
|
pub struct IpcServer {
|
|
listener: UnixListener,
|
|
}
|
|
|
|
impl IpcServer {
|
|
pub fn bind_unix(path: &str) -> anyhow::Result<Self> {
|
|
let p = Path::new(path);
|
|
if p.exists() {
|
|
std::fs::remove_file(p)?;
|
|
}
|
|
|
|
let listener = UnixListener::bind(path)?;
|
|
Ok(Self { listener })
|
|
}
|
|
|
|
pub fn accept(&self) -> anyhow::Result<crate::ipc::conn::Connection> {
|
|
let (stream, _addr) = self.listener.accept()?;
|
|
crate::ipc::conn::Connection::new(stream)
|
|
}
|
|
}
|