zenoh_codec/transport/
keepalive.rs1use zenoh_buffers::{
15 reader::{DidntRead, Reader},
16 writer::{DidntWrite, Writer},
17};
18use zenoh_protocol::{
19 common::imsg,
20 transport::{
21 id,
22 keepalive::{flag, KeepAlive},
23 },
24};
25
26use crate::{common::extension, RCodec, WCodec, Zenoh080, Zenoh080Header};
27
28impl<W> WCodec<&KeepAlive, &mut W> for Zenoh080
29where
30 W: Writer,
31{
32 type Output = Result<(), DidntWrite>;
33
34 fn write(self, writer: &mut W, x: &KeepAlive) -> Self::Output {
35 let KeepAlive = x;
36
37 let header = id::KEEP_ALIVE;
39 self.write(&mut *writer, header)?;
40 Ok(())
41 }
42}
43
44impl<R> RCodec<KeepAlive, &mut R> for Zenoh080
45where
46 R: Reader,
47{
48 type Error = DidntRead;
49
50 fn read(self, reader: &mut R) -> Result<KeepAlive, Self::Error> {
51 let header: u8 = self.read(&mut *reader)?;
52 let codec = Zenoh080Header::new(header);
53 codec.read(reader)
54 }
55}
56
57impl<R> RCodec<KeepAlive, &mut R> for Zenoh080Header
58where
59 R: Reader,
60{
61 type Error = DidntRead;
62
63 fn read(self, reader: &mut R) -> Result<KeepAlive, Self::Error> {
64 if imsg::mid(self.header) != id::KEEP_ALIVE {
65 return Err(DidntRead);
66 }
67
68 let has_ext = imsg::has_flag(self.header, flag::Z);
70 if has_ext {
71 extension::skip_all(reader, "Unknown KeepAlive ext")?;
72 }
73
74 Ok(KeepAlive)
75 }
76}