Loading...
Searching...
No Matches
cpp20_protobuf.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/redis/resp3/serialization.hpp>
9#include <boost/asio/co_spawn.hpp>
10#include <boost/asio/detached.hpp>
11#include <boost/asio/consign.hpp>
12#include <boost/system/errc.hpp>
13#include <iostream>
14
15// See the definition in person.proto. This header is automatically
16// generated by CMakeLists.txt.
17#include "person.pb.h"
18
19#if defined(BOOST_ASIO_HAS_CO_AWAIT)
20
21namespace asio = boost::asio;
22namespace resp3 = boost::redis::resp3;
30
31// The protobuf type described in example/person.proto
32using tutorial::person;
33
34// Boost.Redis customization points (example/protobuf.hpp)
35namespace tutorial
36{
37
38// Below I am using a Boost.Redis to indicate a protobuf error, this
39// is ok for an example, users however might want to define their own
40// error codes.
41void boost_redis_to_bulk(std::string& to, person const& u)
42{
43 std::string tmp;
44 if (!u.SerializeToString(&tmp))
45 throw boost::system::system_error(boost::redis::error::invalid_data_type);
46
47 resp3::boost_redis_to_bulk(to, tmp);
48}
49
50void
51boost_redis_from_bulk(
52 person& u,
53 node_view const& node,
54 boost::system::error_code& ec)
55{
56 std::string const tmp {node.value};
57 if (!u.ParseFromString(tmp))
59}
60
61} // tutorial
62
63using tutorial::boost_redis_to_bulk;
64using tutorial::boost_redis_from_bulk;
65
66asio::awaitable<void> co_main(config cfg)
67{
68 auto ex = co_await asio::this_coro::executor;
69 auto conn = std::make_shared<connection>(ex);
70 conn->async_run(cfg, {}, asio::consign(asio::detached, conn));
71
72 person p;
73 p.set_name("Louis");
74 p.set_id(3);
75 p.set_email("No email yet.");
76
77 request req;
78 req.push("SET", "protobuf-key", p);
79 req.push("GET", "protobuf-key");
80
81 response<ignore_t, person> resp;
82
83 // Sends the request and receives the response.
84 co_await conn->async_exec(req, resp);
85 conn->cancel();
86
87 std::cout
88 << "Name: " << std::get<1>(resp).value().name() << "\n"
89 << "Age: " << std::get<1>(resp).value().id() << "\n"
90 << "Email: " << std::get<1>(resp).value().email() << "\n";
91}
92
93#endif // defined(BOOST_ASIO_HAS_CO_AWAIT)
A basic_connection that type erases the executor.
Creates Redis requests.
Definition request.hpp:46
std::decay_t< decltype(std::ignore)> ignore_t
Type used to ignore responses.
Definition ignore.hpp:31
basic_node< std::string > node
A node in the response tree that owns its data.
Definition node.hpp:60
operation
Connection operations that can be cancelled.
Definition operation.hpp:18
std::tuple< adapter::result< Ts >... > response
Response with compile-time size.
Definition response.hpp:25
@ invalid_data_type
Invalid RESP3 type.
Configure parameters used by the connection classes.
Definition config.hpp:30
A node in the response tree.
Definition node.hpp:28