Skip to main content

umbral_socket/stream/
client.rs

1use std::io;
2use std::os::unix::net::UnixStream;
3
4use crate::stream::protocol::{
5    MAX_PAYLOAD_LEN, MethodId, UmbralStatus, read_response_sync, write_request_sync,
6};
7
8pub struct UmbralClient {
9    stream: UnixStream,
10}
11
12impl UmbralClient {
13    pub fn new(socket: &str) -> io::Result<Self> {
14        let stream = UnixStream::connect(socket)?;
15        Ok(Self { stream })
16    }
17
18    pub fn send(&mut self, method: MethodId, payload: &[u8]) -> io::Result<Vec<u8>> {
19        if payload.len() > MAX_PAYLOAD_LEN {
20            return Err(io::Error::new(
21                io::ErrorKind::InvalidInput,
22                "payload too large",
23            ));
24        }
25
26        write_request_sync(&mut self.stream, method, payload)?;
27        let (status, response) = read_response_sync(&mut self.stream, MAX_PAYLOAD_LEN)?;
28        if status == UmbralStatus::Ok {
29            return Ok(response);
30        }
31        Err(request_error(status))
32    }
33}
34
35fn request_error(status: UmbralStatus) -> io::Error {
36    io::Error::other(format!("umbral request failed with status {status:?}"))
37}