AWS IoT Device SDK C++ v2  1.34.0
AWS IoT Device SDK C++ v2
MqttConnection.h
Go to the documentation of this file.
1 #pragma once
2 
7 #include <aws/crt/Exports.h>
8 #include <aws/crt/StlAllocator.h>
9 #include <aws/crt/Types.h>
12 #include <aws/crt/io/TlsOptions.h>
13 #include <aws/crt/mqtt/MqttTypes.h>
14 
15 #include <aws/mqtt/client.h>
16 #include <aws/mqtt/v5/mqtt5_client.h>
17 
18 #include <functional>
19 #include <memory>
20 
21 namespace Aws
22 {
23  namespace Crt
24  {
25  namespace Http
26  {
27  class HttpRequest;
28  }
29 
30  namespace Mqtt5
31  {
32  class Mqtt5Client;
33  class Mqtt5ClientCore;
34  } // namespace Mqtt5
35 
36  namespace Mqtt
37  {
38  class MqttClient;
39  class MqttConnectionCore;
40  class MqttConnection;
41 
47  {
48  };
49 
54  {
59 
65  };
66 
71  {
76  int error;
77  };
78 
82  using OnConnectionInterruptedHandler = std::function<void(MqttConnection &connection, int error)>;
83 
88  std::function<void(MqttConnection &connection, ReturnCode connectCode, bool sessionPresent)>;
89 
93  using OnConnectionCompletedHandler = std::function<
94  void(MqttConnection &connection, int errorCode, ReturnCode returnCode, bool sessionPresent)>;
95 
103  std::function<void(MqttConnection &connection, OnConnectionClosedData *callbackData)>;
104 
111  std::function<void(MqttConnection &connection, OnConnectionSuccessData *callbackData)>;
112 
119  std::function<void(MqttConnection &connection, OnConnectionFailureData *callbackData)>;
120 
124  using OnDisconnectHandler = std::function<void(MqttConnection &connection)>;
125 
130  std::function<void(MqttConnection &connection, const String &topic, const ByteBuf &payload)>;
131 
137  std::function<void(const std::shared_ptr<Http::HttpRequest> &, int errorCode)>;
138 
145  using OnWebSocketHandshakeIntercept = std::function<
146  void(std::shared_ptr<Http::HttpRequest> req, const OnWebSocketHandshakeInterceptComplete &onComplete)>;
147 
157  class AWS_CRT_CPP_API MqttConnection final : public std::enable_shared_from_this<MqttConnection>
158  {
159  friend class MqttClient;
160  friend class Mqtt5::Mqtt5ClientCore;
161 
162  public:
163  ~MqttConnection();
164  MqttConnection(const MqttConnection &) = delete;
168 
175  static std::shared_ptr<Crt::Mqtt::MqttConnection> NewConnectionFromMqtt5Client(
176  std::shared_ptr<Mqtt5::Mqtt5Client> mqtt5client) noexcept;
177 
181  operator bool() const noexcept;
182 
186  int LastError() const noexcept;
187 
196  bool SetWill(const char *topic, QOS qos, bool retain, const ByteBuf &payload) noexcept;
197 
205  bool SetLogin(const char *username, const char *password) noexcept;
206 
210  bool SetWebsocketProxyOptions(const Http::HttpClientConnectionProxyOptions &proxyOptions) noexcept;
211 
221  bool SetHttpProxyOptions(const Http::HttpClientConnectionProxyOptions &proxyOptions) noexcept;
222 
234  bool SetReconnectTimeout(uint64_t min_seconds, uint64_t max_seconds) noexcept;
235 
252  bool Connect(
253  const char *clientId,
254  bool cleanSession,
255  uint16_t keepAliveTimeSecs = 0,
256  uint32_t pingTimeoutMs = 0,
257  uint32_t protocolOperationTimeoutMs = 0) noexcept;
258 
263  bool Disconnect() noexcept;
264 
266  aws_mqtt_client_connection *GetUnderlyingConnection() noexcept;
267 
280  uint16_t Subscribe(
281  const char *topicFilter,
282  QOS qos,
283  OnMessageReceivedHandler &&onMessage,
284  OnSubAckHandler &&onSubAck) noexcept;
285 
289  uint16_t Subscribe(
290  const char *topicFilter,
291  QOS qos,
292  OnPublishReceivedHandler &&onPublish,
293  OnSubAckHandler &&onSubAck) noexcept;
294 
307  uint16_t Subscribe(
308  const Vector<std::pair<const char *, OnMessageReceivedHandler>> &topicFilters,
309  QOS qos,
310  OnMultiSubAckHandler &&onOpComplete) noexcept;
311 
315  uint16_t Subscribe(
316  const Vector<std::pair<const char *, OnPublishReceivedHandler>> &topicFilters,
317  QOS qos,
318  OnMultiSubAckHandler &&onOpComplete) noexcept;
319 
327  bool SetOnMessageHandler(OnMessageReceivedHandler &&onMessage) noexcept;
328 
332  bool SetOnMessageHandler(OnPublishReceivedHandler &&onPublish) noexcept;
333 
343  uint16_t Unsubscribe(const char *topicFilter, OnOperationCompleteHandler &&onOpComplete) noexcept;
344 
358  uint16_t Publish(
359  const char *topic,
360  QOS qos,
361  bool retain,
362  const ByteBuf &payload,
363  OnOperationCompleteHandler &&onOpComplete) noexcept;
364 
370  const MqttConnectionOperationStatistics &GetOperationStatistics() noexcept;
371 
375  OnConnectionInterruptedHandler OnConnectionInterrupted;
376 
380  OnConnectionResumedHandler OnConnectionResumed;
381 
385  OnConnectionCompletedHandler OnConnectionCompleted;
386 
390  OnDisconnectHandler OnDisconnect;
391 
398  OnWebSocketHandshakeIntercept WebsocketInterceptor;
399 
407  OnConnectionClosedHandler OnConnectionClosed;
408 
415 
422 
423  private:
429  MqttConnection() = default;
430 
439  static std::shared_ptr<MqttConnection> s_CreateMqttConnection(
440  aws_mqtt_client *client,
441  MqttConnectionOptions options) noexcept;
442 
451  static std::shared_ptr<MqttConnection> s_CreateMqttConnection(
452  aws_mqtt5_client *mqtt5Client,
453  MqttConnectionOptions options) noexcept;
458  std::shared_ptr<MqttConnectionCore> m_connectionCore;
459  };
460  } // namespace Mqtt
461  } // namespace Crt
462 } // namespace Aws
Aws::Crt::Mqtt::OnConnectionFailureHandler
std::function< void(MqttConnection &connection, OnConnectionFailureData *callbackData)> OnConnectionFailureHandler
Definition: MqttConnection.h:119
Aws::Crt::Mqtt::MqttConnection::MqttConnection
MqttConnection(const MqttConnection &)=delete
MqttTypes.h
Aws::Crt::Mqtt::MqttConnectionOptions
Definition: MqttTypes.h:28
TlsOptions.h
Aws::Crt::LastError
AWS_CRT_CPP_API int LastError() noexcept
Definition: Api.cpp:422
StlAllocator.h
Aws::Crt::Mqtt::OnWebSocketHandshakeInterceptComplete
std::function< void(const std::shared_ptr< Http::HttpRequest > &, int errorCode)> OnWebSocketHandshakeInterceptComplete
Definition: MqttConnection.h:137
Aws::Iotsecuretunneling::OnConnectionFailure
std::function< void(SecureTunnel *secureTunnel, int errorCode)> OnConnectionFailure
Definition: SecureTunnel.h:509
Aws::Crt::Mqtt::OnSubAckHandler
std::function< void(MqttConnection &connection, uint16_t packetId, const String &topic, QOS qos, int errorCode)> OnSubAckHandler
Definition: MqttTypes.h:68
Aws::Crt::Mqtt::OnConnectionFailureData
Definition: MqttConnection.h:71
Aws::Crt::Mqtt::OnConnectionSuccessHandler
std::function< void(MqttConnection &connection, OnConnectionSuccessData *callbackData)> OnConnectionSuccessHandler
Definition: MqttConnection.h:111
Aws::Crt::Mqtt::OnConnectionFailureData::error
int error
Definition: MqttConnection.h:76
Aws::Crt::Mqtt::MqttConnection
Definition: MqttConnection.h:158
Aws::Crt::Mqtt::OnConnectionSuccessData::returnCode
ReturnCode returnCode
Definition: MqttConnection.h:58
HttpConnection.h
Aws::Crt::Mqtt::QOS
aws_mqtt_qos QOS
Definition: Types.h:40
Aws::Crt::Mqtt::MqttConnection::MqttConnection
MqttConnection(MqttConnection &&)=delete
Aws::Crt::Mqtt::OnMultiSubAckHandler
std::function< void(MqttConnection &connection, uint16_t packetId, const Vector< String > &topics, QOS qos, int errorCode)> OnMultiSubAckHandler
Definition: MqttTypes.h:84
Aws::Crt::Mqtt::MqttConnection::operator=
MqttConnection & operator=(MqttConnection &&)=delete
Aws::Iotsecuretunneling::OnConnectionSuccess
std::function< void(SecureTunnel *secureTunnel, const ConnectionSuccessEventData &)> OnConnectionSuccess
Definition: SecureTunnel.h:503
Aws::Crt::Mqtt::OnConnectionSuccessData::sessionPresent
bool sessionPresent
Definition: MqttConnection.h:64
Aws::Crt::Mqtt::OnOperationCompleteHandler
std::function< void(MqttConnection &connection, uint16_t packetId, int errorCode)> OnOperationCompleteHandler
Definition: MqttTypes.h:97
Types.h
Aws::Crt::Mqtt::OnConnectionResumedHandler
std::function< void(MqttConnection &connection, ReturnCode connectCode, bool sessionPresent)> OnConnectionResumedHandler
Definition: MqttConnection.h:88
Aws::Crt::Mqtt::ReturnCode
aws_mqtt_connect_return_code ReturnCode
Definition: Types.h:41
Aws::Crt::Mqtt::MqttConnectionOperationStatistics
Definition: MqttTypes.h:103
Aws::Crt::Vector
std::vector< T, StlAllocator< T > > Vector
Definition: Types.h:53
Aws::Crt::Mqtt::OnConnectionClosedHandler
std::function< void(MqttConnection &connection, OnConnectionClosedData *callbackData)> OnConnectionClosedHandler
Definition: MqttConnection.h:103
Aws::Crt::ByteBuf
aws_byte_buf ByteBuf
Definition: Types.h:30
std
Definition: StringView.h:852
AWS_CRT_CPP_API
#define AWS_CRT_CPP_API
Definition: Exports.h:37
Aws
Definition: Allocator.h:11
SocketOptions.h
Aws::Crt::Mqtt::OnWebSocketHandshakeIntercept
std::function< void(std::shared_ptr< Http::HttpRequest > req, const OnWebSocketHandshakeInterceptComplete &onComplete)> OnWebSocketHandshakeIntercept
Definition: MqttConnection.h:146
Exports.h
Aws::Crt::Mqtt::OnConnectionClosedData
Definition: MqttConnection.h:47
Aws::Crt::Mqtt::OnDisconnectHandler
std::function< void(MqttConnection &connection)> OnDisconnectHandler
Definition: MqttConnection.h:124
Aws::Crt::Mqtt::OnMessageReceivedHandler
std::function< void(MqttConnection &connection, const String &topic, const ByteBuf &payload, bool dup, QOS qos, bool retain)> OnMessageReceivedHandler
Definition: MqttTypes.h:56
Aws::Crt::Mqtt::MqttClient
Definition: MqttClient.h:48
Aws::Crt::String
std::basic_string< char, std::char_traits< char >, StlAllocator< char > > String
Definition: Types.h:45
Aws::Crt::Mqtt::MqttConnection::operator=
MqttConnection & operator=(const MqttConnection &)=delete
Aws::Crt::Mqtt::OnConnectionSuccessData
Definition: MqttConnection.h:54
Aws::Crt::Mqtt::OnPublishReceivedHandler
std::function< void(MqttConnection &connection, const String &topic, const ByteBuf &payload)> OnPublishReceivedHandler
Definition: MqttConnection.h:130
Aws::Crt::Mqtt::OnConnectionInterruptedHandler
std::function< void(MqttConnection &connection, int error)> OnConnectionInterruptedHandler
Definition: MqttConnection.h:82
Aws::Crt::Mqtt::OnConnectionCompletedHandler
std::function< void(MqttConnection &connection, int errorCode, ReturnCode returnCode, bool sessionPresent)> OnConnectionCompletedHandler
Definition: MqttConnection.h:94