uds
This commit is contained in:
37
src/ipc.rs
37
src/ipc.rs
@ -0,0 +1,37 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::io::{Read, Write};
|
||||
use std::os::unix::net::UnixStream;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub enum IpcRequest {
|
||||
GetHistory { limit: usize },
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub enum IpcResponse {
|
||||
History(Vec<String>),
|
||||
}
|
||||
|
||||
pub fn fetch_history(limit: usize) -> Option<Vec<String>> {
|
||||
let base_dir = directories::ProjectDirs::from("com", "zefad", "rklipd")?
|
||||
.data_dir()
|
||||
.to_path_buf();
|
||||
let socket_path = base_dir.join("rklip.sock");
|
||||
|
||||
if let Ok(mut stream) = UnixStream::connect(&socket_path) {
|
||||
let req = IpcRequest::GetHistory { limit };
|
||||
let req_json = serde_json::to_string(&req).unwrap();
|
||||
|
||||
let _ = stream.write_all(req_json.as_bytes());
|
||||
let _ = stream.shutdown(std::net::Shutdown::Write);
|
||||
|
||||
let mut response_buffer = String::new();
|
||||
if stream.read_to_string(&mut response_buffer).is_ok() {
|
||||
if let Ok(IpcResponse::History(items)) = serde_json::from_str(&response_buffer) {
|
||||
return Some(items);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user