forge
Explore
Log in

Don't you dare go Hollow

ipc/socket: Support multiple requests

Browse files at this commit

2aff1ec71a46487439d8785fd54c53dabe77023eIvan MolodetskikhParent: 3466fc0
niri-ipc/src/socket.rsmodified+42 -18
@@ -16,7 +16,7 @@ pub const SOCKET_PATH_ENV: &str = "NIRI_SOCKET";
1616 /// This struct is used to communicate with the niri IPC server. It handles the socket connection
1717 /// and serialization/deserialization of messages.
1818 pub struct Socket {
19- stream: UnixStream,
19+ stream: BufReader<UnixStream>,
2020 }
2121
2222 impl Socket {
@@ -37,6 +37,7 @@ impl Socket {
3737 /// Connects to the niri IPC socket at the given path.
3838 pub fn connect_to(path: impl AsRef<Path>) -> io::Result<Self> {
3939 let stream = UnixStream::connect(path.as_ref())?;
40+ let stream = BufReader::new(stream);
4041 Ok(Self { stream })
4142 }
4243
@@ -47,31 +48,54 @@ impl Socket {
4748 /// * `Ok(Ok(response))`: successful [`Response`](crate::Response) from niri
4849 /// * `Ok(Err(message))`: error message from niri
4950 /// * `Err(error)`: error communicating with niri
50- ///
51- /// This method also returns a blocking function that you can call to keep reading [`Event`]s
52- /// after requesting an [`EventStream`][Request::EventStream]. This function is not useful
53- /// otherwise.
54- pub fn send(self, request: Request) -> io::Result<(Reply, impl FnMut() -> io::Result<Event>)> {
55- let Self { mut stream } = self;
56-
51+ pub fn send(&mut self, request: Request) -> io::Result<Reply> {
5752 let mut buf = serde_json::to_string(&request).unwrap();
58- stream.write_all(buf.as_bytes())?;
59- stream.shutdown(Shutdown::Write)?;
60-
61- let mut reader = BufReader::new(stream);
53+ buf.push('\n');
54+ self.stream.get_mut().write_all(buf.as_bytes())?;
6255
6356 buf.clear();
64- reader.read_line(&mut buf)?;
57+ self.stream.read_line(&mut buf)?;
6558
6659 let reply = serde_json::from_str(&buf)?;
60+ Ok(reply)
61+ }
6762
68- let events = move || {
63+ /// Starts reading event stream [`Event`]s from the socket.
64+ ///
65+ /// The returned function will block until the next [`Event`] arrives, then return it.
66+ ///
67+ /// Use this only after requesting an [`EventStream`][Request::EventStream].
68+ ///
69+ /// # Examples
70+ ///
71+ /// ```no_run
72+ /// use niri_ipc::{Request, Response};
73+ /// use niri_ipc::socket::Socket;
74+ ///
75+ /// fn main() -> std::io::Result<()> {
76+ /// let mut socket = Socket::connect()?;
77+ ///
78+ /// let reply = socket.send(Request::EventStream)?;
79+ /// if matches!(reply, Ok(Response::Handled)) {
80+ /// let mut read_event = socket.read_events();
81+ /// while let Ok(event) = read_event() {
82+ /// println!("Received event: {event:?}");
83+ /// }
84+ /// }
85+ ///
86+ /// Ok(())
87+ /// }
88+ /// ```
89+ pub fn read_events(self) -> impl FnMut() -> io::Result<Event> {
90+ let Self { mut stream } = self;
91+ let _ = stream.get_mut().shutdown(Shutdown::Write);
92+
93+ let mut buf = String::new();
94+ move || {
6995 buf.clear();
70- reader.read_line(&mut buf)?;
96+ stream.read_line(&mut buf)?;
7197 let event = serde_json::from_str(&buf)?;
7298 Ok(event)
73- };
74-
75- Ok((reply, events))
99+ }
76100 }
77101 }
src/ipc/client.rsmodified+4 -6
@@ -35,9 +35,9 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> {
3535 Msg::OverviewState => Request::OverviewState,
3636 };
3737
38- let socket = Socket::connect().context("error connecting to the niri socket")?;
38+ let mut socket = Socket::connect().context("error connecting to the niri socket")?;
3939
40- let (reply, mut read_event) = socket
40+ let reply = socket
4141 .send(request)
4242 .context("error communicating with niri")?;
4343
@@ -45,10 +45,7 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> {
4545 Err(_) if !matches!(msg, Msg::Version) => {
4646 // If we got an error, it might be that the CLI is a different version from the running
4747 // niri instance. Request the running instance version to compare and print a message.
48- Socket::connect()
49- .and_then(|socket| socket.send(Request::Version))
50- .ok()
51- .map(|(reply, _read_event)| reply)
48+ socket.send(Request::Version).ok()
5249 }
5350 _ => None,
5451 };
@@ -392,6 +389,7 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> {
392389 println!("Started reading events.");
393390 }
394391
392+ let mut read_event = socket.read_events();
395393 loop {
396394 let event = read_event().context("error reading event from niri")?;
397395