mod codec;
mod command;
pub use codec::{HEADER_LEN, checksum, decode_header, encode_header};
pub use command::Command;
use bytes::Bytes;
pub const VERSION: u32 = 0x0100_0001;
pub const VERSION_MIN: u32 = 0x0100_0000;
pub const VERSION_SKIP_CHECKSUM: u32 = 0x0100_0001;
pub const MAX_PAYLOAD: u32 = 1024 * 1024;
pub const LEGACY_MAX_PAYLOAD: u32 = 256 * 1024;
pub const AUTH_TOKEN: u32 = 1;
pub const AUTH_SIGNATURE: u32 = 2;
pub const AUTH_RSAPUBLICKEY: u32 = 3;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Header {
pub command: Command,
pub arg0: u32,
pub arg1: u32,
pub data_length: u32,
pub data_check: u32,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Message {
pub command: Command,
pub arg0: u32,
pub arg1: u32,
pub payload: Bytes,
}
impl Message {
pub fn new(command: Command, arg0: u32, arg1: u32, payload: impl Into<Bytes>) -> Self {
Self {
command,
arg0,
arg1,
payload: payload.into(),
}
}
pub fn empty(command: Command, arg0: u32, arg1: u32) -> Self {
Self::new(command, arg0, arg1, Bytes::new())
}
pub fn header(&self, with_checksum: bool) -> Option<Header> {
let data_length = u32::try_from(self.payload.len()).ok()?;
let data_check = if with_checksum {
checksum(&self.payload)
} else {
0
};
Some(Header {
command: self.command,
arg0: self.arg0,
arg1: self.arg1,
data_length,
data_check,
})
}
pub fn checksum_matches(&self, header: &Header) -> bool {
header.data_check == checksum(&self.payload)
}
}
impl Header {
pub fn with_payload(self, payload: impl Into<Bytes>) -> Message {
Message::new(self.command, self.arg0, self.arg1, payload)
}
}