From b2d9efe8e963abe1e29737ed020458e80d44a44f Mon Sep 17 00:00:00 2001 From: Marcelo Zimbres Date: Sat, 23 Nov 2019 09:12:59 +0100 Subject: [PATCH] Improvements in the examples. --- README.md | 70 ++++++++++++++++++++++++++++++++++++++++++++++++---- aedis.hpp | 7 ------ examples.cpp | 57 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 121 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index ca2f3b85..f74a324b 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Commands can be generated easily and there is support for STL containers when it makes sense ```cpp -void foo() +void example1() { std::list a {"one" ,"two", "three"}; @@ -72,11 +72,71 @@ void foo() } ``` -# Features +The following example shows how to specify the configuration options -* Pubsub -* Pipeline -* Reconnection on connection lost. +```cpp +void example2() +{ + net::io_context ioc; + + session::config cfg + { "127.0.0.1" // host + , "6379" // port + , 256 // Max pipeline size + , std::chrono::milliseconds {500} // Connection retry + , {} // Sentinel addresses + , log::level::info + }; + + session s {ioc, cfg, "id"}; + + s.send(ping()); + + s.run(); + ioc.run(); +} +``` +The maximum pipeline size above refers to the blocks of commands sent +via the `session::send` function and not to the individual commands. +Logging is made using `std::clog`. The string `"id"` passed as third +argument to the session is prefixed to each log message. +Support for redis sentinel is still not implemented. + +# Callbacks + +It is possible to set three callbacks as shown in the example bellow + +```cpp +void example3() +{ + net::io_context ioc; + session s {ioc}; + + s.set_on_conn_handler([]() { + std::cout << "Connected" << std::endl; + }); + + s.set_msg_handler([](auto ec, auto res) { + if (ec) { + std::cerr << "Error: " << ec.message() << std::endl; + return; + } + + std::copy( std::cbegin(res) + , std::cend(res) + , std::ostream_iterator(std::cout, " ")); + + std::cout << std::endl; + }); + + s.send(ping()); + + s.run(); + ioc.run(); +} +``` + +# Missing features The main missing features at the moment are diff --git a/aedis.hpp b/aedis.hpp index 9af7aaf1..beafabdc 100644 --- a/aedis.hpp +++ b/aedis.hpp @@ -464,9 +464,6 @@ class session { public: using on_conn_handler_type = std::function; - using on_disconnect_handler_type = - std::function; - using msg_handler_type = std::function)>; @@ -519,7 +516,6 @@ private: on_conn_handler_type conn_handler_ = [](){}; - on_disconnect_handler_type disconn_handler_ = [](auto const&){}; void start_reading_resp() { @@ -706,9 +702,6 @@ public: void set_on_conn_handler(on_conn_handler_type handler) { conn_handler_ = std::move(handler);}; - void set_on_disconnect_handler(on_disconnect_handler_type handler) - { disconn_handler_ = std::move(handler);}; - void set_msg_handler(msg_handler_type handler) { msg_handler_ = std::move(handler);}; diff --git a/examples.cpp b/examples.cpp index 03c8af69..9cc6fdfa 100644 --- a/examples.cpp +++ b/examples.cpp @@ -20,7 +20,7 @@ void send(std::string cmd) ioc.run(); } -int main(int argc, char* argv[]) +void example1() { std::list a {"one" ,"two", "three"}; @@ -63,3 +63,58 @@ int main(int argc, char* argv[]) send(std::move(s)); } +void example2() +{ + net::io_context ioc; + + session::config cfg + { "127.0.0.1" // host + , "6379" // port + , 256 // Max pipeline size + , std::chrono::milliseconds {500} // Connection retry + , {} // Sentinel addresses + , log::level::info + }; + + session s {ioc, cfg, "id"}; + + s.send(ping()); + + s.run(); + ioc.run(); +} + +void example3() +{ + net::io_context ioc; + session s {ioc}; + + s.set_on_conn_handler([]() { + std::cout << "Connected" << std::endl; + }); + + s.set_msg_handler([](auto ec, auto res) { + if (ec) { + std::cerr << "Error: " << ec.message() << std::endl; + } + + std::copy( std::cbegin(res) + , std::cend(res) + , std::ostream_iterator(std::cout, " ")); + + std::cout << std::endl; + }); + + s.send(ping()); + + s.run(); + ioc.run(); +} + +int main(int argc, char* argv[]) +{ + example1(); + //example2(); + //example3(); +} +