7#include <boost/redis/connection.hpp>
8#include <boost/asio/deferred.hpp>
9#include <boost/asio/signal_set.hpp>
10#include <boost/asio/co_spawn.hpp>
11#include <boost/asio/detached.hpp>
12#include <boost/asio/redirect_error.hpp>
13#include <boost/asio/posix/stream_descriptor.hpp>
17#if defined(BOOST_ASIO_HAS_CO_AWAIT)
18#if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
20namespace asio = boost::asio;
21using stream_descriptor = asio::deferred_t::as_default_on_t<asio::posix::stream_descriptor>;
22using signal_set = asio::deferred_t::as_default_on_t<asio::signal_set>;
23using boost::asio::async_read_until;
24using boost::asio::awaitable;
25using boost::asio::co_spawn;
26using boost::asio::consign;
27using boost::asio::deferred;
28using boost::asio::detached;
29using boost::asio::dynamic_buffer;
30using boost::asio::redirect_error;
31using boost::asio::use_awaitable;
37using boost::system::error_code;
38using namespace std::chrono_literals;
44receiver(std::shared_ptr<connection> conn) -> awaitable<void>
47 req.
push(
"SUBSCRIBE",
"channel");
50 conn->set_receive_response(resp);
52 while (conn->will_reconnect()) {
55 co_await conn->async_exec(req, ignore, deferred);
58 for (error_code ec;;) {
59 co_await conn->async_receive(redirect_error(use_awaitable, ec));
63 << resp.value().at(1).value
64 <<
" " << resp.value().at(2).value
65 <<
" " << resp.value().at(3).value
73auto publisher(std::shared_ptr<stream_descriptor> in, std::shared_ptr<connection> conn) -> awaitable<void>
75 for (std::string msg;;) {
76 auto n =
co_await async_read_until(*in, dynamic_buffer(msg, 1024),
"\n");
78 req.push(
"PUBLISH",
"channel", msg);
79 co_await conn->async_exec(req, ignore, deferred);
85auto co_main(config cfg) -> awaitable<void>
87 auto ex =
co_await asio::this_coro::executor;
88 auto conn = std::make_shared<connection>(ex);
89 auto stream = std::make_shared<stream_descriptor>(ex, ::dup(STDIN_FILENO));
91 co_spawn(ex, receiver(conn), detached);
92 co_spawn(ex, publisher(stream, conn), detached);
93 conn->async_run(cfg, {}, consign(detached, conn));
95 signal_set sig_set{ex, SIGINT, SIGTERM};
96 co_await sig_set.async_wait();
102auto co_main(config
const&) -> awaitable<void>
104 std::cout <<
"Requires support for posix streams." << std::endl;
A basic_connection that type erases the executor.
void push(std::string_view cmd, Ts const &... args)
Appends a new command to the end of the request.
ignore_t ignore
Global ignore object.
adapter::result< std::vector< resp3::node > > generic_response
A generic response to a request.
Configure parameters used by the connection classes.