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 net = boost::asio;
21using stream_descriptor = net::deferred_t::as_default_on_t<net::posix::stream_descriptor>;
22using signal_set = net::deferred_t::as_default_on_t<net::signal_set>;
28using net::redirect_error;
29using net::use_awaitable;
30using boost::system::error_code;
31using namespace std::chrono_literals;
37receiver(std::shared_ptr<connection> conn) -> net::awaitable<void>
40 req.push(
"SUBSCRIBE",
"channel");
42 while (conn->will_reconnect()) {
45 co_await conn->async_exec(req,
ignore, net::deferred);
50 co_await conn->async_receive(resp, redirect_error(use_awaitable, ec));
54 << resp.value().at(1).value
55 <<
" " << resp.value().at(2).value
56 <<
" " << resp.value().at(3).value
64auto publisher(std::shared_ptr<stream_descriptor> in, std::shared_ptr<connection> conn) -> net::awaitable<void>
66 for (std::string msg;;) {
67 auto n =
co_await net::async_read_until(*in, net::dynamic_buffer(msg, 1024),
"\n");
69 req.push(
"PUBLISH",
"channel", msg);
70 co_await conn->async_exec(req,
ignore, net::deferred);
76auto co_main(config cfg) -> net::awaitable<void>
78 auto ex =
co_await net::this_coro::executor;
79 auto conn = std::make_shared<connection>(ex);
80 auto stream = std::make_shared<stream_descriptor>(ex, ::dup(STDIN_FILENO));
82 net::co_spawn(ex, receiver(conn), net::detached);
83 net::co_spawn(ex, publisher(stream, conn), net::detached);
84 conn->async_run(cfg, {}, net::consign(net::detached, conn));
86 signal_set sig_set{ex, SIGINT, SIGTERM};
87 co_await sig_set.async_wait();
93auto co_main(config
const&) -> net::awaitable<void>
95 std::cout <<
"Requires support for posix streams." << std::endl;
A basic_connection that type erases the executor.
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.