rpc/jsonrpc: Implement requests over Unix sockets.

This commit is contained in:
parazyd
2021-10-22 16:45:44 +02:00
parent c6a9d644d1
commit 43ed3052e4

View File

@@ -1,4 +1,5 @@
use std::net::{TcpStream, ToSocketAddrs};
use std::os::unix::net::UnixStream;
use std::str;
use async_std::io::{ReadExt, WriteExt};
@@ -186,3 +187,16 @@ pub async fn send_raw_request(url: &str, data: Value) -> Result<JsonResult, Erro
let reply: JsonResult = serde_json::from_slice(&buf[0..bytes_read])?;
Ok(reply)
}
pub async fn send_unix_request(path: &str, data: Value) -> Result<JsonResult, Error> {
let mut buf = [0; 2048];
let bytes_read: usize;
let data_str = serde_json::to_string(&data)?;
let mut stream = Async::<UnixStream>::connect(path).await?;
stream.write_all(&data_str.as_bytes()).await?;
bytes_read = stream.read(&mut buf[..]).await?;
let reply: JsonResult = serde_json::from_slice(&buf[0..bytes_read])?;
Ok(reply)
}