ipc: Read only a single line on the client
Browse files at this commit397b7e4bb942b7fe3bd6f1652fa31ccec3ecc48eParent: 598b27f2 changed files +9 -6
- niri-ipc/src/socket.rs +7 -5
- src/ipc/server.rs +2 -1
niri-ipc/src/socket.rsmodified+7 -5
| @@ -1,7 +1,7 @@ | |||
| 1 | //! Helper for blocking communication over the niri socket. | 1 | //! Helper for blocking communication over the niri socket. |
| 2 | 2 | ||
| 3 | use std::env; | 3 | use std::env; |
| 4 | -use std::io::{self, Read, Write}; | 4 | +use std::io::{self, BufRead, BufReader, Write}; |
| 5 | use std::net::Shutdown; | 5 | use std::net::Shutdown; |
| 6 | use std::os::unix::net::UnixStream; | 6 | use std::os::unix::net::UnixStream; |
| 7 | use std::path::Path; | 7 | use std::path::Path; |
| @@ -50,14 +50,16 @@ impl Socket { | |||
| 50 | pub fn send(self, request: Request) -> io::Result<Reply> { | 50 | pub fn send(self, request: Request) -> io::Result<Reply> { |
| 51 | let Self { mut stream } = self; | 51 | let Self { mut stream } = self; |
| 52 | 52 | ||
| 53 | - let mut buf = serde_json::to_vec(&request).unwrap(); | 53 | + let mut buf = serde_json::to_string(&request).unwrap(); |
| 54 | - stream.write_all(&buf)?; | 54 | + stream.write_all(buf.as_bytes())?; |
| 55 | stream.shutdown(Shutdown::Write)?; | 55 | stream.shutdown(Shutdown::Write)?; |
| 56 | 56 | ||
| 57 | + let mut reader = BufReader::new(stream); | ||
| 58 | + | ||
| 57 | buf.clear(); | 59 | buf.clear(); |
| 58 | - stream.read_to_end(&mut buf)?; | 60 | + reader.read_line(&mut buf)?; |
| 59 | 61 | ||
| 60 | - let reply = serde_json::from_slice(&buf)?; | 62 | + let reply = serde_json::from_str(&buf)?; |
| 61 | Ok(reply) | 63 | Ok(reply) |
| 62 | } | 64 | } |
| 63 | } | 65 | } |
src/ipc/server.rsmodified+2 -1
| @@ -132,7 +132,8 @@ async fn handle_client(ctx: ClientCtx, stream: Async<'_, UnixStream>) -> anyhow: | |||
| 132 | } | 132 | } |
| 133 | } | 133 | } |
| 134 | 134 | ||
| 135 | - let buf = serde_json::to_vec(&reply).context("error formatting reply")?; | 135 | + let mut buf = serde_json::to_vec(&reply).context("error formatting reply")?; |
| 136 | + buf.push(b'\n'); | ||
| 136 | write.write_all(&buf).await.context("error writing reply")?; | 137 | write.write_all(&buf).await.context("error writing reply")?; |
| 137 | 138 | ||
| 138 | Ok(()) | 139 | Ok(()) |