From fb8b4c069547eef95aacca3fc9382b8e9ec745e1 Mon Sep 17 00:00:00 2001 From: Luca Cominardi Date: Mon, 26 Feb 2024 09:47:12 +0100 Subject: [PATCH] Encoding suffix bitflag --- Cargo.lock | 4 +- commons/zenoh-codec/src/core/encoding.rs | 44 ++++++++++++++++----- commons/zenoh-protocol/src/core/encoding.rs | 14 +++++++ commons/zenoh-protocol/src/core/mod.rs | 2 +- commons/zenoh-protocol/src/zenoh/mod.rs | 4 +- 5 files changed, 54 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7ceb5dbc39..ab79dfcd15 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2221,9 +2221,9 @@ dependencies = [ [[package]] name = "num-traits" -version = "0.2.16" +version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2" +checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a" dependencies = [ "autocfg", "libm", diff --git a/commons/zenoh-codec/src/core/encoding.rs b/commons/zenoh-codec/src/core/encoding.rs index 4b2e2248c9..b14710f48f 100644 --- a/commons/zenoh-codec/src/core/encoding.rs +++ b/commons/zenoh-codec/src/core/encoding.rs @@ -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 } } @@ -32,10 +40,18 @@ where type Output = Result<(), DidntWrite>; fn write(self, writer: &mut W, x: &Encoding) -> Self::Output { - let zodec = Zenoh080Bounded::::new(); - zodec.write(&mut *writer, x.prefix())?; - let zodec = Zenoh080Bounded::::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::::new(); + zodec.write(&mut *writer, prefix)?; + if !suffix.is_empty() { + let zodec = Zenoh080Bounded::::new(); + zodec.write(&mut *writer, suffix)?; + } Ok(()) } } @@ -47,10 +63,18 @@ where type Error = DidntRead; fn read(self, reader: &mut R) -> Result { - let zodec = Zenoh080Bounded::::new(); - let prefix: EncodingPrefix = zodec.read(&mut *reader)?; - let zodec = Zenoh080Bounded::::new(); - let suffix: String = zodec.read(&mut *reader)?; + let zodec = Zenoh080Bounded::::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::::new(); + suffix = zodec.read(&mut *reader)?; + } let mut encoding: Encoding = Encoding::new(prefix); if !suffix.is_empty() { diff --git a/commons/zenoh-protocol/src/core/encoding.rs b/commons/zenoh-protocol/src/core/encoding.rs index c49b315b21..620d6e2b66 100644 --- a/commons/zenoh-protocol/src/core/encoding.rs +++ b/commons/zenoh-protocol/src/core/encoding.rs @@ -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: ~ -- 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 { diff --git a/commons/zenoh-protocol/src/core/mod.rs b/commons/zenoh-protocol/src/core/mod.rs index 55e57d5d81..c2218a7f84 100644 --- a/commons/zenoh-protocol/src/core/mod.rs +++ b/commons/zenoh-protocol/src/core/mod.rs @@ -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; diff --git a/commons/zenoh-protocol/src/zenoh/mod.rs b/commons/zenoh-protocol/src/zenoh/mod.rs index e67576e673..686e9f8ec5 100644 --- a/commons/zenoh-protocol/src/zenoh/mod.rs +++ b/commons/zenoh-protocol/src/zenoh/mod.rs @@ -220,12 +220,14 @@ pub mod ext { } } + /// ```text /// 7 6 5 4 3 2 1 0 /// +-+-+-+-+-+-+-+-+ /// ~ encoding ~ /// +---------------+ - /// ~ pl: [u8;z32] ~ -- Payload + /// ~ pl: ~ -- Payload /// +---------------+ + /// ``` #[derive(Debug, Clone, PartialEq, Eq)] pub struct ValueType { #[cfg(feature = "shared-memory")]