boost_redis 1.4.2
A redis client library
cpp20_chat_room.cpp
1/* Copyright (c) 2018-2022 Marcelo Zimbres Silva (mzimbres@gmail.com)
2 *
3 * Distributed under the Boost Software License, Version 1.0. (See
4 * accompanying file LICENSE.txt)
5 */
6
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>
14#include <unistd.h>
15#include <iostream>
16
17#if defined(BOOST_ASIO_HAS_CO_AWAIT)
18#if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
19
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;
32
33// Chat over Redis pubsub. To test, run this program from multiple
34// terminals and type messages to stdin.
35
36auto
37receiver(std::shared_ptr<connection> conn) -> net::awaitable<void>
38{
39 request req;
40 req.push("SUBSCRIBE", "channel");
41
42 while (conn->will_reconnect()) {
43
44 // Subscribe to channels.
45 co_await conn->async_exec(req, ignore, net::deferred);
46
47 // Loop reading Redis push messages.
48 for (generic_response resp;;) {
49 error_code ec;
50 co_await conn->async_receive(resp, redirect_error(use_awaitable, ec));
51 if (ec)
52 break; // Connection lost, break so we can reconnect to channels.
53 std::cout
54 << resp.value().at(1).value
55 << " " << resp.value().at(2).value
56 << " " << resp.value().at(3).value
57 << std::endl;
58 resp.value().clear();
59 }
60 }
61}
62
63// Publishes stdin messages to a Redis channel.
64auto publisher(std::shared_ptr<stream_descriptor> in, std::shared_ptr<connection> conn) -> net::awaitable<void>
65{
66 for (std::string msg;;) {
67 auto n = co_await net::async_read_until(*in, net::dynamic_buffer(msg, 1024), "\n");
68 request req;
69 req.push("PUBLISH", "channel", msg);
70 co_await conn->async_exec(req, ignore, net::deferred);
71 msg.erase(0, n);
72 }
73}
74
75// Called from the main function (see main.cpp)
76auto co_main(config cfg) -> net::awaitable<void>
77{
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));
81
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));
85
86 signal_set sig_set{ex, SIGINT, SIGTERM};
87 co_await sig_set.async_wait();
88 conn->cancel();
89 stream->cancel();
90}
91
92#else // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
93auto co_main(config const&) -> net::awaitable<void>
94{
95 std::cout << "Requires support for posix streams." << std::endl;
96 co_return;
97}
98#endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
99#endif // defined(BOOST_ASIO_HAS_CO_AWAIT)
A basic_connection that type erases the executor.
Definition: connection.hpp:308
Creates Redis requests.
Definition: request.hpp:46
ignore_t ignore
Global ignore object.
adapter::result< std::vector< resp3::node > > generic_response
A generic response to a request.
Definition: response.hpp:33
Configure parameters used by the connection classes.
Definition: config.hpp:30