ipc/socket: Support multiple requests
Browse files at this commit2aff1ec71a46487439d8785fd54c53dabe77023eParent: 3466fc02 changed files +46 -24
- niri-ipc/src/socket.rs +42 -18
- src/ipc/client.rs +4 -6
niri-ipc/src/socket.rsmodified+42 -18
| @@ -16,7 +16,7 @@ pub const SOCKET_PATH_ENV: &str = "NIRI_SOCKET"; | |||
| 16 | /// This struct is used to communicate with the niri IPC server. It handles the socket connection | 16 | /// This struct is used to communicate with the niri IPC server. It handles the socket connection |
| 17 | /// and serialization/deserialization of messages. | 17 | /// and serialization/deserialization of messages. |
| 18 | pub struct Socket { | 18 | pub struct Socket { |
| 19 | - stream: UnixStream, | 19 | + stream: BufReader<UnixStream>, |
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | impl Socket { | 22 | impl Socket { |
| @@ -37,6 +37,7 @@ impl Socket { | |||
| 37 | /// Connects to the niri IPC socket at the given path. | 37 | /// Connects to the niri IPC socket at the given path. |
| 38 | pub fn connect_to(path: impl AsRef<Path>) -> io::Result<Self> { | 38 | pub fn connect_to(path: impl AsRef<Path>) -> io::Result<Self> { |
| 39 | let stream = UnixStream::connect(path.as_ref())?; | 39 | let stream = UnixStream::connect(path.as_ref())?; |
| 40 | + let stream = BufReader::new(stream); | ||
| 40 | Ok(Self { stream }) | 41 | Ok(Self { stream }) |
| 41 | } | 42 | } |
| 42 | 43 | ||
| @@ -47,31 +48,54 @@ impl Socket { | |||
| 47 | /// * `Ok(Ok(response))`: successful [`Response`](crate::Response) from niri | 48 | /// * `Ok(Ok(response))`: successful [`Response`](crate::Response) from niri |
| 48 | /// * `Ok(Err(message))`: error message from niri | 49 | /// * `Ok(Err(message))`: error message from niri |
| 49 | /// * `Err(error)`: error communicating with niri | 50 | /// * `Err(error)`: error communicating with niri |
| 50 | - /// | 51 | + pub fn send(&mut self, request: Request) -> io::Result<Reply> { |
| 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 | - | ||
| 57 | let mut buf = serde_json::to_string(&request).unwrap(); | 52 | let mut buf = serde_json::to_string(&request).unwrap(); |
| 58 | - stream.write_all(buf.as_bytes())?; | 53 | + buf.push('\n'); |
| 59 | - stream.shutdown(Shutdown::Write)?; | 54 | + self.stream.get_mut().write_all(buf.as_bytes())?; |
| 60 | - | ||
| 61 | - let mut reader = BufReader::new(stream); | ||
| 62 | 55 | ||
| 63 | buf.clear(); | 56 | buf.clear(); |
| 64 | - reader.read_line(&mut buf)?; | 57 | + self.stream.read_line(&mut buf)?; |
| 65 | 58 | ||
| 66 | let reply = serde_json::from_str(&buf)?; | 59 | let reply = serde_json::from_str(&buf)?; |
| 60 | + Ok(reply) | ||
| 61 | + } | ||
| 67 | 62 | ||
| 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 || { | ||
| 69 | buf.clear(); | 95 | buf.clear(); |
| 70 | - reader.read_line(&mut buf)?; | 96 | + stream.read_line(&mut buf)?; |
| 71 | let event = serde_json::from_str(&buf)?; | 97 | let event = serde_json::from_str(&buf)?; |
| 72 | Ok(event) | 98 | Ok(event) |
| 73 | - }; | 99 | + } |
| 74 | - | ||
| 75 | - Ok((reply, events)) | ||
| 76 | } | 100 | } |
| 77 | } | 101 | } |
src/ipc/client.rsmodified+4 -6
| @@ -35,9 +35,9 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { | |||
| 35 | Msg::OverviewState => Request::OverviewState, | 35 | Msg::OverviewState => Request::OverviewState, |
| 36 | }; | 36 | }; |
| 37 | 37 | ||
| 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")?; |
| 39 | 39 | ||
| 40 | - let (reply, mut read_event) = socket | 40 | + let reply = socket |
| 41 | .send(request) | 41 | .send(request) |
| 42 | .context("error communicating with niri")?; | 42 | .context("error communicating with niri")?; |
| 43 | 43 | ||
| @@ -45,10 +45,7 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { | |||
| 45 | Err(_) if !matches!(msg, Msg::Version) => { | 45 | Err(_) if !matches!(msg, Msg::Version) => { |
| 46 | // If we got an error, it might be that the CLI is a different version from the running | 46 | // If we got an error, it might be that the CLI is a different version from the running |
| 47 | // niri instance. Request the running instance version to compare and print a message. | 47 | // niri instance. Request the running instance version to compare and print a message. |
| 48 | - Socket::connect() | 48 | + socket.send(Request::Version).ok() |
| 49 | - .and_then(|socket| socket.send(Request::Version)) | ||
| 50 | - .ok() | ||
| 51 | - .map(|(reply, _read_event)| reply) | ||
| 52 | } | 49 | } |
| 53 | _ => None, | 50 | _ => None, |
| 54 | }; | 51 | }; |
| @@ -392,6 +389,7 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { | |||
| 392 | println!("Started reading events."); | 389 | println!("Started reading events."); |
| 393 | } | 390 | } |
| 394 | 391 | ||
| 392 | + let mut read_event = socket.read_events(); | ||
| 395 | loop { | 393 | loop { |
| 396 | let event = read_event().context("error reading event from niri")?; | 394 | let event = read_event().context("error reading event from niri")?; |
| 397 | 395 | ||