diff --git a/src/connection/queue/mod.rs b/src/connection/queue/mod.rs index 2af3f06..cacf054 100644 --- a/src/connection/queue/mod.rs +++ b/src/connection/queue/mod.rs @@ -267,6 +267,8 @@ impl FragmentQueue { if let Some(meta) = fragment.fragment_meta.clone() { if let Some((size, frames)) = self.fragments.get_mut(&meta.id) { // check if the frame index is out of bounds + // todo: Check if == or >, I think it's > but I'm not sure. + // todo: This is because the index starts at 0 and the size starts at 1. if meta.index >= *size { return Err(FragmentQueueError::FrameIndexOutOfBounds); } @@ -299,13 +301,14 @@ impl FragmentQueue { pub fn collect(&mut self, id: u16) -> Result, FragmentQueueError> { if let Some((size, frames)) = self.fragments.get_mut(&id) { if *size == frames.len() as u32 { - // we have all the frames! + // sort all frames by id, + // because we now have all frames. frames.sort_by(|a, b| { a.fragment_meta .as_ref() .unwrap() - .id - .cmp(&b.fragment_meta.as_ref().unwrap().id) + .index + .cmp(&b.fragment_meta.as_ref().unwrap().index) }); let mut buffer = Vec::::new(); diff --git a/src/protocol/frame.rs b/src/protocol/frame.rs index e3f4a00..40f90a4 100644 --- a/src/protocol/frame.rs +++ b/src/protocol/frame.rs @@ -12,6 +12,13 @@ pub struct FragmentMeta { pub(crate) index: u32, } +impl FragmentMeta { + /// Creates a new fragment meta with the given size, id, and index. + pub fn new(size: u32, id: u16, index: u32) -> Self { + Self { size, id, index } + } +} + use crate::rakrs_debug; use super::reliability::Reliability; @@ -160,6 +167,11 @@ impl Frame { pub fn is_sequenced(&self) -> bool { self.reliability.is_sequenced() } + + pub fn with_meta(mut self, meta: FragmentMeta) -> Self { + self.fragment_meta = Some(meta); + self + } } impl Reader for Frame {