Loading...
Searching...
No Matches
cpp20_unix_sockets.cpp
1//
2// Copyright (c) 2025 Marcelo Zimbres Silva (mzimbres@gmail.com),
3// Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
4//
5// Distributed under the Boost Software License, Version 1.0. (See accompanying
6// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7//
8#include <boost/asio/awaitable.hpp>
9
10#if defined(BOOST_ASIO_HAS_CO_AWAIT)
11
12#include <boost/redis/connection.hpp>
13
14#include <boost/asio/consign.hpp>
15#include <boost/asio/detached.hpp>
16#include <boost/asio/this_coro.hpp>
17
18#include <iostream>
19
20namespace asio = boost::asio;
26
27#ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS
28
29auto co_main(config cfg) -> asio::awaitable<void>
30{
31 // If unix_socket is set to a non-empty string, UNIX domain sockets will be used
32 // instead of TCP. Set this value to the path where your server is listening.
33 // UNIX domain socket connections work in the same way as TCP connections.
34 cfg.unix_socket = "/tmp/redis-socks/redis.sock";
35
36 auto conn = std::make_shared<connection>(co_await asio::this_coro::executor);
37 conn->async_run(cfg, asio::consign(asio::detached, conn));
38
39 request req;
40 req.push("PING");
41
42 response<std::string> resp;
43
44 co_await conn->async_exec(req, resp);
45 conn->cancel();
46
47 std::cout << "Response: " << std::get<0>(resp).value() << std::endl;
48}
49
50#else
51
52auto co_main(config) -> asio::awaitable<void>
53{
54 std::cout << "Sorry, your system does not support UNIX domain sockets\n";
55 co_return;
56}
57
58#endif
59
60#endif
A basic_connection that type erases the executor.
Creates Redis requests.
Definition request.hpp:46
std::tuple< adapter::result< Ts >... > response
Response with compile-time size.
Definition response.hpp:25
Configure parameters used by the connection classes.
Definition config.hpp:30
Defines logging configuration.
Definition logger.hpp:20