Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

Loading...
Searching...
No Matches
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 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;
39
40// Chat over Redis pubsub. To test, run this program from multiple
41// terminals and type messages to stdin.
42
43auto
44receiver(std::shared_ptr<connection> conn) -> awaitable<void>
45{
46 request req;
47 req.push("SUBSCRIBE", "channel");
48
50 conn->set_receive_response(resp);
51
52 while (conn->will_reconnect()) {
53
54 // Subscribe to channels.
55 co_await conn->async_exec(req, ignore, deferred);
56
57 // Loop reading Redis push messages.
58 for (error_code ec;;) {
59 co_await conn->async_receive(redirect_error(use_awaitable, ec));
60 if (ec)
61 break; // Connection lost, break so we can reconnect to channels.
62 std::cout
63 << resp.value().at(1).value
64 << " " << resp.value().at(2).value
65 << " " << resp.value().at(3).value
66 << std::endl;
67 resp.value().clear();
68 }
69 }
70}
71
72// Publishes stdin messages to a Redis channel.
73auto publisher(std::shared_ptr<stream_descriptor> in, std::shared_ptr<connection> conn) -> awaitable<void>
74{
75 for (std::string msg;;) {
76 auto n = co_await async_read_until(*in, dynamic_buffer(msg, 1024), "\n");
77 request req;
78 req.push("PUBLISH", "channel", msg);
79 co_await conn->async_exec(req, ignore, deferred);
80 msg.erase(0, n);
81 }
82}
83
84// Called from the main function (see main.cpp)
85auto co_main(config cfg) -> awaitable<void>
86{
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));
90
91 co_spawn(ex, receiver(conn), detached);
92 co_spawn(ex, publisher(stream, conn), detached);
93 conn->async_run(cfg, {}, consign(detached, conn));
94
95 signal_set sig_set{ex, SIGINT, SIGTERM};
96 co_await sig_set.async_wait();
97 conn->cancel();
98 stream->cancel();
99}
100
101#else // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
102auto co_main(config const&) -> awaitable<void>
103{
104 std::cout << "Requires support for posix streams." << std::endl;
105 co_return;
106}
107#endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
108#endif // defined(BOOST_ASIO_HAS_CO_AWAIT)
A basic_connection that type erases the executor.
Definition: connection.hpp:339
Creates Redis requests.
Definition: request.hpp:46
void push(std::string_view cmd, Ts const &... args)
Appends a new command to the end of the request.
Definition: request.hpp:146
ignore_t ignore
Global ignore object.
adapter::result< std::vector< resp3::node > > generic_response
A generic response to a request.
Definition: response.hpp:35
Configure parameters used by the connection classes.
Definition: config.hpp:30