aws-crt-cpp
C++ wrapper around the aws-c-* libraries. Provides Cross-Platform Transport Protocols and SSL/TLS implementations for C++.
MqttClient.h
Go to the documentation of this file.
1#pragma once
6#include <aws/crt/Config.h>
7#include <aws/crt/Exports.h>
10
11#if !BYO_CRYPTO
12
13namespace Aws
14{
15 namespace Iot
16 {
17 class MqttClient;
18
25 {
26 public:
27 static MqttClientConnectionConfig CreateInvalid(int lastError) noexcept;
28
38 const Crt::String &endpoint,
39 uint16_t port,
40 const Crt::Io::SocketOptions &socketOptions,
41 Crt::Io::TlsContext &&tlsContext);
42
61 const Crt::String &endpoint,
62 uint16_t port,
63 const Crt::Io::SocketOptions &socketOptions,
64 Crt::Io::TlsContext &&tlsContext,
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
82 const Crt::String &endpoint,
83 uint16_t port,
84 const Crt::Io::SocketOptions &socketOptions,
85 Crt::Io::TlsContext &&tlsContext,
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;
96 int m_lastError;
97
98 friend class MqttClient;
100 };
101
102 using CreateSigningConfig = std::function<std::shared_ptr<Crt::Auth::ISigningConfig>(void)>;
103
108 {
119 const Crt::String &signingRegion,
120 Crt::Io::ClientBootstrap *bootstrap,
121 Crt::Allocator *allocator = Crt::g_allocator) noexcept;
122
133 WebsocketConfig(const Crt::String &signingRegion, Crt::Allocator *allocator = Crt::g_allocator) noexcept;
134
144 const Crt::String &signingRegion,
145 const std::shared_ptr<Crt::Auth::ICredentialsProvider> &credentialsProvider,
146 Crt::Allocator *allocator = Crt::g_allocator) noexcept;
147
162 const std::shared_ptr<Crt::Auth::ICredentialsProvider> &credentialsProvider,
163 const std::shared_ptr<Crt::Auth::IHttpRequestSigner> &signer,
164 CreateSigningConfig createSigningConfig) noexcept;
165
166 std::shared_ptr<Crt::Auth::ICredentialsProvider> CredentialsProvider;
167 std::shared_ptr<Crt::Auth::IHttpRequestSigner> Signer;
169
179 };
180
187 {
188 public:
190
199 const char *certPath,
200 const char *pkeyPath,
201 Crt::Allocator *allocator = Crt::g_allocator) noexcept;
202
211 const Crt::ByteCursor &cert,
212 const Crt::ByteCursor &pkey,
213 Crt::Allocator *allocator = Crt::g_allocator) noexcept;
214
221 const Crt::Io::TlsContextPkcs11Options &pkcs11Options,
222 Crt::Allocator *allocator = Crt::g_allocator) noexcept;
223
235 const char *windowsCertStorePath,
236 Crt::Allocator *allocator = Crt::g_allocator) noexcept;
237
244 const WebsocketConfig &config,
245 Crt::Allocator *allocator = Crt::g_allocator) noexcept;
246
254 MqttClientConnectionConfigBuilder &WithEndpoint(const Crt::String &endpoint);
255
263 MqttClientConnectionConfigBuilder &WithEndpoint(Crt::String &&endpoint);
264
274 MqttClientConnectionConfigBuilder &WithPortOverride(uint16_t port) noexcept;
275
284 MqttClientConnectionConfigBuilder &WithCertificateAuthority(const char *caPath) noexcept;
285
294 MqttClientConnectionConfigBuilder &WithCertificateAuthority(const Crt::ByteCursor &cert) noexcept;
295
301 MqttClientConnectionConfigBuilder &WithTcpKeepAlive() noexcept;
302
310 MqttClientConnectionConfigBuilder &WithTcpConnectTimeout(uint32_t connectTimeoutMs) noexcept;
311
320 MqttClientConnectionConfigBuilder &WithTcpKeepAliveTimeout(uint16_t keepAliveTimeoutSecs) noexcept;
321
331 MqttClientConnectionConfigBuilder &WithTcpKeepAliveInterval(uint16_t keepAliveIntervalSecs) noexcept;
332
341 MqttClientConnectionConfigBuilder &WithTcpKeepAliveMaxProbes(uint16_t maxProbes) noexcept;
342
350 MqttClientConnectionConfigBuilder &WithMinimumTlsVersion(aws_tls_versions minimumTlsVersion) noexcept;
351
359 MqttClientConnectionConfigBuilder &WithHttpProxyOptions(
360 const Crt::Http::HttpClientConnectionProxyOptions &proxyOptions) noexcept;
361
370 MqttClientConnectionConfigBuilder &WithMetricsCollection(bool enabled);
371
379 MqttClientConnectionConfigBuilder &WithSdkName(const Crt::String &sdkName);
380
388 MqttClientConnectionConfigBuilder &WithSdkVersion(const Crt::String &sdkVersion);
389
395 MqttClientConnectionConfig Build() noexcept;
396
400 explicit operator bool() const noexcept { return m_lastError == 0; }
401
405 int LastError() const noexcept { return m_lastError ? m_lastError : AWS_ERROR_UNKNOWN; }
406
407 private:
408 // Common setup shared by all valid constructors
410
411 Crt::Allocator *m_allocator;
412 Crt::String m_endpoint;
413 uint16_t m_portOverride;
414 Crt::Io::SocketOptions m_socketOptions;
415 Crt::Io::TlsContextOptions m_contextOptions;
416 Crt::Optional<WebsocketConfig> m_websocketConfig;
418 bool m_enableMetricsCollection = true;
419 Crt::String m_sdkName = "CPPv2";
420 Crt::String m_sdkVersion = AWS_CRT_CPP_VERSION;
421
422 int m_lastError;
423 };
424
431 {
432 public:
433 MqttClient(Crt::Io::ClientBootstrap &bootstrap, Crt::Allocator *allocator = Crt::g_allocator) noexcept;
434
441 MqttClient(Crt::Allocator *allocator = Crt::g_allocator) noexcept;
442
448 std::shared_ptr<Crt::Mqtt::MqttConnection> NewConnection(const MqttClientConnectionConfig &config) noexcept;
449
453 int LastError() const noexcept { return m_client.LastError(); }
454
458 explicit operator bool() const noexcept { return m_client ? true : false; }
459
460 private:
461 Crt::Mqtt::MqttClient m_client;
462 int m_lastError;
463 };
464 } // namespace Iot
465} // namespace Aws
466
467#endif // !BYO_CRYPTO
#define AWS_CRT_CPP_VERSION
Definition: Config.h:6
#define AWS_CRT_CPP_API
Definition: Exports.h:37
Definition: HttpConnection.h:270
Definition: Bootstrap.h:35
Definition: SocketOptions.h:48
Definition: TlsOptions.h:343
Definition: TlsOptions.h:36
Definition: TlsOptions.h:210
Definition: MqttClient.h:423
Definition: Optional.h:17
int LastError() const noexcept
Definition: MqttClient.h:405
Definition: MqttClient.h:25
int LastError() const noexcept
Definition: MqttClient.h:76
Definition: MqttClient.h:431
int LastError() const noexcept
Definition: MqttClient.h:453
std::function< void(std::shared_ptr< Http::HttpRequest > req, const OnWebSocketHandshakeInterceptComplete &onComplete)> OnWebSocketHandshakeIntercept
Definition: MqttClient.h:122
aws_byte_cursor ByteCursor
Definition: Types.h:33
aws_allocator Allocator
Definition: StlAllocator.h:17
AWS_CRT_CPP_API Allocator * g_allocator
Definition: Api.cpp:23
std::basic_string< char, std::char_traits< char >, StlAllocator< char > > String
Definition: Types.h:47
std::function< std::shared_ptr< Crt::Auth::ISigningConfig >(void)> CreateSigningConfig
Definition: MqttClient.h:102
Definition: Api.h:14
return true
Definition: cJSON.cpp:2306
Definition: MqttClient.h:108
std::shared_ptr< Crt::Auth::IHttpRequestSigner > Signer
Definition: MqttClient.h:167
Crt::String ServiceName
Definition: MqttClient.h:178
Crt::String SigningRegion
Definition: MqttClient.h:177
Crt::Optional< Crt::Http::HttpClientConnectionProxyOptions > ProxyOptions
Definition: MqttClient.h:176
CreateSigningConfig CreateSigningConfigCb
Definition: MqttClient.h:168
std::shared_ptr< Crt::Auth::ICredentialsProvider > CredentialsProvider
Definition: MqttClient.h:166