zenoh_protocol/network/
request.rs1use core::sync::atomic::AtomicU32;
15
16use zenoh_buffers::buffer::Buffer;
17
18use crate::{core::WireExpr, zenoh::RequestBody};
19
20pub type RequestId = u32;
22pub type AtomicRequestId = AtomicU32;
23
24pub mod flag {
25 pub const N: u8 = 1 << 5; pub const M: u8 = 1 << 6; pub const Z: u8 = 1 << 7; }
29
30#[derive(Debug, Clone, PartialEq, Eq)]
57pub struct Request {
58 pub id: RequestId,
59 pub wire_expr: WireExpr<'static>,
60 pub ext_qos: ext::QoSType,
61 pub ext_tstamp: Option<ext::TimestampType>,
62 pub ext_nodeid: ext::NodeIdType,
63 pub ext_target: ext::QueryTarget,
64 pub ext_budget: Option<ext::BudgetType>,
65 pub ext_timeout: Option<ext::TimeoutType>,
66 pub ext_ts_stack: Option<ext::TsStackType>,
67 pub payload: RequestBody,
68}
69
70pub mod ext {
71 use core::{num::NonZeroU32, time::Duration};
72
73 use serde::Deserialize;
74
75 use crate::{zextz64, zextzbuf};
76
77 pub type QoS = zextz64!(0x1, false);
78 pub type QoSType = crate::network::ext::QoSType<{ QoS::ID }>;
79
80 pub type Timestamp = zextzbuf!(0x2, false);
81 pub type TimestampType = crate::network::ext::TimestampType<{ Timestamp::ID }>;
82
83 pub type NodeId = zextz64!(0x3, true);
84 pub type NodeIdType = crate::network::ext::NodeIdType<{ NodeId::ID }>;
85
86 pub type Target = zextz64!(0x4, true);
87 #[repr(u8)]
96 #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Deserialize)]
97 pub enum QueryTarget {
98 #[default]
100 BestMatching,
101 All,
103 AllComplete,
105 }
106
107 impl QueryTarget {
108 pub const DEFAULT: Self = Self::BestMatching;
109
110 #[cfg(feature = "test")]
111 #[doc(hidden)]
112 pub fn rand() -> Self {
113 use rand::prelude::*;
114 let mut rng = rand::thread_rng();
115
116 *[
117 QueryTarget::All,
118 QueryTarget::AllComplete,
119 QueryTarget::BestMatching,
120 ]
121 .choose(&mut rng)
122 .unwrap()
123 }
124 }
125
126 pub type Budget = zextz64!(0x5, false);
128 pub type BudgetType = NonZeroU32;
129
130 pub type Timeout = zextz64!(0x6, false);
132 pub type TimeoutType = Duration;
133
134 pub type TsStack = zextzbuf!(0x7, false);
135 pub type TsStackType = crate::network::timestamp_stack::TsStackType<{ TsStack::ID }>;
136}
137
138impl Request {
139 pub fn payload_size(&self) -> usize {
140 match &self.payload {
141 RequestBody::Query(q) => {
142 q.ext_body.as_ref().map_or(0, |b| b.payload.len())
143 + q.ext_attachment.as_ref().map_or(0, |a| a.buffer.len())
144 }
145 }
146 }
147
148 #[cfg(feature = "test")]
149 #[doc(hidden)]
150 pub fn rand() -> Self {
151 use core::num::NonZeroU32;
152
153 use rand::Rng;
154
155 let mut rng = rand::thread_rng();
156 let wire_expr = WireExpr::rand();
157 let id: RequestId = rng.gen();
158 let payload = RequestBody::rand();
159 let ext_qos = ext::QoSType::rand();
160 let ext_tstamp = rng.gen_bool(0.5).then(ext::TimestampType::rand);
161 let ext_nodeid = ext::NodeIdType::rand();
162 let ext_target = ext::QueryTarget::rand();
163 let ext_budget = if rng.gen_bool(0.5) {
164 NonZeroU32::new(rng.gen())
165 } else {
166 None
167 };
168 let ext_timeout = if rng.gen_bool(0.5) {
169 Some(ext::TimeoutType::from_millis(rng.gen()))
170 } else {
171 None
172 };
173 let ext_ts_stack = rng.gen_bool(0.5).then(ext::TsStackType::rand);
174
175 Self {
176 wire_expr,
177 id,
178 payload,
179 ext_qos,
180 ext_tstamp,
181 ext_nodeid,
182 ext_target,
183 ext_budget,
184 ext_timeout,
185 ext_ts_stack,
186 }
187 }
188}