37 lines
1.0 KiB
Rust
37 lines
1.0 KiB
Rust
//! IPC client — connects to the daemon's Unix socket and sends/receives
|
|||
|
|
//! framed JSON messages.
|
||
|
|
|
||
|
|
use std::os::unix::net::UnixStream;
|
||
|
|
use std::sync::Mutex;
|
||
|
|
|
||
|
|
/// A thread-safe IPC client connected to a Zesdex daemon over a Unix socket.
|
||
|
|
pub struct IpcClient {
|
||
|
|
conn: Mutex<crate::ipc::conn::Connection>,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl IpcClient {
|
||
|
|
pub fn connect_unix(path: &str) -> anyhow::Result<Self> {
|
||
|
|
let stream = UnixStream::connect(path)?;
|
||
|
|
let conn = crate::ipc::conn::Connection::new(stream);
|
||
|
|
Ok(Self {
|
||
|
|
conn: Mutex::new(conn),
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn send<T: serde::Serialize>(&self, msg: &T) -> anyhow::Result<()> {
|
||
|
|
let mut guard = self
|
||
|
|
.conn
|
||
|
|
.lock()
|
||
|
|
.expect("IpcClient mutex poisoned");
|
||
|
|
guard.send(msg)
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn receive<T: serde::de::DeserializeOwned>(&self) -> anyhow::Result<Option<T>> {
|
||
|
|
let mut guard = self
|
||
|
|
.conn
|
||
|
|
.lock()
|
||
|
|
.expect("IpcClient mutex poisoned");
|
||
|
|
guard.receive()
|
||
|
|
}
|
||
|
|
}
|