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 
108  void Close() noexcept;
109 
110  ~Mqtt5ClientCore();
111 
112  struct aws_mqtt5_client *GetUnderlyingHandle() const noexcept { return m_client; }
113 
114  private:
115  Mqtt5ClientCore(const Mqtt5ClientOptions &options, Allocator *allocator = ApiAllocator()) noexcept;
116 
125  std::shared_ptr<Crt::Mqtt::MqttConnection> NewConnection(
126  const Mqtt5::Mqtt5to3AdapterOptions *options) noexcept;
127 
128  /* Static Callbacks */
129  static void s_publishCompletionCallback(
130  enum aws_mqtt5_packet_type packet_type,
131  const void *packet,
132  int error_code,
133  void *complete_ctx);
134 
135  static void s_subscribeCompletionCallback(
136  const struct aws_mqtt5_packet_suback_view *puback,
137  int error_code,
138  void *complete_ctx);
139 
140  static void s_unsubscribeCompletionCallback(
141  const struct aws_mqtt5_packet_unsuback_view *puback,
142  int error_code,
143  void *complete_ctx);
144 
145  static void s_lifeCycleEventCallback(const aws_mqtt5_client_lifecycle_event *event);
146 
147  static void s_publishReceivedCallback(const aws_mqtt5_packet_publish_view *publish, void *user_data);
148 
149  static void s_onWebsocketHandshake(
150  aws_http_message *rawRequest,
151  void *user_data,
152  aws_mqtt5_transform_websocket_handshake_complete_fn *complete_fn,
153  void *complete_ctx);
154 
155  static void s_clientTerminationCompletion(void *complete_ctx);
156 
157  /* The handler is set by clientoptions */
158  OnWebSocketHandshakeIntercept websocketInterceptor;
162  OnConnectionSuccessHandler onConnectionSuccess;
163 
167  OnConnectionFailureHandler onConnectionFailure;
168 
172  OnDisconnectionHandler onDisconnection;
173 
177  OnStoppedHandler onStopped;
178 
182  OnAttemptingConnectHandler onAttemptingConnect;
183 
187  OnPublishReceivedHandler onPublishReceived;
188 
193  std::shared_ptr<Mqtt5ClientCore> m_selfReference;
194 
195  /*
196  * The Mqtt5to3 Adapter Options. Used to create a mqtt311 connection from mqtt5 client
197  */
198  ScopedResource<Mqtt5to3AdapterOptions> m_mqtt5to3AdapterOptions;
199 
200  /*
201  * The callback flag used to indicate if it is safe to invoke the callbacks
202  */
203  enum CallbackFlag
204  {
205  INVOKE,
206  IGNORE
207  } m_callbackFlag;
208 
209  /*
210  * Lock for the callbacks. This is used to protect the callback flag and callbacks.
211  */
212  std::recursive_mutex m_callback_lock;
213 
214  aws_mqtt5_client *m_client;
215  Allocator *m_allocator;
216  };
217 
221  class Mqtt5to3AdapterOptions
222  {
223  friend class Mqtt5ClientOptions;
224  friend class Mqtt5ClientCore;
225  friend class Mqtt::MqttConnection;
226 
227  public:
228  /* Default constructor */
229  Mqtt5to3AdapterOptions();
230  /*
231  * Allocate and create a new Mqtt5to3AdapterOptions. This function is internally used by Mqtt5Client to
232  * support the Mqtt5to3Adapter.
233  *
234  * @return Mqtt5to3AdapterOptions
235  */
236  static ScopedResource<Mqtt5to3AdapterOptions> NewMqtt5to3AdapterOptions(
237  const Mqtt5ClientOptions &options) noexcept;
238 
239  private:
240  Mqtt::MqttConnectionOptions m_mqtt3Options;
241 
242  /* Reserve to store memory for m_mqtt3options.hostname */
243  String m_hostname;
244 
245  /*
246  * The transform function invoked during websocket handshake.
247  */
248  Crt::Mqtt::OnWebSocketHandshakeIntercept m_webSocketInterceptor;
249 
250  /* Store the user intercept handshake function */
251  OnWebSocketHandshakeIntercept m_websocketHandshakeTransform;
252 
256  Crt::Optional<Crt::Http::HttpClientConnectionProxyOptions> m_proxyOptions;
257  };
258 
259  } // namespace Mqtt5
260  } // namespace Crt
261 } // namespace Aws
#define AWS_CRT_CPP_API
Definition: Exports.h:36
std::function< void(const OnStoppedEventData &)> OnStoppedHandler
Definition: Mqtt5Client.h:288
std::function< void(const OnConnectionFailureEventData &)> OnConnectionFailureHandler
Definition: Mqtt5Client.h:271
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:303
std::function< void(const OnConnectionSuccessEventData &)> OnConnectionSuccessHandler
Definition: Mqtt5Client.h:266
AWS_CRT_CPP_API Allocator * ApiAllocator() noexcept
Definition: Allocator.cpp:24
std::function< void(int, std::shared_ptr< PublishResult >)> OnPublishCompletionHandler
Definition: Mqtt5Client.h:293
AWS_CRT_CPP_API int LastError() noexcept
Definition: Api.cpp:425
std::function< void(int, std::shared_ptr< SubAckPacket >)> OnSubscribeCompletionHandler
Definition: Mqtt5Client.h:298
std::function< void(const OnDisconnectionEventData &)> OnDisconnectionHandler
Definition: Mqtt5Client.h:276
std::unique_ptr< T, std::function< void(T *)>> ScopedResource
Definition: Types.h:163
std::function< void(const OnAttemptingConnectEventData &)> OnAttemptingConnectHandler
Definition: Mqtt5Client.h:282
std::function< void(const PublishReceivedEventData &)> OnPublishReceivedHandler
Definition: Mqtt5Client.h:308
std::function< void(std::shared_ptr< Http::HttpRequest >, const OnWebSocketHandshakeInterceptComplete &)> OnWebSocketHandshakeIntercept
Definition: Mqtt5Client.h:324
Definition: Allocator.h:10