From 1854e849499317c293156f78f46cad0a1d218dcb Mon Sep 17 00:00:00 2001 From: fluo10 Date: Tue, 17 Jun 2025 08:47:06 +0900 Subject: [PATCH] Add ipc request --- .../src/ipc/client/unix.rs | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/lazy-supplements-desktop/src/ipc/client/unix.rs b/lazy-supplements-desktop/src/ipc/client/unix.rs index e69de29..17e51f6 100644 --- a/lazy-supplements-desktop/src/ipc/client/unix.rs +++ b/lazy-supplements-desktop/src/ipc/client/unix.rs @@ -0,0 +1,50 @@ +use std::path::Path; +use tokio::{io::Interest, net::UnixStream}; + +use crate::{ + error::Error, + ipc::message::{Request, Response, ResponseContent}, +}; + + +pub async fn request(path: T, request: U) -> Result +where + T: AsRef, + U: Into +{ + let stream = UnixStream::connect(path).await?; + let ready = stream.ready(Interest::WRITABLE).await?; + let request: Request = request.into(); + let mut response_buf = Vec::new(); + if let Err(e) = ciborium::into_writer(&request, &mut response_buf) { + todo!(); + }; + match stream.try_write(&response_buf) { + Ok(x) => { + println!("write {} bytes", x) + } + Err(e) => { + return Err(e.into()) + } + } + loop { + let ready_write = stream.ready(Interest::READABLE).await?; + let mut read_buf : Vec = Vec::new(); + match stream.try_read_buf(&mut read_buf) { + Ok(x) => { + println!("read {} bytes", x) + } + Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => { + continue; + } + Err(e) => { + return Err(e.into()) + } + } + let mut buf : Vec = Vec::new(); + let response: Response = ciborium::from_reader_with_buffer(read_buf.as_slice(), &mut buf)?; + if response.id == request.id { + return Ok(response.content) + } + } +} \ No newline at end of file