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>
19
20#include <aws/mqtt/client.h>
21#include <aws/mqtt/v5/mqtt5_client.h>
22
23#include <functional>
24#include <memory>
25
26namespace Aws
27{
28 namespace Crt
29 {
30 namespace Mqtt
31 {
32 class MqttConnection;
33
42 class MqttConnectionCore final : public std::enable_shared_from_this<MqttConnectionCore>
43 {
44 friend MqttConnection;
45
46 public:
47 ~MqttConnectionCore();
48 MqttConnectionCore(const MqttConnectionCore &) = delete;
49 MqttConnectionCore(MqttConnectionCore &&) = delete;
50 MqttConnectionCore &operator=(const MqttConnectionCore &) = delete;
51 MqttConnectionCore &operator=(MqttConnectionCore &&) = delete;
52
57 operator bool() const noexcept;
58
66 void Destroy();
67
72 int LastError() const noexcept;
73
83 bool SetWill(const char *topic, QOS qos, bool retain, const ByteBuf &payload) noexcept;
84
93 bool SetLogin(const char *username, const char *password) noexcept;
94
105 bool SetHttpProxyOptions(const Http::HttpClientConnectionProxyOptions &proxyOptions) noexcept;
106
119 bool SetReconnectTimeout(uint64_t min_seconds, uint64_t max_seconds) noexcept;
120
139 bool Connect(
140 const char *clientId,
141 bool cleanSession,
142 uint16_t keepAliveTimeSecs,
143 uint32_t pingTimeoutMs,
144 uint32_t protocolOperationTimeoutMs,
145 bool setWebSocketInterceptor) noexcept;
146
152 bool Disconnect() noexcept;
153
155 aws_mqtt_client_connection *GetUnderlyingConnection() const noexcept;
156
170 uint16_t Subscribe(
171 const char *topicFilter,
172 QOS qos,
173 OnMessageReceivedHandler &&onMessage,
174 OnSubAckHandler &&onSubAck) noexcept;
175
189 uint16_t Subscribe(
190 const Vector<std::pair<const char *, OnMessageReceivedHandler>> &topicFilters,
191 QOS qos,
192 OnMultiSubAckHandler &&onOpComplete) noexcept;
193
202 bool SetOnMessageHandler(OnMessageReceivedHandler &&onMessage) noexcept;
203
214 uint16_t Unsubscribe(const char *topicFilter, OnOperationCompleteHandler &&onOpComplete) noexcept;
215
230 uint16_t Publish(
231 const char *topic,
232 QOS qos,
233 bool retain,
234 const ByteBuf &payload,
235 OnOperationCompleteHandler &&onOpComplete) noexcept;
236
243 const MqttConnectionOperationStatistics &GetOperationStatistics() noexcept;
244
245 private:
253 static std::shared_ptr<MqttConnectionCore> s_createMqttConnectionCore(
254 aws_mqtt_client *client,
255 std::shared_ptr<MqttConnection> connection,
256 MqttConnectionOptions options) noexcept;
257
265 static std::shared_ptr<MqttConnectionCore> s_createMqttConnectionCore(
266 aws_mqtt5_client *mqtt5Client,
267 std::shared_ptr<MqttConnection> connection,
268 MqttConnectionOptions options) noexcept;
269
270 static void s_onConnectionTermination(void *userData);
271
272 static void s_onConnectionInterrupted(aws_mqtt_client_connection *, int errorCode, void *userData);
273 static void s_onConnectionCompleted(
274 aws_mqtt_client_connection *,
275 int errorCode,
276 enum aws_mqtt_connect_return_code returnCode,
277 bool sessionPresent,
278 void *userData);
279
280 static void s_onConnectionSuccess(
281 aws_mqtt_client_connection *,
282 ReturnCode returnCode,
283 bool sessionPresent,
284 void *userData);
285
286 static void s_onConnectionFailure(aws_mqtt_client_connection *, int errorCode, void *userData);
287
288 static void s_onConnectionResumed(
289 aws_mqtt_client_connection *,
290 ReturnCode returnCode,
291 bool sessionPresent,
292 void *userData);
293
294 static void s_onConnectionClosed(
295 aws_mqtt_client_connection *,
296 on_connection_closed_data *data,
297 void *userData);
298
299 static void s_onDisconnect(aws_mqtt_client_connection *connection, void *userData);
300 static void s_onPublish(
301 aws_mqtt_client_connection *connection,
302 const aws_byte_cursor *topic,
303 const aws_byte_cursor *payload,
304 bool dup,
305 enum aws_mqtt_qos qos,
306 bool retain,
307 void *userData);
308
309 static void s_onSubAck(
310 aws_mqtt_client_connection *connection,
311 uint16_t packetId,
312 const struct aws_byte_cursor *topic,
313 enum aws_mqtt_qos qos,
314 int errorCode,
315 void *userdata);
316 static void s_onMultiSubAck(
317 aws_mqtt_client_connection *connection,
318 uint16_t packetId,
319 const struct aws_array_list *topicSubacks,
320 int errorCode,
321 void *userdata);
322 static void s_onOpComplete(
323 aws_mqtt_client_connection *connection,
324 uint16_t packetId,
325 int errorCode,
326 void *userdata);
327
328 static void s_onWebsocketHandshake(
329 struct aws_http_message *request,
330 void *userData,
331 aws_mqtt_transform_websocket_handshake_complete_fn *completeFn,
332 void *completeCtx);
333
347 MqttConnectionCore(
348 aws_mqtt_client *client,
349 aws_mqtt5_client *mqtt5Client,
350 std::shared_ptr<MqttConnection> connection,
351 MqttConnectionOptions options) noexcept;
352
353 void createUnderlyingConnection(aws_mqtt_client *mqttClient);
354 void createUnderlyingConnection(aws_mqtt5_client *mqtt5Client);
355 void connectionInit();
356
363 std::shared_ptr<MqttConnection> obtainConnectionInstance();
364
365 aws_mqtt_client_connection *m_underlyingConnection;
366 String m_hostName;
367 uint32_t m_port;
368 Crt::Io::TlsContext m_tlsContext;
369 Io::TlsConnectionOptions m_tlsOptions;
370 Io::SocketOptions m_socketOptions;
371 Crt::Optional<Http::HttpClientConnectionProxyOptions> m_proxyOptions;
372 void *m_onAnyCbData;
373 bool m_useTls;
374 bool m_useWebsocket;
375 MqttConnectionOperationStatistics m_operationStatistics;
376 bool m_enableMetrics = true;
377 IoTDeviceSDKMetrics m_sdkMetrics;
378 Allocator *m_allocator;
379
386 std::weak_ptr<MqttConnection> m_connection;
387
393 std::shared_ptr<MqttConnectionCore> m_self;
394 };
395 } // namespace Mqtt
396 } // namespace Crt
397} // namespace Aws
std::unique_ptr< T, std::function< void(T *)> > ScopedResource
Definition Types.h:163
Definition Allocator.h:11
Definition StringView.h:862