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 payload: RequestBody,
67}
68
69pub mod ext {
70 use core::{num::NonZeroU32, time::Duration};
71
72 use serde::Deserialize;
73
74 use crate::{zextz64, zextzbuf};
75
76 pub type QoS = zextz64!(0x1, false);
77 pub type QoSType = crate::network::ext::QoSType<{ QoS::ID }>;
78
79 pub type Timestamp = zextzbuf!(0x2, false);
80 pub type TimestampType = crate::network::ext::TimestampType<{ Timestamp::ID }>;
81
82 pub type NodeId = zextz64!(0x3, true);
83 pub type NodeIdType = crate::network::ext::NodeIdType<{ NodeId::ID }>;
84
85 pub type Target = zextz64!(0x4, true);
86 #[repr(u8)]
95 #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Deserialize)]
96 pub enum QueryTarget {
97 #[default]
99 BestMatching,
100 All,
102 AllComplete,
104 }
105
106 impl QueryTarget {
107 pub const DEFAULT: Self = Self::BestMatching;
108
109 #[cfg(feature = "test")]
110 #[doc(hidden)]
111 pub fn rand() -> Self {
112 use rand::prelude::*;
113 let mut rng = rand::thread_rng();
114
115 *[
116 QueryTarget::All,
117 QueryTarget::AllComplete,
118 QueryTarget::BestMatching,
119 ]
120 .choose(&mut rng)
121 .unwrap()
122 }
123 }
124
125 pub type Budget = zextz64!(0x5, false);
127 pub type BudgetType = NonZeroU32;
128
129 pub type Timeout = zextz64!(0x6, false);
131 pub type TimeoutType = Duration;
132}
133
134impl Request {
135 pub fn payload_size(&self) -> usize {
136 match &self.payload {
137 RequestBody::Query(q) => {
138 q.ext_body.as_ref().map_or(0, |b| b.payload.len())
139 + q.ext_attachment.as_ref().map_or(0, |a| a.buffer.len())
140 }
141 }
142 }
143
144 #[cfg(feature = "test")]
145 #[doc(hidden)]
146 pub fn rand() -> Self {
147 use core::num::NonZeroU32;
148
149 use rand::Rng;
150
151 let mut rng = rand::thread_rng();
152 let wire_expr = WireExpr::rand();
153 let id: RequestId = rng.gen();
154 let payload = RequestBody::rand();
155 let ext_qos = ext::QoSType::rand();
156 let ext_tstamp = rng.gen_bool(0.5).then(ext::TimestampType::rand);
157 let ext_nodeid = ext::NodeIdType::rand();
158 let ext_target = ext::QueryTarget::rand();
159 let ext_budget = if rng.gen_bool(0.5) {
160 NonZeroU32::new(rng.gen())
161 } else {
162 None
163 };
164 let ext_timeout = if rng.gen_bool(0.5) {
165 Some(ext::TimeoutType::from_millis(rng.gen()))
166 } else {
167 None
168 };
169
170 Self {
171 wire_expr,
172 id,
173 payload,
174 ext_qos,
175 ext_tstamp,
176 ext_nodeid,
177 ext_target,
178 ext_budget,
179 ext_timeout,
180 }
181 }
182}