CS3700-project3/hptp/src/msg.rs

56 lines
1.2 KiB
Rust

pub use super::seg::{MAX_TOTAL_PACKET_SIZE, UP_HEADER_SIZE, DOWN_HEADER_SIZE};
#[derive(Clone, Debug)]
pub enum UpMsg {
Data {
payload: Vec<u8>,
// seg_idx: SegIdx,
// encoding: SegmentEncoding,
// is_final_packet: bool,
},
}
#[derive(Clone, Debug)]
pub enum DownMsg {
Ack {}, // ackd: SegmentSet
}
#[derive(Error, Debug)]
#[error("deserialization failed; malformed packet")]
pub struct DesError;
pub trait SerDes: Sized {
// For both methods: `buf.len()` must be >= `MAX_SERIALIZED_SIZE`
fn des(buf: &[u8]) -> Result<Self, DesError>;
fn ser_to(&self, buf: &mut [u8]) -> usize;
}
impl SerDes for UpMsg {
fn des(buf: &[u8]) -> Result<Self, DesError> {
Ok(UpMsg::Data {
payload: buf.into(),
})
}
fn ser_to(&self, buf: &mut [u8]) -> usize {
match self {
UpMsg::Data { payload, .. } => {
let len = payload.len();
buf[..len].copy_from_slice(&payload[..]);
len
}
}
}
}
impl SerDes for DownMsg {
fn des(_buf: &[u8]) -> Result<Self, DesError> {
Ok(DownMsg::Ack {})
}
fn ser_to(&self, _buf: &mut [u8]) -> usize {
1
}
}