Hashmaps in the contiuation model

This commit is contained in:
QuantumEntangledAndy committed 2020-07-31 14:45:24 +07:00
1 parent 1f98cf517b
commit 9d4ebe0bf8
2 files changed
+45 -21

No files matched your search

+37 -13
View File
@@ -116,7 +116,7 @@ fn bc_modern_msg<'a, 'b, 'c>(
Err, 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 // 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 // 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 // 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 // behavior of future passes of this function even if we didn't yet consume the
// message). // message).
context.in_bin_mode.insert(header.msg_id);
let mut binary_data = BinaryData { let mut binary_data = BinaryData {
data: payload, data: payload,
@@ -182,30 +181,55 @@ fn bc_modern_msg<'a, 'b, 'c>(
let body_size = binary_data.body_size(); let body_size = binary_data.body_size();
let data_size = binary_data.data_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 // Handle continuation
if binary_kind != BinaryDataKind::Unknown && data_size > CHUNK_SIZE { if binary_kind != BinaryDataKind::Unknown && data_size > body_size {
context.remaining_binary_bytes = data_size - body_size; // If it is a known type and the size is larger than the bodysize expect more
context.last_binary_kind = Some(binary_kind); // 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 } else if binary_kind == BinaryDataKind::Unknown
&& context.remaining_binary_bytes > 0 && remaining_binary_bytes > 0
&& context.remaining_binary_bytes >= body_size && 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 { if body_size == CHUNK_SIZE {
binary_data.continuation_of = context.last_binary_kind; // All known continuation packets are of size CHUNK_SIZE
context.remaining_binary_bytes -= body_size; binary_data.continuation_of = last_binary_kind;
context
.remaining_binary_bytes
.insert(header.msg_id, remaining_binary_bytes - body_size);
} else { } 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 // Reset but suggest that this might be an unknown magic code
trace!( trace!(
"Possibly new magic code: {:x?}", "Possibly new magic code: {:x?}",
&binary_data.body()[..std::cmp::min(body_size, 32)] &binary_data.body()[..std::cmp::min(body_size, 32)]
); );
context.remaining_binary_bytes = 0; context.remaining_binary_bytes.insert(header.msg_id, 0);
context.last_binary_kind = None; context.last_binary_kind.insert(header.msg_id, None);
} }
} else { } 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 // Reset
context.remaining_binary_bytes = 0; context.remaining_binary_bytes.insert(header.msg_id, 0);
context.last_binary_kind = None; context.last_binary_kind.insert(header.msg_id, None);
} }
binary = Some(binary_data); binary = Some(binary_data);
+8 -8
View File
@@ -1,6 +1,6 @@
use super::xml::BcXml; use super::xml::BcXml;
use log::trace; use log::trace;
use std::collections::HashSet; use std::collections::HashMap;
use std::convert::TryInto; use std::convert::TryInto;
pub(super) const MAGIC_HEADER: u32 = 0xabcdef0; pub(super) const MAGIC_HEADER: u32 = 0xabcdef0;
@@ -149,7 +149,9 @@ impl BinaryData {
BinaryDataKind::VideoDataPframe 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 BinaryDataKind::Unknown
} }
} }
@@ -212,9 +214,8 @@ pub(super) struct BcSendInfo {
#[derive(Debug)] #[derive(Debug)]
pub struct BcContext { pub struct BcContext {
pub(super) in_bin_mode: HashSet<u32>, pub(super) last_binary_kind: HashMap<u32, Option<BinaryDataKind>>,
pub(super) last_binary_kind: Option<BinaryDataKind>, pub(super) remaining_binary_bytes: HashMap<u32, usize>,
pub(super) remaining_binary_bytes: usize,
} }
impl Bc { impl Bc {
@@ -234,9 +235,8 @@ impl Bc {
impl BcContext { impl BcContext {
pub fn new() -> BcContext { pub fn new() -> BcContext {
BcContext { BcContext {
in_bin_mode: HashSet::new(), last_binary_kind: HashMap::new(),
last_binary_kind: None, remaining_binary_bytes: HashMap::new(),
remaining_binary_bytes: 0,
} }
} }
} }