Skip to content

Commit 47d82f7

Browse files
Updates to support mqtt api and global bootstrap.
1 parent 395a1a9 commit 47d82f7

File tree

16 files changed

+313
-84
lines changed

16 files changed

+313
-84
lines changed

CMakeLists.txt

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ cmake_minimum_required(VERSION 3.1)
22
project(aws-crt-cpp CXX)
33

44
list(APPEND CMAKE_MODULE_PATH "${CMAKE_INSTALL_PREFIX}/lib/cmake")
5-
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
65

76
if (NOT CMAKE_CXX_STANDARD)
87
set(CMAKE_CXX_STANDARD 11)
@@ -75,30 +74,30 @@ target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC
7574
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
7675
$<INSTALL_INTERFACE:include>)
7776

78-
find_package(aws-c-common REQUIRED)
79-
find_package(aws-c-io REQUIRED)
80-
81-
if (UNIX AND NOT APPLE)
82-
find_package(s2n REQUIRED)
83-
endif()
84-
8577
find_package(aws-c-mqtt REQUIRED)
86-
target_link_libraries(${CMAKE_PROJECT_NAME} AWS::aws-c-common aws-c-io aws-c-mqtt ${PLATFORM_LIBS})
78+
target_link_libraries(${CMAKE_PROJECT_NAME} AWS::aws-c-mqtt)
8779

8880
install(FILES ${AWS_CRT_HEADERS} DESTINATION "include/aws/crt")
8981
install(FILES ${AWS_CRT_IO_HEADERS} DESTINATION "include/aws/crt/io")
9082
install(FILES ${AWS_CRT_MQTT_HEADERS} DESTINATION "include/aws/crt/mqtt")
9183

92-
9384
install(
94-
TARGETS ${CMAKE_PROJECT_NAME} EXPORT ${CMAKE_PROJECT_NAME}-config
85+
TARGETS ${CMAKE_PROJECT_NAME}
86+
EXPORT ${CMAKE_PROJECT_NAME}-targets
9587
ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
9688
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
97-
COMPONENT library
9889
)
9990

100-
export(TARGETS ${CMAKE_PROJECT_NAME} FILE ${CMAKE_PROJECT_NAME}-config.cmake)
101-
install(EXPORT ${CMAKE_PROJECT_NAME}-config DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/${CMAKE_PROJECT_NAME}/cmake/")
91+
install(EXPORT "${CMAKE_PROJECT_NAME}-targets"
92+
DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/${CMAKE_PROJECT_NAME}/cmake/"
93+
NAMESPACE AWS::)
94+
95+
configure_file("cmake/${CMAKE_PROJECT_NAME}-config.cmake"
96+
"${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}-config.cmake"
97+
@ONLY)
98+
99+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}-config.cmake"
100+
DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/${CMAKE_PROJECT_NAME}/cmake/")
102101

103102
enable_testing()
104103
add_subdirectory(tests)

cmake/aws-crt-cpp-config.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include(CMakeFindDependencyMacro)
2+
3+
find_dependency(aws-c-mqtt)
4+
5+
include(${CMAKE_CURRENT_LIST_DIR}/@[email protected])

include/aws/crt/Api.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
* permissions and limitations under the License.
1515
*/
1616
#include <aws/crt/Types.h>
17-
#include <aws/crt/io/EventLoopGroup.h>
18-
#include <aws/crt/io/TLSOptions.h>
19-
#include <aws/crt/mqtt/MqttClient.h>
17+
#include <aws/crt/Exports.h>
2018

2119
#include <aws/io/socket.h>
2220

include/aws/crt/io/Bootstrap.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#pragma once
2+
/*
3+
* Copyright 2010-2018 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+
17+
#include <aws/crt/Api.h>
18+
#include <aws/crt/Exports.h>
19+
#include <aws/crt/Types.h>
20+
#include <aws/crt/io/EventLoopGroup.h>
21+
22+
#include <aws/io/channel_bootstrap.h>
23+
24+
class EventLoopGroup;
25+
26+
namespace Aws
27+
{
28+
namespace Crt
29+
{
30+
namespace Io
31+
{
32+
class AWS_CRT_CPP_API ClientBootstrap final
33+
{
34+
public:
35+
ClientBootstrap(const EventLoopGroup& elGroup = EventLoopGroup(),
36+
Allocator* allocator = DefaultAllocator()) noexcept;
37+
~ClientBootstrap();
38+
ClientBootstrap(const ClientBootstrap&) = delete;
39+
ClientBootstrap& operator=(const ClientBootstrap&) = delete;
40+
ClientBootstrap(ClientBootstrap&&) noexcept;
41+
ClientBootstrap& operator=(ClientBootstrap&&) noexcept;
42+
43+
operator bool() noexcept;
44+
int LastError() noexcept;
45+
46+
const aws_client_bootstrap* GetUnderlyingHandle() const;
47+
private:
48+
aws_client_bootstrap m_bootstrap;
49+
int m_lastError;
50+
};
51+
}
52+
}
53+
}

include/aws/crt/io/EventLoopGroup.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
* express or implied. See the License for the specific language governing
1414
* permissions and limitations under the License.
1515
*/
16-
#include <aws/io/event_loop.h>
16+
#include <aws/crt/Api.h>
1717

18+
#include <aws/io/event_loop.h>
1819
#include <aws/crt/Exports.h>
1920

2021
namespace Aws
@@ -38,9 +39,8 @@ namespace Aws
3839
class AWS_CRT_CPP_API EventLoopGroup final
3940
{
4041
public:
41-
EventLoopGroup() noexcept;
42-
EventLoopGroup(uint16_t threadCount) noexcept;
43-
EventLoopGroup(aws_allocator* allocator, uint16_t threadCount) noexcept;
42+
EventLoopGroup(Allocator* allocator = DefaultAllocator()) noexcept;
43+
EventLoopGroup(uint16_t threadCount, Allocator* allocator = DefaultAllocator()) noexcept;
4444
~EventLoopGroup();
4545
EventLoopGroup(const EventLoopGroup&) = delete;
4646
EventLoopGroup(EventLoopGroup&&) noexcept;
@@ -50,7 +50,7 @@ namespace Aws
5050
operator bool() const;
5151
int LastError() const;
5252

53-
const aws_event_loop_group *GetUnderlyingHandle() const;
53+
const aws_event_loop_group* GetUnderlyingHandle() const;
5454

5555
private:
5656
aws_event_loop_group m_eventLoopGroup;

include/aws/crt/io/TLSOptions.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* permissions and limitations under the License.
1515
*/
1616

17+
#include <aws/crt/Api.h>
1718
#include <aws/crt/Exports.h>
1819
#include <aws/crt/Types.h>
1920

@@ -28,6 +29,33 @@ namespace Aws
2829
namespace Io
2930
{
3031
using TLSCtxOptions = aws_tls_ctx_options;
32+
using TlSConnectionOptions = aws_tls_connection_options;
33+
34+
enum class TLSMode
35+
{
36+
CLIENT,
37+
SERVER,
38+
};
39+
40+
class AWS_CRT_CPP_API TLSContext final
41+
{
42+
public:
43+
TLSContext(TLSCtxOptions& options, TLSMode mode, Allocator* allocator = DefaultAllocator()) noexcept;
44+
~TLSContext();
45+
TLSContext(const TLSContext&) = delete;
46+
TLSContext& operator=(const TLSContext&) = delete;
47+
TLSContext(TLSContext&&) noexcept;
48+
TLSContext& operator=(TLSContext&&) noexcept;
49+
50+
TlSConnectionOptions NewConnectionOptions() const noexcept;
51+
52+
operator bool() const noexcept;
53+
int LastError() const noexcept;
54+
55+
private:
56+
aws_tls_ctx* m_ctx;
57+
int m_lastError;
58+
};
3159

3260
AWS_CRT_CPP_API void InitDefaultClient(TLSCtxOptions& options) noexcept;
3361
AWS_CRT_CPP_API void InitClientWithMTLS(TLSCtxOptions& options,

include/aws/crt/mqtt/MqttClient.h

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <aws/crt/Types.h>
1818

1919
#include <aws/crt/io/TLSOptions.h>
20-
2120
#include <aws/mqtt/client.h>
2221

2322
#include <functional>
@@ -29,7 +28,7 @@ namespace Aws
2928
{
3029
namespace Io
3130
{
32-
class EventLoopGroup;
31+
class ClientBootstrap;
3332
}
3433

3534
namespace Mqtt
@@ -146,7 +145,7 @@ namespace Aws
146145
private:
147146
MqttConnection(MqttClient* client, const std::string& hostName, uint16_t port,
148147
const Io::SocketOptions& socketOptions,
149-
const Io::TLSCtxOptions& tlsCtxOptions) noexcept;
148+
Io::TlSConnectionOptions&& tlsConnOptions) noexcept;
150149

151150
MqttClient *m_owningClient;
152151
aws_mqtt_client_connection *m_underlyingConnection;
@@ -179,14 +178,10 @@ namespace Aws
179178

180179
public:
181180
/**
182-
* Initialize an MqttClient using eventLoopGroup. The default
183-
* allocator will be used.
184-
*/
185-
MqttClient(const Io::EventLoopGroup& eventLoopGroup) noexcept;
186-
/**
187-
* Inialize an MqttClient using allocator and eventLoopGroup.
181+
* Initialize an MqttClient using bootstrap and allocator
188182
*/
189-
MqttClient(Allocator& allocator, const Io::EventLoopGroup& eventLoopGroup) noexcept;
183+
MqttClient(const Io::ClientBootstrap& bootstrap, Allocator* allocator = DefaultAllocator()) noexcept;
184+
190185
~MqttClient();
191186
MqttClient(const MqttClient&) = delete;
192187
MqttClient(MqttClient&&) noexcept;
@@ -201,7 +196,7 @@ namespace Aws
201196
* all of its connection instances.
202197
*/
203198
MqttConnection NewConnection(const std::string& hostName, uint16_t port,
204-
const Io::SocketOptions& socketOptions, const Io::TLSCtxOptions& tlsCtxOptions) noexcept;
199+
const Io::SocketOptions& socketOptions, Io::TlSConnectionOptions&& tlsConnOptions) noexcept;
205200

206201
private:
207202
aws_mqtt_client m_client;

samples/mqtt_pub_sub/CMakeLists.txt

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
project(aws-crt-cpp-mqtt-pub-sub CXX)
22

3+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_INSTALL_PREFIX}/lib/cmake")
4+
35
file(GLOB MQTT_PUB_SUB_SRC
46
"*.cpp"
57
)
@@ -25,14 +27,5 @@ target_include_directories(${PUB_SUB_PROJECT_NAME} PUBLIC
2527
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
2628
$<INSTALL_INTERFACE:include>)
2729

28-
find_package(aws-c-common REQUIRED)
29-
find_package(aws-c-io REQUIRED)
30-
31-
if (UNIX AND NOT APPLE)
32-
find_package(s2n REQUIRED)
33-
endif()
34-
35-
find_package(aws-c-mqtt REQUIRED)
36-
37-
target_link_libraries(${PUB_SUB_PROJECT_NAME} AWS::aws-c-common aws-c-io aws-c-mqtt aws-crt-cpp ${PLATFORM_LIBS})
30+
target_link_libraries(${PUB_SUB_PROJECT_NAME} aws-crt-cpp)
3831

samples/mqtt_pub_sub/main.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@
1313
* permissions and limitations under the License.
1414
*/
1515
#include <aws/crt/Api.h>
16+
#include <aws/crt/io/EventLoopGroup.h>
17+
#include <aws/crt/io/TLSOptions.h>
18+
#include <aws/crt/mqtt/MqttClient.h>
1619

1720
#include <iostream>
1821
#include <mutex>
1922
#include <condition_variable>
23+
#include <algorithm>
24+
#include <aws/crt/io/Bootstrap.h>
2025

2126
using namespace Aws::Crt;
2227

@@ -101,7 +106,6 @@ int main(int argc, char* argv[])
101106
*/
102107
Io::TLSCtxOptions tlsCtxOptions;
103108
Io::InitClientWithMTLS(tlsCtxOptions, certificatePath.c_str(), keyPath.c_str());
104-
105109
/*
106110
* If we have a custom CA, set that up here.
107111
*/
@@ -121,6 +125,15 @@ int main(int argc, char* argv[])
121125
port = 443;
122126
}
123127

128+
Io::TLSContext tlsCtx(tlsCtxOptions, Io::TLSMode::CLIENT);
129+
130+
if (!tlsCtx)
131+
{
132+
fprintf(stderr, "Tls Context creation failed with error %s\n",
133+
ErrorDebugString(tlsCtx.LastError()));
134+
exit(-1);
135+
}
136+
124137
/*
125138
* Default Socket options to use. IPV4 will be ignored based on what DNS
126139
* tells us.
@@ -133,12 +146,21 @@ int main(int argc, char* argv[])
133146
socketOptions.keep_alive_timeout_sec = 0;
134147
socketOptions.keepalive = false;
135148

149+
Io::ClientBootstrap bootstrap(eventLoopGroup);
150+
151+
if (!bootstrap)
152+
{
153+
fprintf(stderr, "ClientBootstrap failed with error %s\n",
154+
ErrorDebugString(bootstrap.LastError()));
155+
exit(-1);
156+
}
157+
136158
/*
137159
* Now Create a client. This can not throw.
138160
* An instance of a client must outlive its connections.
139161
* It is the users responsibility to make sure of this.
140162
*/
141-
Mqtt::MqttClient mqttClient(eventLoopGroup);
163+
Mqtt::MqttClient mqttClient(bootstrap);
142164

143165
/*
144166
* Since no exceptions are used, always check the bool operator
@@ -156,7 +178,7 @@ int main(int argc, char* argv[])
156178
* and its underlying memory is managed by the client.
157179
*/
158180
Mqtt::MqttConnection connection =
159-
mqttClient.NewConnection(endpoint, port, socketOptions, tlsCtxOptions);
181+
mqttClient.NewConnection(endpoint, port, socketOptions, tlsCtx.NewConnectionOptions());
160182

161183
if (!connection)
162184
{

source/Api.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515
#include <aws/crt/Api.h>
1616

17+
#include <aws/crt/io/TLSOptions.h>
18+
1719
namespace Aws
1820
{
1921
namespace Crt

0 commit comments

Comments
 (0)