2
0
mirror of https://github.com/boostorg/redis.git synced 2026-02-05 10:02:20 +00:00
Files
redis/examples/async_basic.cpp
Marcelo Zimbres 05aff2f3c6 Refactoring listed below:
- Improvements in the organization.
- Adds support for the default token on the consumer.
2021-10-10 15:26:06 +02:00

79 lines
1.9 KiB
C++

/* Copyright (c) 2019 - 2021 Marcelo Zimbres Silva (mzimbres at gmail dot com)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <aedis/aedis.hpp>
using namespace aedis;
using tcp_socket = net::use_awaitable_t<>::as_default_on_t<net::ip::tcp::socket>;
void print_event(std::pair<command, std::string> const& p)
{
std::cout << "Event: " << p.first << ".";
if (!std::empty(p.second))
std::cout << " Key: " << p.second << ".";
std::cout << std::endl;
}
net::awaitable<void>
example(
tcp_socket& socket,
std::queue<resp3::request>& requests)
{
requests.push({});
requests.back().hello("3");
resp3::response resp;
resp3::consumer cs;
for (;;) {
resp.clear();
co_await cs.async_consume(socket, requests, resp);
std::cout << resp << std::endl;
if (resp.get_type() == resp3::type::push)
continue;
auto const id = requests.front().ids.front();
print_event(id);
switch (id.first) {
case command::hello:
{
prepare_next(requests);
requests.back().ping();
requests.back().subscribe("some-channel");
} break;
case command::publish: break;
case command::quit: break;
case command::ping:
{
prepare_next(requests);
requests.back().publish("some-channel", "Some message");
requests.back().quit();
} break;
default: { }
}
}
}
int main()
{
net::io_context ioc;
net::ip::tcp::resolver resolver{ioc};
auto const res = resolver.resolve("127.0.0.1", "6379");
tcp_socket socket{ioc};
net::connect(socket, res);
std::queue<resp3::request> requests;
co_spawn(ioc, example(socket, requests), net::detached);
ioc.run();
}