1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// Copyright (c) 2023 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
//
use crate::{RCodec, WCodec, Zenoh060};
use zenoh_buffers::{
    reader::{DidntRead, Reader},
    writer::{DidntWrite, Writer},
    SplitBuffer, ZBuf,
};
#[cfg(feature = "shared-memory")]
use {
    crate::Zenoh060Condition, core::any::TypeId, zenoh_buffers::ZSlice,
    zenoh_shm::SharedMemoryBufInfoSerialized,
};

// ZBuf flat
impl<W> WCodec<&ZBuf, &mut W> for Zenoh060
where
    W: Writer,
{
    type Output = Result<(), DidntWrite>;

    fn write(self, writer: &mut W, x: &ZBuf) -> Self::Output {
        self.write(&mut *writer, x.len())?;
        for s in x.zslices() {
            writer.write_zslice(s)?;
        }
        Ok(())
    }
}

impl<R> RCodec<ZBuf, &mut R> for Zenoh060
where
    R: Reader,
{
    type Error = DidntRead;

    fn read(self, reader: &mut R) -> Result<ZBuf, Self::Error> {
        let len: usize = self.read(&mut *reader)?;
        let mut zbuf = ZBuf::default();
        reader.read_zslices(len, |s| zbuf.push_zslice(s))?;
        Ok(zbuf)
    }
}

// ZBuf sliced
#[cfg(feature = "shared-memory")]
#[derive(Default)]
struct Zenoh060Sliced {
    codec: Zenoh060,
}

#[cfg(feature = "shared-memory")]
impl<W> WCodec<&ZBuf, &mut W> for Zenoh060Sliced
where
    W: Writer,
{
    type Output = Result<(), DidntWrite>;

    fn write(self, writer: &mut W, x: &ZBuf) -> Self::Output {
        self.codec.write(&mut *writer, x.zslices().count())?;

        for zs in x.zslices() {
            if zs.buf.as_any().type_id() == TypeId::of::<SharedMemoryBufInfoSerialized>() {
                self.codec
                    .write(&mut *writer, super::zslice::kind::SHM_INFO)?;
            } else {
                self.codec.write(&mut *writer, super::zslice::kind::RAW)?;
            }

            self.codec.write(&mut *writer, zs)?;
        }

        Ok(())
    }
}

#[cfg(feature = "shared-memory")]
impl<R> RCodec<ZBuf, &mut R> for Zenoh060Sliced
where
    R: Reader,
{
    type Error = DidntRead;

    fn read(self, reader: &mut R) -> Result<ZBuf, Self::Error> {
        let num: usize = self.codec.read(&mut *reader)?;
        let mut zbuf = ZBuf::default();
        for _ in 0..num {
            let kind: u8 = self.codec.read(&mut *reader)?;
            match kind {
                super::zslice::kind::RAW => {
                    let len: usize = self.codec.read(&mut *reader)?;
                    reader.read_zslices(len, |s| zbuf.push_zslice(s))?;
                }
                super::zslice::kind::SHM_INFO => {
                    let bytes: Vec<u8> = self.codec.read(&mut *reader)?;
                    let shm_info: SharedMemoryBufInfoSerialized = bytes.into();
                    let zslice: ZSlice = shm_info.into();
                    zbuf.push_zslice(zslice);
                }
                _ => return Err(DidntRead),
            }
        }
        Ok(zbuf)
    }
}

#[cfg(feature = "shared-memory")]
impl<W> WCodec<&ZBuf, &mut W> for Zenoh060Condition
where
    W: Writer,
{
    type Output = Result<(), DidntWrite>;

    fn write(self, writer: &mut W, x: &ZBuf) -> Self::Output {
        let is_sliced = self.condition;

        if is_sliced {
            let codec = Zenoh060Sliced::default();
            codec.write(&mut *writer, x)
        } else {
            self.codec.write(&mut *writer, x)
        }
    }
}

#[cfg(feature = "shared-memory")]
impl<R> RCodec<ZBuf, &mut R> for Zenoh060Condition
where
    R: Reader,
{
    type Error = DidntRead;

    fn read(self, reader: &mut R) -> Result<ZBuf, Self::Error> {
        let is_sliced = self.condition;

        if is_sliced {
            let codec = Zenoh060Sliced::default();
            codec.read(&mut *reader)
        } else {
            self.codec.read(&mut *reader)
        }
    }
}