-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement version checking; streamed IPC streamed IPC will allow multiple requests per connection add nonsense request change inline struct to json macro only check version if request actually fails fix usage of inspect_err (MSRV 1.72.0; stabilized 1.76.0) "nonsense request" -> "return error" oneshot connections
- Loading branch information
Showing
8 changed files
with
205 additions
and
53 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use std::io::{self, Write}; | ||
use std::os::unix::net::UnixStream; | ||
use std::path::Path; | ||
|
||
use serde_json::de::IoRead; | ||
use serde_json::StreamDeserializer; | ||
|
||
use crate::{Reply, Request}; | ||
|
||
/// Name of the environment variable containing the niri IPC socket path. | ||
pub const SOCKET_PATH_ENV: &str = "NIRI_SOCKET"; | ||
|
||
/// A client for the niri IPC server. | ||
/// | ||
/// This struct is used to communicate with the niri IPC server. It handles the socket connection | ||
/// and serialization/deserialization of messages. | ||
pub struct NiriSocket { | ||
stream: UnixStream, | ||
responses: StreamDeserializer<'static, IoRead<UnixStream>, Reply>, | ||
} | ||
|
||
impl TryFrom<UnixStream> for NiriSocket { | ||
type Error = io::Error; | ||
fn try_from(stream: UnixStream) -> io::Result<Self> { | ||
let responses = serde_json::Deserializer::from_reader(stream.try_clone()?).into_iter(); | ||
Ok(Self { stream, responses }) | ||
} | ||
} | ||
|
||
impl NiriSocket { | ||
/// Connects to the default niri IPC socket | ||
/// | ||
/// This is equivalent to calling [Self::connect] with the value of the [SOCKET_PATH_ENV] | ||
/// environment variable. | ||
pub fn new() -> io::Result<Self> { | ||
let socket_path = std::env::var_os(SOCKET_PATH_ENV).ok_or_else(|| { | ||
io::Error::new( | ||
io::ErrorKind::NotFound, | ||
format!("{SOCKET_PATH_ENV} is not set, are you running this within niri?"), | ||
) | ||
})?; | ||
Self::connect(socket_path) | ||
} | ||
|
||
/// Connect to the socket at the given path | ||
/// | ||
/// See also: [UnixStream::connect] | ||
pub fn connect(path: impl AsRef<Path>) -> io::Result<Self> { | ||
Self::try_from(UnixStream::connect(path.as_ref())?) | ||
} | ||
|
||
/// Handle a request to the niri IPC server | ||
/// | ||
/// # Returns | ||
/// Ok(Ok([Response](crate::Response))) corresponds to a successful response from the running | ||
/// niri instance. Ok(Err([String])) corresponds to an error received from the running niri | ||
/// instance. Err([std::io::Error]) corresponds to an error in the IPC communication. | ||
pub fn send(mut self, request: Request) -> io::Result<Reply> { | ||
let mut buf = serde_json::to_vec(&request).unwrap(); | ||
writeln!(buf).unwrap(); | ||
self.stream.write_all(&buf)?; // .context("error writing IPC request")?; | ||
self.stream.flush()?; | ||
|
||
if let Some(next) = self.responses.next() { | ||
next.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err)) | ||
} else { | ||
Err(io::Error::new( | ||
io::ErrorKind::UnexpectedEof, | ||
"no response from server", | ||
)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters