7#include <boost/redis/connection.hpp>
8#include <boost/asio/deferred.hpp>
9#include <boost/asio/signal_set.hpp>
10#include <boost/asio/detached.hpp>
11#include <boost/asio/redirect_error.hpp>
12#include <boost/asio/co_spawn.hpp>
15#if defined(BOOST_ASIO_HAS_CO_AWAIT)
17namespace net = boost::asio;
18using tcp_socket = net::deferred_t::as_default_on_t<net::ip::tcp::socket>;
19using tcp_acceptor = net::deferred_t::as_default_on_t<net::ip::tcp::acceptor>;
20using signal_set = net::deferred_t::as_default_on_t<net::signal_set>;
24using boost::system::error_code;
26using namespace std::chrono_literals;
28auto echo_server_session(tcp_socket socket, std::shared_ptr<connection> conn) -> net::awaitable<void>
31 response<std::string> resp;
33 for (std::string buffer;;) {
34 auto n =
co_await net::async_read_until(socket, net::dynamic_buffer(buffer, 1024),
"\n");
35 req.push(
"PING", buffer);
36 co_await conn->async_exec(req, resp, net::deferred);
38 std::get<0>(resp).value().clear();
45auto listener(std::shared_ptr<connection> conn) -> net::awaitable<void>
48 auto ex =
co_await net::this_coro::executor;
49 tcp_acceptor acc(ex, {net::ip::tcp::v4(), 55555});
51 net::co_spawn(ex, echo_server_session(
co_await acc.async_accept(), conn), net::detached);
52 }
catch (std::exception
const& e) {
53 std::clog <<
"Listener: " << e.what() << std::endl;
58auto co_main(config cfg) -> net::awaitable<void>
60 auto ex =
co_await net::this_coro::executor;
61 auto conn = std::make_shared<connection>(ex);
62 net::co_spawn(ex, listener(conn), net::detached);
63 conn->async_run(cfg, {}, net::consign(net::detached, conn));
65 signal_set sig_set(ex, SIGINT, SIGTERM);
66 co_await sig_set.async_wait();
A basic_connection that type erases the executor.
std::tuple< adapter::result< Ts >... > response
Response with compile-time size.
Configure parameters used by the connection classes.
auto async_write(AsyncWriteStream &stream, request const &req, CompletionToken &&token=asio::default_completion_token_t< typename AsyncWriteStream::executor_type >{})
Writes a request asynchronously.