Hashmaps in the contiuation model
This commit is contained in:
2 files changed
+45
-21
No files matched your search
+37
-13
@@ -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);
|
||||
|
||||
+8
-8
@@ -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<u32>,
|
||||
pub(super) last_binary_kind: Option<BinaryDataKind>,
|
||||
pub(super) remaining_binary_bytes: usize,
|
||||
pub(super) last_binary_kind: HashMap<u32, Option<BinaryDataKind>>,
|
||||
pub(super) remaining_binary_bytes: HashMap<u32, usize>,
|
||||
}
|
||||
|
||||
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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user