1use bincode::{Decode, Encode, config::standard};
2use std::borrow::Cow;
3use std::io;
4use std::io::{BufRead, ErrorKind, Write};
5use std::sync::LazyLock;
6
7pub mod client;
8pub mod env;
9pub mod server;
10
11#[cfg(windows)]
12pub const NO_AGENT_ERROR_KIND: ErrorKind = ErrorKind::NotFound;
13#[cfg(not(windows))]
14pub const NO_AGENT_ERROR_KIND: ErrorKind = ErrorKind::ConnectionRefused;
15
16const EOT: u8 = 4;
18
19fn send_serialized<R: Encode, C: Write>(request: &R, con: &mut C) -> io::Result<()> {
20 con.write_all(
21 bincode::encode_to_vec(request, standard())
22 .map_err(io::Error::other)?
23 .as_slice(),
24 )?;
25 con.write_all([EOT].as_ref())
26}
27
28fn read_deserialized<R: Decode<()>, C: BufRead>(con: &mut C) -> io::Result<R> {
29 let mut buf = Vec::with_capacity(32);
30 con.read_until(EOT, &mut buf)?;
31 buf.pop();
32 bincode::decode_from_slice(buf.as_slice(), standard())
33 .map(|(decoded, _)| decoded)
34 .map_err(io::Error::other)
35}
36
37static SOCKET_NAME: LazyLock<Cow<str>> = LazyLock::new(env::agent_socket_name);