AWS IoT Device SDK C++ v2  1.35.0
AWS IoT Device SDK C++ v2
Mqtt5Client.h
Go to the documentation of this file.
1 #pragma once
2 
9 
10 namespace Aws
11 {
12  namespace Crt
13  {
14  namespace Mqtt5
15  {
16  class ConnectPacket;
17  class ConnAckPacket;
18  class DisconnectPacket;
19  class Mqtt5Client;
20  class Mqtt5ClientOptions;
21  class NegotiatedSettings;
22  class PublishResult;
23  class PublishPacket;
24  class PubAckPacket;
25  class SubscribePacket;
26  class SubAckPacket;
27  class UnsubscribePacket;
28  class UnSubAckPacket;
29  class Mqtt5ClientCore;
30 
31  class Mqtt5to3AdapterOptions;
32 
40  {
41 
46  Default = AWS_MQTT5_COTABT_DEFAULT,
47 
56  Manual = AWS_MQTT5_COTABT_MANUAL,
57 
62  LRU = AWS_MQTT5_COTABT_LRU,
63 
67  Disabled = AWS_MQTT5_COTABT_DISABLED,
68  };
69 
78  {
79 
84  Default = AWS_MQTT5_CITABT_DEFAULT,
85 
89  Enabled = AWS_MQTT5_CITABT_ENABLED,
90 
94  Disabled = AWS_MQTT5_CITABT_DISABLED,
95  };
96 
101  {
102 
112 
124 
132 
142  };
143 
145  {
151 
157 
163 
170  };
171 
176  {
182 
188 
194 
200  };
201 
207  {
209  };
210 
215  {
216  OnConnectionFailureEventData() : errorCode(AWS_ERROR_SUCCESS), connAckPacket(nullptr) {}
217 
219  std::shared_ptr<ConnAckPacket> connAckPacket;
220  };
221 
226  {
227  OnConnectionSuccessEventData() : connAckPacket(nullptr), negotiatedSettings(nullptr) {}
228 
229  std::shared_ptr<ConnAckPacket> connAckPacket;
230  std::shared_ptr<NegotiatedSettings> negotiatedSettings;
231  };
232 
237  {
238  OnDisconnectionEventData() : errorCode(AWS_ERROR_SUCCESS), disconnectPacket(nullptr) {}
239 
241  std::shared_ptr<DisconnectPacket> disconnectPacket;
242  };
243 
249  {
251  };
252 
257  {
258  PublishReceivedEventData() : publishPacket(nullptr) {}
259  std::shared_ptr<PublishPacket> publishPacket;
260  };
261 
266  using OnConnectionSuccessHandler = std::function<void(const OnConnectionSuccessEventData &)>;
267 
271  using OnConnectionFailureHandler = std::function<void(const OnConnectionFailureEventData &)>;
272 
276  using OnDisconnectionHandler = std::function<void(const OnDisconnectionEventData &)>;
277 
282  using OnAttemptingConnectHandler = std::function<void(const OnAttemptingConnectEventData &)>;
283 
288  using OnStoppedHandler = std::function<void(const OnStoppedEventData &)>;
289 
293  using OnPublishCompletionHandler = std::function<void(int, std::shared_ptr<PublishResult>)>;
294 
298  using OnSubscribeCompletionHandler = std::function<void(int, std::shared_ptr<SubAckPacket>)>;
299 
303  using OnUnsubscribeCompletionHandler = std::function<void(int, std::shared_ptr<UnSubAckPacket>)>;
304 
308  using OnPublishReceivedHandler = std::function<void(const PublishReceivedEventData &)>;
309 
315  std::function<void(const std::shared_ptr<Http::HttpRequest> &, int)>;
316 
324  std::function<void(std::shared_ptr<Http::HttpRequest>, const OnWebSocketHandshakeInterceptComplete &)>;
325 
331  class AWS_CRT_CPP_API Mqtt5Client final : public std::enable_shared_from_this<Mqtt5Client>
332  {
333  friend class Mqtt::MqttConnection;
334 
335  public:
343  static std::shared_ptr<Mqtt5Client> NewMqtt5Client(
344  const Mqtt5ClientOptions &options,
345  Allocator *allocator = ApiAllocator()) noexcept;
346 
353  std::shared_ptr<Mqtt5Client> getptr() { return shared_from_this(); }
354 
358  operator bool() const noexcept;
359 
363  int LastError() const noexcept;
364 
372  bool Start() const noexcept;
373 
380  bool Stop() noexcept;
381 
391  bool Stop(std::shared_ptr<DisconnectPacket> disconnectPacket) noexcept;
392 
401  bool Publish(
402  std::shared_ptr<PublishPacket> publishPacket,
403  OnPublishCompletionHandler onPublishCompletionCallback = NULL) noexcept;
404 
413  bool Subscribe(
414  std::shared_ptr<SubscribePacket> subscribePacket,
415  OnSubscribeCompletionHandler onSubscribeCompletionCallback = NULL) noexcept;
416 
425  bool Unsubscribe(
426  std::shared_ptr<UnsubscribePacket> unsubscribePacket,
427  OnUnsubscribeCompletionHandler onUnsubscribeCompletionCallback = NULL) noexcept;
428 
434  const Mqtt5ClientOperationStatistics &GetOperationStatistics() noexcept;
435 
436  virtual ~Mqtt5Client();
437 
438  struct aws_mqtt5_client *GetUnderlyingHandle() const noexcept;
439 
440  private:
441  Mqtt5Client(const Mqtt5ClientOptions &options, Allocator *allocator = ApiAllocator()) noexcept;
442 
443  /* The client core to handle the user callbacks and c client termination */
444  std::shared_ptr<Mqtt5ClientCore> m_client_core;
445 
446  Mqtt5ClientOperationStatistics m_operationStatistics;
447  };
448 
453  {
454  friend class Mqtt5ClientCore;
455  friend class Mqtt5to3AdapterOptions;
456 
457  public:
461  Mqtt5ClientOptions(Crt::Allocator *allocator = ApiAllocator()) noexcept;
462 
470  Mqtt5ClientOptions &WithHostName(Crt::String hostname);
471 
479  Mqtt5ClientOptions &WithPort(uint32_t port) noexcept;
480 
489  Mqtt5ClientOptions &WithBootstrap(Io::ClientBootstrap *bootStrap) noexcept;
490 
498  Mqtt5ClientOptions &WithSocketOptions(Io::SocketOptions socketOptions) noexcept;
499 
507  Mqtt5ClientOptions &WithTlsConnectionOptions(const Io::TlsConnectionOptions &tslOptions) noexcept;
508 
516  Mqtt5ClientOptions &WithHttpProxyOptions(
517  const Crt::Http::HttpClientConnectionProxyOptions &proxyOptions) noexcept;
518 
526  Mqtt5ClientOptions &WithConnectOptions(std::shared_ptr<ConnectPacket> connectPacket) noexcept;
527 
535  Mqtt5ClientOptions &WithSessionBehavior(ClientSessionBehaviorType sessionBehavior) noexcept;
536 
546  Mqtt5ClientOptions &WithClientExtendedValidationAndFlowControl(
547  ClientExtendedValidationAndFlowControl clientExtendedValidationAndFlowControl) noexcept;
548 
559  Mqtt5ClientOptions &WithOfflineQueueBehavior(
560  ClientOperationQueueBehaviorType offlineQueueBehavior) noexcept;
561 
570  Mqtt5ClientOptions &WithReconnectOptions(ReconnectOptions reconnectOptions) noexcept;
571 
578  Mqtt5ClientOptions &WithTopicAliasingOptions(TopicAliasingOptions topicAliasingOptions) noexcept;
579 
588  Mqtt5ClientOptions &WithPingTimeoutMs(uint32_t pingTimeoutMs) noexcept;
589 
598  Mqtt5ClientOptions &WithConnackTimeoutMs(uint32_t connackTimeoutMs) noexcept;
599 
610  Mqtt5ClientOptions &WithAckTimeoutSeconds(uint32_t ackTimeoutSec) noexcept;
611 
620  Mqtt5ClientOptions &WithAckTimeoutSec(uint32_t ackTimeoutSec) noexcept;
621 
633  Mqtt5ClientOptions &WithWebsocketHandshakeTransformCallback(
634  OnWebSocketHandshakeIntercept callback) noexcept;
635 
643  Mqtt5ClientOptions &WithClientConnectionSuccessCallback(OnConnectionSuccessHandler callback) noexcept;
644 
652  Mqtt5ClientOptions &WithClientConnectionFailureCallback(OnConnectionFailureHandler callback) noexcept;
653 
661  Mqtt5ClientOptions &WithClientDisconnectionCallback(OnDisconnectionHandler callback) noexcept;
662 
670  Mqtt5ClientOptions &WithClientStoppedCallback(OnStoppedHandler callback) noexcept;
671 
679  Mqtt5ClientOptions &WithClientAttemptingConnectCallback(OnAttemptingConnectHandler callback) noexcept;
680 
688  Mqtt5ClientOptions &WithPublishReceivedCallback(OnPublishReceivedHandler callback) noexcept;
689 
697  bool initializeRawOptions(aws_mqtt5_client_options &raw_options) const noexcept;
698 
699  virtual ~Mqtt5ClientOptions();
702  Mqtt5ClientOptions &operator=(const Mqtt5ClientOptions &) = delete;
703  Mqtt5ClientOptions &operator=(Mqtt5ClientOptions &&) = delete;
704 
705  private:
712  OnWebSocketHandshakeIntercept websocketHandshakeTransform;
713 
717  OnConnectionSuccessHandler onConnectionSuccess;
718 
722  OnConnectionFailureHandler onConnectionFailure;
723 
727  OnDisconnectionHandler onDisconnection;
728 
734  OnStoppedHandler onStopped;
735 
741  OnAttemptingConnectHandler onAttemptingConnect;
742 
749  OnPublishReceivedHandler onPublishReceived;
750 
754  Crt::String m_hostName;
755 
759  uint32_t m_port;
760 
764  Io::ClientBootstrap *m_bootstrap;
765 
770  Crt::Io::SocketOptions m_socketOptions;
771 
776  Crt::Optional<Crt::Io::TlsConnectionOptions> m_tlsConnectionOptions;
777 
781  Crt::Optional<Crt::Http::HttpClientConnectionProxyOptions> m_proxyOptions;
782 
787  std::shared_ptr<ConnectPacket> m_connectOptions;
788 
792  ClientSessionBehaviorType m_sessionBehavior;
793 
798  ClientExtendedValidationAndFlowControl m_extendedValidationAndFlowControlOptions;
799 
806  ClientOperationQueueBehaviorType m_offlineQueueBehavior;
807 
811  ReconnectOptions m_reconnectionOptions;
812 
816  aws_mqtt5_client_topic_alias_options m_topicAliasingOptions;
817 
822  uint32_t m_pingTimeoutMs;
823 
828  uint32_t m_connackTimeoutMs;
829 
834  uint32_t m_ackTimeoutSec;
835 
836  /* Underlying Parameters */
837  Crt::Allocator *m_allocator;
838  aws_http_proxy_options m_httpProxyOptionsStorage;
839  aws_mqtt5_packet_connect_view m_packetConnectViewStorage;
840  };
841 
842  } // namespace Mqtt5
843  } // namespace Crt
844 } // namespace Aws
Aws::Crt::Mqtt5::OnWebSocketHandshakeInterceptComplete
std::function< void(const std::shared_ptr< Http::HttpRequest > &, int)> OnWebSocketHandshakeInterceptComplete
Definition: Mqtt5Client.h:315
Aws::Crt::Mqtt5::InboundTopicAliasBehaviorType
InboundTopicAliasBehaviorType
Definition: Mqtt5Client.h:78
Aws::Crt::Mqtt5::Mqtt5ClientOperationStatistics::incompleteOperationSize
uint64_t incompleteOperationSize
Definition: Mqtt5Client.h:187
Aws::Crt::Mqtt5::OnConnectionFailureEventData::errorCode
int errorCode
Definition: Mqtt5Client.h:218
Aws::Crt::ApiAllocator
AWS_CRT_CPP_API Allocator * ApiAllocator() noexcept
Definition: Allocator.cpp:24
Aws::Crt::Mqtt5::OnConnectionSuccessEventData::negotiatedSettings
std::shared_ptr< NegotiatedSettings > negotiatedSettings
Definition: Mqtt5Client.h:230
Aws::Crt::Mqtt5::OnStoppedEventData::OnStoppedEventData
OnStoppedEventData()
Definition: Mqtt5Client.h:250
Aws::Crt::Mqtt5::ReconnectOptions::m_minConnectedTimeToResetReconnectDelayMs
uint64_t m_minConnectedTimeToResetReconnectDelayMs
Definition: Mqtt5Client.h:169
Aws::Crt::Mqtt5::OutboundTopicAliasBehaviorType::Default
@ Default
Aws::Crt::Mqtt5::Mqtt5ClientOperationStatistics::incompleteOperationCount
uint64_t incompleteOperationCount
Definition: Mqtt5Client.h:181
Aws::Crt::Mqtt5::PublishPacket
Definition: Mqtt5Packets.h:53
Aws::Crt::Mqtt5::PublishReceivedEventData::PublishReceivedEventData
PublishReceivedEventData()
Definition: Mqtt5Client.h:258
Aws::Crt::Mqtt5::OnDisconnectionEventData::OnDisconnectionEventData
OnDisconnectionEventData()
Definition: Mqtt5Client.h:238
Aws::Crt::Mqtt5::Mqtt5ClientOperationStatistics
Definition: Mqtt5Client.h:176
Aws::Crt::Mqtt5::DisconnectPacket
Definition: Mqtt5Packets.h:1551
Aws::Crt::Mqtt5::Mqtt5ClientOptions
Definition: Mqtt5Client.h:453
Aws::Crt::Mqtt5::OnConnectionFailureEventData::OnConnectionFailureEventData
OnConnectionFailureEventData()
Definition: Mqtt5Client.h:216
Aws::Crt::LastError
AWS_CRT_CPP_API int LastError() noexcept
Definition: Api.cpp:422
Aws::Crt::Mqtt5::ReconnectOptions::m_reconnectMode
ExponentialBackoffJitterMode m_reconnectMode
Definition: Mqtt5Client.h:150
Aws::Crt::Mqtt5::TopicAliasingOptions::m_outboundBehavior
Crt::Optional< OutboundTopicAliasBehaviorType > m_outboundBehavior
Definition: Mqtt5Client.h:111
Aws::Crt::Mqtt5::OnAttemptingConnectEventData
Definition: Mqtt5Client.h:207
Aws::Crt::Mqtt5::OnConnectionSuccessEventData
Definition: Mqtt5Client.h:226
Aws::Crt::Mqtt5::InboundTopicAliasBehaviorType::Default
@ Default
Aws::Crt::Mqtt::MqttConnection
Definition: MqttConnection.h:158
Aws::Crt::Mqtt5::Mqtt5Client
Definition: Mqtt5Client.h:332
Aws::Crt::Mqtt5::ClientExtendedValidationAndFlowControl
aws_mqtt5_extended_validation_and_flow_control_options ClientExtendedValidationAndFlowControl
Definition: Mqtt5Types.h:200
Aws::Crt::Mqtt5::OnAttemptingConnectEventData::OnAttemptingConnectEventData
OnAttemptingConnectEventData()
Definition: Mqtt5Client.h:208
HttpConnection.h
Aws::Crt::Mqtt5::OnConnectionSuccessEventData::OnConnectionSuccessEventData
OnConnectionSuccessEventData()
Definition: Mqtt5Client.h:227
Aws::Crt::Mqtt5::Mqtt5ClientOperationStatistics::unackedOperationSize
uint64_t unackedOperationSize
Definition: Mqtt5Client.h:199
Aws::Crt::Mqtt5::OnDisconnectionEventData::errorCode
int errorCode
Definition: Mqtt5Client.h:240
Aws::Crt::Mqtt5::OnPublishReceivedHandler
std::function< void(const PublishReceivedEventData &)> OnPublishReceivedHandler
Definition: Mqtt5Client.h:308
Aws::Crt::Mqtt5::OnConnectionFailureEventData::connAckPacket
std::shared_ptr< ConnAckPacket > connAckPacket
Definition: Mqtt5Client.h:219
Aws::Crt::Mqtt5::OnDisconnectionEventData
Definition: Mqtt5Client.h:237
Aws::Crt::Mqtt5::OnWebSocketHandshakeIntercept
std::function< void(std::shared_ptr< Http::HttpRequest >, const OnWebSocketHandshakeInterceptComplete &)> OnWebSocketHandshakeIntercept
Definition: Mqtt5Client.h:324
Aws::Crt::Mqtt5::SubscribePacket
Definition: Mqtt5Packets.h:2029
Aws::Crt::Mqtt5::TopicAliasingOptions::m_inboundBehavior
Crt::Optional< InboundTopicAliasBehaviorType > m_inboundBehavior
Definition: Mqtt5Client.h:131
Aws::Crt::Mqtt5::OnConnectionFailureEventData
Definition: Mqtt5Client.h:215
Aws::Crt::Optional< OutboundTopicAliasBehaviorType >
Aws::Crt::Mqtt5::PublishReceivedEventData
Definition: Mqtt5Client.h:257
Aws::Crt::Mqtt5::OnConnectionSuccessEventData::connAckPacket
std::shared_ptr< ConnAckPacket > connAckPacket
Definition: Mqtt5Client.h:229
Aws::Crt::Mqtt5::TopicAliasingOptions::m_inboundCacheMaxSize
Crt::Optional< uint16_t > m_inboundCacheMaxSize
Definition: Mqtt5Client.h:141
Aws::Crt::Mqtt5::OnAttemptingConnectHandler
std::function< void(const OnAttemptingConnectEventData &)> OnAttemptingConnectHandler
Definition: Mqtt5Client.h:282
Aws::Crt::Mqtt5::Mqtt5ClientOperationStatistics::unackedOperationCount
uint64_t unackedOperationCount
Definition: Mqtt5Client.h:193
Aws::Crt::Mqtt5::TopicAliasingOptions
Definition: Mqtt5Client.h:101
Aws::Crt::Mqtt5::ConnectPacket
Definition: Mqtt5Packets.h:663
Aws::Crt::Mqtt5::OnConnectionFailureHandler
std::function< void(const OnConnectionFailureEventData &)> OnConnectionFailureHandler
Definition: Mqtt5Client.h:271
Aws::Crt::Mqtt5::OnDisconnectionHandler
std::function< void(const OnDisconnectionEventData &)> OnDisconnectionHandler
Definition: Mqtt5Client.h:276
Aws::Crt::Mqtt5::PublishReceivedEventData::publishPacket
std::shared_ptr< PublishPacket > publishPacket
Definition: Mqtt5Client.h:259
std
Definition: StringView.h:852
Aws::Crt::Mqtt5::OnPublishCompletionHandler
std::function< void(int, std::shared_ptr< PublishResult >)> OnPublishCompletionHandler
Definition: Mqtt5Client.h:293
AWS_CRT_CPP_API
#define AWS_CRT_CPP_API
Definition: Exports.h:37
Aws
Definition: Allocator.h:11
Aws::Crt::Mqtt5::ReconnectOptions
Definition: Mqtt5Client.h:145
Aws::Crt::Mqtt5::UnsubscribePacket
Definition: Mqtt5Packets.h:2235
Aws::Crt::Mqtt5::OnDisconnectionEventData::disconnectPacket
std::shared_ptr< DisconnectPacket > disconnectPacket
Definition: Mqtt5Client.h:241
Aws::Crt::Allocator
aws_allocator Allocator
Definition: Allocator.h:14
Aws::Crt::Mqtt5::OnStoppedEventData
Definition: Mqtt5Client.h:249
MqttClient.h
Aws::Crt::Mqtt5::TopicAliasingOptions::m_outboundCacheMaxSize
Crt::Optional< uint16_t > m_outboundCacheMaxSize
Definition: Mqtt5Client.h:123
Aws::Crt::Mqtt5::OnSubscribeCompletionHandler
std::function< void(int, std::shared_ptr< SubAckPacket >)> OnSubscribeCompletionHandler
Definition: Mqtt5Client.h:298
Aws::Crt::Mqtt5::ReconnectOptions::m_maxReconnectDelayMs
uint64_t m_maxReconnectDelayMs
Definition: Mqtt5Client.h:162
Aws::Crt::Mqtt5::ClientSessionBehaviorType
aws_mqtt5_client_session_behavior_type ClientSessionBehaviorType
Definition: Mqtt5Types.h:186
Aws::Crt::Mqtt5::OnConnectionSuccessHandler
std::function< void(const OnConnectionSuccessEventData &)> OnConnectionSuccessHandler
Definition: Mqtt5Client.h:266
Aws::Crt::Mqtt5::ReconnectOptions::m_minReconnectDelayMs
uint64_t m_minReconnectDelayMs
Definition: Mqtt5Client.h:156
Aws::Crt::Mqtt5::OnStoppedHandler
std::function< void(const OnStoppedEventData &)> OnStoppedHandler
Definition: Mqtt5Client.h:288
Mqtt5Types.h
Aws::Crt::String
std::basic_string< char, std::char_traits< char >, StlAllocator< char > > String
Definition: Types.h:45
Aws::Crt::Mqtt5::OutboundTopicAliasBehaviorType
OutboundTopicAliasBehaviorType
Definition: Mqtt5Client.h:40
Aws::Crt::Mqtt5::ExponentialBackoffJitterMode
aws_exponential_backoff_jitter_mode ExponentialBackoffJitterMode
Definition: Mqtt5Types.h:242
Aws::Crt::Mqtt5::ClientOperationQueueBehaviorType
aws_mqtt5_client_operation_queue_behavior_type ClientOperationQueueBehaviorType
Definition: Mqtt5Types.h:223
Aws::Crt::Mqtt5::OnUnsubscribeCompletionHandler
std::function< void(int, std::shared_ptr< UnSubAckPacket >)> OnUnsubscribeCompletionHandler
Definition: Mqtt5Client.h:303