use tokio::io::{AsyncReadExt, AsyncWriteExt};
use crate::sync_core::{frame_bytes, MAX_FRAME};
pub async fn send_frame<W: AsyncWriteExt + Unpin>(
w: &mut W,
data: &[u8],
) -> tokio::io::Result<()> {
let frame = frame_bytes(data)?;
w.write_all(&frame).await
}
pub async fn recv_frame<R: AsyncReadExt + Unpin>(r: &mut R) -> tokio::io::Result<Vec<u8>> {
let mut len_buf = [0u8; 4];
r.read_exact(&mut len_buf).await?;
let len = u32::from_le_bytes(len_buf) as usize;
if len == 0 || len > MAX_FRAME {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"bad frame length",
));
}
let mut buf = vec![0u8; len];
r.read_exact(&mut buf).await?;
Ok(buf)
}