Skip to content

Commit

Permalink
bug-fix(queue/mod.rs): Fix frame re-ordering, closes #55
Browse files Browse the repository at this point in the history
  • Loading branch information
john-bv committed Jan 26, 2024
1 parent 6188e52 commit 37b96ee
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/connection/queue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -299,13 +301,14 @@ impl FragmentQueue {
pub fn collect(&mut self, id: u16) -> Result<Vec<u8>, 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::<u8>::new();
Expand Down
12 changes: 12 additions & 0 deletions src/protocol/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Frame> for Frame {
Expand Down

0 comments on commit 37b96ee

Please sign in to comment.