/src/zenoh/commons/zenoh-codec/src/network/request.rs
Line | Count | Source |
1 | | // |
2 | | // Copyright (c) 2022 ZettaScale Technology |
3 | | // |
4 | | // This program and the accompanying materials are made available under the |
5 | | // terms of the Eclipse Public License 2.0 which is available at |
6 | | // http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
7 | | // which is available at https://www.apache.org/licenses/LICENSE-2.0. |
8 | | // |
9 | | // SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
10 | | // |
11 | | // Contributors: |
12 | | // ZettaScale Zenoh Team, <zenoh@zettascale.tech> |
13 | | // |
14 | | use zenoh_buffers::{ |
15 | | reader::{DidntRead, Reader}, |
16 | | writer::{DidntWrite, Writer}, |
17 | | }; |
18 | | use zenoh_protocol::{ |
19 | | common::{iext, imsg}, |
20 | | core::WireExpr, |
21 | | network::{ |
22 | | id, |
23 | | request::{ext, flag}, |
24 | | Mapping, Request, RequestId, |
25 | | }, |
26 | | zenoh::RequestBody, |
27 | | }; |
28 | | |
29 | | use crate::{ |
30 | | common::extension, RCodec, WCodec, Zenoh080, Zenoh080Bounded, Zenoh080Condition, Zenoh080Header, |
31 | | }; |
32 | | |
33 | | // Target |
34 | | impl<W> WCodec<(&ext::QueryTarget, bool), &mut W> for Zenoh080 |
35 | | where |
36 | | W: Writer, |
37 | | { |
38 | | type Output = Result<(), DidntWrite>; |
39 | | |
40 | 675 | fn write(self, writer: &mut W, x: (&ext::QueryTarget, bool)) -> Self::Output { |
41 | 675 | let (x, more) = x; |
42 | | |
43 | 675 | let v = match x { |
44 | 0 | ext::QueryTarget::BestMatching => 0, |
45 | 511 | ext::QueryTarget::All => 1, |
46 | 164 | ext::QueryTarget::AllComplete => 2, |
47 | | }; |
48 | 675 | let ext = ext::Target::new(v); |
49 | 675 | self.write(&mut *writer, (&ext, more)) |
50 | 675 | } <zenoh_codec::Zenoh080 as zenoh_codec::WCodec<(&zenoh_protocol::network::request::ext::QueryTarget, bool), &mut &mut alloc::vec::Vec<u8>>>::write Line | Count | Source | 40 | 675 | fn write(self, writer: &mut W, x: (&ext::QueryTarget, bool)) -> Self::Output { | 41 | 675 | let (x, more) = x; | 42 | | | 43 | 675 | let v = match x { | 44 | 0 | ext::QueryTarget::BestMatching => 0, | 45 | 511 | ext::QueryTarget::All => 1, | 46 | 164 | ext::QueryTarget::AllComplete => 2, | 47 | | }; | 48 | 675 | let ext = ext::Target::new(v); | 49 | 675 | self.write(&mut *writer, (&ext, more)) | 50 | 675 | } |
Unexecuted instantiation: <zenoh_codec::Zenoh080 as zenoh_codec::WCodec<(&zenoh_protocol::network::request::ext::QueryTarget, bool), &mut _>>::write |
51 | | } |
52 | | |
53 | | impl<R> RCodec<(ext::QueryTarget, bool), &mut R> for Zenoh080Header |
54 | | where |
55 | | R: Reader, |
56 | | { |
57 | | type Error = DidntRead; |
58 | | |
59 | 1.65k | fn read(self, reader: &mut R) -> Result<(ext::QueryTarget, bool), Self::Error> { |
60 | 1.65k | let (ext, more): (ext::Target, bool) = self.read(&mut *reader)?; |
61 | 1.65k | let rt = match ext.value { |
62 | 198 | 0 => ext::QueryTarget::BestMatching, |
63 | 1.05k | 1 => ext::QueryTarget::All, |
64 | 395 | 2 => ext::QueryTarget::AllComplete, |
65 | 1 | _ => return Err(DidntRead), |
66 | | }; |
67 | 1.64k | Ok((rt, more)) |
68 | 1.65k | } <zenoh_codec::Zenoh080Header as zenoh_codec::RCodec<(zenoh_protocol::network::request::ext::QueryTarget, bool), &mut &[u8]>>::read Line | Count | Source | 59 | 1.65k | fn read(self, reader: &mut R) -> Result<(ext::QueryTarget, bool), Self::Error> { | 60 | 1.65k | let (ext, more): (ext::Target, bool) = self.read(&mut *reader)?; | 61 | 1.65k | let rt = match ext.value { | 62 | 198 | 0 => ext::QueryTarget::BestMatching, | 63 | 1.05k | 1 => ext::QueryTarget::All, | 64 | 395 | 2 => ext::QueryTarget::AllComplete, | 65 | 1 | _ => return Err(DidntRead), | 66 | | }; | 67 | 1.64k | Ok((rt, more)) | 68 | 1.65k | } |
Unexecuted instantiation: <zenoh_codec::Zenoh080Header as zenoh_codec::RCodec<(zenoh_protocol::network::request::ext::QueryTarget, bool), &mut _>>::read |
69 | | } |
70 | | |
71 | | impl<W> WCodec<&Request, &mut W> for Zenoh080 |
72 | | where |
73 | | W: Writer, |
74 | | { |
75 | | type Output = Result<(), DidntWrite>; |
76 | | |
77 | 9.65k | fn write(self, writer: &mut W, x: &Request) -> Self::Output { |
78 | | let Request { |
79 | 9.65k | id, |
80 | 9.65k | wire_expr, |
81 | 9.65k | ext_qos, |
82 | 9.65k | ext_tstamp, |
83 | 9.65k | ext_nodeid, |
84 | 9.65k | ext_target, |
85 | 9.65k | ext_budget, |
86 | 9.65k | ext_timeout, |
87 | 9.65k | ext_ts_stack, |
88 | 9.65k | payload, |
89 | 9.65k | } = x; |
90 | | |
91 | | // Header |
92 | 9.65k | let mut header = id::REQUEST; |
93 | 9.65k | let mut n_exts = ((ext_qos != &ext::QoSType::DEFAULT) as u8) |
94 | 9.65k | + (ext_tstamp.is_some() as u8) |
95 | 9.65k | + ((ext_target != &ext::QueryTarget::DEFAULT) as u8) |
96 | 9.65k | + (ext_budget.is_some() as u8) |
97 | 9.65k | + (ext_timeout.is_some() as u8) |
98 | 9.65k | + ((ext_nodeid != &ext::NodeIdType::DEFAULT) as u8) |
99 | 9.65k | + (ext_ts_stack.is_some() as u8); |
100 | 9.65k | if n_exts != 0 { |
101 | 4.74k | header |= flag::Z; |
102 | 4.91k | } |
103 | 9.65k | if wire_expr.mapping != Mapping::DEFAULT { |
104 | 7.55k | header |= flag::M; |
105 | 7.55k | } |
106 | 9.65k | if wire_expr.has_suffix() { |
107 | 795 | header |= flag::N; |
108 | 8.86k | } |
109 | 9.65k | self.write(&mut *writer, header)?; |
110 | | |
111 | | // Body |
112 | 9.65k | self.write(&mut *writer, id)?; |
113 | 9.65k | self.write(&mut *writer, wire_expr)?; |
114 | | |
115 | | // Extensions |
116 | 9.65k | if ext_qos != &ext::QoSType::DEFAULT { |
117 | 774 | n_exts -= 1; |
118 | 774 | self.write(&mut *writer, (*ext_qos, n_exts != 0))?; |
119 | 8.88k | } |
120 | 9.65k | if let Some(ts) = ext_tstamp.as_ref() { |
121 | 535 | n_exts -= 1; |
122 | 535 | self.write(&mut *writer, (ts, n_exts != 0))?; |
123 | 9.12k | } |
124 | 9.65k | if ext_target != &ext::QueryTarget::DEFAULT { |
125 | 675 | n_exts -= 1; |
126 | 675 | self.write(&mut *writer, (ext_target, n_exts != 0))?; |
127 | 8.98k | } |
128 | 9.65k | if let Some(l) = ext_budget.as_ref() { |
129 | 743 | n_exts -= 1; |
130 | 743 | let e = ext::Budget::new(l.get() as u64); |
131 | 743 | self.write(&mut *writer, (&e, n_exts != 0))?; |
132 | 8.91k | } |
133 | 9.65k | if let Some(to) = ext_timeout.as_ref() { |
134 | 1.13k | n_exts -= 1; |
135 | 1.13k | let e = ext::Timeout::new(to.as_millis() as u64); |
136 | 1.13k | self.write(&mut *writer, (&e, n_exts != 0))?; |
137 | 8.51k | } |
138 | 9.65k | if ext_nodeid != &ext::NodeIdType::DEFAULT { |
139 | 1.26k | n_exts -= 1; |
140 | 1.26k | self.write(&mut *writer, (*ext_nodeid, n_exts != 0))?; |
141 | 8.39k | } |
142 | 9.65k | if let Some(ts_stack) = ext_ts_stack.as_ref() { |
143 | 1.65k | n_exts -= 1; |
144 | 1.65k | self.write(&mut *writer, (ts_stack, n_exts != 0))?; |
145 | 8.00k | } |
146 | | |
147 | | // Payload |
148 | 9.65k | self.write(&mut *writer, payload)?; |
149 | | |
150 | 9.65k | Ok(()) |
151 | 9.65k | } <zenoh_codec::Zenoh080 as zenoh_codec::WCodec<&zenoh_protocol::network::request::Request, &mut &mut alloc::vec::Vec<u8>>>::write Line | Count | Source | 77 | 9.65k | fn write(self, writer: &mut W, x: &Request) -> Self::Output { | 78 | | let Request { | 79 | 9.65k | id, | 80 | 9.65k | wire_expr, | 81 | 9.65k | ext_qos, | 82 | 9.65k | ext_tstamp, | 83 | 9.65k | ext_nodeid, | 84 | 9.65k | ext_target, | 85 | 9.65k | ext_budget, | 86 | 9.65k | ext_timeout, | 87 | 9.65k | ext_ts_stack, | 88 | 9.65k | payload, | 89 | 9.65k | } = x; | 90 | | | 91 | | // Header | 92 | 9.65k | let mut header = id::REQUEST; | 93 | 9.65k | let mut n_exts = ((ext_qos != &ext::QoSType::DEFAULT) as u8) | 94 | 9.65k | + (ext_tstamp.is_some() as u8) | 95 | 9.65k | + ((ext_target != &ext::QueryTarget::DEFAULT) as u8) | 96 | 9.65k | + (ext_budget.is_some() as u8) | 97 | 9.65k | + (ext_timeout.is_some() as u8) | 98 | 9.65k | + ((ext_nodeid != &ext::NodeIdType::DEFAULT) as u8) | 99 | 9.65k | + (ext_ts_stack.is_some() as u8); | 100 | 9.65k | if n_exts != 0 { | 101 | 4.74k | header |= flag::Z; | 102 | 4.91k | } | 103 | 9.65k | if wire_expr.mapping != Mapping::DEFAULT { | 104 | 7.55k | header |= flag::M; | 105 | 7.55k | } | 106 | 9.65k | if wire_expr.has_suffix() { | 107 | 795 | header |= flag::N; | 108 | 8.86k | } | 109 | 9.65k | self.write(&mut *writer, header)?; | 110 | | | 111 | | // Body | 112 | 9.65k | self.write(&mut *writer, id)?; | 113 | 9.65k | self.write(&mut *writer, wire_expr)?; | 114 | | | 115 | | // Extensions | 116 | 9.65k | if ext_qos != &ext::QoSType::DEFAULT { | 117 | 774 | n_exts -= 1; | 118 | 774 | self.write(&mut *writer, (*ext_qos, n_exts != 0))?; | 119 | 8.88k | } | 120 | 9.65k | if let Some(ts) = ext_tstamp.as_ref() { | 121 | 535 | n_exts -= 1; | 122 | 535 | self.write(&mut *writer, (ts, n_exts != 0))?; | 123 | 9.12k | } | 124 | 9.65k | if ext_target != &ext::QueryTarget::DEFAULT { | 125 | 675 | n_exts -= 1; | 126 | 675 | self.write(&mut *writer, (ext_target, n_exts != 0))?; | 127 | 8.98k | } | 128 | 9.65k | if let Some(l) = ext_budget.as_ref() { | 129 | 743 | n_exts -= 1; | 130 | 743 | let e = ext::Budget::new(l.get() as u64); | 131 | 743 | self.write(&mut *writer, (&e, n_exts != 0))?; | 132 | 8.91k | } | 133 | 9.65k | if let Some(to) = ext_timeout.as_ref() { | 134 | 1.13k | n_exts -= 1; | 135 | 1.13k | let e = ext::Timeout::new(to.as_millis() as u64); | 136 | 1.13k | self.write(&mut *writer, (&e, n_exts != 0))?; | 137 | 8.51k | } | 138 | 9.65k | if ext_nodeid != &ext::NodeIdType::DEFAULT { | 139 | 1.26k | n_exts -= 1; | 140 | 1.26k | self.write(&mut *writer, (*ext_nodeid, n_exts != 0))?; | 141 | 8.39k | } | 142 | 9.65k | if let Some(ts_stack) = ext_ts_stack.as_ref() { | 143 | 1.65k | n_exts -= 1; | 144 | 1.65k | self.write(&mut *writer, (ts_stack, n_exts != 0))?; | 145 | 8.00k | } | 146 | | | 147 | | // Payload | 148 | 9.65k | self.write(&mut *writer, payload)?; | 149 | | | 150 | 9.65k | Ok(()) | 151 | 9.65k | } |
Unexecuted instantiation: <zenoh_codec::Zenoh080 as zenoh_codec::WCodec<&zenoh_protocol::network::request::Request, &mut _>>::write |
152 | | } |
153 | | |
154 | | impl<R> RCodec<Request, &mut R> for Zenoh080 |
155 | | where |
156 | | R: Reader, |
157 | | { |
158 | | type Error = DidntRead; |
159 | | |
160 | 0 | fn read(self, reader: &mut R) -> Result<Request, Self::Error> { |
161 | 0 | let header: u8 = self.read(&mut *reader)?; |
162 | 0 | let codec = Zenoh080Header::new(header); |
163 | 0 | codec.read(reader) |
164 | 0 | } |
165 | | } |
166 | | |
167 | | impl<R> RCodec<Request, &mut R> for Zenoh080Header |
168 | | where |
169 | | R: Reader, |
170 | | { |
171 | | type Error = DidntRead; |
172 | | |
173 | 20.0k | fn read(self, reader: &mut R) -> Result<Request, Self::Error> { |
174 | 20.0k | if imsg::mid(self.header) != id::REQUEST { |
175 | 0 | return Err(DidntRead); |
176 | 20.0k | } |
177 | | |
178 | | // Body |
179 | 20.0k | let bodec = Zenoh080Bounded::<RequestId>::new(); |
180 | 20.0k | let id: RequestId = bodec.read(&mut *reader)?; |
181 | 20.0k | let ccond = Zenoh080Condition::new(imsg::has_flag(self.header, flag::N)); |
182 | 20.0k | let mut wire_expr: WireExpr<'static> = ccond.read(&mut *reader)?; |
183 | 19.8k | wire_expr.mapping = if imsg::has_flag(self.header, flag::M) { |
184 | 15.5k | Mapping::Sender |
185 | | } else { |
186 | 4.31k | Mapping::Receiver |
187 | | }; |
188 | | |
189 | | // Extensions |
190 | 19.8k | let mut ext_qos = ext::QoSType::DEFAULT; |
191 | 19.8k | let mut ext_tstamp = None; |
192 | 19.8k | let mut ext_nodeid = ext::NodeIdType::DEFAULT; |
193 | 19.8k | let mut ext_target = ext::QueryTarget::DEFAULT; |
194 | 19.8k | let mut ext_limit = None; |
195 | 19.8k | let mut ext_timeout = None; |
196 | 19.8k | let mut ext_ts_stack = None; |
197 | | |
198 | 19.8k | let mut has_ext = imsg::has_flag(self.header, flag::Z); |
199 | 38.3k | while has_ext { |
200 | 18.7k | let ext: u8 = self.codec.read(&mut *reader)?; |
201 | 18.6k | let eodec = Zenoh080Header::new(ext); |
202 | 18.6k | match iext::eid(ext) { |
203 | 1.81k | ext::QoS::ID => { |
204 | 1.81k | let (q, ext): (ext::QoSType, bool) = eodec.read(&mut *reader)?; |
205 | 1.80k | ext_qos = q; |
206 | 1.80k | has_ext = ext; |
207 | | } |
208 | 1.22k | ext::Timestamp::ID => { |
209 | 1.22k | let (t, ext): (ext::TimestampType, bool) = eodec.read(&mut *reader)?; |
210 | 1.12k | ext_tstamp = Some(t); |
211 | 1.12k | has_ext = ext; |
212 | | } |
213 | 2.73k | ext::NodeId::ID => { |
214 | 2.73k | let (nid, ext): (ext::NodeIdType, bool) = eodec.read(&mut *reader)?; |
215 | 2.73k | ext_nodeid = nid; |
216 | 2.73k | has_ext = ext; |
217 | | } |
218 | 1.65k | ext::Target::ID => { |
219 | 1.65k | let (rt, ext): (ext::QueryTarget, bool) = eodec.read(&mut *reader)?; |
220 | 1.64k | ext_target = rt; |
221 | 1.64k | has_ext = ext; |
222 | | } |
223 | 1.67k | ext::Budget::ID => { |
224 | 1.67k | let (l, ext): (ext::Budget, bool) = eodec.read(&mut *reader)?; |
225 | 1.67k | ext_limit = ext::BudgetType::new(l.value as u32); |
226 | 1.67k | has_ext = ext; |
227 | | } |
228 | 2.48k | ext::Timeout::ID => { |
229 | 2.48k | let (to, ext): (ext::Timeout, bool) = eodec.read(&mut *reader)?; |
230 | 2.48k | ext_timeout = Some(ext::TimeoutType::from_millis(to.value)); |
231 | 2.48k | has_ext = ext; |
232 | | } |
233 | 4.19k | ext::TsStack::ID => { |
234 | 4.19k | let (ts, ext): (ext::TsStackType, bool) = eodec.read(&mut *reader)?; |
235 | 4.17k | ext_ts_stack = Some(ts); |
236 | 4.17k | has_ext = ext; |
237 | | } |
238 | | _ => { |
239 | 2.85k | has_ext = extension::skip(reader, "Request", ext)?; |
240 | | } |
241 | | } |
242 | | } |
243 | | |
244 | | // Payload |
245 | 19.6k | let payload: RequestBody = self.codec.read(&mut *reader)?; |
246 | | |
247 | 19.1k | Ok(Request { |
248 | 19.1k | id, |
249 | 19.1k | wire_expr, |
250 | 19.1k | payload, |
251 | 19.1k | ext_qos, |
252 | 19.1k | ext_tstamp, |
253 | 19.1k | ext_nodeid, |
254 | 19.1k | ext_target, |
255 | 19.1k | ext_budget: ext_limit, |
256 | 19.1k | ext_timeout, |
257 | 19.1k | ext_ts_stack, |
258 | 19.1k | }) |
259 | 20.0k | } <zenoh_codec::Zenoh080Header as zenoh_codec::RCodec<zenoh_protocol::network::request::Request, &mut &[u8]>>::read Line | Count | Source | 173 | 20.0k | fn read(self, reader: &mut R) -> Result<Request, Self::Error> { | 174 | 20.0k | if imsg::mid(self.header) != id::REQUEST { | 175 | 0 | return Err(DidntRead); | 176 | 20.0k | } | 177 | | | 178 | | // Body | 179 | 20.0k | let bodec = Zenoh080Bounded::<RequestId>::new(); | 180 | 20.0k | let id: RequestId = bodec.read(&mut *reader)?; | 181 | 20.0k | let ccond = Zenoh080Condition::new(imsg::has_flag(self.header, flag::N)); | 182 | 20.0k | let mut wire_expr: WireExpr<'static> = ccond.read(&mut *reader)?; | 183 | 19.8k | wire_expr.mapping = if imsg::has_flag(self.header, flag::M) { | 184 | 15.5k | Mapping::Sender | 185 | | } else { | 186 | 4.31k | Mapping::Receiver | 187 | | }; | 188 | | | 189 | | // Extensions | 190 | 19.8k | let mut ext_qos = ext::QoSType::DEFAULT; | 191 | 19.8k | let mut ext_tstamp = None; | 192 | 19.8k | let mut ext_nodeid = ext::NodeIdType::DEFAULT; | 193 | 19.8k | let mut ext_target = ext::QueryTarget::DEFAULT; | 194 | 19.8k | let mut ext_limit = None; | 195 | 19.8k | let mut ext_timeout = None; | 196 | 19.8k | let mut ext_ts_stack = None; | 197 | | | 198 | 19.8k | let mut has_ext = imsg::has_flag(self.header, flag::Z); | 199 | 38.3k | while has_ext { | 200 | 18.7k | let ext: u8 = self.codec.read(&mut *reader)?; | 201 | 18.6k | let eodec = Zenoh080Header::new(ext); | 202 | 18.6k | match iext::eid(ext) { | 203 | 1.81k | ext::QoS::ID => { | 204 | 1.81k | let (q, ext): (ext::QoSType, bool) = eodec.read(&mut *reader)?; | 205 | 1.80k | ext_qos = q; | 206 | 1.80k | has_ext = ext; | 207 | | } | 208 | 1.22k | ext::Timestamp::ID => { | 209 | 1.22k | let (t, ext): (ext::TimestampType, bool) = eodec.read(&mut *reader)?; | 210 | 1.12k | ext_tstamp = Some(t); | 211 | 1.12k | has_ext = ext; | 212 | | } | 213 | 2.73k | ext::NodeId::ID => { | 214 | 2.73k | let (nid, ext): (ext::NodeIdType, bool) = eodec.read(&mut *reader)?; | 215 | 2.73k | ext_nodeid = nid; | 216 | 2.73k | has_ext = ext; | 217 | | } | 218 | 1.65k | ext::Target::ID => { | 219 | 1.65k | let (rt, ext): (ext::QueryTarget, bool) = eodec.read(&mut *reader)?; | 220 | 1.64k | ext_target = rt; | 221 | 1.64k | has_ext = ext; | 222 | | } | 223 | 1.67k | ext::Budget::ID => { | 224 | 1.67k | let (l, ext): (ext::Budget, bool) = eodec.read(&mut *reader)?; | 225 | 1.67k | ext_limit = ext::BudgetType::new(l.value as u32); | 226 | 1.67k | has_ext = ext; | 227 | | } | 228 | 2.48k | ext::Timeout::ID => { | 229 | 2.48k | let (to, ext): (ext::Timeout, bool) = eodec.read(&mut *reader)?; | 230 | 2.48k | ext_timeout = Some(ext::TimeoutType::from_millis(to.value)); | 231 | 2.48k | has_ext = ext; | 232 | | } | 233 | 4.19k | ext::TsStack::ID => { | 234 | 4.19k | let (ts, ext): (ext::TsStackType, bool) = eodec.read(&mut *reader)?; | 235 | 4.17k | ext_ts_stack = Some(ts); | 236 | 4.17k | has_ext = ext; | 237 | | } | 238 | | _ => { | 239 | 2.85k | has_ext = extension::skip(reader, "Request", ext)?; | 240 | | } | 241 | | } | 242 | | } | 243 | | | 244 | | // Payload | 245 | 19.6k | let payload: RequestBody = self.codec.read(&mut *reader)?; | 246 | | | 247 | 19.1k | Ok(Request { | 248 | 19.1k | id, | 249 | 19.1k | wire_expr, | 250 | 19.1k | payload, | 251 | 19.1k | ext_qos, | 252 | 19.1k | ext_tstamp, | 253 | 19.1k | ext_nodeid, | 254 | 19.1k | ext_target, | 255 | 19.1k | ext_budget: ext_limit, | 256 | 19.1k | ext_timeout, | 257 | 19.1k | ext_ts_stack, | 258 | 19.1k | }) | 259 | 20.0k | } |
Unexecuted instantiation: <zenoh_codec::Zenoh080Header as zenoh_codec::RCodec<zenoh_protocol::network::request::Request, &mut _>>::read |
260 | | } |