Compare commits
3 commits
70107257c2
...
1854e84949
Author | SHA1 | Date | |
---|---|---|---|
1854e84949 | |||
1a5ea87780 | |||
7569d296b1 |
2 changed files with 53 additions and 1 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -20,4 +20,6 @@ Cargo.lock
|
|||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
||||
tmp/
|
||||
tmp/
|
||||
|
||||
.DS_Store
|
|
@ -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<T, U>(path: T, request: U) -> Result<ResponseContent, Error>
|
||||
where
|
||||
T: AsRef<Path>,
|
||||
U: Into<Request>
|
||||
{
|
||||
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<u8> = 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<u8> = 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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue