AWS IoT Device SDK C++ v2: crt/aws-crt-cpp/include/aws/iot/MqttClient.h Source File

AWS IoT Device SDK C++ v2 1.14.0 AWS IoT Device SDK C++ v2

MqttClient.h
Go to the documentation of this file.
1#pragma once
6#include <aws/crt/Config.h>
7#include <aws/crt/Exports.h>
8#include <aws/crt/auth/Sigv4Signing.h>
9#include <aws/crt/mqtt/MqttClient.h>
10
11#if !BYO_CRYPTO
12
13namespace Aws
14{
15 namespace Iot
16 {
17 class MqttClient;
18
24 class AWS_CRT_CPP_API MqttClientConnectionConfig final
25 {
26 public:
27 static MqttClientConnectionConfig CreateInvalid(int lastError) noexcept;
28
37 MqttClientConnectionConfig(
38 const Crt::String &endpoint,
39 uint16_t port,
40 const Crt::Io::SocketOptions &socketOptions,
41 Crt::Io::TlsContext &&tlsContext);
42
60 MqttClientConnectionConfig(
61 const Crt::String &endpoint,
62 uint16_t port,
63 const Crt::Io::SocketOptions &socketOptions,
64 Crt::Io::TlsContext &&tlsContext,
65 Crt::Mqtt::OnWebSocketHandshakeIntercept &&interceptor,
66 const Crt::Optional<Crt::Http::HttpClientConnectionProxyOptions> &proxyOptions);
67
71 explicit operator bool() const noexcept { return m_context ? true : false; }
72
76 int LastError() const noexcept { return m_lastError; }
77
78 private:
79 MqttClientConnectionConfig(int lastError) noexcept;
80
81 MqttClientConnectionConfig(
82 const Crt::String &endpoint,
83 uint16_t port,
84 const Crt::Io::SocketOptions &socketOptions,
85 Crt::Io::TlsContext &&tlsContext,
86 const Crt::Optional<Crt::Http::HttpClientConnectionProxyOptions> &proxyOptions);
87
88 Crt::String m_endpoint;
89 uint16_t m_port;
90 Crt::Io::TlsContext m_context;
91 Crt::Io::SocketOptions m_socketOptions;
92 Crt::Mqtt::OnWebSocketHandshakeIntercept m_webSocketInterceptor;
93 Crt::String m_username;
94 Crt::String m_password;
95 Crt::Optional<Crt::Http::HttpClientConnectionProxyOptions> m_proxyOptions;
96 int m_lastError;
97
98 friend class MqttClient;
99 friend class MqttClientConnectionConfigBuilder;
100 };
101
102 using CreateSigningConfig = std::function<std::shared_ptr<Crt::Auth::ISigningConfig>(void)>;
103
107 struct AWS_CRT_CPP_API WebsocketConfig
108 {
118 WebsocketConfig(
119 const Crt::String &signingRegion,
120 Crt::Io::ClientBootstrap *bootstrap,
121 Crt::Allocator *allocator = Crt::g_allocator) noexcept;
122
131 WebsocketConfig(
132 const Crt::String &signingRegion,
133 const std::shared_ptr<Crt::Auth::ICredentialsProvider> &credentialsProvider,
134 Crt::Allocator *allocator = Crt::g_allocator) noexcept;
135
149 WebsocketConfig(
150 const std::shared_ptr<Crt::Auth::ICredentialsProvider> &credentialsProvider,
151 const std::shared_ptr<Crt::Auth::IHttpRequestSigner> &signer,
152 CreateSigningConfig createSigningConfig) noexcept;
153
154 std::shared_ptr<Crt::Auth::ICredentialsProvider> CredentialsProvider;
155 std::shared_ptr<Crt::Auth::IHttpRequestSigner> Signer;
156 CreateSigningConfig CreateSigningConfigCb;
157
164 Crt::Optional<Crt::Http::HttpClientConnectionProxyOptions> ProxyOptions;
165 Crt::String SigningRegion;
166 Crt::String ServiceName;
167 };
168
174 class AWS_CRT_CPP_API MqttClientConnectionConfigBuilder final
175 {
176 public:
177 MqttClientConnectionConfigBuilder();
178
186 MqttClientConnectionConfigBuilder(
187 const char *certPath,
188 const char *pkeyPath,
189 Crt::Allocator *allocator = Crt::g_allocator) noexcept;
190
198 MqttClientConnectionConfigBuilder(
199 const Crt::ByteCursor &cert,
200 const Crt::ByteCursor &pkey,
201 Crt::Allocator *allocator = Crt::g_allocator) noexcept;
202
206 MqttClientConnectionConfigBuilder(
207 const Crt::Io::TlsContextPkcs11Options &pkcs11Options,
208 Crt::Allocator *allocator = Crt::g_allocator) noexcept;
209
215 MqttClientConnectionConfigBuilder(
216 const WebsocketConfig &config,
217 Crt::Allocator *allocator = Crt::g_allocator) noexcept;
218
226 MqttClientConnectionConfigBuilder &WithEndpoint(const Crt::String &endpoint);
227
235 MqttClientConnectionConfigBuilder &WithEndpoint(Crt::String &&endpoint);
236
246 MqttClientConnectionConfigBuilder &WithPortOverride(uint16_t port) noexcept;
247
256 MqttClientConnectionConfigBuilder &WithCertificateAuthority(const char *caPath) noexcept;
257
266 MqttClientConnectionConfigBuilder &WithCertificateAuthority(const Crt::ByteCursor &cert) noexcept;
267
273 MqttClientConnectionConfigBuilder &WithTcpKeepAlive() noexcept;
274
282 MqttClientConnectionConfigBuilder &WithTcpConnectTimeout(uint32_t connectTimeoutMs) noexcept;
283
292 MqttClientConnectionConfigBuilder &WithTcpKeepAliveTimeout(uint16_t keepAliveTimeoutSecs) noexcept;
293
303 MqttClientConnectionConfigBuilder &WithTcpKeepAliveInterval(uint16_t keepAliveIntervalSecs) noexcept;
304
313 MqttClientConnectionConfigBuilder &WithTcpKeepAliveMaxProbes(uint16_t maxProbes) noexcept;
314
322 MqttClientConnectionConfigBuilder &WithMinimumTlsVersion(aws_tls_versions minimumTlsVersion) noexcept;
323
331 MqttClientConnectionConfigBuilder &WithHttpProxyOptions(
332 const Crt::Http::HttpClientConnectionProxyOptions &proxyOptions) noexcept;
333
342 MqttClientConnectionConfigBuilder &WithMetricsCollection(bool enabled);
343
351 MqttClientConnectionConfigBuilder &WithSdkName(const Crt::String &sdkName);
352
360 MqttClientConnectionConfigBuilder &WithSdkVersion(const Crt::String &sdkVersion);
361
367 MqttClientConnectionConfig Build() noexcept;
368
372 explicit operator bool() const noexcept { return m_lastError == 0; }
373
377 int LastError() const noexcept { return m_lastError ? m_lastError : AWS_ERROR_UNKNOWN; }
378
379 private:
380 // Common setup shared by all valid constructors
381 MqttClientConnectionConfigBuilder(Crt::Allocator *allocator) noexcept;
382
383 Crt::Allocator *m_allocator;
384 Crt::String m_endpoint;
385 uint16_t m_portOverride;
386 Crt::Io::SocketOptions m_socketOptions;
387 Crt::Io::TlsContextOptions m_contextOptions;
388 Crt::Optional<WebsocketConfig> m_websocketConfig;
389 Crt::Optional<Crt::Http::HttpClientConnectionProxyOptions> m_proxyOptions;
390 bool m_enableMetricsCollection = true;
391 Crt::String m_sdkName = "CPPv2";
392 Crt::String m_sdkVersion = AWS_CRT_CPP_VERSION;
393
394 int m_lastError;
395 };
396
402 class AWS_CRT_CPP_API MqttClient final
403 {
404 public:
405 MqttClient(Crt::Io::ClientBootstrap &bootstrap, Crt::Allocator *allocator = Crt::g_allocator) noexcept;
406
412 std::shared_ptr<Crt::Mqtt::MqttConnection> NewConnection(const MqttClientConnectionConfig &config) noexcept;
413
417 int LastError() const noexcept { return m_client.LastError(); }
418
422 explicit operator bool() const noexcept { return m_client ? true : false; }
423
424 private:
425 Crt::Mqtt::MqttClient m_client;
426 int m_lastError;
427 };
428 } // namespace Iot
429} // namespace Aws
430
431#endif // !BYO_CRYPTO
Config.h
AWS_CRT_CPP_VERSION
#define AWS_CRT_CPP_VERSION
Definition: Config.h:6
Sigv4Signing.h
Aws::Crt::Http::HttpClientConnectionProxyOptions
Definition: HttpConnection.h:270
Aws::Crt::Io::ClientBootstrap
Definition: Bootstrap.h:35
Aws::Crt::Io::SocketOptions
Definition: SocketOptions.h:48
Aws::Crt::Io::TlsContext
Definition: TlsOptions.h:331
Aws::Crt::Io::TlsContextOptions
Definition: TlsOptions.h:36
Aws::Crt::Io::TlsContextPkcs11Options
Definition: TlsOptions.h:198
Aws::Crt::Mqtt::MqttClient
Definition: MqttClient.h:423
Aws::Crt::Optional
Definition: Optional.h:17
Aws::Iot::MqttClientConnectionConfigBuilder
Definition: MqttClient.h:175
Aws::Iot::MqttClientConnectionConfigBuilder::LastError
int LastError() const noexcept
Definition: MqttClient.h:377
Aws::Iot::MqttClientConnectionConfig
Definition: MqttClient.h:25
Aws::Iot::MqttClientConnectionConfig::LastError
int LastError() const noexcept
Definition: MqttClient.h:76
Aws::Iot::MqttClient
Definition: MqttClient.h:403
Aws::Iot::MqttClient::LastError
int LastError() const noexcept
Definition: MqttClient.h:417
Exports.h
AWS_CRT_CPP_API
#define AWS_CRT_CPP_API
Definition: Exports.h:37
MqttClient.h
Aws::Crt::Mqtt::OnWebSocketHandshakeIntercept
std::function< void(std::shared_ptr< Http::HttpRequest > req, const OnWebSocketHandshakeInterceptComplete &onComplete)> OnWebSocketHandshakeIntercept
Definition: MqttClient.h:122
Aws::Crt::ByteCursor
aws_byte_cursor ByteCursor
Definition: Types.h:33
Aws::Crt::Allocator
aws_allocator Allocator
Definition: StlAllocator.h:17
Aws::Crt::g_allocator
AWS_CRT_CPP_API Allocator * g_allocator
Definition: Api.cpp:21
Aws::Crt::String
std::basic_string< char, std::char_traits< char >, StlAllocator< char > > String
Definition: Types.h:47
Aws::Iot::CreateSigningConfig
std::function< std::shared_ptr< Crt::Auth::ISigningConfig >(void)> CreateSigningConfig
Definition: MqttClient.h:102
Aws
Definition: Api.h:17
Aws::true
return true
Definition: cJSON.cpp:2306
Aws::Iot::WebsocketConfig
Definition: MqttClient.h:108
Aws::Iot::WebsocketConfig::Signer
std::shared_ptr< Crt::Auth::IHttpRequestSigner > Signer
Definition: MqttClient.h:155
Aws::Iot::WebsocketConfig::ServiceName
Crt::String ServiceName
Definition: MqttClient.h:166
Aws::Iot::WebsocketConfig::SigningRegion
Crt::String SigningRegion
Definition: MqttClient.h:165
Aws::Iot::WebsocketConfig::ProxyOptions
Crt::Optional< Crt::Http::HttpClientConnectionProxyOptions > ProxyOptions
Definition: MqttClient.h:164
Aws::Iot::WebsocketConfig::CreateSigningConfigCb
CreateSigningConfig CreateSigningConfigCb
Definition: MqttClient.h:156
Aws::Iot::WebsocketConfig::CredentialsProvider
std::shared_ptr< Crt::Auth::ICredentialsProvider > CredentialsProvider
Definition: MqttClient.h:154