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>
10#include <aws/iot/MqttCommon.h>
11
12#if !BYO_CRYPTO
13
14namespace Aws
15{
16 namespace Iot
17 {
18 class MqttClient;
19
26 {
27 public:
28 static MqttClientConnectionConfig CreateInvalid(int lastError) noexcept;
29
39 const Crt::String &endpoint,
40 uint32_t port,
41 const Crt::Io::SocketOptions &socketOptions,
42 Crt::Io::TlsContext &&tlsContext);
43
62 const Crt::String &endpoint,
63 uint32_t port,
64 const Crt::Io::SocketOptions &socketOptions,
65 Crt::Io::TlsContext &&tlsContext,
68
72 explicit operator bool() const noexcept { return m_context ? true : false; }
73
77 int LastError() const noexcept { return m_lastError; }
78
79 private:
80 MqttClientConnectionConfig(int lastError) noexcept;
81
83 const Crt::String &endpoint,
84 uint32_t port,
85 const Crt::Io::SocketOptions &socketOptions,
86 Crt::Io::TlsContext &&tlsContext,
88
89 Crt::String m_endpoint;
90 uint32_t m_port;
91 Crt::Io::TlsContext m_context;
92 Crt::Io::SocketOptions m_socketOptions;
93 Crt::Mqtt::OnWebSocketHandshakeIntercept m_webSocketInterceptor;
94 Crt::String m_username;
95 Crt::String m_password;
97 int m_lastError;
98
99 friend class MqttClient;
101 };
102
109 {
110 public:
112
122 const char *certPath,
123 const char *pkeyPath,
124 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
125
135 const Crt::ByteCursor &cert,
136 const Crt::ByteCursor &pkey,
137 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
138
148 const Crt::Io::TlsContextPkcs11Options &pkcs11Options,
149 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
150
161 const struct Pkcs12Options &options,
162 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
163
175 const char *windowsCertStorePath,
176 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
177
185 const WebsocketConfig &config,
186 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
187
193 static MqttClientConnectionConfigBuilder NewDefaultBuilder() noexcept;
194
202 MqttClientConnectionConfigBuilder &WithEndpoint(const Crt::String &endpoint);
203
211 MqttClientConnectionConfigBuilder &WithEndpoint(Crt::String &&endpoint);
212
222 MqttClientConnectionConfigBuilder &WithPortOverride(uint32_t port) noexcept;
223
232 MqttClientConnectionConfigBuilder &WithCertificateAuthority(const char *caPath) noexcept;
233
242 MqttClientConnectionConfigBuilder &WithCertificateAuthority(const Crt::ByteCursor &cert) noexcept;
243
249 MqttClientConnectionConfigBuilder &WithTcpKeepAlive() noexcept;
250
258 MqttClientConnectionConfigBuilder &WithTcpConnectTimeout(uint32_t connectTimeoutMs) noexcept;
259
268 MqttClientConnectionConfigBuilder &WithTcpKeepAliveTimeout(uint16_t keepAliveTimeoutSecs) noexcept;
269
279 MqttClientConnectionConfigBuilder &WithTcpKeepAliveInterval(uint16_t keepAliveIntervalSecs) noexcept;
280
289 MqttClientConnectionConfigBuilder &WithTcpKeepAliveMaxProbes(uint16_t maxProbes) noexcept;
290
298 MqttClientConnectionConfigBuilder &WithMinimumTlsVersion(aws_tls_versions minimumTlsVersion) noexcept;
299
310 MqttClientConnectionConfigBuilder &WithTlsCipherPreference(aws_tls_cipher_pref cipherPref) noexcept;
311
319 MqttClientConnectionConfigBuilder &WithHttpProxyOptions(
320 const Crt::Http::HttpClientConnectionProxyOptions &proxyOptions) noexcept;
321
330 MqttClientConnectionConfigBuilder &WithMetricsCollection(bool enabled);
331
339 MqttClientConnectionConfigBuilder &WithSdkName(const Crt::String &sdkName);
340
348 MqttClientConnectionConfigBuilder &WithSdkVersion(const Crt::String &sdkVersion);
349
369 MqttClientConnectionConfigBuilder &WithCustomAuthorizer(
370 const Crt::String &username,
371 const Crt::String &authorizerName,
372 const Crt::String &authorizerSignature,
373 const Crt::String &password) noexcept;
374
397 MqttClientConnectionConfigBuilder &WithCustomAuthorizer(
398 const Crt::String &username,
399 const Crt::String &authorizerName,
400 const Crt::String &authorizerSignature,
401 const Crt::String &password,
402 const Crt::String &tokenKeyName,
403 const Crt::String &tokenValue) noexcept;
404
412 MqttClientConnectionConfigBuilder &WithUsername(const Crt::String &username) noexcept;
413
421 MqttClientConnectionConfigBuilder &WithPassword(const Crt::String &password) noexcept;
422
428 MqttClientConnectionConfig Build() noexcept;
429
433 explicit operator bool() const noexcept { return m_lastError == 0; }
434
438 int LastError() const noexcept { return m_lastError ? m_lastError : AWS_ERROR_UNKNOWN; }
439
440 private:
441 // Common setup shared by all valid constructors
443
444 // Helper function to add parameters to the username in the WithCustomAuthorizer function
445 Crt::String AddToUsernameParameter(
446 Crt::String currentUsername,
447 Crt::String parameterValue,
448 Crt::String parameterPreText);
449
450 Crt::Allocator *m_allocator;
451 Crt::String m_endpoint;
452 uint32_t m_portOverride;
453 Crt::Io::SocketOptions m_socketOptions;
454 Crt::Io::TlsContextOptions m_contextOptions;
455 Crt::Optional<WebsocketConfig> m_websocketConfig;
457 bool m_enableMetricsCollection = true;
458 Crt::String m_sdkName = "CPPv2";
459 Crt::String m_sdkVersion;
460 Crt::String m_username = "";
461 Crt::String m_password = "";
462 bool m_isUsingCustomAuthorizer = false;
463
464 int m_lastError;
465 };
466
473 {
474 public:
475 MqttClient(Crt::Io::ClientBootstrap &bootstrap, Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
476
483 MqttClient(Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
484
490 std::shared_ptr<Crt::Mqtt::MqttConnection> NewConnection(const MqttClientConnectionConfig &config) noexcept;
491
495 int LastError() const noexcept { return m_client.LastError(); }
496
500 explicit operator bool() const noexcept { return m_client ? true : false; }
501
502 private:
503 Crt::Mqtt::MqttClient m_client;
504 int m_lastError;
505 };
506 } // namespace Iot
507} // namespace Aws
508
509#endif // !BYO_CRYPTO
#define AWS_CRT_CPP_API
Definition Exports.h:36
Definition Bootstrap.h:35
Definition SocketOptions.h:48
Definition TlsOptions.h:350
Definition TlsOptions.h:36
Definition TlsOptions.h:217
Definition MqttClient.h:48
int LastError() const noexcept
Definition MqttClient.h:438
Definition MqttClient.h:26
int LastError() const noexcept
Definition MqttClient.h:77
Definition MqttClient.h:473
int LastError() const noexcept
Definition MqttClient.h:495
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:163
Definition Allocator.h:11
Definition MqttCommon.h:105
Definition MqttCommon.h:24