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
Mqtt5Client.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 using namespace Crt::Mqtt5;
17
18 namespace Io
19 {
20 class ClientBootstrap;
21 class SocketOptions;
22 class TlsContextOptions;
23 class WebsocketConfig;
24 } // namespace Io
25
26 namespace Iot
27 {
28
33 {
34 public:
38 Mqtt5CustomAuthConfig(Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
39 virtual ~Mqtt5CustomAuthConfig();
40
43
44 Mqtt5CustomAuthConfig &operator=(const Mqtt5CustomAuthConfig &rhs);
46
47 Mqtt5CustomAuthConfig &WithAuthorizerName(Crt::String authName);
48 Mqtt5CustomAuthConfig &WithUsername(Crt::String username);
49 Mqtt5CustomAuthConfig &WithPassword(Crt::ByteCursor password);
50 Mqtt5CustomAuthConfig &WithTokenKeyName(Crt::String tokenKeyName);
51 Mqtt5CustomAuthConfig &WithTokenValue(Crt::String tokenValue);
52 Mqtt5CustomAuthConfig &WithTokenSignature(Crt::String tokenSignature);
53
54 const Crt::Optional<Crt::String> &GetAuthorizerName();
55 const Crt::Optional<Crt::String> &GetUsername();
56 const Crt::Optional<Crt::ByteCursor> &GetPassword();
57 const Crt::Optional<Crt::String> &GetTokenKeyName();
58 const Crt::Optional<Crt::String> &GetTokenValue();
59 const Crt::Optional<Crt::String> &GetTokenSignature();
60
61 private:
68 Crt::Optional<Crt::String> m_authorizerName;
69
83
89
96 Crt::Optional<Crt::String> m_tokenKeyName;
97
104 Crt::Optional<Crt::String> m_tokenValue;
105
112 Crt::Optional<Crt::String> m_tokenSignature;
113
114 Crt::ByteBuf m_passwordStorage;
115 Crt::Allocator *m_allocator;
116 };
117
123 {
124 public:
136 static std::shared_ptr<Mqtt5ClientBuilder> CreateMqtt5ClientBuilderWithMtlsFromPath(
137 const Crt::String &hostName,
138 const char *certPath,
139 const char *pkeyPath,
140 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
141
153 static std::shared_ptr<Mqtt5ClientBuilder> CreateMqtt5ClientBuilderWithMtlsFromMemory(
154 const Crt::String &hostName,
155 const Crt::ByteCursor &cert,
156 const Crt::ByteCursor &pkey,
157 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
158
170 static std::shared_ptr<Mqtt5ClientBuilder> CreateMqtt5ClientBuilderWithMtlsPkcs11(
171 const Crt::String &hostName,
172 const Crt::Io::TlsContextPkcs11Options &pkcs11Options,
173 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
174
186 static std::shared_ptr<Mqtt5ClientBuilder> CreateMqtt5ClientBuilderWithMtlsPkcs12(
187 const Crt::String &hostName,
188 const struct Pkcs12Options &options,
189 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
190
204 static std::shared_ptr<Mqtt5ClientBuilder> CreateMqtt5ClientBuilderWithWindowsCertStorePath(
205 const Crt::String &hostName,
206 const char *windowsCertStorePath,
207 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
208
218 static std::shared_ptr<Mqtt5ClientBuilder> CreateMqtt5ClientBuilderWithWebsocket(
219 const Crt::String &hostName,
220 const WebsocketConfig &config,
221 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
222
232 static std::shared_ptr<Mqtt5ClientBuilder> CreateMqtt5ClientBuilderWithCustomAuthorizer(
233 const Crt::String &hostName,
234 const Mqtt5CustomAuthConfig &customAuthConfig,
235 Crt::Allocator *allocator) noexcept;
236
247 static std::shared_ptr<Mqtt5ClientBuilder> CreateMqtt5ClientBuilderWithCustomAuthorizerWebsocket(
248 const Crt::String &hostName,
249 const Mqtt5CustomAuthConfig &customAuthConfig,
250 const WebsocketConfig &config,
251 Crt::Allocator *allocator) noexcept;
252
260 Mqtt5ClientBuilder &WithHostName(Crt::String hostname);
261
269 Mqtt5ClientBuilder &WithPort(uint32_t port) noexcept;
270
279 Mqtt5ClientBuilder &WithBootstrap(Crt::Io::ClientBootstrap *bootStrap) noexcept;
280
289 Mqtt5ClientBuilder &WithCertificateAuthority(const char *caPath) noexcept;
290
299 Mqtt5ClientBuilder &WithCertificateAuthority(const Crt::ByteCursor &cert) noexcept;
300
308 Mqtt5ClientBuilder &WithSocketOptions(Crt::Io::SocketOptions socketOptions) noexcept;
309
317 Mqtt5ClientBuilder &WithHttpProxyOptions(
318 const Crt::Http::HttpClientConnectionProxyOptions &proxyOptions) noexcept;
319
325 Mqtt5ClientBuilder &WithCustomAuthorizer(const Iot::Mqtt5CustomAuthConfig &config) noexcept;
326
334 Mqtt5ClientBuilder &WithConnectOptions(std::shared_ptr<ConnectPacket> packetConnect) noexcept;
335
343 Mqtt5ClientBuilder &WithSessionBehavior(ClientSessionBehaviorType sessionBehavior) noexcept;
344
354 Mqtt5ClientBuilder &WithClientExtendedValidationAndFlowControl(
355 ClientExtendedValidationAndFlowControl clientExtendedValidationAndFlowControl) noexcept;
356
367 Mqtt5ClientBuilder &WithOfflineQueueBehavior(
368 ClientOperationQueueBehaviorType offlineQueueBehavior) noexcept;
369
378 Mqtt5ClientBuilder &WithReconnectOptions(ReconnectOptions reconnectOptions) noexcept;
379
386 Mqtt5ClientBuilder &WithTopicAliasingOptions(TopicAliasingOptions topicAliasingOptions) noexcept;
387
398 uint64_t minConnectedTimeToResetReconnectDelayMs) noexcept;
399
408 Mqtt5ClientBuilder &WithPingTimeoutMs(uint32_t pingTimeoutMs) noexcept;
409
418 Mqtt5ClientBuilder &WithConnackTimeoutMs(uint32_t connackTimeoutMs) noexcept;
419
428 Mqtt5ClientBuilder &WithAckTimeoutSec(uint32_t ackTimeoutSec) noexcept;
429
440 Mqtt5ClientBuilder &WithAckTimeoutSeconds(uint32_t ackTimeoutSec) noexcept;
441
449 Mqtt5ClientBuilder &WithSdkName(const Crt::String &sdkName);
450
458 Mqtt5ClientBuilder &WithSdkVersion(const Crt::String &sdkVersion);
459
465 std::shared_ptr<Mqtt5Client> Build() noexcept;
466
470 explicit operator bool() const noexcept { return m_lastError == 0; }
471
475 int LastError() const noexcept { return m_lastError ? m_lastError : AWS_ERROR_UNKNOWN; }
476
478 {
479 if (m_options)
480 {
481 delete m_options;
482 }
483 };
488
496 Mqtt5ClientBuilder &WithClientConnectionSuccessCallback(OnConnectionSuccessHandler callback) noexcept;
497
505 Mqtt5ClientBuilder &WithClientConnectionFailureCallback(OnConnectionFailureHandler callback) noexcept;
506
514 Mqtt5ClientBuilder &WithClientDisconnectionCallback(OnDisconnectionHandler callback) noexcept;
515
523 Mqtt5ClientBuilder &WithClientStoppedCallback(OnStoppedHandler callback) noexcept;
524
532 Mqtt5ClientBuilder &WithClientAttemptingConnectCallback(OnAttemptingConnectHandler callback) noexcept;
533
541 Mqtt5ClientBuilder &WithPublishReceivedCallback(OnPublishReceivedHandler callback) noexcept;
542
546 static Mqtt5ClientBuilder *NewMqtt5ClientBuilderWithMtlsFromPath(
547 const Crt::String hostName,
548 const char *certPath,
549 const char *pkeyPath,
550 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
551
555 static Mqtt5ClientBuilder *NewMqtt5ClientBuilderWithMtlsFromMemory(
556 const Crt::String hostName,
557 const Crt::ByteCursor &cert,
558 const Crt::ByteCursor &pkey,
559 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
560
564 static Mqtt5ClientBuilder *NewMqtt5ClientBuilderWithMtlsPkcs11(
565 const Crt::String hostName,
566 const Crt::Io::TlsContextPkcs11Options &pkcs11Options,
567 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
568
572 static Mqtt5ClientBuilder *NewMqtt5ClientBuilderWithMtlsPkcs12(
573 const Crt::String hostName,
574 const struct Pkcs12Options &options,
575 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
576
580 static Mqtt5ClientBuilder *NewMqtt5ClientBuilderWithWindowsCertStorePath(
581 const Crt::String hostName,
582 const char *windowsCertStorePath,
583 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
584
588 static Mqtt5ClientBuilder *NewMqtt5ClientBuilderWithWebsocket(
589 const Crt::String hostName,
590 const WebsocketConfig &config,
591 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
592
596 static Mqtt5ClientBuilder *NewMqtt5ClientBuilderWithCustomAuthorizer(
597 const Crt::String hostName,
598 const Mqtt5CustomAuthConfig &customAuthConfig,
599 Crt::Allocator *allocator) noexcept;
600
604 static Mqtt5ClientBuilder *NewMqtt5ClientBuilderWithCustomAuthorizerWebsocket(
605 const Crt::String hostName,
606 const Mqtt5CustomAuthConfig &customAuthConfig,
607 const WebsocketConfig &config,
608 Crt::Allocator *allocator) noexcept;
609
610 private:
611 // Common setup shared by all valid constructors
612 Mqtt5ClientBuilder(Crt::Allocator *allocator) noexcept;
613 // Common setup shared by all valid constructors
614 Mqtt5ClientBuilder(int error, Crt::Allocator *allocator) noexcept;
615
616 /*
617 * Creates a new Mqtt5ClientBuilder instance with default values.
618 */
619 static std::shared_ptr<Mqtt5ClientBuilder> CreateMqtt5ClientBuilder(
620 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
621
622 Crt::Allocator *m_allocator;
623
627 uint32_t m_port;
628
633 Crt::Optional<Crt::Io::TlsContextOptions> m_tlsConnectionOptions;
634
638 Crt::Optional<Crt::Http::HttpClientConnectionProxyOptions> m_proxyOptions;
639
643 Crt::Optional<WebsocketConfig> m_websocketConfig;
644
648 Crt::Optional<Mqtt5CustomAuthConfig> m_customAuthConfig;
649
654 std::shared_ptr<ConnectPacket> m_connectOptions;
655
656 Crt::Mqtt5::Mqtt5ClientOptions *m_options;
657
658 /* Error */
659 int m_lastError;
660
661 bool m_enableMetricsCollection;
662
663 Crt::String m_sdkName = "CPPv2";
664 Crt::String m_sdkVersion = AWS_CRT_CPP_VERSION;
665 };
666
667 } // namespace Iot
668} // namespace Aws
669
670#endif // !BYO_CRYPTO
#define AWS_CRT_CPP_API
Definition Exports.h:36
Definition Bootstrap.h:35
Definition SocketOptions.h:48
Definition TlsOptions.h:217
Definition Mqtt5Packets.h:672
Definition Mqtt5Client.h:453
Definition Optional.h:19
Definition Mqtt5Client.h:123
Mqtt5ClientBuilder & operator=(const Mqtt5ClientBuilder &)=delete
Mqtt5ClientBuilder(const Mqtt5ClientBuilder &)=delete
Mqtt5ClientBuilder & operator=(Mqtt5ClientBuilder &&)=delete
Mqtt5ClientBuilder(Mqtt5ClientBuilder &&)=delete
Mqtt5ClientBuilder & WithMinConnectedTimeToResetReconnectDelayMs(uint64_t minConnectedTimeToResetReconnectDelayMs) noexcept
~Mqtt5ClientBuilder()
Definition Mqtt5Client.h:477
int LastError() const noexcept
Definition Mqtt5Client.h:475
Definition Mqtt5Client.h:33
Mqtt5CustomAuthConfig & operator=(Mqtt5CustomAuthConfig &&rhs)=delete
Mqtt5CustomAuthConfig(Mqtt5CustomAuthConfig &&rhs)=delete
aws_mqtt5_client_operation_queue_behavior_type ClientOperationQueueBehaviorType
Definition Mqtt5Types.h:223
aws_mqtt5_client_session_behavior_type ClientSessionBehaviorType
Definition Mqtt5Types.h:186
std::function< void(const PublishReceivedEventData &)> OnPublishReceivedHandler
Definition Mqtt5Client.h:308
std::function< void(const OnStoppedEventData &)> OnStoppedHandler
Definition Mqtt5Client.h:288
std::function< void(const OnDisconnectionEventData &)> OnDisconnectionHandler
Definition Mqtt5Client.h:276
std::function< void(const OnConnectionSuccessEventData &)> OnConnectionSuccessHandler
Definition Mqtt5Client.h:266
std::function< void(const OnAttemptingConnectEventData &)> OnAttemptingConnectHandler
Definition Mqtt5Client.h:282
std::function< void(const OnConnectionFailureEventData &)> OnConnectionFailureHandler
Definition Mqtt5Client.h:271
aws_mqtt5_extended_validation_and_flow_control_options ClientExtendedValidationAndFlowControl
Definition Mqtt5Types.h:200
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
aws_byte_buf ByteBuf
Definition Types.h:30
Definition Allocator.h:11
Definition StringView.h:862
Definition Mqtt5Client.h:145
Definition Mqtt5Client.h:101
Definition MqttCommon.h:105
Definition MqttCommon.h:24