blob: b3ff1d623d3cf34f4a4f3832eb35981434f1cbda (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
use anyhow::Context as _;
use std::io::{BufRead as _, Write as _};
pub struct Sock(std::os::unix::net::UnixStream);
impl Sock {
pub fn connect() -> std::io::Result<Self> {
Ok(Self(std::os::unix::net::UnixStream::connect(
rbw::dirs::socket_file(),
)?))
}
pub fn send(
&mut self,
msg: &rbw::protocol::Request,
) -> anyhow::Result<()> {
let Self(sock) = self;
sock.write_all(
serde_json::to_string(msg)
.context("failed to serialize message to agent")?
.as_bytes(),
)
.context("failed to send message to agent")?;
sock.write_all(b"\n")
.context("failed to send message to agent")?;
Ok(())
}
pub fn recv(&mut self) -> anyhow::Result<rbw::protocol::Response> {
let Self(sock) = self;
let mut buf = std::io::BufReader::new(sock);
let mut line = String::new();
buf.read_line(&mut line)
.context("failed to read message from agent")?;
Ok(serde_json::from_str(&line)
.context("failed to parse message from agent")?)
}
}
|