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