diff --git a/Makefile b/Makefile index b806d69f..01d2273a 100644 --- a/Makefile +++ b/Makefile @@ -6,9 +6,9 @@ CPPFLAGS += -std=c++17 -g CPPFLAGS += -I/opt/boost_1_74_0/include -CPPFLAGS += -D BOOST_ASIO_CONCURRENCY_HINT_1=BOOST_ASIO_CONCURRENCY_HINT_UNSAFE -CPPFLAGS += -D BOOST_ASIO_NO_DEPRECATED -CPPFLAGS += -D BOOST_ASIO_NO_TS_EXECUTORS +#CPPFLAGS += -D BOOST_ASIO_CONCURRENCY_HINT_1=BOOST_ASIO_CONCURRENCY_HINT_UNSAFE +#CPPFLAGS += -D BOOST_ASIO_NO_DEPRECATED +#CPPFLAGS += -D BOOST_ASIO_NO_TS_EXECUTORS all: examples tests @@ -20,10 +20,13 @@ Makefile.dep: examples: % : %.o $(CXX) -o $@ $^ $(CPPFLAGS) -lfmt -lpthread +coroutine: coroutine.cpp + g++ -std=c++20 -g -I/opt/boost_1_74_0/include -fcoroutines -pthread coroutine.cpp + tests: % : %.o $(CXX) -o $@ $^ $(CPPFLAGS) -lfmt -lpthread .PHONY: clean clean: - rm -f examples examples.o tests tests.o Makefile.dep + rm -f coroutine.o coroutine examples examples.o tests tests.o Makefile.dep diff --git a/aedis.hpp b/aedis.hpp index 79017a6f..90a62b01 100644 --- a/aedis.hpp +++ b/aedis.hpp @@ -151,6 +151,13 @@ std::size_t get_length(char const* p) return len; } +void print(std::vector const& v) +{ + for (auto const& o : v) + std::cout << o << " "; + std::cout << std::endl; +} + void print_command_raw(std::string const& data, int n) { for (int i = 0; i < n; ++i) { @@ -254,11 +261,14 @@ struct parse_op { template < class AsyncReadStream, - class CompletionToken> + class CompletionToken = + net::default_completion_token_t + > auto async_read( AsyncReadStream& stream, resp::buffer* buffer, - CompletionToken&& token) + CompletionToken&& token = + net::default_completion_token_t{}) { return net::async_compose < CompletionToken diff --git a/coroutine.cpp b/coroutine.cpp new file mode 100644 index 00000000..e1ad5d72 --- /dev/null +++ b/coroutine.cpp @@ -0,0 +1,51 @@ +#include + +#include "aedis.hpp" + +namespace net = aedis::net; +using tcp = net::ip::tcp; +using tcp_socket = net::use_awaitable_t<>::as_default_on_t; + +namespace this_coro = net::this_coro; + +using namespace net; +using namespace aedis; + +awaitable example() +{ + auto executor = co_await this_coro::executor; + + tcp::resolver resolver_(executor); + auto const res = resolver_.resolve("127.0.0.1", "6379"); + + tcp_socket socket {executor}; + co_await async_connect(socket, res); + + auto cmd = ping() + + role() + + multi() + + set("age", {"39"}) + + incr("age") + + get("age") + + expire("Age", 10) + + publish("channel", "message") + + exec() + + quit() + ; + + co_await async_write(socket, buffer(cmd)); + + resp::buffer buffer; + for (;;) { + co_await resp::async_read(socket, &buffer); + resp::print(buffer.res); + buffer.res.clear(); + } +} + +int main() +{ + io_context ioc {1}; + co_spawn(ioc, example(), detached); + ioc.run(); +} diff --git a/tests.cpp b/tests.cpp index fee0cf96..a7df59dd 100644 --- a/tests.cpp +++ b/tests.cpp @@ -195,13 +195,6 @@ struct test_handler { } }; -void print(std::vector const& v) -{ - for (auto const& o : v) - std::cout << o << " "; - std::cout << std::endl; -} - int main(int argc, char* argv[]) { rpush_lrange();