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 Crt
18 {
19 namespace Io
20 {
21 class L4ProxyConfig;
22 } // namespace Io
23 } // namespace Crt
24
25 namespace Iot
26 {
27 class MqttClient;
28
35 {
36 public:
37 static MqttClientConnectionConfig CreateInvalid(int lastError) noexcept;
38
50 const Crt::String &endpoint,
51 uint32_t port,
52 const Crt::Io::SocketOptions &socketOptions,
53 Crt::Io::TlsContext &&tlsContext,
54 bool enableMetrics = true,
56
77 const Crt::String &endpoint,
78 uint32_t port,
79 const Crt::Io::SocketOptions &socketOptions,
80 Crt::Io::TlsContext &&tlsContext,
83 bool enableMetrics = true,
85
89 explicit operator bool() const noexcept { return m_context ? true : false; }
90
94 int LastError() const noexcept { return m_lastError; }
95
96 private:
97 MqttClientConnectionConfig(int lastError) noexcept;
98
100 const Crt::String &endpoint,
101 uint32_t port,
102 const Crt::Io::SocketOptions &socketOptions,
103 Crt::Io::TlsContext &&tlsContext,
105 bool enableMetrics = true,
107
108 Crt::String m_endpoint;
109 uint32_t m_port;
110 Crt::Io::TlsContext m_context;
111 Crt::Io::SocketOptions m_socketOptions;
112 Crt::Mqtt::OnWebSocketHandshakeIntercept m_webSocketInterceptor;
113 Crt::String m_username;
114 Crt::String m_password;
116 std::shared_ptr<Aws::Crt::Io::L4ProxyConfig> m_l4ProxyConfig;
117 bool m_enableMetricsCollection;
119 int m_lastError;
120
121 friend class MqttClient;
123 };
124
131 {
132 public:
134
144 const char *certPath,
145 const char *pkeyPath,
146 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
147
157 const Crt::ByteCursor &cert,
158 const Crt::ByteCursor &pkey,
159 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
160
170 const Crt::Io::TlsContextPkcs11Options &pkcs11Options,
171 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
172
183 const struct Pkcs12Options &options,
184 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
185
197 const char *windowsCertStorePath,
198 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
199
207 const WebsocketConfig &config,
208 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
209
215 static MqttClientConnectionConfigBuilder NewDefaultBuilder() noexcept;
216
224 MqttClientConnectionConfigBuilder &WithEndpoint(const Crt::String &endpoint);
225
233 MqttClientConnectionConfigBuilder &WithEndpoint(Crt::String &&endpoint);
234
244 MqttClientConnectionConfigBuilder &WithPortOverride(uint32_t port) noexcept;
245
254 MqttClientConnectionConfigBuilder &WithCertificateAuthority(const char *caPath) noexcept;
255
264 MqttClientConnectionConfigBuilder &WithCertificateAuthority(const Crt::ByteCursor &cert) noexcept;
265
271 MqttClientConnectionConfigBuilder &WithTcpKeepAlive() noexcept;
272
280 MqttClientConnectionConfigBuilder &WithTcpConnectTimeout(uint32_t connectTimeoutMs) noexcept;
281
290 MqttClientConnectionConfigBuilder &WithTcpKeepAliveTimeout(uint16_t keepAliveTimeoutSecs) noexcept;
291
301 MqttClientConnectionConfigBuilder &WithTcpKeepAliveInterval(uint16_t keepAliveIntervalSecs) noexcept;
302
311 MqttClientConnectionConfigBuilder &WithTcpKeepAliveMaxProbes(uint16_t maxProbes) noexcept;
312
320 MqttClientConnectionConfigBuilder &WithMinimumTlsVersion(aws_tls_versions minimumTlsVersion) noexcept;
321
332 MqttClientConnectionConfigBuilder &WithTlsCipherPreference(aws_tls_cipher_pref cipherPref) noexcept;
333
341 MqttClientConnectionConfigBuilder &WithHttpProxyOptions(
342 const Crt::Http::HttpClientConnectionProxyOptions &proxyOptions) noexcept;
343
351 MqttClientConnectionConfigBuilder &WithL4ProxyOptions(
352 const std::shared_ptr<Aws::Crt::Io::L4ProxyConfig> &proxyOptions) noexcept;
353
362 MqttClientConnectionConfigBuilder &WithMetricsCollection(bool enabled);
363
371 MqttClientConnectionConfigBuilder &WithSdkName(const Crt::String &sdkName);
372
380 MqttClientConnectionConfigBuilder &WithSdkVersion(const Crt::String &sdkVersion);
381
401 MqttClientConnectionConfigBuilder &WithCustomAuthorizer(
402 const Crt::String &username,
403 const Crt::String &authorizerName,
404 const Crt::String &authorizerSignature,
405 const Crt::String &password) noexcept;
406
429 MqttClientConnectionConfigBuilder &WithCustomAuthorizer(
430 const Crt::String &username,
431 const Crt::String &authorizerName,
432 const Crt::String &authorizerSignature,
433 const Crt::String &password,
434 const Crt::String &tokenKeyName,
435 const Crt::String &tokenValue) noexcept;
436
444 MqttClientConnectionConfigBuilder &WithUsername(const Crt::String &username) noexcept;
445
453 MqttClientConnectionConfigBuilder &WithPassword(const Crt::String &password) noexcept;
454
460 MqttClientConnectionConfig Build() noexcept;
461
465 explicit operator bool() const noexcept { return m_lastError == 0; }
466
470 int LastError() const noexcept { return m_lastError ? m_lastError : AWS_ERROR_UNKNOWN; }
471
472 private:
473 // Common setup shared by all valid constructors
475
476 // Helper function to add parameters to the username in the WithCustomAuthorizer function
477 Crt::String AddToUsernameParameter(
478 Crt::String currentUsername,
479 Crt::String parameterValue,
480 Crt::String parameterPreText);
481
482 Crt::Allocator *m_allocator;
483 Crt::String m_endpoint;
484 uint32_t m_portOverride;
485 Crt::Io::SocketOptions m_socketOptions;
486 Crt::Io::TlsContextOptions m_contextOptions;
487 Crt::Optional<WebsocketConfig> m_websocketConfig;
489 std::shared_ptr<Aws::Crt::Io::L4ProxyConfig> m_l4ProxyConfig;
490 bool m_enableMetricsCollection = true;
491 Crt::String m_sdkName;
492 Crt::String m_sdkVersion;
493 Crt::String m_username = "";
494 Crt::String m_password = "";
495 bool m_isUsingCustomAuthorizer = false;
496
497 int m_lastError;
498 };
499
506 {
507 public:
508 MqttClient(Crt::Io::ClientBootstrap &bootstrap, Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
509
516 MqttClient(Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
517
523 std::shared_ptr<Crt::Mqtt::MqttConnection> NewConnection(const MqttClientConnectionConfig &config) noexcept;
524
528 int LastError() const noexcept { return m_client.LastError(); }
529
533 explicit operator bool() const noexcept { return m_client ? true : false; }
534
535 private:
536 Crt::Mqtt::MqttClient m_client;
537 int m_lastError;
538 };
539 } // namespace Iot
540} // namespace Aws
541
542#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:470
Definition MqttClient.h:35
int LastError() const noexcept
Definition MqttClient.h:94
Definition MqttClient.h:506
int LastError() const noexcept
Definition MqttClient.h:528
std::function< void(std::shared_ptr< Http::HttpRequest > req, const OnWebSocketHandshakeInterceptComplete &onComplete)> OnWebSocketHandshakeIntercept
Definition MqttConnection.h:151
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