From 9d4ebe0bf8876e86b0eee34b53b5a585fa74e960 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Fri, 31 Jul 2020 14:45:24 +0700 Subject: [PATCH] Hashmaps in the contiuation model --- src/bc/de.rs | 50 ++++++++++++++++++++++++++++++++++++------------- src/bc/model.rs | 16 ++++++++-------- 2 files changed, 45 insertions(+), 21 deletions(-) diff --git a/src/bc/de.rs b/src/bc/de.rs index 81b8767..b34f8d5 100644 --- a/src/bc/de.rs +++ b/src/bc/de.rs @@ -116,7 +116,7 @@ fn bc_modern_msg<'a, 'b, 'c>( Err, }; - let mut in_bin_mode = context.in_bin_mode.contains(&header.msg_id); + let mut in_bin_mode = context.last_binary_kind.contains_key(&header.msg_id); // We'd like to know where the XML stops, but we haven't parsed the XML yet to see if the // binaryData offset in the header is valid @@ -172,7 +172,6 @@ fn bc_modern_msg<'a, 'b, 'c>( // receive enough bytes before modifying the context (otherwise we'll alter the // behavior of future passes of this function even if we didn't yet consume the // message). - context.in_bin_mode.insert(header.msg_id); let mut binary_data = BinaryData { data: payload, @@ -182,30 +181,55 @@ fn bc_modern_msg<'a, 'b, 'c>( let body_size = binary_data.body_size(); let data_size = binary_data.data_size(); + let remaining_binary_bytes = match context.remaining_binary_bytes.get(&header.msg_id) { + Some(remaining_binary_bytes) => remaining_binary_bytes.clone(), + None => 0, + }; + let last_binary_kind = match context.last_binary_kind.get(&header.msg_id) { + Some(last_binary_kind) => last_binary_kind.clone(), + None => None, + }; + // Handle continuation - if binary_kind != BinaryDataKind::Unknown && data_size > CHUNK_SIZE { - context.remaining_binary_bytes = data_size - body_size; - context.last_binary_kind = Some(binary_kind); + if binary_kind != BinaryDataKind::Unknown && data_size > body_size { + // If it is a known type and the size is larger than the bodysize expect more + // packets to follow + context + .remaining_binary_bytes + .insert(header.msg_id, data_size - body_size); + context + .last_binary_kind + .insert(header.msg_id, Some(binary_kind)); } else if binary_kind == BinaryDataKind::Unknown - && context.remaining_binary_bytes > 0 - && context.remaining_binary_bytes >= body_size + && remaining_binary_bytes > 0 + && remaining_binary_bytes >= body_size { + // If the data is unknown and we are expecting packets then proccess them + // Ensure however that consuming this packet as binary will not result in + // negative remaining_binary_bytes if body_size == CHUNK_SIZE { - binary_data.continuation_of = context.last_binary_kind; - context.remaining_binary_bytes -= body_size; + // All known continuation packets are of size CHUNK_SIZE + binary_data.continuation_of = last_binary_kind; + context + .remaining_binary_bytes + .insert(header.msg_id, remaining_binary_bytes - body_size); } else { + // If its not of chunk size this may be an unknown magic code // Reset but suggest that this might be an unknown magic code trace!( "Possibly new magic code: {:x?}", &binary_data.body()[..std::cmp::min(body_size, 32)] ); - context.remaining_binary_bytes = 0; - context.last_binary_kind = None; + context.remaining_binary_bytes.insert(header.msg_id, 0); + context.last_binary_kind.insert(header.msg_id, None); } } else { + // In all other cases just proccess data as normal non-continuation packets + // We also reset the remaining_binary_bytes which could be non zero if + // The continuation was inturrepted by an audio frame // Reset - context.remaining_binary_bytes = 0; - context.last_binary_kind = None; + context.remaining_binary_bytes.insert(header.msg_id, 0); + context.last_binary_kind.insert(header.msg_id, None); } binary = Some(binary_data); diff --git a/src/bc/model.rs b/src/bc/model.rs index 230159f..7abd350 100644 --- a/src/bc/model.rs +++ b/src/bc/model.rs @@ -1,6 +1,6 @@ use super::xml::BcXml; use log::trace; -use std::collections::HashSet; +use std::collections::HashMap; use std::convert::TryInto; pub(super) const MAGIC_HEADER: u32 = 0xabcdef0; @@ -149,7 +149,9 @@ impl BinaryData { BinaryDataKind::VideoDataPframe } _ => { - trace!("Unknown magic type"); // When large data is chunked it goes here + // When large data is chunked it goes here + // We work out whether or not it is a continued chunked in the deserialization + trace!("Unknown magic type"); BinaryDataKind::Unknown } } @@ -212,9 +214,8 @@ pub(super) struct BcSendInfo { #[derive(Debug)] pub struct BcContext { - pub(super) in_bin_mode: HashSet, - pub(super) last_binary_kind: Option, - pub(super) remaining_binary_bytes: usize, + pub(super) last_binary_kind: HashMap>, + pub(super) remaining_binary_bytes: HashMap, } impl Bc { @@ -234,9 +235,8 @@ impl Bc { impl BcContext { pub fn new() -> BcContext { BcContext { - in_bin_mode: HashSet::new(), - last_binary_kind: None, - remaining_binary_bytes: 0, + last_binary_kind: HashMap::new(), + remaining_binary_bytes: HashMap::new(), } } }