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 friend class IoTSDKMetricsEncoder;
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
106 bool SetHttpProxyOptions(const Http::HttpClientConnectionProxyOptions &proxyOptions) noexcept;
107
116 bool SetL4ProxyOptions(const std::shared_ptr<Aws::Crt::Io::L4ProxyConfig> &l4ProxyOptions) noexcept;
117
130 bool SetReconnectTimeout(uint64_t min_seconds, uint64_t max_seconds) noexcept;
131
150 bool Connect(
151 const char *clientId,
152 bool cleanSession,
153 uint16_t keepAliveTimeSecs,
154 uint32_t pingTimeoutMs,
155 uint32_t protocolOperationTimeoutMs,
156 bool setWebSocketInterceptor) noexcept;
157
163 bool Disconnect() noexcept;
164
166 aws_mqtt_client_connection *GetUnderlyingConnection() const noexcept;
167
181 uint16_t Subscribe(
182 const char *topicFilter,
183 QOS qos,
184 OnMessageReceivedHandler &&onMessage,
185 OnSubAckHandler &&onSubAck) noexcept;
186
200 uint16_t Subscribe(
201 const Vector<std::pair<const char *, OnMessageReceivedHandler>> &topicFilters,
202 QOS qos,
203 OnMultiSubAckHandler &&onOpComplete) noexcept;
204
213 bool SetOnMessageHandler(OnMessageReceivedHandler &&onMessage) noexcept;
214
225 uint16_t Unsubscribe(const char *topicFilter, OnOperationCompleteHandler &&onOpComplete) noexcept;
226
241 uint16_t Publish(
242 const char *topic,
243 QOS qos,
244 bool retain,
245 const ByteBuf &payload,
246 OnOperationCompleteHandler &&onOpComplete) noexcept;
247
254 const MqttConnectionOperationStatistics &GetOperationStatistics() noexcept;
255
256 private:
264 static std::shared_ptr<MqttConnectionCore> s_createMqttConnectionCore(
265 aws_mqtt_client *client,
266 std::shared_ptr<MqttConnection> connection,
267 MqttConnectionOptions options) noexcept;
268
276 static std::shared_ptr<MqttConnectionCore> s_createMqttConnectionCore(
277 aws_mqtt5_client *mqtt5Client,
278 std::shared_ptr<MqttConnection> connection,
279 MqttConnectionOptions options) noexcept;
280
281 static void s_onConnectionTermination(void *userData);
282
283 static void s_onConnectionInterrupted(aws_mqtt_client_connection *, int errorCode, void *userData);
284 static void s_onConnectionCompleted(
285 aws_mqtt_client_connection *,
286 int errorCode,
287 enum aws_mqtt_connect_return_code returnCode,
288 bool sessionPresent,
289 void *userData);
290
291 static void s_onConnectionSuccess(
292 aws_mqtt_client_connection *,
293 ReturnCode returnCode,
294 bool sessionPresent,
295 void *userData);
296
297 static void s_onConnectionFailure(aws_mqtt_client_connection *, int errorCode, void *userData);
298
299 static void s_onConnectionResumed(
300 aws_mqtt_client_connection *,
301 ReturnCode returnCode,
302 bool sessionPresent,
303 void *userData);
304
305 static void s_onConnectionClosed(
306 aws_mqtt_client_connection *,
307 on_connection_closed_data *data,
308 void *userData);
309
310 static void s_onDisconnect(aws_mqtt_client_connection *connection, void *userData);
311 static void s_onPublish(
312 aws_mqtt_client_connection *connection,
313 const aws_byte_cursor *topic,
314 const aws_byte_cursor *payload,
315 bool dup,
316 enum aws_mqtt_qos qos,
317 bool retain,
318 void *userData);
319
320 static void s_onSubAck(
321 aws_mqtt_client_connection *connection,
322 uint16_t packetId,
323 const struct aws_byte_cursor *topic,
324 enum aws_mqtt_qos qos,
325 int errorCode,
326 void *userdata);
327 static void s_onMultiSubAck(
328 aws_mqtt_client_connection *connection,
329 uint16_t packetId,
330 const struct aws_array_list *topicSubacks,
331 int errorCode,
332 void *userdata);
333 static void s_onOpComplete(
334 aws_mqtt_client_connection *connection,
335 uint16_t packetId,
336 int errorCode,
337 void *userdata);
338
339 static void s_onWebsocketHandshake(
340 struct aws_http_message *request,
341 void *userData,
342 aws_mqtt_transform_websocket_handshake_complete_fn *completeFn,
343 void *completeCtx);
344
358 MqttConnectionCore(
359 aws_mqtt_client *client,
360 aws_mqtt5_client *mqtt5Client,
361 std::shared_ptr<MqttConnection> connection,
362 MqttConnectionOptions options) noexcept;
363
364 void createUnderlyingConnection(aws_mqtt_client *mqttClient);
365 void createUnderlyingConnection(aws_mqtt5_client *mqtt5Client);
366 void connectionInit();
367
374 std::shared_ptr<MqttConnection> obtainConnectionInstance();
375
376 aws_mqtt_client_connection *m_underlyingConnection;
377 String m_hostName;
378 uint32_t m_port;
379 Crt::Io::TlsContext m_tlsContext;
380 Io::TlsConnectionOptions m_tlsOptions;
381 Io::SocketOptions m_socketOptions;
382 Crt::Optional<Http::HttpClientConnectionProxyOptions> m_proxyOptions;
383 std::shared_ptr<Aws::Crt::Io::L4ProxyConfig> m_l4ProxyOptions;
384 void *m_onAnyCbData;
385 bool m_useTls;
386 bool m_useWebsocket;
387 MqttConnectionOperationStatistics m_operationStatistics;
388 bool m_enableMetrics = true;
389 Crt::Optional<AWSIoTMetrics> m_sdkMetrics;
390 Allocator *m_allocator;
391
398 std::weak_ptr<MqttConnection> m_connection;
399
405 std::shared_ptr<MqttConnectionCore> m_self;
406 };
407 } // namespace Mqtt
408 } // namespace Crt
409} // namespace Aws
std::unique_ptr< T, std::function< void(T *)> > ScopedResource
Definition Types.h:168
Definition Allocator.h:11
Definition StringView.h:862