aws-crt-cpp
C++ wrapper around the aws-c-* libraries. Provides Cross-Platform Transport Protocols and SSL/TLS implementations for C++.
Loading...
Searching...
No Matches
MqttClient.h
Go to the documentation of this file.
1#pragma once
6#include <aws/crt/Config.h>
7#include <aws/crt/Exports.h>
11#include <aws/iot/MqttCommon.h>
12
13#if !BYO_CRYPTO
14
15namespace Aws
16{
17 namespace Iot
18 {
19 class MqttClient;
20
27 {
28 public:
29 static MqttClientConnectionConfig CreateInvalid(int lastError) noexcept;
30
42 const Crt::String &endpoint,
43 uint32_t port,
44 const Crt::Io::SocketOptions &socketOptions,
45 Crt::Io::TlsContext &&tlsContext,
46 bool enableMetrics = true,
48
69 const Crt::String &endpoint,
70 uint32_t port,
71 const Crt::Io::SocketOptions &socketOptions,
72 Crt::Io::TlsContext &&tlsContext,
75 bool enableMetrics = true,
77
81 explicit operator bool() const noexcept { return m_context ? true : false; }
82
86 int LastError() const noexcept { return m_lastError; }
87
88 private:
89 MqttClientConnectionConfig(int lastError) noexcept;
90
92 const Crt::String &endpoint,
93 uint32_t port,
94 const Crt::Io::SocketOptions &socketOptions,
95 Crt::Io::TlsContext &&tlsContext,
97 bool enableMetrics = true,
99
100 Crt::String m_endpoint;
101 uint32_t m_port;
102 Crt::Io::TlsContext m_context;
103 Crt::Io::SocketOptions m_socketOptions;
104 Crt::Mqtt::OnWebSocketHandshakeIntercept m_webSocketInterceptor;
105 Crt::String m_username;
106 Crt::String m_password;
108 bool m_enableMetricsCollection;
110 int m_lastError;
111
112 friend class MqttClient;
114 };
115
122 {
123 public:
125
135 const char *certPath,
136 const char *pkeyPath,
137 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
138
148 const Crt::ByteCursor &cert,
149 const Crt::ByteCursor &pkey,
150 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
151
161 const Crt::Io::TlsContextPkcs11Options &pkcs11Options,
162 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
163
174 const struct Pkcs12Options &options,
175 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
176
188 const char *windowsCertStorePath,
189 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
190
198 const WebsocketConfig &config,
199 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
200
206 static MqttClientConnectionConfigBuilder NewDefaultBuilder() noexcept;
207
215 MqttClientConnectionConfigBuilder &WithEndpoint(const Crt::String &endpoint);
216
224 MqttClientConnectionConfigBuilder &WithEndpoint(Crt::String &&endpoint);
225
235 MqttClientConnectionConfigBuilder &WithPortOverride(uint32_t port) noexcept;
236
245 MqttClientConnectionConfigBuilder &WithCertificateAuthority(const char *caPath) noexcept;
246
255 MqttClientConnectionConfigBuilder &WithCertificateAuthority(const Crt::ByteCursor &cert) noexcept;
256
262 MqttClientConnectionConfigBuilder &WithTcpKeepAlive() noexcept;
263
271 MqttClientConnectionConfigBuilder &WithTcpConnectTimeout(uint32_t connectTimeoutMs) noexcept;
272
281 MqttClientConnectionConfigBuilder &WithTcpKeepAliveTimeout(uint16_t keepAliveTimeoutSecs) noexcept;
282
292 MqttClientConnectionConfigBuilder &WithTcpKeepAliveInterval(uint16_t keepAliveIntervalSecs) noexcept;
293
302 MqttClientConnectionConfigBuilder &WithTcpKeepAliveMaxProbes(uint16_t maxProbes) noexcept;
303
311 MqttClientConnectionConfigBuilder &WithMinimumTlsVersion(aws_tls_versions minimumTlsVersion) noexcept;
312
323 MqttClientConnectionConfigBuilder &WithTlsCipherPreference(aws_tls_cipher_pref cipherPref) noexcept;
324
332 MqttClientConnectionConfigBuilder &WithHttpProxyOptions(
333 const Crt::Http::HttpClientConnectionProxyOptions &proxyOptions) noexcept;
334
343 MqttClientConnectionConfigBuilder &WithMetricsCollection(bool enabled);
344
352 MqttClientConnectionConfigBuilder &WithSdkName(const Crt::String &sdkName);
353
361 MqttClientConnectionConfigBuilder &WithSdkVersion(const Crt::String &sdkVersion);
362
382 MqttClientConnectionConfigBuilder &WithCustomAuthorizer(
383 const Crt::String &username,
384 const Crt::String &authorizerName,
385 const Crt::String &authorizerSignature,
386 const Crt::String &password) noexcept;
387
410 MqttClientConnectionConfigBuilder &WithCustomAuthorizer(
411 const Crt::String &username,
412 const Crt::String &authorizerName,
413 const Crt::String &authorizerSignature,
414 const Crt::String &password,
415 const Crt::String &tokenKeyName,
416 const Crt::String &tokenValue) noexcept;
417
425 MqttClientConnectionConfigBuilder &WithUsername(const Crt::String &username) noexcept;
426
434 MqttClientConnectionConfigBuilder &WithPassword(const Crt::String &password) noexcept;
435
441 MqttClientConnectionConfig Build() noexcept;
442
446 explicit operator bool() const noexcept { return m_lastError == 0; }
447
451 int LastError() const noexcept { return m_lastError ? m_lastError : AWS_ERROR_UNKNOWN; }
452
453 private:
454 // Common setup shared by all valid constructors
456
457 // Helper function to add parameters to the username in the WithCustomAuthorizer function
458 Crt::String AddToUsernameParameter(
459 Crt::String currentUsername,
460 Crt::String parameterValue,
461 Crt::String parameterPreText);
462
463 Crt::Allocator *m_allocator;
464 Crt::String m_endpoint;
465 uint32_t m_portOverride;
466 Crt::Io::SocketOptions m_socketOptions;
467 Crt::Io::TlsContextOptions m_contextOptions;
468 Crt::Optional<WebsocketConfig> m_websocketConfig;
470 bool m_enableMetricsCollection = true;
471 Crt::String m_sdkName;
472 Crt::String m_sdkVersion;
473 Crt::String m_username = "";
474 Crt::String m_password = "";
475 bool m_isUsingCustomAuthorizer = false;
476
477 int m_lastError;
478 };
479
486 {
487 public:
488 MqttClient(Crt::Io::ClientBootstrap &bootstrap, Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
489
496 MqttClient(Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
497
503 std::shared_ptr<Crt::Mqtt::MqttConnection> NewConnection(const MqttClientConnectionConfig &config) noexcept;
504
508 int LastError() const noexcept { return m_client.LastError(); }
509
513 explicit operator bool() const noexcept { return m_client ? true : false; }
514
515 private:
516 Crt::Mqtt::MqttClient m_client;
517 int m_lastError;
518 };
519 } // namespace Iot
520} // namespace Aws
521
522#endif // !BYO_CRYPTO
#define AWS_CRT_CPP_API
Definition Exports.h:36
Definition Bootstrap.h:35
Definition SocketOptions.h:70
Definition TlsOptions.h:390
Definition TlsOptions.h:38
Definition TlsOptions.h:242
Definition MqttClient.h:48
int LastError() const noexcept
Definition MqttClient.h:451
Definition MqttClient.h:27
int LastError() const noexcept
Definition MqttClient.h:86
Definition MqttClient.h:486
int LastError() const noexcept
Definition MqttClient.h:508
std::function< void(std::shared_ptr< Http::HttpRequest > req, const OnWebSocketHandshakeInterceptComplete &onComplete)> OnWebSocketHandshakeIntercept
Definition MqttConnection.h:146
aws_byte_cursor ByteCursor
Definition Types.h:31
aws_allocator Allocator
Definition Allocator.h:14
std::basic_string< char, std::char_traits< char >, StlAllocator< char > > String
Definition Types.h:45
std::unique_ptr< T, std::function< void(T *)> > ScopedResource
Definition Types.h:168
Definition Allocator.h:11
Definition MqttCommon.h:105
Definition MqttCommon.h:24