aws-crt-cpp
C++ wrapper around the aws-c-* libraries. Provides Cross-Platform Transport Protocols and SSL/TLS implementations for C++.
Loading...
Searching...
No Matches
MqttConnectionCore.h
Go to the documentation of this file.
1
5#pragma once
11#include <aws/crt/Exports.h>
13#include <aws/crt/Types.h>
18
19#include <aws/mqtt/client.h>
20#include <aws/mqtt/v5/mqtt5_client.h>
21
22#include <functional>
23#include <memory>
24
25namespace Aws
26{
27 namespace Crt
28 {
29 namespace Mqtt
30 {
31 class MqttConnection;
32
41 class MqttConnectionCore final : public std::enable_shared_from_this<MqttConnectionCore>
42 {
43 friend MqttConnection;
44
45 public:
46 ~MqttConnectionCore();
47 MqttConnectionCore(const MqttConnectionCore &) = delete;
48 MqttConnectionCore(MqttConnectionCore &&) = delete;
49 MqttConnectionCore &operator=(const MqttConnectionCore &) = delete;
50 MqttConnectionCore &operator=(MqttConnectionCore &&) = delete;
51
56 operator bool() const noexcept;
57
65 void Destroy();
66
71 int LastError() const noexcept;
72
82 bool SetWill(const char *topic, QOS qos, bool retain, const ByteBuf &payload) noexcept;
83
92 bool SetLogin(const char *username, const char *password) noexcept;
93
104 bool SetHttpProxyOptions(const Http::HttpClientConnectionProxyOptions &proxyOptions) noexcept;
105
118 bool SetReconnectTimeout(uint64_t min_seconds, uint64_t max_seconds) noexcept;
119
138 bool Connect(
139 const char *clientId,
140 bool cleanSession,
141 uint16_t keepAliveTimeSecs,
142 uint32_t pingTimeoutMs,
143 uint32_t protocolOperationTimeoutMs,
144 bool setWebSocketInterceptor) noexcept;
145
151 bool Disconnect() noexcept;
152
154 aws_mqtt_client_connection *GetUnderlyingConnection() const noexcept;
155
169 uint16_t Subscribe(
170 const char *topicFilter,
171 QOS qos,
172 OnMessageReceivedHandler &&onMessage,
173 OnSubAckHandler &&onSubAck) noexcept;
174
188 uint16_t Subscribe(
189 const Vector<std::pair<const char *, OnMessageReceivedHandler>> &topicFilters,
190 QOS qos,
191 OnMultiSubAckHandler &&onOpComplete) noexcept;
192
201 bool SetOnMessageHandler(OnMessageReceivedHandler &&onMessage) noexcept;
202
213 uint16_t Unsubscribe(const char *topicFilter, OnOperationCompleteHandler &&onOpComplete) noexcept;
214
229 uint16_t Publish(
230 const char *topic,
231 QOS qos,
232 bool retain,
233 const ByteBuf &payload,
234 OnOperationCompleteHandler &&onOpComplete) noexcept;
235
242 const MqttConnectionOperationStatistics &GetOperationStatistics() noexcept;
243
244 private:
252 static std::shared_ptr<MqttConnectionCore> s_createMqttConnectionCore(
253 aws_mqtt_client *client,
254 std::shared_ptr<MqttConnection> connection,
255 MqttConnectionOptions options) noexcept;
256
264 static std::shared_ptr<MqttConnectionCore> s_createMqttConnectionCore(
265 aws_mqtt5_client *mqtt5Client,
266 std::shared_ptr<MqttConnection> connection,
267 MqttConnectionOptions options) noexcept;
268
269 static void s_onConnectionTermination(void *userData);
270
271 static void s_onConnectionInterrupted(aws_mqtt_client_connection *, int errorCode, void *userData);
272 static void s_onConnectionCompleted(
273 aws_mqtt_client_connection *,
274 int errorCode,
275 enum aws_mqtt_connect_return_code returnCode,
276 bool sessionPresent,
277 void *userData);
278
279 static void s_onConnectionSuccess(
280 aws_mqtt_client_connection *,
281 ReturnCode returnCode,
282 bool sessionPresent,
283 void *userData);
284
285 static void s_onConnectionFailure(aws_mqtt_client_connection *, int errorCode, void *userData);
286
287 static void s_onConnectionResumed(
288 aws_mqtt_client_connection *,
289 ReturnCode returnCode,
290 bool sessionPresent,
291 void *userData);
292
293 static void s_onConnectionClosed(
294 aws_mqtt_client_connection *,
295 on_connection_closed_data *data,
296 void *userData);
297
298 static void s_onDisconnect(aws_mqtt_client_connection *connection, void *userData);
299 static void s_onPublish(
300 aws_mqtt_client_connection *connection,
301 const aws_byte_cursor *topic,
302 const aws_byte_cursor *payload,
303 bool dup,
304 enum aws_mqtt_qos qos,
305 bool retain,
306 void *userData);
307
308 static void s_onSubAck(
309 aws_mqtt_client_connection *connection,
310 uint16_t packetId,
311 const struct aws_byte_cursor *topic,
312 enum aws_mqtt_qos qos,
313 int errorCode,
314 void *userdata);
315 static void s_onMultiSubAck(
316 aws_mqtt_client_connection *connection,
317 uint16_t packetId,
318 const struct aws_array_list *topicSubacks,
319 int errorCode,
320 void *userdata);
321 static void s_onOpComplete(
322 aws_mqtt_client_connection *connection,
323 uint16_t packetId,
324 int errorCode,
325 void *userdata);
326
327 static void s_onWebsocketHandshake(
328 struct aws_http_message *request,
329 void *userData,
330 aws_mqtt_transform_websocket_handshake_complete_fn *completeFn,
331 void *completeCtx);
332
346 MqttConnectionCore(
347 aws_mqtt_client *client,
348 aws_mqtt5_client *mqtt5Client,
349 std::shared_ptr<MqttConnection> connection,
350 MqttConnectionOptions options) noexcept;
351
352 void createUnderlyingConnection(aws_mqtt_client *mqttClient);
353 void createUnderlyingConnection(aws_mqtt5_client *mqtt5Client);
354 void connectionInit();
355
362 std::shared_ptr<MqttConnection> obtainConnectionInstance();
363
364 aws_mqtt_client_connection *m_underlyingConnection;
365 String m_hostName;
366 uint32_t m_port;
367 Crt::Io::TlsContext m_tlsContext;
368 Io::TlsConnectionOptions m_tlsOptions;
369 Io::SocketOptions m_socketOptions;
370 Crt::Optional<Http::HttpClientConnectionProxyOptions> m_proxyOptions;
371 void *m_onAnyCbData;
372 bool m_useTls;
373 bool m_useWebsocket;
374 MqttConnectionOperationStatistics m_operationStatistics;
375 Allocator *m_allocator;
376
383 std::weak_ptr<MqttConnection> m_connection;
384
390 std::shared_ptr<MqttConnectionCore> m_self;
391 };
392 } // namespace Mqtt
393 } // namespace Crt
394} // namespace Aws
std::unique_ptr< T, std::function< void(T *)> > ScopedResource
Definition Types.h:163
Definition Allocator.h:11
Definition StringView.h:862