Skip to content

Commit fd96720

Browse files
committed
Implements non-member async_run for plain connections.
This function will resolve and connect before calling member async_run.
1 parent cd00047 commit fd96720

Some content is hidden

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

43 files changed

+719
-439
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

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 16 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,12 @@
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 namespace std::chrono_literals;
2319

2420
auto main(int argc, char * argv[]) -> int
2521
{
@@ -36,67 +32,29 @@ auto main(int argc, char * argv[]) -> int
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, host, port, 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

examples/cpp17_intro_sync.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,25 @@
66

77
#include <tuple>
88
#include <string>
9+
#include <chrono>
910
#include <thread>
1011
#include <iostream>
1112
#include <boost/asio.hpp>
1213
#include <boost/redis.hpp>
14+
#include <boost/redis/check_health.hpp>
1315

1416
// Include this in no more than one .cpp file.
1517
#include <boost/redis/src.hpp>
1618

1719
namespace net = boost::asio;
1820
using connection = boost::redis::connection;
21+
using boost::redis::operation;
1922
using boost::redis::request;
2023
using boost::redis::response;
2124
using boost::redis::ignore_t;
25+
using boost::redis::async_run;
26+
using boost::redis::async_check_health;
27+
using namespace std::chrono_literals;
2228

2329
template <class Response>
2430
auto exec(std::shared_ptr<connection> conn, request const& req, Response& resp)
@@ -29,9 +35,6 @@ auto exec(std::shared_ptr<connection> conn, request const& req, Response& resp)
2935
(net::use_future).get();
3036
}
3137

32-
auto logger = [](auto const& ec)
33-
{ std::clog << "Run: " << ec.message() << std::endl; };
34-
3538
auto main(int argc, char * argv[]) -> int
3639
{
3740
try {
@@ -47,17 +50,17 @@ auto main(int argc, char * argv[]) -> int
4750

4851
auto conn = std::make_shared<connection>(ioc);
4952

50-
// Resolves the address
51-
net::ip::tcp::resolver resv{ioc};
52-
auto const res = resv.resolve(host, port);
53+
// Starts a thread that will can io_context::run on which the
54+
// connection will run.
55+
std::thread t{[&ioc, conn, host, port]() {
56+
async_run(*conn, host, port, 10s, 10s, [conn](auto){
57+
conn->cancel();
58+
});
5359

54-
// Connect to Redis
55-
net::connect(conn->next_layer(), res);
60+
async_check_health(*conn, "Boost.Redis", 2s, [conn](auto) {
61+
conn->cancel();
62+
});
5663

57-
// Starts a thread that will can io_context::run on which
58-
// the connection will run.
59-
std::thread t{[conn, &ioc]() {
60-
conn->async_run(logger);
6164
ioc.run();
6265
}};
6366

examples/cpp20_chat_room.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ namespace net = boost::asio;
1111
#if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
1212
#include <boost/asio/experimental/awaitable_operators.hpp>
1313
#include <boost/redis.hpp>
14-
#include <boost/redis/experimental/run.hpp>
14+
#include <boost/redis/check_health.hpp>
1515
#include <unistd.h>
1616

17-
#include "common/common.hpp"
18-
1917
using namespace net::experimental::awaitable_operators;
2018
using stream_descriptor = net::use_awaitable_t<>::as_default_on_t<net::posix::stream_descriptor>;
2119
using signal_set = net::use_awaitable_t<>::as_default_on_t<net::signal_set>;
2220
using boost::redis::request;
2321
using boost::redis::generic_response;
24-
using boost::redis::experimental::async_check_health;
22+
using boost::redis::async_check_health;
23+
using boost::redis::async_run;
24+
using connection = boost::asio::use_awaitable_t<>::as_default_on_t<boost::redis::connection>;
2525

2626
// Chat over Redis pubsub. To test, run this program from multiple
2727
// terminals and type messages to stdin.
@@ -60,8 +60,7 @@ auto co_main(std::string host, std::string port) -> net::awaitable<void>
6060
req.push("HELLO", 3);
6161
req.push("SUBSCRIBE", "chat-channel");
6262

63-
co_await connect(conn, host, port);
64-
co_await ((conn->async_run() || publisher(stream, conn) || receiver(conn) ||
63+
co_await ((async_run(*conn, host, port) || publisher(stream, conn) || receiver(conn) ||
6564
async_check_health(*conn) || sig.async_wait()) &&
6665
conn->async_exec(req));
6766
}

examples/cpp20_containers.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@
66

77
#include <boost/asio.hpp>
88
#if defined(BOOST_ASIO_HAS_CO_AWAIT)
9-
#include <boost/asio/experimental/awaitable_operators.hpp>
109
#include <boost/redis.hpp>
1110
#include <map>
1211
#include <vector>
13-
14-
#include "common/common.hpp"
12+
#include <iostream>
1513

1614
namespace net = boost::asio;
1715
namespace redis = boost::redis;
18-
using namespace net::experimental::awaitable_operators;
1916
using boost::redis::request;
2017
using boost::redis::response;
2118
using boost::redis::ignore_t;
19+
using boost::redis::async_run;
20+
using connection = boost::asio::use_awaitable_t<>::as_default_on_t<boost::redis::connection>;
2221

2322
void print(std::map<std::string, std::string> const& cont)
2423
{
@@ -34,8 +33,7 @@ void print(std::vector<int> const& cont)
3433

3534
auto run(std::shared_ptr<connection> conn, std::string host, std::string port) -> net::awaitable<void>
3635
{
37-
co_await connect(conn, host, port);
38-
co_await conn->async_run();
36+
co_await async_run(*conn, host, port);
3937
}
4038

4139
// Stores the content of some STL containers in Redis.

0 commit comments

Comments
 (0)