Skip to content

Commit

Permalink
packing Key Request Header, and Buffer into single message, remove ha…
Browse files Browse the repository at this point in the history
…shmap
  • Loading branch information
Charles-Schleich committed Oct 1, 2024
1 parent 9ebe16a commit 1782a73
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions zenoh-plugin-ros2dds/src/ros2_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
//

use std::{
collections::HashMap,
env::VarError,
sync::atomic::{AtomicU32, Ordering},
};
Expand Down Expand Up @@ -220,20 +219,14 @@ impl CddsRequestHeader {
}

pub fn as_attachment(&self) -> ZBytes {
let mut hashmap = HashMap::new();

// concat header + endianness flag
let mut buf = [0u8; 17];
buf[0..16].copy_from_slice(&self.header);
buf[16] = self.is_little_endian as u8;

hashmap.insert(ATTACHMENT_KEY_REQUEST_HEADER, buf);

let mut writer = ZBytes::writer();
for (k, v) in hashmap.iter() {
writer.append(ZBytes::from(k));
writer.append(ZBytes::from(v));
}
writer.append(ZBytes::from(ATTACHMENT_KEY_REQUEST_HEADER));
writer.append(ZBytes::from(buf));
writer.finish()
}
}
Expand All @@ -242,23 +235,34 @@ impl TryFrom<&ZBytes> for CddsRequestHeader {
type Error = ZError;

fn try_from(value: &ZBytes) -> Result<Self, Self::Error> {
let hashmap: HashMap<[u8; 3], [u8; 17]> = zenoh_ext::z_deserialize(value).unwrap();

match hashmap.get(&ATTACHMENT_KEY_REQUEST_HEADER) {
Some(buf) => {
if buf.len() == 17 {
let header: [u8; 16] = buf[0..16]
.try_into()
.expect("Shouldn't happen: buf is 17 bytes");
Ok(CddsRequestHeader {
header,
is_little_endian: buf[16] != 0,
})
} else {
bail!("Attachment 'header' is not 16 bytes: {buf:02x?}")
}
let bytes = value.to_bytes();

let header = match bytes.get(0..ATTACHMENT_KEY_REQUEST_HEADER.len()) {
Some(header) => header,
None => bail!("No 'key request header' bytes found in attachment"),
};

if header != ATTACHMENT_KEY_REQUEST_HEADER {
bail!(
"Initial {:?} bytes do not match ATTACHMENT_KEY_REQUEST_HEADER",
ATTACHMENT_KEY_REQUEST_HEADER.len()
)
}

if let Some(buf) = bytes.get(ATTACHMENT_KEY_REQUEST_HEADER.len()..) {
if buf.len() == 17 {
let header: [u8; 16] = buf[0..16]
.try_into()
.expect("Shouldn't happen: buf is 17 bytes");
Ok(CddsRequestHeader {
header,
is_little_endian: buf[16] != 0,
})
} else {
bail!("Attachment 'header' is not 16 bytes: {buf:02x?}")
}
None => bail!("No 'header' key found in Attachment"),
} else {
bail!("Could not Read Remaining Attachment Buffer")
}
}
}
Expand Down

0 comments on commit 1782a73

Please sign in to comment.