Skip to content

Commit c289696

Browse files
authored
Merge pull request #86 from boostorg/83-fix-reconnect-loop-in-the-subscriber-example
83 fix reconnect loop in the subscriber example
2 parents cd00047 + c7f49c6 commit c289696

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1055
-675
lines changed

CMakeLists.txt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ include_directories(include)
5959
#=======================================================================
6060

6161
add_library(common STATIC
62-
examples/common/common.cpp
63-
examples/common/main.cpp
64-
examples/common/boost_redis.cpp
62+
examples/start.cpp
63+
examples/main.cpp
64+
examples/boost_redis.cpp
6565
)
6666
target_compile_features(common PUBLIC cxx_std_20)
6767
if (MSVC)
@@ -98,12 +98,10 @@ if (MSVC)
9898
target_compile_definitions(cpp17_intro PRIVATE _WIN32_WINNT=0x0601)
9999
endif()
100100

101+
if (NOT MSVC)
101102
add_executable(cpp17_intro_sync examples/cpp17_intro_sync.cpp)
102103
target_compile_features(cpp17_intro_sync PUBLIC cxx_std_17)
103104
add_test(cpp17_intro_sync cpp17_intro_sync)
104-
if (MSVC)
105-
target_compile_options(cpp17_intro_sync PRIVATE /bigobj)
106-
target_compile_definitions(cpp17_intro_sync PRIVATE _WIN32_WINNT=0x0601)
107105
endif()
108106

109107
if (NOT MSVC)
@@ -320,6 +318,14 @@ target_link_libraries(test_conn_check_health common)
320318
add_test(test_conn_check_health test_conn_check_health)
321319
endif()
322320

321+
add_executable(test_run tests/run.cpp)
322+
target_compile_features(test_run PUBLIC cxx_std_17)
323+
add_test(test_run test_run)
324+
if (MSVC)
325+
target_compile_options(test_run PRIVATE /bigobj)
326+
target_compile_definitions(test_run PRIVATE _WIN32_WINNT=0x0601)
327+
endif()
328+
323329
# Install
324330
#=======================================================================
325331

CMakePresets.json

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,37 +40,37 @@
4040
}
4141
},
4242
{
43-
"name": "g++-11-cpp17",
43+
"name": "g++-11",
4444
"generator": "Unix Makefiles",
4545
"hidden": false,
4646
"inherits": ["cmake-pedantic"],
47-
"binaryDir": "${sourceDir}/build/g++-11-cpp17",
47+
"binaryDir": "${sourceDir}/build/g++-11",
4848
"cacheVariables": {
4949
"CMAKE_BUILD_TYPE": "Debug",
5050
"CMAKE_CXX_EXTENSIONS": "OFF",
5151
"CMAKE_CXX_FLAGS": "-Wall -Wextra -fsanitize=address",
5252
"CMAKE_CXX_COMPILER": "g++-11",
5353
"CMAKE_SHARED_LINKER_FLAGS": "-fsanitize=address",
5454
"CMAKE_CXX_STANDARD_REQUIRED": "ON",
55-
"PROJECT_BINARY_DIR": "${sourceDir}/build/g++-11-cpp17",
56-
"DOXYGEN_OUTPUT_DIRECTORY": "${sourceDir}/build/g++-11-cpp17/doc/"
55+
"PROJECT_BINARY_DIR": "${sourceDir}/build/g++-11",
56+
"DOXYGEN_OUTPUT_DIRECTORY": "${sourceDir}/build/g++-11/doc/"
5757
}
5858
},
5959
{
60-
"name": "g++-11-cpp20",
60+
"name": "clang++-13",
6161
"generator": "Unix Makefiles",
6262
"hidden": false,
6363
"inherits": ["cmake-pedantic"],
64-
"binaryDir": "${sourceDir}/build/g++-11-cpp20",
64+
"binaryDir": "${sourceDir}/build/clang++-13",
6565
"cacheVariables": {
6666
"CMAKE_BUILD_TYPE": "Debug",
6767
"CMAKE_CXX_EXTENSIONS": "OFF",
6868
"CMAKE_CXX_FLAGS": "-Wall -Wextra -fsanitize=address",
6969
"CMAKE_CXX_COMPILER": "g++-11",
7070
"CMAKE_SHARED_LINKER_FLAGS": "-fsanitize=address",
7171
"CMAKE_CXX_STANDARD_REQUIRED": "ON",
72-
"PROJECT_BINARY_DIR": "${sourceDir}/build/g++-11-cpp20",
73-
"DOXYGEN_OUTPUT_DIRECTORY": "${sourceDir}/build/g++-11-cpp20/doc/"
72+
"PROJECT_BINARY_DIR": "${sourceDir}/build/clang++-13",
73+
"DOXYGEN_OUTPUT_DIRECTORY": "${sourceDir}/build/clang++-13/doc/"
7474
}
7575
},
7676
{
@@ -113,7 +113,7 @@
113113
"name": "clang-tidy",
114114
"generator": "Unix Makefiles",
115115
"hidden": false,
116-
"inherits": ["g++-11-cpp17"],
116+
"inherits": ["g++-11"],
117117
"binaryDir": "${sourceDir}/build/clang-tidy",
118118
"cacheVariables": {
119119
"CMAKE_CXX_CLANG_TIDY": "clang-tidy;--header-filter=${sourceDir}/include/*",
@@ -123,8 +123,8 @@
123123
],
124124
"buildPresets": [
125125
{ "name": "coverage", "configurePreset": "coverage" },
126-
{ "name": "g++-11-cpp17", "configurePreset": "g++-11-cpp17" },
127-
{ "name": "g++-11-cpp20", "configurePreset": "g++-11-cpp20" },
126+
{ "name": "g++-11", "configurePreset": "g++-11" },
127+
{ "name": "clang++-13", "configurePreset": "clang++-13" },
128128
{ "name": "libc++-14-cpp17", "configurePreset": "libc++-14-cpp17" },
129129
{ "name": "libc++-14-cpp20", "configurePreset": "libc++-14-cpp20" },
130130
{ "name": "clang-tidy", "configurePreset": "clang-tidy" }
@@ -136,10 +136,11 @@
136136
"output": {"outputOnFailure": true},
137137
"execution": {"noTestsAction": "error", "stopOnFailure": true}
138138
},
139-
{ "name": "coverage", "configurePreset": "coverage", "inherits": ["test"] },
140-
{ "name": "g++-11-cpp17", "configurePreset": "g++-11-cpp17", "inherits": ["test"] },
141-
{ "name": "libc++-14-cpp17", "configurePreset": "libc++-14-cpp17", "inherits": ["test"] },
142-
{ "name": "libc++-14-cpp20", "configurePreset": "libc++-14-cpp20", "inherits": ["test"] },
143-
{ "name": "clang-tidy", "configurePreset": "clang-tidy", "inherits": ["test"] }
139+
{ "name": "coverage", "configurePreset": "coverage", "inherits": ["test"] },
140+
{ "name": "g++-11", "configurePreset": "g++-11", "inherits": ["test"] },
141+
{ "name": "clang++-13", "configurePreset": "clang++-13", "inherits": ["test"] },
142+
{ "name": "libc++-14-cpp17", "configurePreset": "libc++-14-cpp17", "inherits": ["test"] },
143+
{ "name": "libc++-14-cpp20", "configurePreset": "libc++-14-cpp20", "inherits": ["test"] },
144+
{ "name": "clang-tidy", "configurePreset": "clang-tidy", "inherits": ["test"] }
144145
]
145146
}

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ void boost_redis_from_bulk(mystruct& obj, char const* p, std::size_t size, boost
527527

528528
These functions are accessed over ADL and therefore they must be
529529
imported in the global namespace by the user. In the
530-
[Examples](#Examples) section the reader can find examples showing how
530+
[Examples](#examples) section the reader can find examples showing how
531531
to serialize using json and [protobuf](https://protobuf.dev/).
532532

533533
<a name="examples"></a>
@@ -831,6 +831,9 @@ https://lists.boost.org/Archives/boost/2023/01/253944.php.
831831
* Adds a function that performs health checks, see
832832
`boost::redis::experimental::async_check_health`.
833833

834+
* Adds non-member `async_run` function that resolves, connects and
835+
calls member `async_run` on a connection object.
836+
834837
### v1.4.0-1
835838

836839
* Renames `retry_on_connection_lost` to `cancel_if_unresponded`. (v1.4.1)
File renamed without changes.

examples/common/common.cpp

Lines changed: 0 additions & 68 deletions
This file was deleted.

examples/common/common.hpp

Lines changed: 0 additions & 33 deletions
This file was deleted.

examples/cpp17_intro.cpp

Lines changed: 22 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,98 +5,56 @@
55
*/
66

77
#include <iostream>
8-
#include <boost/asio.hpp>
9-
#include <boost/redis.hpp>
8+
#include <boost/redis/run.hpp>
9+
#include <boost/redis/address.hpp>
1010
#include <boost/redis/src.hpp>
1111

1212
namespace net = boost::asio;
13-
namespace redis = boost::redis;
14-
using redis::operation;
15-
using redis::request;
16-
using redis::response;
17-
using redis::ignore_t;
18-
19-
void log(boost::system::error_code const& ec, char const* prefix)
20-
{
21-
std::clog << prefix << ec.message() << std::endl;
22-
}
13+
using boost::redis::connection;
14+
using boost::redis::request;
15+
using boost::redis::response;
16+
using boost::redis::ignore_t;
17+
using boost::redis::async_run;
18+
using boost::redis::address;
19+
using namespace std::chrono_literals;
2320

2421
auto main(int argc, char * argv[]) -> int
2522
{
2623
try {
27-
std::string host = "127.0.0.1";
28-
std::string port = "6379";
24+
address addr;
2925

3026
if (argc == 3) {
31-
host = argv[1];
32-
port = argv[2];
27+
addr.host = argv[1];
28+
addr.port = argv[2];
3329
}
3430

3531
// The request
3632
request req;
3733
req.push("HELLO", 3);
3834
req.push("PING", "Hello world");
39-
req.push("QUIT");
4035

4136
// The response.
42-
response<ignore_t, std::string, ignore_t> resp;
37+
response<ignore_t, std::string> resp;
4338

4439
net::io_context ioc;
40+
connection conn{ioc};
4541

46-
// IO objects.
47-
net::ip::tcp::resolver resv{ioc};
48-
redis::connection conn{ioc};
49-
50-
// Resolve endpoints.
51-
net::ip::tcp::resolver::results_type endpoints;
52-
53-
// async_run callback.
54-
auto on_run = [](auto ec)
55-
{
56-
if (ec)
57-
return log(ec, "on_run: ");
58-
};
59-
60-
// async_exec callback.
61-
auto on_exec = [&](auto ec, auto)
62-
{
63-
if (ec) {
64-
conn.cancel(operation::run);
65-
return log(ec, "on_exec: ");
66-
}
42+
async_run(conn, addr, 10s, 10s, [&](auto){
43+
conn.cancel();
44+
});
6745

68-
std::cout << "PING: " << std::get<1>(resp).value() << std::endl;
69-
};
70-
71-
// Connect callback.
72-
auto on_connect = [&](auto ec, auto)
73-
{
74-
if (ec)
75-
return log(ec, "on_connect: ");
76-
77-
conn.async_run(on_run);
78-
conn.async_exec(req, resp, on_exec);
79-
};
80-
81-
// Resolve callback.
82-
auto on_resolve = [&](auto ec, auto const& addrs)
83-
{
84-
if (ec)
85-
return log(ec, "on_resolve: ");
86-
87-
endpoints = addrs;
88-
net::async_connect(conn.next_layer(), endpoints, on_connect);
89-
};
90-
91-
resv.async_resolve(host, port, on_resolve);
46+
conn.async_exec(req, resp, [&](auto ec, auto){
47+
if (!ec)
48+
std::cout << "PING: " << std::get<1>(resp).value() << std::endl;
49+
conn.cancel();
50+
});
9251

9352
ioc.run();
9453
return 0;
9554

9655
} catch (std::exception const& e) {
9756
std::cerr << "Error: " << e.what() << std::endl;
9857
}
99-
10058
return 1;
10159
}
10260

0 commit comments

Comments
 (0)