Skip to content

Commit

Permalink
Encoding suffix bitflag
Browse files Browse the repository at this point in the history
  • Loading branch information
Mallets committed Feb 26, 2024
1 parent 1ef582f commit fb8b4c0
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 34 additions & 10 deletions commons/zenoh-codec/src/core/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@ use zenoh_buffers::{
reader::{DidntRead, Reader},
writer::{DidntWrite, Writer},
};
use zenoh_protocol::core::{Encoding, EncodingPrefix};
use zenoh_protocol::{
common::imsg,
core::encoding::{flag, Encoding, EncodingPrefix},
};

impl LCodec<&Encoding> for Zenoh080 {
fn w_len(self, x: &Encoding) -> usize {
self.w_len(x.prefix()) + self.w_len(x.suffix())
let (prefix, suffix) = (x.prefix(), x.suffix());
let mut len = self.w_len((prefix as u32) << 1);
if !suffix.is_empty() {
len += self.w_len(x.suffix());
}
len
}
}

Expand All @@ -32,10 +40,18 @@ where
type Output = Result<(), DidntWrite>;

fn write(self, writer: &mut W, x: &Encoding) -> Self::Output {
let zodec = Zenoh080Bounded::<EncodingPrefix>::new();
zodec.write(&mut *writer, x.prefix())?;
let zodec = Zenoh080Bounded::<u8>::new();
zodec.write(&mut *writer, x.suffix())?;
let mut prefix = (x.prefix() as u32) << 1;
let suffix = x.suffix();

if !suffix.is_empty() {
prefix |= flag::S;
}
let zodec = Zenoh080Bounded::<u32>::new();
zodec.write(&mut *writer, prefix)?;
if !suffix.is_empty() {
let zodec = Zenoh080Bounded::<u8>::new();
zodec.write(&mut *writer, suffix)?;
}
Ok(())
}
}
Expand All @@ -47,10 +63,18 @@ where
type Error = DidntRead;

fn read(self, reader: &mut R) -> Result<Encoding, Self::Error> {
let zodec = Zenoh080Bounded::<EncodingPrefix>::new();
let prefix: EncodingPrefix = zodec.read(&mut *reader)?;
let zodec = Zenoh080Bounded::<u8>::new();
let suffix: String = zodec.read(&mut *reader)?;
let zodec = Zenoh080Bounded::<u32>::new();
let prefix: u32 = zodec.read(&mut *reader)?;
let (prefix, has_suffix) = (
(prefix >> 1) as EncodingPrefix,
imsg::has_flag(prefix as u8, flag::S as u8),
);

let mut suffix = String::new();
if has_suffix {
let zodec = Zenoh080Bounded::<u8>::new();
suffix = zodec.read(&mut *reader)?;
}

let mut encoding: Encoding = Encoding::new(prefix);
if !suffix.is_empty() {
Expand Down
14 changes: 14 additions & 0 deletions commons/zenoh-protocol/src/core/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ pub struct Encoding {
suffix: CowStr<'static>,
}

/// # Encoding field
///
/// ```text
/// 7 6 5 4 3 2 1 0
/// +-+-+-+-+-+-+-+-+
/// ~ prefix: z16 |S~
/// +---------------+
/// ~suffix: <u8;z8>~ -- if S==1
/// +---------------+
/// ```
pub mod flag {
pub const S: u32 = 1; // 0x01 Suffix if S==1 then suffix is present
}

impl Encoding {
/// Returns a new [`Encoding`] object provided the prefix ID.
pub const fn new(prefix: EncodingPrefix) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion commons/zenoh-protocol/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub use wire_expr::*;

mod cowstr;
pub use cowstr::CowStr;
mod encoding;
pub mod encoding;
pub use encoding::{Encoding, EncodingPrefix};

pub mod locator;
Expand Down
4 changes: 3 additions & 1 deletion commons/zenoh-protocol/src/zenoh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,14 @@ pub mod ext {
}
}

/// ```text
/// 7 6 5 4 3 2 1 0
/// +-+-+-+-+-+-+-+-+
/// ~ encoding ~
/// +---------------+
/// ~ pl: [u8;z32] ~ -- Payload
/// ~ pl: <u8;z32> ~ -- Payload
/// +---------------+
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ValueType<const VID: u8, const SID: u8> {
#[cfg(feature = "shared-memory")]
Expand Down

0 comments on commit fb8b4c0

Please sign in to comment.