change ack msg to store as array
This commit is contained in:
parent
a37edf2112
commit
6b3d8acdf1
|
@ -1,7 +1,6 @@
|
|||
use super::seg::SegIdx;
|
||||
pub use super::seg::{DOWN_HEADER_SIZE, MAX_TOTAL_PACKET_SIZE, UP_HEADER_SIZE};
|
||||
use byteorder::ByteOrder;
|
||||
use std::collections::HashSet;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum UpMsg {
|
||||
|
@ -14,7 +13,8 @@ pub enum UpMsg {
|
|||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum DownMsg {
|
||||
Ack { idxs: HashSet<SegIdx> },
|
||||
/// `idxs` must be distinct and in increasing order.
|
||||
Ack { idxs: Vec<SegIdx> },
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
|
@ -56,19 +56,29 @@ impl SerDes for UpMsg {
|
|||
|
||||
impl SerDes for DownMsg {
|
||||
fn des(buf: &[u8]) -> Result<Self, DesError> {
|
||||
let mut idxs = HashSet::new();
|
||||
let mut idxs = vec![];
|
||||
for (i, b) in buf.iter().cloned().enumerate() {
|
||||
for j in 0..8 {
|
||||
if b & (1 << j) != 0 {
|
||||
idxs.insert((i * 8 + j) as SegIdx);
|
||||
idxs.push((i * 8 + j) as SegIdx);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(DownMsg::Ack { idxs })
|
||||
}
|
||||
|
||||
fn ser_to(&self, _buf: &mut [u8]) -> usize {
|
||||
// TODO: implement this!
|
||||
0
|
||||
fn ser_to(&self, buf: &mut [u8]) -> usize {
|
||||
match self {
|
||||
DownMsg::Ack { idxs } => {
|
||||
let mut max_i = 0;
|
||||
for seg_idx in idxs.iter().cloned() {
|
||||
let i = seg_idx as usize / 8;
|
||||
let j = (seg_idx % 8) as u8;
|
||||
buf[i] |= 1 << j;
|
||||
max_i = std::cmp::max(i, max_i);
|
||||
}
|
||||
max_i + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue