1use futures_util::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
2
3pub mod bridge;
4pub mod client_control;
5pub mod exit;
6
7pub async fn write_prepend_length<W: AsyncWrite + Unpin>(
9 value: &[u8],
10 mut out: W,
11) -> std::io::Result<()> {
12 let len = value.len() as u32;
13 let len_buf = len.to_be_bytes();
14
15 out.write_all(&len_buf).await?;
16 out.write_all(value).await?;
17 out.flush().await
18}
19
20pub async fn read_prepend_length<R: AsyncRead + Unpin>(mut input: R) -> std::io::Result<Vec<u8>> {
22 let mut len_buf = [0u8; 4];
23 input.read_exact(&mut len_buf).await?;
24 let len = u32::from_be_bytes(len_buf) as usize;
25 if len > 100_000 {
26 return Err(std::io::Error::other(
27 "cannot read length-prepended messages that are too big",
28 ));
29 }
30 let mut value = vec![0u8; len];
31 input.read_exact(&mut value).await?;
32
33 Ok(value)
34}