ntex_mqtt/config.rs
1use ntex_service::cfg::{CfgContext, Configuration};
2use ntex_util::time::{Millis, Seconds};
3
4use crate::types::QoS;
5
6#[derive(Copy, Clone, Debug)]
7pub struct MqttServiceConfig {
8 pub(crate) max_qos: QoS,
9 pub(crate) max_size: u32,
10 pub(crate) max_receive: u16,
11 pub(crate) max_receive_size: usize,
12 pub(crate) max_topic_alias: u16,
13 pub(crate) max_send: u16,
14 pub(crate) max_send_size: (u32, u32),
15 pub(crate) min_chunk_size: u32,
16 pub(crate) max_payload_buffer_size: usize,
17 pub(crate) handle_qos_after_disconnect: Option<QoS>,
18 pub(crate) connect_timeout: Seconds,
19 pub(crate) handshake_timeout: Seconds,
20 pub(crate) protocol_version_timeout: Millis,
21 config: CfgContext,
22}
23
24impl Default for MqttServiceConfig {
25 fn default() -> Self {
26 Self::new()
27 }
28}
29
30impl Configuration for MqttServiceConfig {
31 const NAME: &str = "MQTT Service configuration";
32
33 fn ctx(&self) -> &CfgContext {
34 &self.config
35 }
36
37 fn set_ctx(&mut self, ctx: CfgContext) {
38 self.config = ctx;
39 }
40}
41
42impl MqttServiceConfig {
43 pub fn new() -> Self {
44 Self {
45 max_qos: QoS::AtLeastOnce,
46 max_size: 0,
47 max_send: 16,
48 max_send_size: (65535, 512),
49 max_receive: 16,
50 max_receive_size: 65535,
51 max_topic_alias: 32,
52 min_chunk_size: 32 * 1024,
53 max_payload_buffer_size: 32 * 1024,
54 handle_qos_after_disconnect: None,
55 connect_timeout: Seconds::ZERO,
56 handshake_timeout: Seconds::ZERO,
57 protocol_version_timeout: Millis(5_000),
58 config: CfgContext::default(),
59 }
60 }
61
62 /// Set client timeout reading protocol version.
63 ///
64 /// Defines a timeout for reading protocol version. If a client does not transmit
65 /// version of the protocol within this time, the connection is terminated with
66 /// Mqtt::Handshake(HandshakeError::Timeout) error.
67 ///
68 /// By default, timeuot is 5 seconds.
69 pub fn protocol_version_timeout(mut self, timeout: Seconds) -> Self {
70 self.protocol_version_timeout = timeout.into();
71 self
72 }
73
74 /// Set client timeout for first `Connect` frame.
75 ///
76 /// Defines a timeout for reading `Connect` frame. If a client does not transmit
77 /// the entire frame within this time, the connection is terminated with
78 /// Mqtt::Handshake(HandshakeError::Timeout) error.
79 ///
80 /// By default, connect timeout is disabled.
81 pub fn set_connect_timeout(mut self, timeout: Seconds) -> Self {
82 self.connect_timeout = timeout;
83 self
84 }
85
86 /// Set max allowed QoS.
87 ///
88 /// If peer sends publish with higher qos then ProtocolError::MaxQoSViolated(..)
89 /// By default max qos is set to `ExactlyOnce`.
90 pub fn set_max_qos(mut self, qos: QoS) -> Self {
91 self.max_qos = qos;
92 self
93 }
94
95 /// Set max inbound frame size.
96 ///
97 /// If max size is set to `0`, size is unlimited.
98 /// By default max size is set to `0`
99 pub fn set_max_size(mut self, size: u32) -> Self {
100 self.max_size = size;
101 self
102 }
103
104 /// Set `receive max`
105 ///
106 /// Number of in-flight publish packets. By default receive max is set to 15 packets.
107 /// To disable timeout set value to 0.
108 pub fn set_max_receive(mut self, val: u16) -> Self {
109 self.max_receive = val;
110 self
111 }
112
113 /// Total size of received in-flight messages.
114 ///
115 /// By default total in-flight size is set to 64Kb
116 pub fn set_max_receive_size(mut self, val: usize) -> Self {
117 self.max_receive_size = val;
118 self
119 }
120
121 /// Number of topic aliases.
122 ///
123 /// By default value is set to 32
124 pub fn set_max_topic_alias(mut self, val: u16) -> Self {
125 self.max_topic_alias = val;
126 self
127 }
128
129 /// Number of outgoing concurrent messages.
130 ///
131 /// By default outgoing is set to 16 messages
132 pub fn set_max_send(mut self, val: u16) -> Self {
133 self.max_send = val;
134 self
135 }
136
137 /// Total size of outgoing messages.
138 ///
139 /// By default total outgoing size is set to 64Kb
140 pub fn set_max_send_size(mut self, val: u32) -> Self {
141 self.max_send_size = (val, val / 10);
142 self
143 }
144
145 /// Set min payload chunk size.
146 ///
147 /// If the minimum size is set to `0`, incoming payload chunks
148 /// will be processed immediately. Otherwise, the codec will
149 /// accumulate chunks until the total size reaches the specified minimum.
150 /// By default min size is set to `0`
151 pub fn set_min_chunk_size(mut self, size: u32) -> Self {
152 self.min_chunk_size = size;
153 self
154 }
155
156 /// Max payload buffer size for payload streaming.
157 ///
158 /// By default buffer size is set to 32Kb
159 pub fn set_max_payload_buffer_size(mut self, val: usize) -> Self {
160 self.max_payload_buffer_size = val;
161 self
162 }
163
164 /// Handle max received QoS messages after client disconnect.
165 ///
166 /// By default, messages received before dispatched to the publish service will be dropped if
167 /// the client disconnect is detected on the server.
168 ///
169 /// If this option is set to `Some(QoS::AtMostOnce)`, only the received QoS 0 messages will
170 /// always be handled by the server's publish service no matter if the client is disconnected
171 /// or not.
172 ///
173 /// If this option is set to `Some(QoS::AtLeastOnce)`, the received QoS 0 and QoS 1 messages
174 /// will always be handled by the server's publish service no matter if the client
175 /// is disconnected or not. The QoS 2 messages will be dropped if the client disconnecting is
176 /// detected before the server dispatches them to the publish service.
177 ///
178 /// If this option is set to `Some(QoS::ExactlyOnce)`, all the messages received will always
179 /// be handled by the server's publish service no matter if the client is disconnected or not.
180 ///
181 /// The received messages which QoS larger than the `max_handle_qos` will not be guaranteed to
182 /// be handled or not after the client disconnect. It depends on the network condition.
183 ///
184 /// By default handle-qos-after-disconnect is set to `None`
185 pub fn set_handle_qos_after_disconnect(mut self, max_handle_qos: Option<QoS>) -> Self {
186 self.handle_qos_after_disconnect = max_handle_qos;
187 self
188 }
189
190 /// Set handshake timeout.
191 ///
192 /// Handshake includes `connect` packet and response `connect-ack`.
193 /// By default handshake timeuot is disabled.
194 pub fn set_handshake_timeout(mut self, timeout: Seconds) -> Self {
195 self.handshake_timeout = timeout;
196 self
197 }
198}