zenoh_protocol/network/
response.rs1use zenoh_buffers::buffer::Buffer;
2
3use crate::zenoh::reply::ReplyBody;
4use crate::{core::WireExpr, network::RequestId, zenoh::ResponseBody};
18
19pub mod flag {
46 pub const N: u8 = 1 << 5; pub const M: u8 = 1 << 6; pub const Z: u8 = 1 << 7; }
50
51#[derive(Debug, Clone, PartialEq, Eq)]
52pub struct Response {
53 pub rid: RequestId,
54 pub wire_expr: WireExpr<'static>,
55 pub payload: ResponseBody,
56 pub ext_qos: ext::QoSType,
57 pub ext_tstamp: Option<ext::TimestampType>,
58 pub ext_respid: Option<ext::ResponderIdType>,
59 pub ext_ts_stack: Option<ext::TsStackType>,
60}
61
62pub mod ext {
63 use crate::{zextz64, zextzbuf};
64 pub type QoS = zextz64!(0x1, false);
65 pub type QoSType = crate::network::ext::QoSType<{ QoS::ID }>;
66
67 pub type Timestamp = zextzbuf!(0x2, false);
68 pub type TimestampType = crate::network::ext::TimestampType<{ Timestamp::ID }>;
69
70 pub type ResponderId = zextzbuf!(0x3, false);
71 pub type ResponderIdType = crate::network::ext::EntityGlobalIdType<{ ResponderId::ID }>;
72
73 pub type TsStack = zextzbuf!(0x7, false);
74 pub type TsStackType = crate::network::timestamp_stack::TsStackType<{ TsStack::ID }>;
75}
76
77impl Response {
78 pub fn payload_size(&self) -> usize {
79 match &self.payload {
80 ResponseBody::Reply(r) => match &r.payload {
81 ReplyBody::Put(p) => {
82 p.payload.len() + p.ext_attachment.as_ref().map_or(0, |a| a.buffer.len())
83 }
84 ReplyBody::Del(d) => d.ext_attachment.as_ref().map_or(0, |a| a.buffer.len()),
85 },
86 ResponseBody::Err(e) => e.payload.len(),
87 }
88 }
89
90 #[cfg(feature = "test")]
91 #[doc(hidden)]
92 pub fn rand() -> Self {
93 use rand::Rng;
94 let mut rng = rand::thread_rng();
95
96 let rid: RequestId = rng.gen();
97 let wire_expr = WireExpr::rand();
98 let payload = ResponseBody::rand();
99 let ext_qos = ext::QoSType::rand();
100 let ext_tstamp = rng.gen_bool(0.5).then(ext::TimestampType::rand);
101 let ext_respid = rng.gen_bool(0.5).then(ext::ResponderIdType::rand);
102 let ext_ts_stack = rng.gen_bool(0.5).then(ext::TsStackType::rand);
103
104 Self {
105 rid,
106 wire_expr,
107 payload,
108 ext_qos,
109 ext_tstamp,
110 ext_respid,
111 ext_ts_stack,
112 }
113 }
114}
115
116#[derive(Debug, Clone, PartialEq, Eq)]
136pub struct ResponseFinal {
137 pub rid: RequestId,
138 pub ext_qos: ext::QoSType,
139 pub ext_tstamp: Option<ext::TimestampType>,
140}
141
142impl ResponseFinal {
143 #[cfg(feature = "test")]
144 #[doc(hidden)]
145 pub fn rand() -> Self {
146 use rand::Rng;
147
148 let mut rng = rand::thread_rng();
149 let rid: RequestId = rng.gen();
150 let ext_qos = ext::QoSType::rand();
151 let ext_tstamp = rng.gen_bool(0.5).then(ext::TimestampType::rand);
152
153 Self {
154 rid,
155 ext_qos,
156 ext_tstamp,
157 }
158 }
159}