mirror of
https://github.com/boostorg/redis.git
synced 2026-02-22 03:32:22 +00:00
Improvements in the examples.
This commit is contained in:
47
examples/basic2.cpp
Normal file
47
examples/basic2.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
/* 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 <iostream>
|
||||
|
||||
#include <aedis/aedis.hpp>
|
||||
|
||||
#include "types.hpp"
|
||||
#include "utils.ipp"
|
||||
|
||||
using namespace aedis;
|
||||
|
||||
/* Similar to the basic1 example but
|
||||
*
|
||||
* 1. Reads the response in a loop.
|
||||
* 2. Prints the command to which the response belongs.
|
||||
*/
|
||||
net::awaitable<void> ping()
|
||||
{
|
||||
auto socket = co_await make_connection();
|
||||
resp3::stream<tcp_socket> stream{std::move(socket)};
|
||||
|
||||
resp3::request req;
|
||||
req.push(command::hello, 3);
|
||||
req.push(command::ping);
|
||||
req.push(command::quit);
|
||||
|
||||
co_await stream.async_write(req);
|
||||
|
||||
while (!std::empty(req.commands)) {
|
||||
resp3::response resp;
|
||||
co_await stream.async_read(resp);
|
||||
std::cout << req.commands.front() << ":\n" << resp << std::endl;
|
||||
req.commands.pop();
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
net::io_context ioc;
|
||||
co_spawn(ioc, ping(), net::detached);
|
||||
ioc.run();
|
||||
}
|
||||
Reference in New Issue
Block a user