Skip to content

Commit 6f9fd5b

Browse files
committed
Unifies ssl and plain connections.
1 parent 30a6e34 commit 6f9fd5b

37 files changed

+251
-425
lines changed

CMakeLists.txt

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ endif()
8484
#=======================================================================
8585

8686
add_executable(cpp20_intro examples/cpp20_intro.cpp)
87-
target_link_libraries(cpp20_intro common)
87+
target_link_libraries(cpp20_intro PRIVATE OpenSSL::Crypto OpenSSL::SSL common)
8888
target_compile_features(cpp20_intro PUBLIC cxx_std_20)
8989
add_test(cpp20_intro cpp20_intro)
9090
if (MSVC)
@@ -93,14 +93,15 @@ if (MSVC)
9393
endif()
9494

9595
add_executable(cpp20_streams examples/cpp20_streams.cpp)
96-
target_link_libraries(cpp20_streams common)
96+
target_link_libraries(cpp20_streams PRIVATE OpenSSL::Crypto OpenSSL::SSL common)
9797
target_compile_features(cpp20_streams PUBLIC cxx_std_20)
9898
if (MSVC)
9999
target_compile_options(cpp20_streams PRIVATE /bigobj)
100100
target_compile_definitions(cpp20_streams PRIVATE _WIN32_WINNT=0x0601)
101101
endif()
102102

103103
add_executable(cpp17_intro examples/cpp17_intro.cpp)
104+
target_link_libraries(cpp17_intro PRIVATE OpenSSL::Crypto OpenSSL::SSL)
104105
target_compile_features(cpp17_intro PUBLIC cxx_std_17)
105106
add_test(cpp17_intro cpp17_intro)
106107
if (MSVC)
@@ -111,18 +112,19 @@ endif()
111112
if (NOT MSVC)
112113
add_executable(cpp17_intro_sync examples/cpp17_intro_sync.cpp)
113114
target_compile_features(cpp17_intro_sync PUBLIC cxx_std_17)
115+
target_link_libraries(cpp17_intro_sync PRIVATE OpenSSL::Crypto OpenSSL::SSL)
114116
add_test(cpp17_intro_sync cpp17_intro_sync)
115117
endif()
116118

117119
if (NOT MSVC)
118120
add_executable(cpp20_chat_room examples/cpp20_chat_room.cpp)
119121
target_compile_features(cpp20_chat_room PUBLIC cxx_std_20)
120-
target_link_libraries(cpp20_chat_room common)
122+
target_link_libraries(cpp20_chat_room PRIVATE OpenSSL::Crypto OpenSSL::SSL common)
121123
endif()
122124

123125
add_executable(cpp20_containers examples/cpp20_containers.cpp)
124126
target_compile_features(cpp20_containers PUBLIC cxx_std_20)
125-
target_link_libraries(cpp20_containers common)
127+
target_link_libraries(cpp20_containers PRIVATE OpenSSL::Crypto OpenSSL::SSL common)
126128
add_test(cpp20_containers cpp20_containers)
127129
if (MSVC)
128130
target_compile_options(cpp20_containers PRIVATE /bigobj)
@@ -132,12 +134,12 @@ endif()
132134
if (NOT MSVC)
133135
add_executable(cpp20_echo_server examples/cpp20_echo_server.cpp)
134136
target_compile_features(cpp20_echo_server PUBLIC cxx_std_20)
135-
target_link_libraries(cpp20_echo_server common)
137+
target_link_libraries(cpp20_echo_server PRIVATE OpenSSL::Crypto OpenSSL::SSL common)
136138
endif()
137139

138140
add_executable(cpp20_resolve_with_sentinel examples/cpp20_resolve_with_sentinel.cpp)
139141
target_compile_features(cpp20_resolve_with_sentinel PUBLIC cxx_std_20)
140-
target_link_libraries(cpp20_resolve_with_sentinel common)
142+
target_link_libraries(cpp20_resolve_with_sentinel PRIVATE OpenSSL::Crypto OpenSSL::SSL common)
141143
#add_test(cpp20_resolve_with_sentinel cpp20_resolve_with_sentinel)
142144
if (MSVC)
143145
target_compile_options(cpp20_resolve_with_sentinel PRIVATE /bigobj)
@@ -146,7 +148,7 @@ endif()
146148

147149
add_executable(cpp20_json examples/cpp20_json.cpp)
148150
target_compile_features(cpp20_json PUBLIC cxx_std_20)
149-
target_link_libraries(cpp20_json common)
151+
target_link_libraries(cpp20_json PRIVATE OpenSSL::Crypto OpenSSL::SSL common)
150152
add_test(cpp20_json cpp20_json)
151153
if (MSVC)
152154
target_compile_options(cpp20_json PRIVATE /bigobj)
@@ -157,7 +159,7 @@ if (Protobuf_FOUND)
157159
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS examples/person.proto)
158160
add_executable(cpp20_protobuf examples/cpp20_protobuf.cpp ${PROTO_SRCS} ${PROTO_HDRS})
159161
target_compile_features(cpp20_protobuf PUBLIC cxx_std_20)
160-
target_link_libraries(cpp20_protobuf common ${Protobuf_LIBRARIES})
162+
target_link_libraries(cpp20_protobuf PRIVATE OpenSSL::Crypto OpenSSL::SSL common ${Protobuf_LIBRARIES})
161163
target_include_directories(cpp20_protobuf PUBLIC ${Protobuf_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR})
162164
add_test(cpp20_protobuf cpp20_protobuf)
163165
if (MSVC)
@@ -168,7 +170,7 @@ endif()
168170

169171
add_executable(cpp20_subscriber examples/cpp20_subscriber.cpp)
170172
target_compile_features(cpp20_subscriber PUBLIC cxx_std_20)
171-
target_link_libraries(cpp20_subscriber common)
173+
target_link_libraries(cpp20_subscriber PRIVATE OpenSSL::Crypto OpenSSL::SSL common)
172174
if (MSVC)
173175
target_compile_options(cpp20_subscriber PRIVATE /bigobj)
174176
target_compile_definitions(cpp20_subscriber PRIVATE _WIN32_WINNT=0x0601)
@@ -177,8 +179,7 @@ endif()
177179
add_executable(cpp20_intro_tls examples/cpp20_intro_tls.cpp)
178180
target_compile_features(cpp20_intro_tls PUBLIC cxx_std_20)
179181
add_test(cpp20_intro_tls cpp20_intro_tls)
180-
target_link_libraries(cpp20_intro_tls OpenSSL::Crypto OpenSSL::SSL)
181-
target_link_libraries(cpp20_intro_tls common)
182+
target_link_libraries(cpp20_intro_tls PRIVATE OpenSSL::Crypto OpenSSL::SSL common)
182183
if (MSVC)
183184
target_compile_options(cpp20_intro_tls PRIVATE /bigobj)
184185
target_compile_definitions(cpp20_intro_tls PRIVATE _WIN32_WINNT=0x0601)
@@ -187,7 +188,7 @@ endif()
187188
add_executable(cpp20_low_level_async tests/cpp20_low_level_async.cpp)
188189
target_compile_features(cpp20_low_level_async PUBLIC cxx_std_20)
189190
add_test(cpp20_low_level_async cpp20_low_level_async)
190-
target_link_libraries(cpp20_low_level_async common)
191+
target_link_libraries(cpp20_low_level_async PRIVATE OpenSSL::Crypto OpenSSL::SSL common)
191192
if (MSVC)
192193
target_compile_options(cpp20_low_level_async PRIVATE /bigobj)
193194
target_compile_definitions(cpp20_low_level_async PRIVATE _WIN32_WINNT=0x0601)
@@ -217,7 +218,7 @@ endif()
217218

218219
add_executable(test_conn_exec tests/conn_exec.cpp)
219220
target_compile_features(test_conn_exec PUBLIC cxx_std_20)
220-
target_link_libraries(test_conn_exec test_common)
221+
target_link_libraries(test_conn_exec PRIVATE OpenSSL::Crypto OpenSSL::SSL test_common)
221222
add_test(test_conn_exec test_conn_exec)
222223
if (MSVC)
223224
target_compile_options(test_conn_exec PRIVATE /bigobj)
@@ -226,7 +227,7 @@ endif()
226227

227228
add_executable(test_conn_exec_retry tests/conn_exec_retry.cpp)
228229
target_compile_features(test_conn_exec_retry PUBLIC cxx_std_20)
229-
target_link_libraries(test_conn_exec_retry test_common)
230+
target_link_libraries(test_conn_exec_retry PRIVATE OpenSSL::Crypto OpenSSL::SSL test_common)
230231
add_test(test_conn_exec_retry test_conn_exec_retry)
231232
if (MSVC)
232233
target_compile_options(test_conn_exec_retry PRIVATE /bigobj)
@@ -235,7 +236,7 @@ endif()
235236

236237
add_executable(test_conn_push tests/conn_push.cpp)
237238
target_compile_features(test_conn_push PUBLIC cxx_std_20)
238-
target_link_libraries(test_conn_push test_common)
239+
target_link_libraries(test_conn_push PRIVATE OpenSSL::Crypto OpenSSL::SSL test_common)
239240
add_test(test_conn_push test_conn_push)
240241
if (MSVC)
241242
target_compile_options(test_conn_push PRIVATE /bigobj)
@@ -244,6 +245,7 @@ endif()
244245

245246
add_executable(test_conn_quit tests/conn_quit.cpp)
246247
target_compile_features(test_conn_quit PUBLIC cxx_std_17)
248+
target_link_libraries(test_conn_quit PRIVATE OpenSSL::Crypto OpenSSL::SSL test_common)
247249
add_test(test_conn_quit test_conn_quit)
248250
if (MSVC)
249251
target_compile_options(test_conn_quit PRIVATE /bigobj)
@@ -252,7 +254,7 @@ endif()
252254

253255
add_executable(test_conn_reconnect tests/conn_reconnect.cpp)
254256
target_compile_features(test_conn_reconnect PUBLIC cxx_std_20)
255-
target_link_libraries(test_conn_reconnect common test_common)
257+
target_link_libraries(test_conn_reconnect PRIVATE OpenSSL::Crypto OpenSSL::SSL common test_common)
256258
add_test(test_conn_reconnect test_conn_reconnect)
257259
if (MSVC)
258260
target_compile_options(test_conn_reconnect PRIVATE /bigobj)
@@ -262,7 +264,7 @@ endif()
262264
add_executable(test_conn_tls tests/conn_tls.cpp)
263265
add_test(test_conn_tls test_conn_tls)
264266
target_compile_features(test_conn_tls PUBLIC cxx_std_17)
265-
target_link_libraries(test_conn_tls OpenSSL::Crypto OpenSSL::SSL)
267+
target_link_libraries(test_conn_tls PRIVATE OpenSSL::Crypto OpenSSL::SSL)
266268
if (MSVC)
267269
target_compile_options(test_conn_tls PRIVATE /bigobj)
268270
target_compile_definitions(test_conn_tls PRIVATE _WIN32_WINNT=0x0601)
@@ -279,14 +281,15 @@ endif()
279281
add_executable(test_conn_run_cancel tests/conn_run_cancel.cpp)
280282
target_compile_features(test_conn_run_cancel PUBLIC cxx_std_20)
281283
add_test(test_conn_run_cancel test_conn_run_cancel)
284+
target_link_libraries(test_conn_run_cancel PRIVATE OpenSSL::Crypto OpenSSL::SSL)
282285
if (MSVC)
283286
target_compile_options(test_conn_run_cancel PRIVATE /bigobj)
284287
target_compile_definitions(test_conn_run_cancel PRIVATE _WIN32_WINNT=0x0601)
285288
endif()
286289

287290
add_executable(test_conn_exec_cancel tests/conn_exec_cancel.cpp)
288291
target_compile_features(test_conn_exec_cancel PUBLIC cxx_std_20)
289-
target_link_libraries(test_conn_exec_cancel common test_common)
292+
target_link_libraries(test_conn_exec_cancel PRIVATE OpenSSL::Crypto OpenSSL::SSL common test_common)
290293
add_test(test_conn_exec_cancel test_conn_exec_cancel)
291294
if (MSVC)
292295
target_compile_options(test_conn_exec_cancel PRIVATE /bigobj)
@@ -295,7 +298,7 @@ endif()
295298

296299
add_executable(test_conn_exec_cancel2 tests/conn_exec_cancel2.cpp)
297300
target_compile_features(test_conn_exec_cancel2 PUBLIC cxx_std_20)
298-
target_link_libraries(test_conn_exec_cancel2 common test_common)
301+
target_link_libraries(test_conn_exec_cancel2 PRIVATE OpenSSL::Crypto OpenSSL::SSL common test_common)
299302
add_test(test_conn_exec_cancel2 test_conn_exec_cancel2)
300303
if (MSVC)
301304
target_compile_options(test_conn_exec_cancel2 PRIVATE /bigobj)
@@ -304,7 +307,7 @@ endif()
304307

305308
add_executable(test_conn_exec_error tests/conn_exec_error.cpp)
306309
target_compile_features(test_conn_exec_error PUBLIC cxx_std_17)
307-
target_link_libraries(test_conn_exec_error common test_common)
310+
target_link_libraries(test_conn_exec_error PRIVATE OpenSSL::Crypto OpenSSL::SSL common test_common)
308311
add_test(test_conn_exec_error test_conn_exec_error)
309312
if (MSVC)
310313
target_compile_options(test_conn_exec_error PRIVATE /bigobj)
@@ -313,7 +316,7 @@ endif()
313316

314317
add_executable(test_conn_echo_stress tests/conn_echo_stress.cpp)
315318
target_compile_features(test_conn_echo_stress PUBLIC cxx_std_20)
316-
target_link_libraries(test_conn_echo_stress common test_common)
319+
target_link_libraries(test_conn_echo_stress PRIVATE OpenSSL::Crypto OpenSSL::SSL common test_common)
317320
add_test(test_conn_echo_stress test_conn_echo_stress)
318321
if (MSVC)
319322
target_compile_options(test_conn_echo_stress PRIVATE /bigobj)
@@ -330,7 +333,7 @@ endif()
330333

331334
add_executable(test_issue_50 tests/issue_50.cpp)
332335
target_compile_features(test_issue_50 PUBLIC cxx_std_20)
333-
target_link_libraries(test_issue_50 common)
336+
target_link_libraries(test_issue_50 PRIVATE OpenSSL::Crypto OpenSSL::SSL common)
334337
add_test(test_issue_50 test_issue_50)
335338
if (MSVC)
336339
target_compile_options(test_issue_50 PRIVATE /bigobj)
@@ -339,7 +342,7 @@ endif()
339342

340343
add_executable(test_conn_check_health tests/conn_check_health.cpp)
341344
target_compile_features(test_conn_check_health PUBLIC cxx_std_17)
342-
target_link_libraries(test_conn_check_health common)
345+
target_link_libraries(test_conn_check_health PRIVATE OpenSSL::Crypto OpenSSL::SSL common)
343346
add_test(test_conn_check_health test_conn_check_health)
344347
if (MSVC)
345348
target_compile_options(test_conn_check_health PRIVATE /bigobj)
@@ -348,7 +351,7 @@ endif()
348351

349352
add_executable(test_run tests/run.cpp)
350353
target_compile_features(test_run PUBLIC cxx_std_17)
351-
target_link_libraries(test_run test_common)
354+
target_link_libraries(test_run PRIVATE OpenSSL::Crypto OpenSSL::SSL test_common)
352355
add_test(test_run test_run)
353356
if (MSVC)
354357
target_compile_options(test_run PRIVATE /bigobj)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ Acknowledgement to people that helped shape Boost.Redis
678678
* Mohammad Nejati ([ashtum](https://github.com/ashtum)): For pointing out scenarios where calls to `async_exec` should fail when the connection is lost.
679679
* Klemens Morgenstern ([klemens-morgenstern](https://github.com/klemens-morgenstern)): For useful discussion about timeouts, cancellation, synchronous interfaces and general help with Asio.
680680
* Vinnie Falco ([vinniefalco](https://github.com/vinniefalco)): For general suggestions about how to improve the code and the documentation.
681-
* Bram Veldhoen ([bveldhoen](https://github.com/bveldhoen)): For contributing a Redis streams example.
681+
* Bram Veldhoen ([bveldhoen](https://github.com/bveldhoen)): For contributing a Redis-streams example.
682682

683683
Also many thanks to all individuals that participated in the Boost
684684
review

examples/cpp17_intro.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ auto main(int argc, char * argv[]) -> int
3232
response<std::string> resp;
3333

3434
net::io_context ioc;
35-
connection conn{ioc};
35+
net::ssl::context ctx{net::ssl::context::tls_client};
36+
connection conn{ioc, ctx};
3637

3738
conn.async_run(cfg, {}, net::detached);
3839

examples/cpp17_intro_sync.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// Include this in no more than one .cpp file.
1313
#include <boost/redis/src.hpp>
1414

15+
namespace net = boost::asio;
1516
using boost::redis::sync_connection;
1617
using boost::redis::request;
1718
using boost::redis::response;
@@ -27,7 +28,8 @@ auto main(int argc, char * argv[]) -> int
2728
cfg.addr.port = argv[2];
2829
}
2930

30-
sync_connection conn;
31+
net::ssl::context ctx{net::ssl::context::tls_client};
32+
sync_connection conn{ctx};
3133
conn.run(cfg);
3234

3335
request req;

examples/cpp20_chat_room.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ receiver(std::shared_ptr<connection> conn) -> net::awaitable<void>
3838
request req;
3939
req.push("SUBSCRIBE", "channel");
4040

41-
while (!conn->is_cancelled()) {
41+
while (conn->will_reconnect()) {
4242

4343
// Subscribe to channels.
4444
co_await conn->async_exec(req);
@@ -75,12 +75,13 @@ auto publisher(std::shared_ptr<stream_descriptor> in, std::shared_ptr<connection
7575
auto co_main(config cfg) -> net::awaitable<void>
7676
{
7777
auto ex = co_await net::this_coro::executor;
78-
auto conn = std::make_shared<connection>(ex);
78+
auto ctx = std::make_shared<net::ssl::context>(net::ssl::context::tls_client);
79+
auto conn = std::make_shared<connection>(ex, *ctx);
7980
auto stream = std::make_shared<stream_descriptor>(ex, ::dup(STDIN_FILENO));
8081

8182
net::co_spawn(ex, receiver(conn), net::detached);
8283
net::co_spawn(ex, publisher(stream, conn), net::detached);
83-
conn->async_run(cfg, {}, net::consign(net::detached, conn));
84+
conn->async_run(cfg, {}, net::consign(net::detached, conn, ctx));
8485

8586
signal_set sig_set{ex, SIGINT, SIGTERM};
8687
co_await sig_set.async_wait();

examples/cpp20_containers.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ auto transaction(std::shared_ptr<connection> conn) -> net::awaitable<void>
9191
// Called from the main function (see main.cpp)
9292
net::awaitable<void> co_main(config cfg)
9393
{
94-
auto conn = std::make_shared<connection>(co_await net::this_coro::executor);
95-
conn->async_run(cfg, {}, net::consign(net::detached, conn));
94+
auto ctx = std::make_shared<net::ssl::context>(net::ssl::context::tls_client);
95+
auto conn = std::make_shared<connection>(co_await net::this_coro::executor, *ctx);
96+
conn->async_run(cfg, {}, net::consign(net::detached, conn, ctx));
9697

9798
co_await store(conn);
9899
co_await transaction(conn);

examples/cpp20_echo_server.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ auto listener(std::shared_ptr<connection> conn) -> net::awaitable<void>
5858
auto co_main(config cfg) -> net::awaitable<void>
5959
{
6060
auto ex = co_await net::this_coro::executor;
61-
auto conn = std::make_shared<connection>(ex);
61+
auto ctx = std::make_shared<net::ssl::context>(net::ssl::context::tls_client);
62+
auto conn = std::make_shared<connection>(ex, *ctx);
6263
net::co_spawn(ex, listener(conn), net::detached);
63-
conn->async_run(cfg, {}, net::consign(net::detached, conn));
64+
conn->async_run(cfg, {}, net::consign(net::detached, conn, ctx));
6465

6566
signal_set sig_set(ex, SIGINT, SIGTERM);
6667
co_await sig_set.async_wait();

examples/cpp20_intro.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ using connection = net::deferred_t::as_default_on_t<boost::redis::connection>;
2323
// Called from the main function (see main.cpp)
2424
auto co_main(config cfg) -> net::awaitable<void>
2525
{
26-
auto conn = std::make_shared<connection>(co_await net::this_coro::executor);
27-
conn->async_run(cfg, {}, net::consign(net::detached, conn));
26+
auto ctx = std::make_shared<net::ssl::context>(net::ssl::context::tls_client);
27+
auto conn = std::make_shared<connection>(co_await net::this_coro::executor, *ctx);
28+
conn->async_run(cfg, {}, net::consign(net::detached, conn, ctx));
2829

2930
// A request containing only a ping command.
3031
request req;

examples/cpp20_intro_tls.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* accompanying file LICENSE.txt)
55
*/
66

7-
#include <boost/redis/ssl/connection.hpp>
7+
#include <boost/redis/connection.hpp>
88
#include <boost/asio/deferred.hpp>
99
#include <boost/asio/use_awaitable.hpp>
1010
#include <boost/asio/detached.hpp>
@@ -18,7 +18,7 @@ using boost::redis::request;
1818
using boost::redis::response;
1919
using boost::redis::config;
2020
using boost::redis::logger;
21-
using connection = net::deferred_t::as_default_on_t<boost::redis::ssl::connection>;
21+
using connection = net::deferred_t::as_default_on_t<boost::redis::connection>;
2222

2323
auto verify_certificate(bool, net::ssl::verify_context&) -> bool
2424
{
@@ -28,14 +28,15 @@ auto verify_certificate(bool, net::ssl::verify_context&) -> bool
2828

2929
auto co_main(config cfg) -> net::awaitable<void>
3030
{
31+
cfg.use_ssl = true;
3132
cfg.username = "aedis";
3233
cfg.password = "aedis";
3334
cfg.addr.host = "db.occase.de";
3435
cfg.addr.port = "6380";
3536

36-
net::ssl::context ctx{net::ssl::context::sslv23};
37-
auto conn = std::make_shared<connection>(co_await net::this_coro::executor, ctx);
38-
conn->async_run(cfg, {}, net::consign(net::detached, conn));
37+
auto ctx = std::make_shared<net::ssl::context>(net::ssl::context::tls_client);
38+
auto conn = std::make_shared<connection>(co_await net::this_coro::executor, *ctx);
39+
conn->async_run(cfg, {}, net::consign(net::detached, conn, ctx));
3940

4041
request req;
4142
req.push("PING");

examples/cpp20_json.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ void boost_redis_from_bulk(user& u, std::string_view sv, boost::system::error_co
4848
auto co_main(config cfg) -> net::awaitable<void>
4949
{
5050
auto ex = co_await net::this_coro::executor;
51-
auto conn = std::make_shared<connection>(ex);
52-
conn->async_run(cfg, {}, net::consign(net::detached, conn));
51+
auto ctx = std::make_shared<net::ssl::context>(net::ssl::context::tls_client);
52+
auto conn = std::make_shared<connection>(ex, *ctx);
53+
conn->async_run(cfg, {}, net::consign(net::detached, conn, ctx));
5354

5455
// user object that will be stored in Redis in json format.
5556
user const u{"Joao", "58", "Brazil"};

0 commit comments

Comments
 (0)