AWS IoT Device SDK C++ v2  1.34.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  private:
439  Mqtt5Client(const Mqtt5ClientOptions &options, Allocator *allocator = ApiAllocator()) noexcept;
440 
441  /* The client core to handle the user callbacks and c client termination */
442  std::shared_ptr<Mqtt5ClientCore> m_client_core;
443 
444  Mqtt5ClientOperationStatistics m_operationStatistics;
445  };
446 
451  {
452  friend class Mqtt5ClientCore;
453  friend class Mqtt5to3AdapterOptions;
454 
455  public:
459  Mqtt5ClientOptions(Crt::Allocator *allocator = ApiAllocator()) noexcept;
460 
468  Mqtt5ClientOptions &WithHostName(Crt::String hostname);
469 
477  Mqtt5ClientOptions &WithPort(uint32_t port) noexcept;
478 
487  Mqtt5ClientOptions &WithBootstrap(Io::ClientBootstrap *bootStrap) noexcept;
488 
496  Mqtt5ClientOptions &WithSocketOptions(Io::SocketOptions socketOptions) noexcept;
497 
505  Mqtt5ClientOptions &WithTlsConnectionOptions(const Io::TlsConnectionOptions &tslOptions) noexcept;
506 
514  Mqtt5ClientOptions &WithHttpProxyOptions(
515  const Crt::Http::HttpClientConnectionProxyOptions &proxyOptions) noexcept;
516 
524  Mqtt5ClientOptions &WithConnectOptions(std::shared_ptr<ConnectPacket> connectPacket) noexcept;
525 
533  Mqtt5ClientOptions &WithSessionBehavior(ClientSessionBehaviorType sessionBehavior) noexcept;
534 
544  Mqtt5ClientOptions &WithClientExtendedValidationAndFlowControl(
545  ClientExtendedValidationAndFlowControl clientExtendedValidationAndFlowControl) noexcept;
546 
557  Mqtt5ClientOptions &WithOfflineQueueBehavior(
558  ClientOperationQueueBehaviorType offlineQueueBehavior) noexcept;
559 
568  Mqtt5ClientOptions &WithReconnectOptions(ReconnectOptions reconnectOptions) noexcept;
569 
576  Mqtt5ClientOptions &WithTopicAliasingOptions(TopicAliasingOptions topicAliasingOptions) noexcept;
577 
586  Mqtt5ClientOptions &WithPingTimeoutMs(uint32_t pingTimeoutMs) noexcept;
587 
596  Mqtt5ClientOptions &WithConnackTimeoutMs(uint32_t connackTimeoutMs) noexcept;
597 
608  Mqtt5ClientOptions &WithAckTimeoutSeconds(uint32_t ackTimeoutSec) noexcept;
609 
618  Mqtt5ClientOptions &WithAckTimeoutSec(uint32_t ackTimeoutSec) noexcept;
619 
631  Mqtt5ClientOptions &WithWebsocketHandshakeTransformCallback(
632  OnWebSocketHandshakeIntercept callback) noexcept;
633 
641  Mqtt5ClientOptions &WithClientConnectionSuccessCallback(OnConnectionSuccessHandler callback) noexcept;
642 
650  Mqtt5ClientOptions &WithClientConnectionFailureCallback(OnConnectionFailureHandler callback) noexcept;
651 
659  Mqtt5ClientOptions &WithClientDisconnectionCallback(OnDisconnectionHandler callback) noexcept;
660 
668  Mqtt5ClientOptions &WithClientStoppedCallback(OnStoppedHandler callback) noexcept;
669 
677  Mqtt5ClientOptions &WithClientAttemptingConnectCallback(OnAttemptingConnectHandler callback) noexcept;
678 
686  Mqtt5ClientOptions &WithPublishReceivedCallback(OnPublishReceivedHandler callback) noexcept;
687 
695  bool initializeRawOptions(aws_mqtt5_client_options &raw_options) const noexcept;
696 
697  virtual ~Mqtt5ClientOptions();
700  Mqtt5ClientOptions &operator=(const Mqtt5ClientOptions &) = delete;
701  Mqtt5ClientOptions &operator=(Mqtt5ClientOptions &&) = delete;
702 
703  private:
710  OnWebSocketHandshakeIntercept websocketHandshakeTransform;
711 
715  OnConnectionSuccessHandler onConnectionSuccess;
716 
720  OnConnectionFailureHandler onConnectionFailure;
721 
725  OnDisconnectionHandler onDisconnection;
726 
732  OnStoppedHandler onStopped;
733 
739  OnAttemptingConnectHandler onAttemptingConnect;
740 
747  OnPublishReceivedHandler onPublishReceived;
748 
752  Crt::String m_hostName;
753 
757  uint32_t m_port;
758 
762  Io::ClientBootstrap *m_bootstrap;
763 
768  Crt::Io::SocketOptions m_socketOptions;
769 
774  Crt::Optional<Crt::Io::TlsConnectionOptions> m_tlsConnectionOptions;
775 
779  Crt::Optional<Crt::Http::HttpClientConnectionProxyOptions> m_proxyOptions;
780 
785  std::shared_ptr<ConnectPacket> m_connectOptions;
786 
790  ClientSessionBehaviorType m_sessionBehavior;
791 
796  ClientExtendedValidationAndFlowControl m_extendedValidationAndFlowControlOptions;
797 
804  ClientOperationQueueBehaviorType m_offlineQueueBehavior;
805 
809  ReconnectOptions m_reconnectionOptions;
810 
814  aws_mqtt5_client_topic_alias_options m_topicAliasingOptions;
815 
820  uint32_t m_pingTimeoutMs;
821 
826  uint32_t m_connackTimeoutMs;
827 
832  uint32_t m_ackTimeoutSec;
833 
834  /* Underlying Parameters */
835  Crt::Allocator *m_allocator;
836  aws_http_proxy_options m_httpProxyOptionsStorage;
837  aws_mqtt5_packet_connect_view m_packetConnectViewStorage;
838  };
839 
840  } // namespace Mqtt5
841  } // namespace Crt
842 } // 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:451
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