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
132 const Crt::String &signingRegion,
133 const std::shared_ptr<Crt::Auth::ICredentialsProvider> &credentialsProvider,
134 Crt::Allocator *allocator = Crt::g_allocator) noexcept;
135
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;
157
167 };
168
175 {
176 public:
178
187 const char *certPath,
188 const char *pkeyPath,
189 Crt::Allocator *allocator = Crt::g_allocator) noexcept;
190
199 const Crt::ByteCursor &cert,
200 const Crt::ByteCursor &pkey,
201 Crt::Allocator *allocator = Crt::g_allocator) noexcept;
202
207 const Crt::Io::TlsContextPkcs11Options &pkcs11Options,
208 Crt::Allocator *allocator = Crt::g_allocator) noexcept;
209
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
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;
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
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
#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:331
Definition: TlsOptions.h:36
Definition: TlsOptions.h:198
Definition: MqttClient.h:423
Definition: Optional.h:17
int LastError() const noexcept
Definition: MqttClient.h:377
Definition: MqttClient.h:25
int LastError() const noexcept
Definition: MqttClient.h:76
Definition: MqttClient.h:403
int LastError() const noexcept
Definition: MqttClient.h:417
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:21
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:17
return true
Definition: cJSON.cpp:2306
Definition: MqttClient.h:108
std::shared_ptr< Crt::Auth::IHttpRequestSigner > Signer
Definition: MqttClient.h:155
Crt::String ServiceName
Definition: MqttClient.h:166
Crt::String SigningRegion
Definition: MqttClient.h:165
Crt::Optional< Crt::Http::HttpClientConnectionProxyOptions > ProxyOptions
Definition: MqttClient.h:164
CreateSigningConfig CreateSigningConfigCb
Definition: MqttClient.h:156
std::shared_ptr< Crt::Auth::ICredentialsProvider > CredentialsProvider
Definition: MqttClient.h:154