/* Copyright (c) 2018-2022 Marcelo Zimbres Silva (mzimbres@gmail.com) * * Distributed under the Boost Software License, Version 1.0. (See * accompanying file LICENSE.txt) */ #include #include #include #include #include #include #include #include #include #if defined(BOOST_ASIO_HAS_CO_AWAIT) #if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) namespace net = boost::asio; using stream_descriptor = net::deferred_t::as_default_on_t; using signal_set = net::deferred_t::as_default_on_t; using boost::redis::request; using boost::redis::generic_response; using boost::redis::config; using boost::redis::connection; using boost::redis::ignore; using net::redirect_error; using net::use_awaitable; using boost::system::error_code; using namespace std::chrono_literals; // Chat over Redis pubsub. To test, run this program from multiple // terminals and type messages to stdin. auto receiver(std::shared_ptr conn) -> net::awaitable { request req; req.push("SUBSCRIBE", "channel"); while (conn->will_reconnect()) { // Subscribe to channels. co_await conn->async_exec(req, ignore, net::deferred); // Loop reading Redis push messages. for (generic_response resp;;) { error_code ec; co_await conn->async_receive(resp, redirect_error(use_awaitable, ec)); if (ec) break; // Connection lost, break so we can reconnect to channels. std::cout << resp.value().at(1).value << " " << resp.value().at(2).value << " " << resp.value().at(3).value << std::endl; resp.value().clear(); } } } // Publishes stdin messages to a Redis channel. auto publisher(std::shared_ptr in, std::shared_ptr conn) -> net::awaitable { for (std::string msg;;) { auto n = co_await net::async_read_until(*in, net::dynamic_buffer(msg, 1024), "\n"); request req; req.push("PUBLISH", "channel", msg); co_await conn->async_exec(req, ignore, net::deferred); msg.erase(0, n); } } // Called from the main function (see main.cpp) auto co_main(config cfg) -> net::awaitable { auto ex = co_await net::this_coro::executor; auto conn = std::make_shared(ex); auto stream = std::make_shared(ex, ::dup(STDIN_FILENO)); net::co_spawn(ex, receiver(conn), net::detached); net::co_spawn(ex, publisher(stream, conn), net::detached); conn->async_run(cfg, {}, net::consign(net::detached, conn)); signal_set sig_set{ex, SIGINT, SIGTERM}; co_await sig_set.async_wait(); conn->cancel(); stream->cancel(); } #else // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) auto co_main(config const&) -> net::awaitable { std::cout << "Requires support for posix streams." << std::endl; co_return; } #endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) #endif // defined(BOOST_ASIO_HAS_CO_AWAIT)