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
24 {
25 public:
26 static MqttClientConnectionConfig CreateInvalid(int lastError) noexcept;
27
32 const Crt::String &endpoint,
33 uint16_t port,
34 const Crt::Io::SocketOptions &socketOptions,
35 Crt::Io::TlsContext &&tlsContext);
36
48 const Crt::String &endpoint,
49 uint16_t port,
50 const Crt::Io::SocketOptions &socketOptions,
51 Crt::Io::TlsContext &&tlsContext,
54
58 explicit operator bool() const noexcept { return m_context ? true : false; }
62 int LastError() const noexcept { return m_lastError; }
63
64 private:
65 MqttClientConnectionConfig(int lastError) noexcept;
66
68 const Crt::String &endpoint,
69 uint16_t port,
70 const Crt::Io::SocketOptions &socketOptions,
71 Crt::Io::TlsContext &&tlsContext,
73
74 Crt::String m_endpoint;
75 uint16_t m_port;
76 Crt::Io::TlsContext m_context;
77 Crt::Io::SocketOptions m_socketOptions;
78 Crt::Mqtt::OnWebSocketHandshakeIntercept m_webSocketInterceptor;
79 Crt::String m_username;
80 Crt::String m_password;
82 int m_lastError;
83
84 friend class MqttClient;
86 };
87
88 using CreateSigningConfig = std::function<std::shared_ptr<Crt::Auth::ISigningConfig>(void)>;
89
91 {
97 const Crt::String &signingRegion,
98 Crt::Io::ClientBootstrap *bootstrap,
99 Crt::Allocator *allocator = Crt::g_allocator) noexcept;
100
106 const Crt::String &signingRegion,
107 const std::shared_ptr<Crt::Auth::ICredentialsProvider> &credentialsProvider,
108 Crt::Allocator *allocator = Crt::g_allocator) noexcept;
109
120 const std::shared_ptr<Crt::Auth::ICredentialsProvider> &credentialsProvider,
121 const std::shared_ptr<Crt::Auth::IHttpRequestSigner> &signer,
122 CreateSigningConfig createSigningConfig) noexcept;
123
124 std::shared_ptr<Crt::Auth::ICredentialsProvider> CredentialsProvider;
125 std::shared_ptr<Crt::Auth::IHttpRequestSigner> Signer;
127
137 };
138
145 {
146 public:
148
154 const char *certPath,
155 const char *pkeyPath,
156 Crt::Allocator *allocator = Crt::g_allocator) noexcept;
157
163 const Crt::ByteCursor &cert,
164 const Crt::ByteCursor &pkey,
165 Crt::Allocator *allocator = Crt::g_allocator) noexcept;
166
171 const WebsocketConfig &config,
172 Crt::Allocator *allocator = Crt::g_allocator) noexcept;
173
177 MqttClientConnectionConfigBuilder &WithEndpoint(const Crt::String &endpoint);
178
182 MqttClientConnectionConfigBuilder &WithEndpoint(Crt::String &&endpoint);
183
189 MqttClientConnectionConfigBuilder &WithPortOverride(uint16_t port) noexcept;
190
195 MqttClientConnectionConfigBuilder &WithCertificateAuthority(const char *caPath) noexcept;
196
201 MqttClientConnectionConfigBuilder &WithCertificateAuthority(const Crt::ByteCursor &cert) noexcept;
202
204 MqttClientConnectionConfigBuilder &WithTcpKeepAlive() noexcept;
205
207 MqttClientConnectionConfigBuilder &WithTcpConnectTimeout(uint32_t connectTimeoutMs) noexcept;
208
210 MqttClientConnectionConfigBuilder &WithTcpKeepAliveTimeout(uint16_t keepAliveTimeoutSecs) noexcept;
211
216 MqttClientConnectionConfigBuilder &WithTcpKeepAliveInterval(uint16_t keepAliveIntervalSecs) noexcept;
217
222 MqttClientConnectionConfigBuilder &WithTcpKeepAliveMaxProbes(uint16_t maxProbes) noexcept;
223
224 MqttClientConnectionConfigBuilder &WithMinimumTlsVersion(aws_tls_versions minimumTlsVersion) noexcept;
225
231 MqttClientConnectionConfigBuilder &WithHttpProxyOptions(
232 const Crt::Http::HttpClientConnectionProxyOptions &proxyOptions) noexcept;
233
238 MqttClientConnectionConfigBuilder &WithMetricsCollection(bool enabled);
239
243 MqttClientConnectionConfigBuilder &WithSdkName(const Crt::String &sdkName);
244
248 MqttClientConnectionConfigBuilder &WithSdkVersion(const Crt::String &sdkVersion);
249
253 MqttClientConnectionConfig Build() noexcept;
257 explicit operator bool() const noexcept { return m_lastError == 0; }
261 int LastError() const noexcept { return m_lastError ? m_lastError : AWS_ERROR_UNKNOWN; }
262
263 private:
264 Crt::Allocator *m_allocator;
265 Crt::String m_endpoint;
266 uint16_t m_portOverride;
267 Crt::Io::SocketOptions m_socketOptions;
268 Crt::Io::TlsContextOptions m_contextOptions;
269 Crt::Optional<WebsocketConfig> m_websocketConfig;
271 bool m_enableMetricsCollection = true;
272 Crt::String m_sdkName = "CPPv2";
273 Crt::String m_sdkVersion = AWS_CRT_CPP_VERSION;
274
275 int m_lastError;
276 };
277
284 {
285 public:
286 MqttClient(Crt::Io::ClientBootstrap &bootstrap, Crt::Allocator *allocator = Crt::g_allocator) noexcept;
287
288 std::shared_ptr<Crt::Mqtt::MqttConnection> NewConnection(const MqttClientConnectionConfig &config) noexcept;
292 int LastError() const noexcept { return m_client.LastError(); }
296 explicit operator bool() const noexcept { return m_client ? true : false; }
297
298 private:
299 Crt::Mqtt::MqttClient m_client;
300 int m_lastError;
301 };
302 } // namespace Iot
303} // namespace Aws
304
305#endif // !BYO_CRYPTO
#define AWS_CRT_CPP_VERSION
Definition: Config.h:6
#define AWS_CRT_CPP_API
Definition: Exports.h:37
Definition: HttpConnection.h:258
Definition: Bootstrap.h:35
Definition: SocketOptions.h:45
Definition: TlsOptions.h:219
Definition: TlsOptions.h:29
Definition: MqttClient.h:358
Definition: Optional.h:13
int LastError() const noexcept
Definition: MqttClient.h:261
Definition: MqttClient.h:24
int LastError() const noexcept
Definition: MqttClient.h:62
Definition: MqttClient.h:284
int LastError() const noexcept
Definition: MqttClient.h:292
std::function< void(std::shared_ptr< Http::HttpRequest > req, const OnWebSocketHandshakeInterceptComplete &onComplete)> OnWebSocketHandshakeIntercept
Definition: MqttClient.h:118
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:88
Definition: Api.h:17
return true
Definition: cJSON.cpp:2306
Definition: MqttClient.h:91
std::shared_ptr< Crt::Auth::IHttpRequestSigner > Signer
Definition: MqttClient.h:125
Crt::String ServiceName
Definition: MqttClient.h:136
Crt::String SigningRegion
Definition: MqttClient.h:135
Crt::Optional< Crt::Http::HttpClientConnectionProxyOptions > ProxyOptions
Definition: MqttClient.h:134
CreateSigningConfig CreateSigningConfigCb
Definition: MqttClient.h:126
std::shared_ptr< Crt::Auth::ICredentialsProvider > CredentialsProvider
Definition: MqttClient.h:124