Skip to content

Commit 2982f83

Browse files
authored
Merge pull request #98 from boostorg/94-unify-redisconnection-and-redissslconnection
94 unify redisconnection and redissslconnection
2 parents 30a6e34 + 663e9ac commit 2982f83

36 files changed

+238
-487
lines changed

CMakeLists.txt

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,21 @@ find_package(OpenSSL REQUIRED)
5555
enable_testing()
5656
include_directories(include)
5757

58+
#=======================================================================
59+
60+
add_library(boost_redis_src STATIC examples/boost_redis.cpp)
61+
target_compile_features(boost_redis_src PUBLIC cxx_std_20)
62+
if (MSVC)
63+
target_compile_options(boost_redis_src PRIVATE /bigobj)
64+
target_compile_definitions(boost_redis_src PRIVATE _WIN32_WINNT=0x0601)
65+
endif()
66+
5867
# Main function for the examples.
5968
#=======================================================================
6069

61-
add_library(test_common STATIC
62-
tests/common.cpp
63-
)
70+
add_library(test_common STATIC)
71+
target_sources(test_common PUBLIC tests/common.cpp)
72+
target_link_libraries(test_common PUBLIC OpenSSL::Crypto OpenSSL::SSL boost_redis_src)
6473
target_compile_features(test_common PUBLIC cxx_std_17)
6574
if (MSVC)
6675
target_compile_options(test_common PRIVATE /bigobj)
@@ -69,22 +78,19 @@ endif()
6978

7079
#=======================================================================
7180

72-
add_library(common STATIC
73-
examples/start.cpp
74-
examples/main.cpp
75-
examples/boost_redis.cpp
76-
)
77-
target_compile_features(common PUBLIC cxx_std_20)
81+
add_library(examples_common STATIC examples/main.cpp)
82+
target_compile_features(examples_common PUBLIC cxx_std_20)
83+
target_link_libraries(examples_common PRIVATE boost_redis_src)
7884
if (MSVC)
79-
target_compile_options(common PRIVATE /bigobj)
80-
target_compile_definitions(common PRIVATE _WIN32_WINNT=0x0601)
85+
target_compile_options(examples_common PRIVATE /bigobj)
86+
target_compile_definitions(examples_common PRIVATE _WIN32_WINNT=0x0601)
8187
endif()
8288

8389
# Executables
8490
#=======================================================================
8591

8692
add_executable(cpp20_intro examples/cpp20_intro.cpp)
87-
target_link_libraries(cpp20_intro common)
93+
target_link_libraries(cpp20_intro PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common)
8894
target_compile_features(cpp20_intro PUBLIC cxx_std_20)
8995
add_test(cpp20_intro cpp20_intro)
9096
if (MSVC)
@@ -93,14 +99,15 @@ if (MSVC)
9399
endif()
94100

95101
add_executable(cpp20_streams examples/cpp20_streams.cpp)
96-
target_link_libraries(cpp20_streams common)
102+
target_link_libraries(cpp20_streams PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common)
97103
target_compile_features(cpp20_streams PUBLIC cxx_std_20)
98104
if (MSVC)
99105
target_compile_options(cpp20_streams PRIVATE /bigobj)
100106
target_compile_definitions(cpp20_streams PRIVATE _WIN32_WINNT=0x0601)
101107
endif()
102108

103109
add_executable(cpp17_intro examples/cpp17_intro.cpp)
110+
target_link_libraries(cpp17_intro PRIVATE OpenSSL::Crypto OpenSSL::SSL)
104111
target_compile_features(cpp17_intro PUBLIC cxx_std_17)
105112
add_test(cpp17_intro cpp17_intro)
106113
if (MSVC)
@@ -111,18 +118,19 @@ endif()
111118
if (NOT MSVC)
112119
add_executable(cpp17_intro_sync examples/cpp17_intro_sync.cpp)
113120
target_compile_features(cpp17_intro_sync PUBLIC cxx_std_17)
121+
target_link_libraries(cpp17_intro_sync PRIVATE OpenSSL::Crypto OpenSSL::SSL)
114122
add_test(cpp17_intro_sync cpp17_intro_sync)
115123
endif()
116124

117125
if (NOT MSVC)
118126
add_executable(cpp20_chat_room examples/cpp20_chat_room.cpp)
119127
target_compile_features(cpp20_chat_room PUBLIC cxx_std_20)
120-
target_link_libraries(cpp20_chat_room common)
128+
target_link_libraries(cpp20_chat_room PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common)
121129
endif()
122130

123131
add_executable(cpp20_containers examples/cpp20_containers.cpp)
124132
target_compile_features(cpp20_containers PUBLIC cxx_std_20)
125-
target_link_libraries(cpp20_containers common)
133+
target_link_libraries(cpp20_containers PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common)
126134
add_test(cpp20_containers cpp20_containers)
127135
if (MSVC)
128136
target_compile_options(cpp20_containers PRIVATE /bigobj)
@@ -132,12 +140,12 @@ endif()
132140
if (NOT MSVC)
133141
add_executable(cpp20_echo_server examples/cpp20_echo_server.cpp)
134142
target_compile_features(cpp20_echo_server PUBLIC cxx_std_20)
135-
target_link_libraries(cpp20_echo_server common)
143+
target_link_libraries(cpp20_echo_server PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common)
136144
endif()
137145

138146
add_executable(cpp20_resolve_with_sentinel examples/cpp20_resolve_with_sentinel.cpp)
139147
target_compile_features(cpp20_resolve_with_sentinel PUBLIC cxx_std_20)
140-
target_link_libraries(cpp20_resolve_with_sentinel common)
148+
target_link_libraries(cpp20_resolve_with_sentinel PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common)
141149
#add_test(cpp20_resolve_with_sentinel cpp20_resolve_with_sentinel)
142150
if (MSVC)
143151
target_compile_options(cpp20_resolve_with_sentinel PRIVATE /bigobj)
@@ -146,7 +154,7 @@ endif()
146154

147155
add_executable(cpp20_json examples/cpp20_json.cpp)
148156
target_compile_features(cpp20_json PUBLIC cxx_std_20)
149-
target_link_libraries(cpp20_json common)
157+
target_link_libraries(cpp20_json PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common)
150158
add_test(cpp20_json cpp20_json)
151159
if (MSVC)
152160
target_compile_options(cpp20_json PRIVATE /bigobj)
@@ -157,7 +165,7 @@ if (Protobuf_FOUND)
157165
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS examples/person.proto)
158166
add_executable(cpp20_protobuf examples/cpp20_protobuf.cpp ${PROTO_SRCS} ${PROTO_HDRS})
159167
target_compile_features(cpp20_protobuf PUBLIC cxx_std_20)
160-
target_link_libraries(cpp20_protobuf common ${Protobuf_LIBRARIES})
168+
target_link_libraries(cpp20_protobuf PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common ${Protobuf_LIBRARIES})
161169
target_include_directories(cpp20_protobuf PUBLIC ${Protobuf_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR})
162170
add_test(cpp20_protobuf cpp20_protobuf)
163171
if (MSVC)
@@ -168,7 +176,7 @@ endif()
168176

169177
add_executable(cpp20_subscriber examples/cpp20_subscriber.cpp)
170178
target_compile_features(cpp20_subscriber PUBLIC cxx_std_20)
171-
target_link_libraries(cpp20_subscriber common)
179+
target_link_libraries(cpp20_subscriber PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common)
172180
if (MSVC)
173181
target_compile_options(cpp20_subscriber PRIVATE /bigobj)
174182
target_compile_definitions(cpp20_subscriber PRIVATE _WIN32_WINNT=0x0601)
@@ -177,8 +185,7 @@ endif()
177185
add_executable(cpp20_intro_tls examples/cpp20_intro_tls.cpp)
178186
target_compile_features(cpp20_intro_tls PUBLIC cxx_std_20)
179187
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)
188+
target_link_libraries(cpp20_intro_tls PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common)
182189
if (MSVC)
183190
target_compile_options(cpp20_intro_tls PRIVATE /bigobj)
184191
target_compile_definitions(cpp20_intro_tls PRIVATE _WIN32_WINNT=0x0601)
@@ -187,7 +194,7 @@ endif()
187194
add_executable(cpp20_low_level_async tests/cpp20_low_level_async.cpp)
188195
target_compile_features(cpp20_low_level_async PUBLIC cxx_std_20)
189196
add_test(cpp20_low_level_async cpp20_low_level_async)
190-
target_link_libraries(cpp20_low_level_async common)
197+
target_link_libraries(cpp20_low_level_async PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common boost_redis_src)
191198
if (MSVC)
192199
target_compile_options(cpp20_low_level_async PRIVATE /bigobj)
193200
target_compile_definitions(cpp20_low_level_async PRIVATE _WIN32_WINNT=0x0601)
@@ -209,6 +216,7 @@ endif()
209216

210217
add_executable(cpp17_low_level_sync tests/cpp17_low_level_sync.cpp)
211218
target_compile_features(cpp17_low_level_sync PUBLIC cxx_std_17)
219+
target_link_libraries(cpp17_low_level_sync PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common boost_redis_src)
212220
add_test(cpp17_low_level_sync cpp17_low_level_sync)
213221
if (MSVC)
214222
target_compile_options(cpp17_low_level_sync PRIVATE /bigobj)
@@ -217,7 +225,7 @@ endif()
217225

218226
add_executable(test_conn_exec tests/conn_exec.cpp)
219227
target_compile_features(test_conn_exec PUBLIC cxx_std_20)
220-
target_link_libraries(test_conn_exec test_common)
228+
target_link_libraries(test_conn_exec PRIVATE OpenSSL::Crypto OpenSSL::SSL test_common)
221229
add_test(test_conn_exec test_conn_exec)
222230
if (MSVC)
223231
target_compile_options(test_conn_exec PRIVATE /bigobj)
@@ -226,7 +234,7 @@ endif()
226234

227235
add_executable(test_conn_exec_retry tests/conn_exec_retry.cpp)
228236
target_compile_features(test_conn_exec_retry PUBLIC cxx_std_20)
229-
target_link_libraries(test_conn_exec_retry test_common)
237+
target_link_libraries(test_conn_exec_retry PRIVATE OpenSSL::Crypto OpenSSL::SSL test_common)
230238
add_test(test_conn_exec_retry test_conn_exec_retry)
231239
if (MSVC)
232240
target_compile_options(test_conn_exec_retry PRIVATE /bigobj)
@@ -235,7 +243,7 @@ endif()
235243

236244
add_executable(test_conn_push tests/conn_push.cpp)
237245
target_compile_features(test_conn_push PUBLIC cxx_std_20)
238-
target_link_libraries(test_conn_push test_common)
246+
target_link_libraries(test_conn_push PRIVATE OpenSSL::Crypto OpenSSL::SSL test_common)
239247
add_test(test_conn_push test_conn_push)
240248
if (MSVC)
241249
target_compile_options(test_conn_push PRIVATE /bigobj)
@@ -244,6 +252,7 @@ endif()
244252

245253
add_executable(test_conn_quit tests/conn_quit.cpp)
246254
target_compile_features(test_conn_quit PUBLIC cxx_std_17)
255+
target_link_libraries(test_conn_quit PRIVATE OpenSSL::Crypto OpenSSL::SSL test_common)
247256
add_test(test_conn_quit test_conn_quit)
248257
if (MSVC)
249258
target_compile_options(test_conn_quit PRIVATE /bigobj)
@@ -252,7 +261,7 @@ endif()
252261

253262
add_executable(test_conn_reconnect tests/conn_reconnect.cpp)
254263
target_compile_features(test_conn_reconnect PUBLIC cxx_std_20)
255-
target_link_libraries(test_conn_reconnect common test_common)
264+
target_link_libraries(test_conn_reconnect PRIVATE OpenSSL::Crypto OpenSSL::SSL test_common)
256265
add_test(test_conn_reconnect test_conn_reconnect)
257266
if (MSVC)
258267
target_compile_options(test_conn_reconnect PRIVATE /bigobj)
@@ -262,14 +271,15 @@ endif()
262271
add_executable(test_conn_tls tests/conn_tls.cpp)
263272
add_test(test_conn_tls test_conn_tls)
264273
target_compile_features(test_conn_tls PUBLIC cxx_std_17)
265-
target_link_libraries(test_conn_tls OpenSSL::Crypto OpenSSL::SSL)
274+
target_link_libraries(test_conn_tls PRIVATE OpenSSL::Crypto OpenSSL::SSL boost_redis_src)
266275
if (MSVC)
267276
target_compile_options(test_conn_tls PRIVATE /bigobj)
268277
target_compile_definitions(test_conn_tls PRIVATE _WIN32_WINNT=0x0601)
269278
endif()
270279

271280
add_executable(test_low_level tests/low_level.cpp)
272281
target_compile_features(test_low_level PUBLIC cxx_std_17)
282+
target_link_libraries(test_low_level PRIVATE OpenSSL::Crypto OpenSSL::SSL boost_redis_src)
273283
add_test(test_low_level test_low_level)
274284
if (MSVC)
275285
target_compile_options(test_low_level PRIVATE /bigobj)
@@ -279,14 +289,15 @@ endif()
279289
add_executable(test_conn_run_cancel tests/conn_run_cancel.cpp)
280290
target_compile_features(test_conn_run_cancel PUBLIC cxx_std_20)
281291
add_test(test_conn_run_cancel test_conn_run_cancel)
292+
target_link_libraries(test_conn_run_cancel PRIVATE OpenSSL::Crypto OpenSSL::SSL boost_redis_src)
282293
if (MSVC)
283294
target_compile_options(test_conn_run_cancel PRIVATE /bigobj)
284295
target_compile_definitions(test_conn_run_cancel PRIVATE _WIN32_WINNT=0x0601)
285296
endif()
286297

287298
add_executable(test_conn_exec_cancel tests/conn_exec_cancel.cpp)
288299
target_compile_features(test_conn_exec_cancel PUBLIC cxx_std_20)
289-
target_link_libraries(test_conn_exec_cancel common test_common)
300+
target_link_libraries(test_conn_exec_cancel PRIVATE OpenSSL::Crypto OpenSSL::SSL test_common boost_redis_src)
290301
add_test(test_conn_exec_cancel test_conn_exec_cancel)
291302
if (MSVC)
292303
target_compile_options(test_conn_exec_cancel PRIVATE /bigobj)
@@ -295,7 +306,7 @@ endif()
295306

296307
add_executable(test_conn_exec_cancel2 tests/conn_exec_cancel2.cpp)
297308
target_compile_features(test_conn_exec_cancel2 PUBLIC cxx_std_20)
298-
target_link_libraries(test_conn_exec_cancel2 common test_common)
309+
target_link_libraries(test_conn_exec_cancel2 PRIVATE OpenSSL::Crypto OpenSSL::SSL test_common boost_redis_src)
299310
add_test(test_conn_exec_cancel2 test_conn_exec_cancel2)
300311
if (MSVC)
301312
target_compile_options(test_conn_exec_cancel2 PRIVATE /bigobj)
@@ -304,7 +315,7 @@ endif()
304315

305316
add_executable(test_conn_exec_error tests/conn_exec_error.cpp)
306317
target_compile_features(test_conn_exec_error PUBLIC cxx_std_17)
307-
target_link_libraries(test_conn_exec_error common test_common)
318+
target_link_libraries(test_conn_exec_error PRIVATE OpenSSL::Crypto OpenSSL::SSL test_common boost_redis_src)
308319
add_test(test_conn_exec_error test_conn_exec_error)
309320
if (MSVC)
310321
target_compile_options(test_conn_exec_error PRIVATE /bigobj)
@@ -313,7 +324,7 @@ endif()
313324

314325
add_executable(test_conn_echo_stress tests/conn_echo_stress.cpp)
315326
target_compile_features(test_conn_echo_stress PUBLIC cxx_std_20)
316-
target_link_libraries(test_conn_echo_stress common test_common)
327+
target_link_libraries(test_conn_echo_stress PRIVATE OpenSSL::Crypto OpenSSL::SSL test_common boost_redis_src)
317328
add_test(test_conn_echo_stress test_conn_echo_stress)
318329
if (MSVC)
319330
target_compile_options(test_conn_echo_stress PRIVATE /bigobj)
@@ -322,6 +333,7 @@ endif()
322333

323334
add_executable(test_request tests/request.cpp)
324335
target_compile_features(test_request PUBLIC cxx_std_17)
336+
target_link_libraries(test_request PRIVATE OpenSSL::Crypto OpenSSL::SSL test_common boost_redis_src)
325337
add_test(test_request test_request)
326338
if (MSVC)
327339
target_compile_options(test_request PRIVATE /bigobj)
@@ -330,7 +342,7 @@ endif()
330342

331343
add_executable(test_issue_50 tests/issue_50.cpp)
332344
target_compile_features(test_issue_50 PUBLIC cxx_std_20)
333-
target_link_libraries(test_issue_50 common)
345+
target_link_libraries(test_issue_50 PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common boost_redis_src)
334346
add_test(test_issue_50 test_issue_50)
335347
if (MSVC)
336348
target_compile_options(test_issue_50 PRIVATE /bigobj)
@@ -339,7 +351,7 @@ endif()
339351

340352
add_executable(test_conn_check_health tests/conn_check_health.cpp)
341353
target_compile_features(test_conn_check_health PUBLIC cxx_std_17)
342-
target_link_libraries(test_conn_check_health common)
354+
target_link_libraries(test_conn_check_health PRIVATE OpenSSL::Crypto OpenSSL::SSL examples_common boost_redis_src)
343355
add_test(test_conn_check_health test_conn_check_health)
344356
if (MSVC)
345357
target_compile_options(test_conn_check_health PRIVATE /bigobj)
@@ -348,7 +360,7 @@ endif()
348360

349361
add_executable(test_run tests/run.cpp)
350362
target_compile_features(test_run PUBLIC cxx_std_17)
351-
target_link_libraries(test_run test_common)
363+
target_link_libraries(test_run PRIVATE OpenSSL::Crypto OpenSSL::SSL test_common boost_redis_src)
352364
add_test(test_run test_run)
353365
if (MSVC)
354366
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_sync.cpp

Lines changed: 1 addition & 0 deletions
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;

examples/cpp20_chat_room.cpp

Lines changed: 1 addition & 1 deletion
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);

examples/cpp20_intro_tls.cpp

Lines changed: 4 additions & 4 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,13 +28,13 @@ 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);
37+
auto conn = std::make_shared<connection>(co_await net::this_coro::executor);
3838
conn->async_run(cfg, {}, net::consign(net::detached, conn));
3939

4040
request req;

examples/cpp20_subscriber.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ receiver(std::shared_ptr<connection> conn) -> net::awaitable<void>
5151
request req;
5252
req.push("SUBSCRIBE", "channel");
5353

54-
while (!conn->is_cancelled()) {
54+
while (conn->will_reconnect()) {
5555

5656
// Reconnect to channels.
5757
co_await conn->async_exec(req);

0 commit comments

Comments
 (0)