diff --git a/doc/Doxyfile b/doc/Doxyfile index ba67b403..d122fcd2 100644 --- a/doc/Doxyfile +++ b/doc/Doxyfile @@ -829,7 +829,7 @@ WARN_LOGFILE = # spaces. See also FILE_PATTERNS and EXTENSION_MAPPING # Note: If this tag is empty the current directory is searched. -INPUT = include +INPUT = include examples # This tag can be used to specify the character encoding of the source files # that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses diff --git a/examples/advanced.cpp b/examples/advanced.cpp index c465fc17..0a3ab5bc 100644 --- a/examples/advanced.cpp +++ b/examples/advanced.cpp @@ -12,14 +12,22 @@ #include "types.hpp" #include "utils.ipp" -using namespace aedis; +using aedis::command; +using aedis::resp3::request; +using aedis::resp3::type; +using aedis::resp3::response; +using aedis::resp3::async_read; +using aedis::resp3::async_read_type; + +namespace net = aedis::net; + /* A more elaborate way of doing what has been done above where we send a new * command only after the last one has arrived. This is usually the starting * point for more complex applications. Here we also separate the application * logic out out the coroutine for clarity. */ -bool prepare_next(std::queue& reqs) +bool prepare_next(std::queue& reqs) { if (std::empty(reqs)) { reqs.push({}); @@ -35,7 +43,7 @@ bool prepare_next(std::queue& reqs) } net::awaitable -writer(tcp_socket& socket, std::queue& reqs, std::string message) +writer(tcp_socket& socket, std::queue& reqs, std::string message) { auto ex = co_await aedis::net::this_coro::executor; net::steady_timer t{ex}; @@ -53,7 +61,7 @@ writer(tcp_socket& socket, std::queue& reqs, std::string message } } -net::awaitable reader(tcp_socket& socket, std::queue& reqs) +net::awaitable reader(tcp_socket& socket, std::queue& reqs) { // Writes and reads continuosly from the socket. for (std::string buffer;;) { @@ -73,10 +81,10 @@ net::awaitable reader(tcp_socket& socket, std::queue& reqs // Loops to consume the response to all commands in the request. do { // Reads the type of the incoming response. - auto const t = co_await resp3::async_read_type(socket, buffer); + auto const t = co_await async_read_type(socket, buffer); - if (t == resp3::type::push) { - resp3::response resp; + if (t == type::push) { + response resp; co_await async_read(socket, buffer, resp); std::cout << resp << std::endl; } else { @@ -84,7 +92,7 @@ net::awaitable reader(tcp_socket& socket, std::queue& reqs switch (reqs.front().commands.front()) { case command::hello: { - resp3::response resp; + response resp; co_await async_read(socket, buffer, resp); for (auto i = 0; i < 100; ++i) { @@ -95,7 +103,7 @@ net::awaitable reader(tcp_socket& socket, std::queue& reqs } break; default: { - resp3::response resp; + response resp; co_await async_read(socket, buffer, resp); std::cout @@ -122,7 +130,7 @@ net::awaitable advanced() { auto socket = co_await make_connection(); - std::queue reqs; + std::queue reqs; reqs.push({}); reqs.back().push(command::hello, 3); reqs.back().push(command::subscribe, "channel"); diff --git a/examples/basic1.cpp b/examples/basic1.cpp index 420d3c9f..9a42b9ab 100644 --- a/examples/basic1.cpp +++ b/examples/basic1.cpp @@ -13,7 +13,12 @@ #include "types.hpp" #include "utils.ipp" -using namespace aedis; +using aedis::command; +using aedis::resp3::request; +using aedis::resp3::response; +using aedis::resp3::async_read; + +namespace net = aedis::net; /** A simple example that illustrates the basic principles. Three commands are * sent in the same request @@ -28,7 +33,7 @@ using namespace aedis; net::awaitable ping() { try { - resp3::request req; + request req; req.push(command::hello, 3); req.push(command::ping); req.push(command::quit); @@ -37,7 +42,7 @@ net::awaitable ping() co_await async_write(socket, req); std::string buffer; - resp3::response resp; + response resp; // hello co_await async_read(socket, buffer, resp); diff --git a/examples/basic2.cpp b/examples/basic2.cpp index 7e632937..047cf89e 100644 --- a/examples/basic2.cpp +++ b/examples/basic2.cpp @@ -12,29 +12,38 @@ #include "types.hpp" #include "utils.ipp" -using namespace aedis; +using aedis::command; +using aedis::resp3::request; +using aedis::resp3::response; +using aedis::resp3::async_read; + +namespace net = aedis::net; /* Similar to the basic1 example but * * 1. Reads the response in a loop. - * 2. Prints the command to which the response belongs. + * 2. Prints the command to which the response belongs to. */ net::awaitable ping() { - resp3::request req; - req.push(command::hello, 3); - req.push(command::ping); - req.push(command::quit); + try { + request req; + req.push(command::hello, 3); + req.push(command::ping); + req.push(command::quit); - auto socket = co_await make_connection(); - co_await async_write(socket, req); + auto socket = co_await make_connection(); + co_await async_write(socket, req); - std::string buffer; - while (!std::empty(req.commands)) { - resp3::response resp; - co_await async_read(socket, buffer, resp); - std::cout << req.commands.front() << ":\n" << resp << std::endl; - req.commands.pop(); + std::string buffer; + while (!std::empty(req.commands)) { + response resp; + co_await async_read(socket, buffer, resp); + std::cout << req.commands.front() << "\n" << resp << std::endl; + req.commands.pop(); + } + } catch (std::exception const& e) { + std::cerr << e.what() << std::endl; } } diff --git a/examples/basic3.cpp b/examples/basic3.cpp index 866eeb0c..d19748cc 100644 --- a/examples/basic3.cpp +++ b/examples/basic3.cpp @@ -12,7 +12,12 @@ #include "types.hpp" #include "utils.ipp" -using namespace aedis; +using aedis::command; +using aedis::resp3::request; +using aedis::resp3::response; +using aedis::resp3::async_read; + +namespace net = aedis::net; /* A slightly more elaborate way dealing with requests and responses. * @@ -21,19 +26,16 @@ using namespace aedis; * clarity. * * This can be used as a starting point for more complex applications. - * */ // Adds a new element in the queue if necessary. -void prepare_next(std::queue& reqs) +void prepare_next(std::queue& reqs) { if (std::empty(reqs) || std::size(reqs) == 1) reqs.push({}); } -void process_response( - std::queue& reqs, - resp3::response& resp) +void process_response(std::queue& reqs, response& resp) { std::cout << reqs.front().commands.front() << ":\n" @@ -51,23 +53,28 @@ void process_response( net::awaitable ping() { - std::queue reqs; - reqs.push({}); - reqs.back().push(command::hello, 3); + try { + std::queue reqs; + reqs.push({}); + reqs.back().push(command::hello, 3); - auto socket = co_await make_connection(); - std::string buffer; + auto socket = co_await make_connection(); + std::string buffer; - while (!std::empty(reqs)) { - co_await async_write(socket, reqs.front()); - while (!std::empty(reqs.front().commands)) { - resp3::response resp; - co_await async_read(socket, buffer, resp); - process_response(reqs, resp); - reqs.front().commands.pop(); + while (!std::empty(reqs)) { + co_await async_write(socket, reqs.front()); + while (!std::empty(reqs.front().commands)) { + response resp; + co_await async_read(socket, buffer, resp); + process_response(reqs, resp); + reqs.front().commands.pop(); + } + + reqs.pop(); } - reqs.pop(); + } catch (std::exception const& e) { + std::cerr << e.what() << std::endl; } }