aws-crt-cpp
MqttClient.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License").
6  * You may not use this file except in compliance with the License.
7  * A copy of the License is located at
8  *
9  * http://aws.amazon.com/apache2.0
10  *
11  * or in the "license" file accompanying this file. This file is distributed
12  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 #include <aws/crt/Exports.h>
19 
20 namespace Aws
21 {
22  namespace Iot
23  {
24  class MqttClient;
25 
31  {
32  public:
33  static MqttClientConnectionConfig CreateInvalid(int lastError) noexcept;
34 
39  const Crt::String &endpoint,
40  uint16_t port,
41  const Crt::Io::SocketOptions &socketOptions,
42  Crt::Io::TlsContext &&tlsContext);
43 
55  const Crt::String &endpoint,
56  uint16_t port,
57  const Crt::Io::SocketOptions &socketOptions,
58  Crt::Io::TlsContext &&tlsContext,
61 
65  explicit operator bool() const noexcept { return m_context ? true : false; }
69  int LastError() const noexcept { return m_lastError; }
70 
71  private:
72  MqttClientConnectionConfig(int lastError) noexcept;
73  Crt::String m_endpoint;
74  uint16_t m_port;
75  Crt::Io::TlsContext m_context;
76  Crt::Io::SocketOptions m_socketOptions;
77  Crt::Mqtt::OnWebSocketHandshakeIntercept m_webSocketInterceptor;
79  int m_lastError;
80 
81  friend class MqttClient;
82  };
83 
84  using CreateSigningConfig = std::function<std::shared_ptr<Crt::Auth::ISigningConfig>(void)>;
85 
87  {
93  const Crt::String &signingRegion,
94  Crt::Io::ClientBootstrap *bootstrap,
95  Crt::Allocator *allocator = Crt::g_allocator) noexcept;
96 
102  const Crt::String &signingRegion,
103  const std::shared_ptr<Crt::Auth::ICredentialsProvider> &credentialsProvider,
104  Crt::Allocator *allocator = Crt::g_allocator) noexcept;
105 
116  const std::shared_ptr<Crt::Auth::ICredentialsProvider> &credentialsProvider,
117  const std::shared_ptr<Crt::Auth::IHttpRequestSigner> &signer,
118  CreateSigningConfig createSigningConfig) noexcept;
119 
120  std::shared_ptr<Crt::Auth::ICredentialsProvider> CredentialsProvider;
121  std::shared_ptr<Crt::Auth::IHttpRequestSigner> Signer;
123 
130  };
131 
138  {
139  public:
141 
147  const char *certPath,
148  const char *pkeyPath,
149  Crt::Allocator *allocator = Crt::g_allocator) noexcept;
150 
156  const Crt::ByteCursor &cert,
157  const Crt::ByteCursor &pkey,
158  Crt::Allocator *allocator = Crt::g_allocator) noexcept;
159 
164  const WebsocketConfig &config,
165  Crt::Allocator *allocator = Crt::g_allocator) noexcept;
166 
170  MqttClientConnectionConfigBuilder &WithEndpoint(const Crt::String &endpoint);
171 
175  MqttClientConnectionConfigBuilder &WithEndpoint(Crt::String &&endpoint);
176 
182  MqttClientConnectionConfigBuilder &WithPortOverride(uint16_t port) noexcept;
183 
188  MqttClientConnectionConfigBuilder &WithCertificateAuthority(const char *caPath) noexcept;
189 
194  MqttClientConnectionConfigBuilder &WithCertificateAuthority(const Crt::ByteCursor &cert) noexcept;
195 
197  MqttClientConnectionConfigBuilder &WithTcpKeepAlive() noexcept;
198 
200  MqttClientConnectionConfigBuilder &WithTcpConnectTimeout(uint32_t connectTimeoutMs) noexcept;
201 
203  MqttClientConnectionConfigBuilder &WithTcpKeepAliveTimeout(uint16_t keepAliveTimeoutSecs) noexcept;
204 
209  MqttClientConnectionConfigBuilder &WithTcpKeepAliveInterval(uint16_t keepAliveIntervalSecs) noexcept;
210 
215  MqttClientConnectionConfigBuilder &WithTcpKeepAliveMaxProbes(uint16_t maxProbes) noexcept;
216 
220  MqttClientConnectionConfig Build() noexcept;
224  explicit operator bool() const noexcept { return m_isGood; }
228  int LastError() const noexcept { return aws_last_error(); }
229 
230  private:
231  Crt::Allocator *m_allocator;
232  Crt::String m_endpoint;
233  uint16_t m_portOverride;
234  Crt::Io::SocketOptions m_socketOptions;
235  Crt::Io::TlsContextOptions m_contextOptions;
236  Crt::Optional<WebsocketConfig> m_websocketConfig;
237  bool m_isGood;
238  };
239 
246  {
247  public:
248  MqttClient(Crt::Io::ClientBootstrap &bootstrap, Crt::Allocator *allocator = Crt::g_allocator) noexcept;
249 
250  std::shared_ptr<Crt::Mqtt::MqttConnection> NewConnection(const MqttClientConnectionConfig &config) noexcept;
254  int LastError() const noexcept { return m_client.LastError(); }
258  explicit operator bool() const noexcept { return m_client ? true : false; }
259 
260  private:
261  Crt::Mqtt::MqttClient m_client;
262  int m_lastError;
263  };
264  } // namespace Iot
265 } // namespace Aws
Aws::Crt::Allocator
aws_allocator Allocator
Definition: StlAllocator.h:25
Aws::Iot::WebsocketConfig::SigningRegion
Crt::String SigningRegion
Definition: MqttClient.h:128
Aws::Crt::Io::SocketOptions
Definition: SocketOptions.h:55
AWS_CRT_CPP_API
#define AWS_CRT_CPP_API
Definition: Exports.h:34
Aws::Iot::WebsocketConfig::CredentialsProvider
std::shared_ptr< Crt::Auth::ICredentialsProvider > CredentialsProvider
Definition: MqttClient.h:120
Aws::Iot::WebsocketConfig::WebsocketConfig
WebsocketConfig(const Crt::String &signingRegion, Crt::Io::ClientBootstrap *bootstrap, Crt::Allocator *allocator=Crt::g_allocator) noexcept
Definition: MqttClient.cpp:36
Aws::Crt::ByteCursor
aws_byte_cursor ByteCursor
Definition: Types.h:44
Aws::Iot::MqttClientConnectionConfigBuilder
Definition: MqttClient.h:138
Aws::Crt::g_allocator
Allocator * g_allocator
Definition: Api.cpp:28
Aws::Iot::MqttClientConnectionConfigBuilder::LastError
int LastError() const noexcept
Definition: MqttClient.h:228
MqttClient.h
Aws::Iot::WebsocketConfig::ServiceName
Crt::String ServiceName
Definition: MqttClient.h:129
Aws::Iot::WebsocketConfig::Signer
std::shared_ptr< Crt::Auth::IHttpRequestSigner > Signer
Definition: MqttClient.h:121
Sigv4Signing.h
Aws::Crt::Mqtt::OnWebSocketHandshakeIntercept
std::function< void(std::shared_ptr< Http::HttpRequest > req, const OnWebSocketHandshakeInterceptComplete &onComplete)> OnWebSocketHandshakeIntercept
Definition: MqttClient.h:109
Aws::Iot::MqttClientConnectionConfig
Definition: MqttClient.h:31
Aws
Definition: Api.h:25
Aws::Iot::CreateSigningConfig
std::function< std::shared_ptr< Crt::Auth::ISigningConfig >(void)> CreateSigningConfig
Definition: MqttClient.h:84
Aws::Crt::Io::TlsContext
Definition: TlsOptions.h:195
Exports.h
Aws::true
return true
Definition: cJSON.cpp:1907
Aws::Iot::MqttClient::LastError
int LastError() const noexcept
Definition: MqttClient.h:254
Aws::Iot::MqttClientConnectionConfig::LastError
int LastError() const noexcept
Definition: MqttClient.h:69
Aws::Crt::Optional
Definition: Optional.h:32
Aws::Crt::String
std::basic_string< char, std::char_traits< char >, StlAllocator< char > > String
Definition: Types.h:58
Aws::Crt::Io::ClientBootstrap
Definition: Bootstrap.h:45
Aws::Iot::WebsocketConfig
Definition: MqttClient.h:87
Aws::Crt::Mqtt::MqttClient
Definition: MqttClient.h:303
Aws::Crt::Io::TlsContextOptions
Definition: TlsOptions.h:36
Aws::Iot::WebsocketConfig::ProxyOptions
Crt::Optional< Crt::Http::HttpClientConnectionProxyOptions > ProxyOptions
Definition: MqttClient.h:127
Aws::Iot::MqttClient
Definition: MqttClient.h:246
Aws::Iot::WebsocketConfig::CreateSigningConfigCb
CreateSigningConfig CreateSigningConfigCb
Definition: MqttClient.h:122