9 #include <boost/asio.hpp>
13 #include <aedis/src.hpp>
15 namespace net = boost::asio;
18 using executor_type = net::io_context::executor_type;
19 using socket_type = net::basic_stream_socket<net::ip::tcp, executor_type>;
20 using tcp_socket = net::use_awaitable_t<executor_type>::as_default_on_t<socket_type>;
21 using acceptor_type = net::basic_socket_acceptor<net::ip::tcp, executor_type>;
22 using tcp_acceptor = net::use_awaitable_t<executor_type>::as_default_on_t<acceptor_type>;
23 using awaitable_type = net::awaitable<void, executor_type>;
26 awaitable_type echo_loop(tcp_socket socket, std::shared_ptr<connection> db)
29 std::tuple<std::string> resp;
31 for (std::string buffer;;) {
32 auto n = co_await net::async_read_until(socket, net::dynamic_buffer(buffer, 1024),
"\n");
33 req.
push(
"PING", buffer);
34 co_await db->async_exec(req,
adapt(resp));
35 co_await net::async_write(socket, net::buffer(std::get<0>(resp)));
36 std::get<0>(resp).clear();
42 awaitable_type listener()
44 auto ex = co_await net::this_coro::executor;
45 auto db = std::make_shared<connection>(ex);
46 db->async_run(net::detached);
48 tcp_acceptor acc(ex, {net::ip::tcp::v4(), 55555});
50 net::co_spawn(ex, echo_loop(co_await acc.async_accept(), db), net::detached);
56 net::io_context ioc{BOOST_ASIO_CONCURRENCY_HINT_UNSAFE_IO};
57 co_spawn(ioc, listener(), net::detached);
59 }
catch (std::exception
const& e) {
60 std::cerr << e.what() << std::endl;
A high level connection to Redis.
void clear()
Clears the request preserving allocated memory.
void push(boost::string_view cmd, Ts const &... args)
Appends a new command to the end of the request.
auto adapt() noexcept
Creates an adapter that ignores responses.