handle duplicate packets

This commit is contained in:
Milo Turner 2020-03-10 12:32:23 -04:00
parent dd6c4ff088
commit 86af734542
2 changed files with 30 additions and 13 deletions

View File

@ -61,16 +61,27 @@ where
loop { loop {
match peer.recv().await { match peer.recv().await {
Ok(UpMsg::Data { payload, seg_idx }) => { Ok(UpMsg::Data { payload, seg_idx }) => {
let len = payload.len(); if segs.contains_key(&seg_idx) {
segs.entry(seg_idx).or_insert(Some(payload)); log.recv_data_ignored(seg_idx as usize, payload.len()).await;
let ack = DownMsg::Ack { } else {
idxs: segs.keys().cloned().collect(), log.recv_data_accepted(
}; seg_idx as usize,
log.debug_msg(format!("sent ack: {:?}", payload.len(),
segs.keys().collect::<Vec<_>>())).await; hptp::logger::OutOfOrder,
send(peer, ack).await?; )
log.recv_data_accepted(seg_idx as usize, len, hptp::logger::OutOfOrder)
.await; .await;
segs.insert(seg_idx, Some(payload));
let ack = DownMsg::Ack {
idxs: segs.keys().cloned().collect(),
};
log.debug_msg(format!("sent ack: {:?}", {
let mut idxs = segs.keys().collect::<Vec<_>>();
idxs.sort_unstable();
idxs
}))
.await;
send(peer, ack).await?;
}
} }
Err(peer::RecvError::InvalidMessage { .. }) => log.recv_corrupt().await, Err(peer::RecvError::InvalidMessage { .. }) => log.recv_corrupt().await,

View File

@ -44,8 +44,9 @@ impl Logger {
.await .await
} }
pub async fn recv_data_ignored(&mut self) { pub async fn recv_data_ignored(&mut self, start: usize, len: usize) {
self.log_payload(LogPayload::RecvDataIgnored).await self.log_payload(LogPayload::RecvDataIgnored { start, len })
.await
} }
pub async fn recv_ack(&mut self, last_offset: usize) { pub async fn recv_ack(&mut self, last_offset: usize) {
@ -91,7 +92,10 @@ enum LogPayload<'a> {
len: usize, len: usize,
order: AcceptedOrder, order: AcceptedOrder,
}, },
RecvDataIgnored, RecvDataIgnored {
start: usize,
len: usize,
},
RecvAck { RecvAck {
last_offset: usize, last_offset: usize,
}, },
@ -134,7 +138,9 @@ impl Display for LogPayload<'_> {
LogPayload::RecvDataAccepted { start, len, order } => { LogPayload::RecvDataAccepted { start, len, order } => {
write!(f, "[recv data] {} ({}) ACCEPTED ({})", start, len, order) write!(f, "[recv data] {} ({}) ACCEPTED ({})", start, len, order)
} }
LogPayload::RecvDataIgnored => write!(f, "[recv data] IGNORED"), LogPayload::RecvDataIgnored { start, len } => {
write!(f, "[recv data] {} ({}) IGNORED", start, len)
}
LogPayload::RecvCorrupt => write!(f, "[recv corrupt packet]"), LogPayload::RecvCorrupt => write!(f, "[recv corrupt packet]"),
LogPayload::RecvAck { last_offset } => write!(f, "[recv ack] {}", last_offset), LogPayload::RecvAck { last_offset } => write!(f, "[recv ack] {}", last_offset),
LogPayload::SendData { start, len } => write!(f, "[send data] {} ({})", start, len), LogPayload::SendData { start, len } => write!(f, "[send data] {} ({})", start, len),