aws-crt-cpp
MqttClient.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License").
6  * You may not use this file except in compliance with the License.
7  * A copy of the License is located at
8  *
9  * http://aws.amazon.com/apache2.0
10  *
11  * or in the "license" file accompanying this file. This file is distributed
12  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 #include <aws/crt/Exports.h>
17 #include <aws/crt/StlAllocator.h>
18 #include <aws/crt/Types.h>
21 #include <aws/crt/io/TlsOptions.h>
22 
23 #include <aws/mqtt/client.h>
24 
25 #include <atomic>
26 #include <functional>
27 #include <memory>
28 
29 namespace Aws
30 {
31  namespace Crt
32  {
33  namespace Io
34  {
35  class ClientBootstrap;
36  }
37 
38  namespace Http
39  {
40  class HttpRequest;
41  }
42 
43  namespace Mqtt
44  {
45  class MqttClient;
46  class MqttConnection;
47 
51  using OnConnectionInterruptedHandler = std::function<void(MqttConnection &connection, int error)>;
52 
57  std::function<void(MqttConnection &connection, ReturnCode connectCode, bool sessionPresent)>;
58 
62  using OnConnectionCompletedHandler = std::function<
63  void(MqttConnection &connection, int errorCode, ReturnCode returnCode, bool sessionPresent)>;
64 
68  using OnSubAckHandler = std::function<
69  void(MqttConnection &connection, uint16_t packetId, const String &topic, QOS qos, int errorCode)>;
70 
74  using OnMultiSubAckHandler = std::function<void(
75  MqttConnection &connection,
76  uint16_t packetId,
77  const Vector<String> &topics,
78  QOS qos,
79  int errorCode)>;
80 
84  using OnDisconnectHandler = std::function<void(MqttConnection &connection)>;
85 
90  std::function<void(MqttConnection &connection, const String &topic, const ByteBuf &payload)>;
91 
93  std::function<void(MqttConnection &connection, uint16_t packetId, int errorCode)>;
94 
100  std::function<void(const std::shared_ptr<Http::HttpRequest> &, int errorCode)>;
101 
108  using OnWebSocketHandshakeIntercept = std::function<
109  void(std::shared_ptr<Http::HttpRequest> req, const OnWebSocketHandshakeInterceptComplete &onComplete)>;
110 
118  {
119  friend class MqttClient;
120 
121  public:
122  ~MqttConnection();
123  MqttConnection(const MqttConnection &) = delete;
130  operator bool() const noexcept;
134  int LastError() const noexcept;
135 
139  bool SetWill(const char *topic, QOS qos, bool retain, const ByteBuf &payload) noexcept;
140 
145  bool SetLogin(const char *userName, const char *password) noexcept;
146 
150  bool SetWebsocketProxyOptions(const Http::HttpClientConnectionProxyOptions &proxyOptions) noexcept;
151 
156  bool Connect(
157  const char *clientId,
158  bool cleanSession,
159  uint16_t keepAliveTimeSecs = 0,
160  uint32_t pingTimeoutMs = 0) noexcept;
161 
165  bool Disconnect() noexcept;
166 
172  uint16_t Subscribe(
173  const char *topicFilter,
174  QOS qos,
175  OnPublishReceivedHandler &&onPublish,
176  OnSubAckHandler &&onSubAck) noexcept;
177 
183  uint16_t Subscribe(
184  const Vector<std::pair<const char *, OnPublishReceivedHandler>> &topicFilters,
185  QOS qos,
186  OnMultiSubAckHandler &&onOpComplete) noexcept;
187 
192  bool SetOnMessageHandler(OnPublishReceivedHandler &&onPublish) noexcept;
193 
198  uint16_t Unsubscribe(const char *topicFilter, OnOperationCompleteHandler &&onOpComplete) noexcept;
199 
204  uint16_t Publish(
205  const char *topic,
206  QOS qos,
207  bool retain,
208  const ByteBuf &payload,
209  OnOperationCompleteHandler &&onOpComplete) noexcept;
210 
211  OnConnectionInterruptedHandler OnConnectionInterrupted;
212  OnConnectionResumedHandler OnConnectionResumed;
213  OnConnectionCompletedHandler OnConnectionCompleted;
214  OnDisconnectHandler OnDisconnect;
215  OnWebSocketHandshakeIntercept WebsocketInterceptor;
216 
217  private:
218  aws_mqtt_client *m_owningClient;
219  aws_mqtt_client_connection *m_underlyingConnection;
220  String m_hostName;
221  uint16_t m_port;
222  Crt::Io::TlsContext m_tlsContext;
223  Io::TlsConnectionOptions m_tlsOptions;
224  Io::SocketOptions m_socketOptions;
225  Crt::Optional<Http::HttpClientConnectionProxyOptions> m_proxyOptions;
226  void *m_onAnyCbData;
227  bool m_useTls;
228  bool m_useWebsocket;
229 
231  aws_mqtt_client *client,
232  const char *hostName,
233  uint16_t port,
234  const Io::SocketOptions &socketOptions,
235  const Crt::Io::TlsContext &tlsContext,
236  bool useWebsocket) noexcept;
237 
239  aws_mqtt_client *client,
240  const char *hostName,
241  uint16_t port,
242  const Io::SocketOptions &socketOptions,
243  bool useWebsocket) noexcept;
244 
245  static void s_onConnectionInterrupted(aws_mqtt_client_connection *, int errorCode, void *userData);
246  static void s_onConnectionCompleted(
247  aws_mqtt_client_connection *,
248  int errorCode,
249  enum aws_mqtt_connect_return_code returnCode,
250  bool sessionPresent,
251  void *userData);
252  static void s_onConnectionResumed(
253  aws_mqtt_client_connection *,
254  ReturnCode returnCode,
255  bool sessionPresent,
256  void *userData);
257 
258  static void s_onDisconnect(aws_mqtt_client_connection *connection, void *userData);
259  static void s_onPublish(
260  aws_mqtt_client_connection *connection,
261  const aws_byte_cursor *topic,
262  const aws_byte_cursor *payload,
263  void *user_data);
264 
265  static void s_onSubAck(
266  aws_mqtt_client_connection *connection,
267  uint16_t packetId,
268  const struct aws_byte_cursor *topic,
269  enum aws_mqtt_qos qos,
270  int error_code,
271  void *userdata);
272  static void s_onMultiSubAck(
273  aws_mqtt_client_connection *connection,
274  uint16_t packetId,
275  const struct aws_array_list *topic_subacks,
276  int error_code,
277  void *userdata);
278  static void s_onOpComplete(
279  aws_mqtt_client_connection *connection,
280  uint16_t packetId,
281  int errorCode,
282  void *userdata);
283 
284  static void s_onWebsocketHandshake(
285  struct aws_http_message *request,
286  void *user_data,
287  aws_mqtt_transform_websocket_handshake_complete_fn *complete_fn,
288  void *complete_ctx);
289 
290  static void s_connectionInit(
291  MqttConnection *self,
292  const char *hostName,
293  uint16_t port,
294  const Io::SocketOptions &socketOptions);
295  };
296 
303  {
304  public:
308  MqttClient(Io::ClientBootstrap &bootstrap, Allocator *allocator = g_allocator) noexcept;
309 
310  ~MqttClient();
311  MqttClient(const MqttClient &) = delete;
312  MqttClient(MqttClient &&) noexcept;
313  MqttClient &operator=(const MqttClient &) = delete;
314  MqttClient &operator=(MqttClient &&) noexcept;
318  operator bool() const noexcept;
322  int LastError() const noexcept;
323 
328  std::shared_ptr<MqttConnection> NewConnection(
329  const char *hostName,
330  uint16_t port,
331  const Io::SocketOptions &socketOptions,
332  const Crt::Io::TlsContext &tlsContext,
333  bool useWebsocket = false) noexcept;
338  std::shared_ptr<MqttConnection> NewConnection(
339  const char *hostName,
340  uint16_t port,
341  const Io::SocketOptions &socketOptions,
342  bool useWebsocket = false) noexcept;
343 
344  private:
345  aws_mqtt_client *m_client;
346  };
347  } // namespace Mqtt
348  } // namespace Crt
349 } // namespace Aws
Aws::Crt::Allocator
aws_allocator Allocator
Definition: StlAllocator.h:25
Aws::Crt::Vector
std::vector< T, StlAllocator< T > > Vector
Definition: Types.h:66
Aws::Crt::Mqtt::MqttConnection::operator=
MqttConnection & operator=(MqttConnection &&)=delete
Types.h
Aws::Crt::Mqtt::OnOperationCompleteHandler
std::function< void(MqttConnection &connection, uint16_t packetId, int errorCode)> OnOperationCompleteHandler
Definition: MqttClient.h:93
AWS_CRT_CPP_API
#define AWS_CRT_CPP_API
Definition: Exports.h:34
Aws::Crt::Mqtt::OnPublishReceivedHandler
std::function< void(MqttConnection &connection, const String &topic, const ByteBuf &payload)> OnPublishReceivedHandler
Definition: MqttClient.h:90
Aws::Crt::Mqtt::OnMultiSubAckHandler
std::function< void(MqttConnection &connection, uint16_t packetId, const Vector< String > &topics, QOS qos, int errorCode)> OnMultiSubAckHandler
Definition: MqttClient.h:79
TlsOptions.h
error
Definition: cJSON.cpp:84
Aws::Crt::g_allocator
Allocator * g_allocator
Definition: Api.cpp:28
Aws::Crt::Mqtt::MqttConnection::MqttConnection
MqttConnection(MqttConnection &&)=delete
Aws::Crt::LastError
AWS_CRT_CPP_API int LastError() noexcept
Definition: Api.cpp:116
SocketOptions.h
Aws::Crt::ByteBuf
aws_byte_buf ByteBuf
Definition: Types.h:43
Aws::Crt::Mqtt::ReturnCode
aws_mqtt_connect_return_code ReturnCode
Definition: Types.h:54
Aws::Crt::Mqtt::MqttClient::MqttClient
MqttClient(const MqttClient &)=delete
Aws::Crt::Mqtt::OnWebSocketHandshakeIntercept
std::function< void(std::shared_ptr< Http::HttpRequest > req, const OnWebSocketHandshakeInterceptComplete &onComplete)> OnWebSocketHandshakeIntercept
Definition: MqttClient.h:109
HttpConnection.h
Aws::Crt::Mqtt::MqttConnection::MqttConnection
MqttConnection(const MqttConnection &)=delete
Aws
Definition: Api.h:25
Aws::Crt::Mqtt::OnSubAckHandler
std::function< void(MqttConnection &connection, uint16_t packetId, const String &topic, QOS qos, int errorCode)> OnSubAckHandler
Definition: MqttClient.h:69
Exports.h
Aws::Crt::Optional
Definition: Optional.h:32
StlAllocator.h
Aws::Crt::Mqtt::MqttConnection
Definition: MqttClient.h:118
Aws::Crt::String
std::basic_string< char, std::char_traits< char >, StlAllocator< char > > String
Definition: Types.h:58
Aws::Crt::Io::ClientBootstrap
Definition: Bootstrap.h:45
Aws::Crt::Mqtt::MqttClient
Definition: MqttClient.h:303
Aws::Crt::Mqtt::OnConnectionResumedHandler
std::function< void(MqttConnection &connection, ReturnCode connectCode, bool sessionPresent)> OnConnectionResumedHandler
Definition: MqttClient.h:57
Aws::Crt::Mqtt::OnWebSocketHandshakeInterceptComplete
std::function< void(const std::shared_ptr< Http::HttpRequest > &, int errorCode)> OnWebSocketHandshakeInterceptComplete
Definition: MqttClient.h:100
Aws::Crt::Mqtt::OnDisconnectHandler
std::function< void(MqttConnection &connection)> OnDisconnectHandler
Definition: MqttClient.h:84
Aws::Crt::Mqtt::MqttConnection::operator=
MqttConnection & operator=(const MqttConnection &)=delete
Aws::Crt::Mqtt::QOS
aws_mqtt_qos QOS
Definition: Types.h:53
Aws::Crt::Mqtt::OnConnectionCompletedHandler
std::function< void(MqttConnection &connection, int errorCode, ReturnCode returnCode, bool sessionPresent)> OnConnectionCompletedHandler
Definition: MqttClient.h:63
Aws::Crt::Mqtt::OnConnectionInterruptedHandler
std::function< void(MqttConnection &connection, int error)> OnConnectionInterruptedHandler
Definition: MqttClient.h:51