#include #include #include #include "lib/client_base.hpp" #include "lib/user_session.hpp" #include "src.hpp" using aedis::resp3::client_base; using aedis::command; using aedis::user_session; namespace net = aedis::net; using aedis::resp3::client_base; using tcp_acceptor = aedis::net::use_awaitable_t<>::as_default_on_t; using client_base_type = client_base; class my_redis_client : public client_base_type { private: void on_event(aedis::resp3::response_id id) override { // If the user connections is still alive when the response // arrives we send the echo message to the user, otherwise we // just log it has expired. if (auto session = id.session.lock()) { session->deliver(*id.resp); id.resp->clear(); } else { std::cout << "Session expired." << std::endl; } } public: my_redis_client(net::any_io_executor ex) : client_base_type(ex) {} }; struct on_user_msg { std::shared_ptr resp; std::shared_ptr client; std::shared_ptr session; void operator()(std::string const& msg) const { auto filler = [this, &msg](auto& req) { req.push(aedis::resp3::response_id{command::ping, resp, session}, msg); }; client->send(filler); } }; net::awaitable listener() { auto ex = co_await net::this_coro::executor; tcp_acceptor acceptor(ex, {net::ip::tcp::v4(), 55555}); // The redis client instance. auto client = std::make_shared(ex); client->start(); // The response is shared by all connections. auto resp = std::make_shared(); // Loops accepting connections. for (;;) { auto socket = co_await acceptor.async_accept(); auto session = std::make_shared(std::move(socket)); session->start(on_user_msg{resp, client, session}); } } int main() { try { net::io_context ioc{1}; net::signal_set signals(ioc, SIGINT, SIGTERM); signals.async_wait([&](auto, auto){ ioc.stop(); }); co_spawn(ioc, listener(), net::detached); ioc.run(); } catch (std::exception const& e) { std::cerr << e.what() << std::endl; } }