2026-07-11 13:16:10 +07:00
|
|
|
use std::os::unix::net::UnixStream;
|
|
|
|
|
use anyhow::Result;
|
|
|
|
|
use super::frame;
|
|
|
|
|
|
2026-07-11 23:45:13 +07:00
|
|
|
pub struct Connection {
|
|
|
|
|
inner: UnixStream,
|
2026-07-11 13:16:10 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Connection {
|
2026-07-11 23:45:13 +07:00
|
|
|
pub fn from_stream(stream: UnixStream) -> Result<Self> {
|
|
|
|
|
Ok(Connection { inner: stream })
|
2026-07-11 13:16:10 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn connect_unix(path: &str) -> Result<Self> {
|
|
|
|
|
let stream = UnixStream::connect(path)?;
|
2026-07-11 23:45:13 +07:00
|
|
|
Ok(Connection { inner: stream })
|
2026-07-11 13:16:10 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn send<T: serde::Serialize>(&mut self, value: &T) -> Result<()> {
|
|
|
|
|
let data = frame::serialize_frame(value)?;
|
2026-07-11 23:45:13 +07:00
|
|
|
frame::write_frame(&mut self.inner, &data)
|
2026-07-11 13:16:10 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn receive<T: serde::de::DeserializeOwned>(&mut self) -> Result<Option<T>> {
|
2026-07-11 23:45:13 +07:00
|
|
|
let data = frame::read_frame(&mut self.inner)?;
|
2026-07-11 13:16:10 +07:00
|
|
|
match data {
|
|
|
|
|
Some(bytes) => {
|
|
|
|
|
let value: T = frame::deserialize_frame(&bytes)?;
|
|
|
|
|
Ok(Some(value))
|
|
|
|
|
}
|
|
|
|
|
None => Ok(None),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|