use tokio::io::AsyncReadExt;
pub(crate) struct CommandRequestRaw {
pub(crate) magic: u32,
pub(crate) flags: u16,
pub(crate) command_type: u16,
pub(crate) cookie: u64,
pub(crate) offset: u64,
pub(crate) length: u32,
pub(crate) data: Vec<u8>,
}
impl CommandRequestRaw {
const WRITE_COMMAND: u16 = 1;
pub(crate) async fn read<R>(reader: &mut R) -> Result<Self, std::io::Error>
where
R: AsyncReadExt + Unpin,
{
let magic = reader.read_u32().await?;
let flags = reader.read_u16().await?;
let command_type = reader.read_u16().await?;
let cookie = reader.read_u64().await?;
let offset = reader.read_u64().await?;
let length = reader.read_u32().await?;
let data = if command_type == Self::WRITE_COMMAND {
let mut data = vec![0; length as usize];
reader.read_exact(&mut data).await?;
data
} else {
vec![] };
Ok(Self {
magic,
flags,
command_type,
cookie,
offset,
length,
data,
})
}
}