Skip to content

Commit

Permalink
perf(packet): use chunks instead of maintain an indicator for each pa…
Browse files Browse the repository at this point in the history
…cket
  • Loading branch information
fu050409 committed May 17, 2024
1 parent 69cb6eb commit 2729bf4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
28 changes: 16 additions & 12 deletions src/models/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,16 +295,16 @@ impl<'a> OED<'a> {
stream.send(&self.plain_data()?).await?;

self.chunk_count = 0;
let mut remaining_data = &self.encrypted_data[..];
while !remaining_data.is_empty() {
let chunk_size = remaining_data.len().min(1024);

let chunk_length = chunk_size as u32;

stream.send(&chunk_length.to_be_bytes()).await?;
stream.send(&remaining_data[..chunk_size]).await?;

remaining_data = &remaining_data[chunk_size..];
let mut chunks = self.encrypted_data.chunks(1024);
loop {
if let Some(chunk) = chunks.next() {
let chunk_size = chunk.len() as u32;
stream.send(&chunk_size.to_be_bytes()).await?;
stream.send(&chunk).await?;
self.chunk_count += 1;
} else {
break;
}
}
stream.send(&STOP_FLAG).await?;

Expand All @@ -320,7 +320,11 @@ impl<'a> OED<'a> {
Ok(plain_bytes)
}

pub fn get_data(&self) -> Vec<u8> {
self.data.clone().unwrap()
pub fn take(&mut self) -> Vec<u8> {
self.data.take().unwrap()
}

pub fn get_data(&self) -> &[u8] {
self.data.as_ref().unwrap()
}
}
2 changes: 1 addition & 1 deletion src/models/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ impl Session {
let content = OED::new(&self.aes_key)
.from_stream(socket)
.await?
.get_data();
.take();
let status_code = OSC::from_stream(socket).await?.status_code;
let response = Response::new(None, content, None, status_code, flag);

Expand Down

0 comments on commit 2729bf4

Please sign in to comment.