aws-crt-cpp
C++ wrapper around the aws-c-* libraries. Provides Cross-Platform Transport Protocols and SSL/TLS implementations for C++.
Mqtt5ClientCore.h
Go to the documentation of this file.
1 
5 #pragma once
6 
13 
14 #include <mutex>
15 
16 namespace Aws
17 {
18  namespace Crt
19  {
20  namespace Mqtt5
21  {
28  class AWS_CRT_CPP_API Mqtt5ClientCore final : public std::enable_shared_from_this<Mqtt5ClientCore>
29  {
30  friend class Mqtt5Client;
31  friend class Mqtt::MqttConnection;
32 
33  public:
41  static std::shared_ptr<Mqtt5ClientCore> NewMqtt5ClientCore(
42  const Mqtt5ClientOptions &options,
43  Allocator *allocator = ApiAllocator()) noexcept;
44 
51  std::shared_ptr<Mqtt5ClientCore> getptr() { return shared_from_this(); }
52 
56  operator bool() const noexcept;
57 
61  int LastError() const noexcept;
62 
71  bool Publish(
72  std::shared_ptr<PublishPacket> publishOptions,
73  OnPublishCompletionHandler onPublishCompletionCallback = NULL) noexcept;
74 
83  bool Subscribe(
84  std::shared_ptr<SubscribePacket> subscribeOptions,
85  OnSubscribeCompletionHandler onSubscribeCompletionCallback = NULL) noexcept;
86 
95  bool Unsubscribe(
96  std::shared_ptr<UnsubscribePacket> unsubscribeOptions,
97  OnUnsubscribeCompletionHandler onUnsubscribeCompletionCallback = NULL) noexcept;
98 
106  bool InvokePublishAcknowledgement(
107  const PublishAcknowledgementHandle &publishAcknowledgementHandle) noexcept;
108 
118  void Close() noexcept;
119 
120  ~Mqtt5ClientCore();
121 
122  struct aws_mqtt5_client *GetUnderlyingHandle() const noexcept { return m_client; }
123 
124  private:
125  Mqtt5ClientCore(const Mqtt5ClientOptions &options, Allocator *allocator = ApiAllocator()) noexcept;
126 
135  std::shared_ptr<Crt::Mqtt::MqttConnection> NewConnection(
136  const Mqtt5::Mqtt5to3AdapterOptions *options) noexcept;
137 
138  /* Static Callbacks */
139  static void s_publishCompletionCallback(
140  enum aws_mqtt5_packet_type packet_type,
141  const void *packet,
142  int error_code,
143  void *complete_ctx);
144 
145  static void s_subscribeCompletionCallback(
146  const struct aws_mqtt5_packet_suback_view *puback,
147  int error_code,
148  void *complete_ctx);
149 
150  static void s_unsubscribeCompletionCallback(
151  const struct aws_mqtt5_packet_unsuback_view *puback,
152  int error_code,
153  void *complete_ctx);
154 
155  static void s_lifeCycleEventCallback(const aws_mqtt5_client_lifecycle_event *event);
156 
157  static void s_publishReceivedCallback(const aws_mqtt5_packet_publish_view *publish, void *user_data);
158 
159  static void s_onWebsocketHandshake(
160  aws_http_message *rawRequest,
161  void *user_data,
162  aws_mqtt5_transform_websocket_handshake_complete_fn *complete_fn,
163  void *complete_ctx);
164 
165  static void s_clientTerminationCompletion(void *complete_ctx);
166 
167  /* The handler is set by clientoptions */
168  OnWebSocketHandshakeIntercept websocketInterceptor;
172  OnConnectionSuccessHandler onConnectionSuccess;
173 
177  OnConnectionFailureHandler onConnectionFailure;
178 
182  OnDisconnectionHandler onDisconnection;
183 
187  OnStoppedHandler onStopped;
188 
192  OnAttemptingConnectHandler onAttemptingConnect;
193 
197  OnPublishReceivedHandler onPublishReceived;
198 
203  std::shared_ptr<Mqtt5ClientCore> m_selfReference;
204 
205  /*
206  * The Mqtt5to3 Adapter Options. Used to create a mqtt311 connection from mqtt5 client
207  */
208  ScopedResource<Mqtt5to3AdapterOptions> m_mqtt5to3AdapterOptions;
209 
210  /*
211  * The callback flag used to indicate if it is safe to invoke the callbacks
212  */
213  enum CallbackFlag
214  {
215  INVOKE,
216  IGNORE
217  } m_callbackFlag;
218 
219  /*
220  * Lock for the callbacks. This is used to protect the callback flag and callbacks.
221  */
222  std::recursive_mutex m_callback_lock;
223 
224  aws_mqtt5_client *m_client;
225  Allocator *m_allocator;
226  };
227 
231  class Mqtt5to3AdapterOptions
232  {
233  friend class Mqtt5ClientOptions;
234  friend class Mqtt5ClientCore;
235  friend class Mqtt::MqttConnection;
236 
237  public:
238  /* Default constructor */
239  Mqtt5to3AdapterOptions();
240  /*
241  * Allocate and create a new Mqtt5to3AdapterOptions. This function is internally used by Mqtt5Client to
242  * support the Mqtt5to3Adapter.
243  *
244  * @return Mqtt5to3AdapterOptions
245  */
246  static ScopedResource<Mqtt5to3AdapterOptions> NewMqtt5to3AdapterOptions(
247  const Mqtt5ClientOptions &options) noexcept;
248 
249  private:
250  Mqtt::MqttConnectionOptions m_mqtt3Options;
251 
252  /* Reserve to store memory for m_mqtt3options.hostname */
253  String m_hostname;
254 
255  /*
256  * The transform function invoked during websocket handshake.
257  */
258  Crt::Mqtt::OnWebSocketHandshakeIntercept m_webSocketInterceptor;
259 
260  /* Store the user intercept handshake function */
261  OnWebSocketHandshakeIntercept m_websocketHandshakeTransform;
262 
266  Crt::Optional<Crt::Http::HttpClientConnectionProxyOptions> m_proxyOptions;
267  };
268 
269  } // namespace Mqtt5
270  } // namespace Crt
271 } // namespace Aws
#define AWS_CRT_CPP_API
Definition: Exports.h:36
std::function< void(const OnStoppedEventData &)> OnStoppedHandler
Definition: Mqtt5Client.h:309
std::function< void(const OnConnectionFailureEventData &)> OnConnectionFailureHandler
Definition: Mqtt5Client.h:292
Definition: StringView.h:861
std::basic_string< char, std::char_traits< char >, StlAllocator< char >> String
Definition: Types.h:45
aws_allocator Allocator
Definition: Allocator.h:14
std::function< void(std::shared_ptr< Http::HttpRequest > req, const OnWebSocketHandshakeInterceptComplete &onComplete)> OnWebSocketHandshakeIntercept
Definition: MqttConnection.h:146
std::function< void(int, std::shared_ptr< UnSubAckPacket >)> OnUnsubscribeCompletionHandler
Definition: Mqtt5Client.h:324
std::function< void(const OnConnectionSuccessEventData &)> OnConnectionSuccessHandler
Definition: Mqtt5Client.h:287
AWS_CRT_CPP_API Allocator * ApiAllocator() noexcept
Definition: Allocator.cpp:24
std::function< void(int, std::shared_ptr< PublishResult >)> OnPublishCompletionHandler
Definition: Mqtt5Client.h:314
AWS_CRT_CPP_API int LastError() noexcept
Definition: Api.cpp:464
std::function< void(int, std::shared_ptr< SubAckPacket >)> OnSubscribeCompletionHandler
Definition: Mqtt5Client.h:319
std::function< void(const OnDisconnectionEventData &)> OnDisconnectionHandler
Definition: Mqtt5Client.h:297
std::unique_ptr< T, std::function< void(T *)>> ScopedResource
Definition: Types.h:163
std::function< void(const OnAttemptingConnectEventData &)> OnAttemptingConnectHandler
Definition: Mqtt5Client.h:303
std::function< void(const PublishReceivedEventData &)> OnPublishReceivedHandler
Definition: Mqtt5Client.h:337
std::function< void(std::shared_ptr< Http::HttpRequest >, const OnWebSocketHandshakeInterceptComplete &)> OnWebSocketHandshakeIntercept
Definition: Mqtt5Client.h:353
Definition: Allocator.h:10