From 0a360ad09c58c51fe0a32803e3cfcd185ff08985 Mon Sep 17 00:00:00 2001 From: Taivo Pungas Date: Thu, 21 Dec 2017 15:01:35 +0200 Subject: [PATCH 01/14] README: quick link from top to TcpConnection explanation --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 632e9c0..8fc0303 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ sure that your compiler is up-to-date and supports C++11. **Note for the reader:** This readme file has a peculiar structure. We start explaining the pure and hard core low level interface in which you have to take care of opening socket connections yourself. In reality, you probably want -to use the simpler TCP interface that is being described later on. +to use the simpler TCP interface that is being described [later on](#tcp-connections). ABOUT From fff71d21a86a2d79f917d2dffdc95d3162672fce Mon Sep 17 00:00:00 2001 From: Alessandro Pischedda Date: Wed, 17 Jan 2018 15:15:48 +0100 Subject: [PATCH 02/14] Update Makefile Use c++14 in order to use 'initialization lambda' --- tests/Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Makefile b/tests/Makefile index b5bf2f3..e27d884 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,6 +1,6 @@ -CPP = g++ -CPPFLAGS = -Wall -c -I. -O2 -flto -std=c++11 -g -LD = g++ +CPP = g++ +CPPFLAGS = -Wall -c -I. -O2 -flto -std=c++14 -g +LD = g++ LDFLAGS = -lamqpcpp -lcopernica_event -lcopernica_network -lev RESULT = a.out SOURCES = $(wildcard *.cpp) From 11b2c7914fe357b972d7fa2770e7e669dc591254 Mon Sep 17 00:00:00 2001 From: Alessandro Pischedda Date: Wed, 17 Jan 2018 15:16:57 +0100 Subject: [PATCH 03/14] Fix g++ command Needs std=c++14 --- tests/libboostasio.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/libboostasio.cpp b/tests/libboostasio.cpp index ee9555c..c71c9e3 100644 --- a/tests/libboostasio.cpp +++ b/tests/libboostasio.cpp @@ -5,7 +5,7 @@ * * @author Gavin Smith * - * Compile with g++ libboostasio.cpp -o boost_test -lpthread -lboost_system -lamqpcpp + * Compile with g++ -std=c++14 libboostasio.cpp -o boost_test -lpthread -lboost_system -lamqpcpp */ /** From 97124dc6041b047eff333c9d2df84e42530269fe Mon Sep 17 00:00:00 2001 From: Alessandro Pischedda Date: Wed, 17 Jan 2018 15:20:04 +0100 Subject: [PATCH 04/14] Fix compile remove ?boost::placeholders::' . --- include/libboostasio.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/include/libboostasio.h b/include/libboostasio.h index 3a49f03..2ac1930 100644 --- a/include/libboostasio.h +++ b/include/libboostasio.h @@ -152,8 +152,8 @@ private: STRAND_SOCKET_HANDLER( boost::bind(&Watcher::read_handler, this, - boost::placeholders::_1, - boost::placeholders::_2, + _1, + _2, // C++17 has 'weak_from_this()' support. #if __cplusplus >= 201701L weak_from_this(), @@ -197,8 +197,8 @@ private: STRAND_SOCKET_HANDLER( boost::bind(&Watcher::write_handler, this, - boost::placeholders::_1, - boost::placeholders::_2, + _1, + _2, // C++17 has 'weak_from_this()' support. #if __cplusplus >= 201701L weak_from_this(), @@ -266,8 +266,8 @@ private: STRAND_SOCKET_HANDLER( boost::bind(&Watcher::read_handler, this, - boost::placeholders::_1, - boost::placeholders::_2, + _1, + _2, // C++17 has 'weak_from_this()' support. #if __cplusplus >= 201701L weak_from_this(), @@ -290,8 +290,8 @@ private: STRAND_SOCKET_HANDLER( boost::bind(&Watcher::write_handler, this, - boost::placeholders::_1, - boost::placeholders::_2, + _1, + _2, // C++17 has 'weak_from_this()' support. #if __cplusplus >= 201701L weak_from_this(), @@ -360,7 +360,7 @@ private: _timer.async_wait(STRAND_TIMER_HANDLER( boost::bind(&Timer::timeout, this, - boost::placeholders::_1, + _1, // C++17 has 'weak_from_this()' support. #if __cplusplus >= 201701L weak_from_this(), @@ -427,7 +427,7 @@ private: _timer.async_wait(STRAND_TIMER_HANDLER( boost::bind(&Timer::timeout, this, - boost::placeholders::_1, + _1, // C++17 has 'weak_from_this()' support. #if __cplusplus >= 201701L weak_from_this(), From 4c2ef4adb49ed111ed28b57e668c3947a1a83570 Mon Sep 17 00:00:00 2001 From: Alessandro Pischedda Date: Thu, 18 Jan 2018 09:13:19 +0100 Subject: [PATCH 05/14] Revert changes Revert to std=c++11. --- tests/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Makefile b/tests/Makefile index e27d884..ee7f59c 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,5 +1,5 @@ CPP = g++ -CPPFLAGS = -Wall -c -I. -O2 -flto -std=c++14 -g +CPPFLAGS = -Wall -c -I. -O2 -flto -std=c++11 -g LD = g++ LDFLAGS = -lamqpcpp -lcopernica_event -lcopernica_network -lev RESULT = a.out From fd14790166c03596c464024ffe27d69cd79e0bff Mon Sep 17 00:00:00 2001 From: Alessandro Pischedda Date: Thu, 18 Jan 2018 10:08:56 +0100 Subject: [PATCH 06/14] fix boost::placeholders undefined error and remain well explicited than use just '_1' or '_2' --- include/libboostasio.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/include/libboostasio.h b/include/libboostasio.h index 2ac1930..f0d4ed4 100644 --- a/include/libboostasio.h +++ b/include/libboostasio.h @@ -152,8 +152,8 @@ private: STRAND_SOCKET_HANDLER( boost::bind(&Watcher::read_handler, this, - _1, - _2, + boost::arg<1>(), + boost::arg<2>(), // C++17 has 'weak_from_this()' support. #if __cplusplus >= 201701L weak_from_this(), @@ -197,8 +197,8 @@ private: STRAND_SOCKET_HANDLER( boost::bind(&Watcher::write_handler, this, - _1, - _2, + boost::arg<1>(), + boost::arg<2>(), // C++17 has 'weak_from_this()' support. #if __cplusplus >= 201701L weak_from_this(), @@ -266,8 +266,8 @@ private: STRAND_SOCKET_HANDLER( boost::bind(&Watcher::read_handler, this, - _1, - _2, + boost::arg<1>(), + boost::arg<2>(), // C++17 has 'weak_from_this()' support. #if __cplusplus >= 201701L weak_from_this(), @@ -290,8 +290,8 @@ private: STRAND_SOCKET_HANDLER( boost::bind(&Watcher::write_handler, this, - _1, - _2, + boost::arg<1>(), + boost::arg<2>(), // C++17 has 'weak_from_this()' support. #if __cplusplus >= 201701L weak_from_this(), @@ -360,7 +360,7 @@ private: _timer.async_wait(STRAND_TIMER_HANDLER( boost::bind(&Timer::timeout, this, - _1, + boost::arg<1>(), // C++17 has 'weak_from_this()' support. #if __cplusplus >= 201701L weak_from_this(), @@ -427,7 +427,7 @@ private: _timer.async_wait(STRAND_TIMER_HANDLER( boost::bind(&Timer::timeout, this, - _1, + boost::arg<1>(), // C++17 has 'weak_from_this()' support. #if __cplusplus >= 201701L weak_from_this(), From 9935f8414bdda7854bb43c18723fd56b1529a36e Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Tue, 23 Jan 2018 17:17:21 +0100 Subject: [PATCH 07/14] update copyright year when connection is established to rabbitmq --- src/connectionstartframe.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/connectionstartframe.h b/src/connectionstartframe.h index 139d2b9..6bea81c 100644 --- a/src/connectionstartframe.h +++ b/src/connectionstartframe.h @@ -196,8 +196,8 @@ public: properties["product"] = "Copernica AMQP library"; properties["version"] = "Unknown"; properties["platform"] = "Unknown"; - properties["copyright"] = "Copyright 2015 Copernica BV"; - properties["information"] = "http://www.copernica.com"; + properties["copyright"] = "Copyright 2015 - 2018 Copernica BV"; + properties["information"] = "https://www.copernica.com"; properties["capabilities"] = capabilities; // move connection to handshake mode From 4652a9cbdfee3b49c7cc64e8efdb93dfc9c4a386 Mon Sep 17 00:00:00 2001 From: Steven Geddis Date: Tue, 23 Jan 2018 18:55:46 +0100 Subject: [PATCH 08/14] libboostasio.h does not compile with clang-3.9 due to following error when compile if cpp17 preprocessor check: embedding a directive within macro arguments has undefined behavior [-Werror,-Wembedded-directive]. Fix this by moving the bind away from preprocessor macro. Replace the STRAND_SOCKET_HANDLER, STRAND_TIMER_HANDLER macros with template functions. --- include/libboostasio.h | 134 ++++++++++++++++++++++++----------------- 1 file changed, 80 insertions(+), 54 deletions(-) diff --git a/include/libboostasio.h b/include/libboostasio.h index f0d4ed4..96058c1 100644 --- a/include/libboostasio.h +++ b/include/libboostasio.h @@ -1,5 +1,4 @@ -/** - * LibBoostAsio.h +bBoostAsio.h * * Implementation for the AMQP::TcpHandler that is optimized for boost::asio. You can * use this class instead of a AMQP::TcpHandler class, just pass the boost asio service @@ -27,33 +26,35 @@ #include -/////////////////////////////////////////////////////////////////// -#define STRAND_SOCKET_HANDLER(_fn) \ -[fn = _fn, strand = _strand](const boost::system::error_code &ec, \ - const std::size_t bytes_transferred) \ -{ \ - const std::shared_ptr apStrand = strand.lock(); \ - if (!apStrand) \ - { \ - fn(boost::system::errc::make_error_code(boost::system::errc::operation_canceled),std::size_t{0}); \ - return; \ - } \ - \ - apStrand->dispatch(boost::bind(fn,ec,bytes_transferred)); \ +template +void strand_sock_dispatch_func( + Func fn, + std::weak_ptr strand, + const boost::system::error_code &ec, + const std::size_t bytes_transferred +) { + const std::shared_ptr apStrand = strand.lock(); + if (!apStrand) + { + fn(boost::system::errc::make_error_code(boost::system::errc::operation_canceled), std::size_t{0}); + return; + } + apStrand->dispatch(boost::bind(fn, ec, bytes_transferred)); } -/////////////////////////////////////////////////////////////////// -#define STRAND_TIMER_HANDLER(_fn) \ -[fn = _fn, strand = _strand](const boost::system::error_code &ec) \ -{ \ - const std::shared_ptr apStrand = strand.lock(); \ - if (!apStrand) \ - { \ - fn(boost::system::errc::make_error_code(boost::system::errc::operation_canceled)); \ - return; \ - } \ - \ - apStrand->dispatch(boost::bind(fn,ec)); \ +template +void strand_timer_dispatch_func( + Func fn, + std::weak_ptr strand, + const boost::system::error_code &ec +) { + const std::shared_ptr apStrand = strand.lock(); + if (!apStrand) + { + fn(boost::system::errc::make_error_code(boost::system::errc::operation_canceled)); + return; + } + apStrand->dispatch(boost::bind(fn,ec)); } /** @@ -148,9 +149,7 @@ private: _read_pending = true; - _socket.async_read_some(boost::asio::null_buffers(), - STRAND_SOCKET_HANDLER( - boost::bind(&Watcher::read_handler, + auto func = boost::bind(&Watcher::read_handler, this, boost::arg<1>(), boost::arg<2>(), @@ -161,7 +160,13 @@ private: shared_from_this(), #endif connection, - fd))); + fd); + _socket.async_read_some( + boost::asio::null_buffers(), + [f = func, strand = _strand](const boost::system::error_code &ec, const std::size_t bytes_transferred) { + strand_sock_dispatch_func(f, strand, ec, bytes_transferred); + } + ); } } @@ -193,9 +198,7 @@ private: _write_pending = true; - _socket.async_write_some(boost::asio::null_buffers(), - STRAND_SOCKET_HANDLER( - boost::bind(&Watcher::write_handler, + auto func = boost::bind(&Watcher::write_handler, this, boost::arg<1>(), boost::arg<2>(), @@ -206,7 +209,13 @@ private: shared_from_this(), #endif connection, - fd))); + fd); + _socket.async_write_some( + boost::asio::null_buffers(), + [f = func, strand = _strand](const boost::system::error_code &ec, const std::size_t bytes_transferred) { + strand_sock_dispatch_func(f, strand, ec, bytes_transferred); + } + ); } } @@ -262,9 +271,7 @@ private: { _read_pending = true; - _socket.async_read_some(boost::asio::null_buffers(), - STRAND_SOCKET_HANDLER( - boost::bind(&Watcher::read_handler, + auto func = boost::bind(&Watcher::read_handler, this, boost::arg<1>(), boost::arg<2>(), @@ -275,7 +282,13 @@ private: shared_from_this(), #endif connection, - fd))); + fd); + _socket.async_read_some( + boost::asio::null_buffers(), + [f = func, strand = _strand](const boost::system::error_code &ec, const std::size_t bytes_transferred) { + strand_sock_dispatch_func(f, strand, ec, bytes_transferred); + } + ); } // 2. Handle writes? @@ -286,9 +299,7 @@ private: { _write_pending = true; - _socket.async_write_some(boost::asio::null_buffers(), - STRAND_SOCKET_HANDLER( - boost::bind(&Watcher::write_handler, + auto func = boost::bind(&Watcher::write_handler, this, boost::arg<1>(), boost::arg<2>(), @@ -299,7 +310,13 @@ private: shared_from_this(), #endif connection, - fd))); + fd); + _socket.async_write_some( + boost::asio::null_buffers(), + [f = func, strand = _strand](const boost::system::error_code &ec, const std::size_t bytes_transferred) { + strand_sock_dispatch_func(f, strand, ec, bytes_transferred); + } + ); } } }; @@ -353,12 +370,7 @@ private: connection->heartbeat(); } - // Reschedule the timer for the future: - _timer.expires_at(_timer.expires_at() + boost::posix_time::seconds(timeout)); - - // Posts the timer event - _timer.async_wait(STRAND_TIMER_HANDLER( - boost::bind(&Timer::timeout, + auto func = boost::bind(&Timer::timeout, this, boost::arg<1>(), // C++17 has 'weak_from_this()' support. @@ -368,7 +380,17 @@ private: shared_from_this(), #endif connection, - timeout))); + timeout); + + // Reschedule the timer for the future: + _timer.expires_at(_timer.expires_at() + boost::posix_time::seconds(timeout)); + + // Posts the timer event + _timer.async_wait( + [f = func, strand = _strand](const boost::system::error_code &ec) { + strand_timer_dispatch_func(f, strand, ec); + } + ); } } @@ -423,9 +445,7 @@ private: // stop timer in case it was already set stop(); - _timer.expires_from_now(boost::posix_time::seconds(timeout)); - _timer.async_wait(STRAND_TIMER_HANDLER( - boost::bind(&Timer::timeout, + auto func = boost::bind(&Timer::timeout, this, boost::arg<1>(), // C++17 has 'weak_from_this()' support. @@ -435,7 +455,13 @@ private: shared_from_this(), #endif connection, - timeout))); + timeout); + _timer.expires_from_now(boost::posix_time::seconds(timeout)); + _timer.async_wait( + [f = func, strand = _strand](const boost::system::error_code &ec) { + strand_timer_dispatch_func(f, strand, ec); + } + ); } }; From d71040b2e564a9f0c23cca2bffc441abd513f918 Mon Sep 17 00:00:00 2001 From: Steven Geddis Date: Tue, 23 Jan 2018 19:16:44 +0100 Subject: [PATCH 09/14] fix bad opening comment block --- include/libboostasio.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/libboostasio.h b/include/libboostasio.h index 96058c1..dd229f9 100644 --- a/include/libboostasio.h +++ b/include/libboostasio.h @@ -1,4 +1,5 @@ -bBoostAsio.h +/** + * LibBoostAsio.h * * Implementation for the AMQP::TcpHandler that is optimized for boost::asio. You can * use this class instead of a AMQP::TcpHandler class, just pass the boost asio service From 9863fd87ee767adc14c0c69fb6e993ecacd0b05c Mon Sep 17 00:00:00 2001 From: zerodefect Date: Tue, 23 Jan 2018 20:23:11 +0000 Subject: [PATCH 10/14] Added travis.yml file. --- .travis.yml | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..4adc133 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,112 @@ +################ +# project config +################ + +# C++ project +language: cpp + +dist: trusty +sudo: required +group: edge + + +################ +# build matrix +################ + +matrix: + include: + + ################ + # Linux / GCC + ################ + + - os: linux + compiler: gcc + env: + - COMPILER=g++-4.9 + - CXXFLAGS=-std=c++11 + addons: + apt: + sources: ['ubuntu-toolchain-r-test'] + packages: ['g++-4.9', 'ninja-build'] + + - os: linux + compiler: gcc + env: + - COMPILER=g++-5 + - CXXFLAGS=-std=c++11 + addons: + apt: + sources: ['ubuntu-toolchain-r-test'] + packages: ['g++-5', 'ninja-build'] + + - os: linux + compiler: gcc + env: + - COMPILER=g++-6 + - CXXFLAGS=-std=c++11 + addons: + apt: + sources: ['ubuntu-toolchain-r-test'] + packages: ['g++-6', 'ninja-build'] + + - os: linux + compiler: gcc + env: + - COMPILER=g++-7 + - CXXFLAGS=-std=c++11 + addons: + apt: + sources: ['ubuntu-toolchain-r-test'] + packages: ['g++-7', 'ninja-build'] + + ################ + # Linux / Clang + ################ + + - os: linux + compiler: clang + env: + - COMPILER=clang++-3.9 + - CXXFLAGS=-std=c++11 + addons: + apt: + sources: ['ubuntu-toolchain-r-test'] + packages: ['g++-6', 'clang-3.9', 'ninja-build'] + + - os: linux + compiler: clang + env: + - COMPILER=clang++-4.0 + - CXXFLAGS=-std=c++11 + addons: + apt: + sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-trusty-4.0'] + packages: ['clang-4.0', 'ninja-build'] + + - os: linux + compiler: clang + env: + - COMPILER=clang++-5.0 + - CXXFLAGS=-std=c++11 + addons: + apt: + sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-trusty-5.0'] + packages: ['clang-5.0', 'ninja-build'] + +################ +# build / test +################ + +script: + + # show OS/compiler version + - uname -a + - $CXX --version + + # compile and execute unit tests + - mkdir -p build.release && cd build.release + - cmake .. ${CMAKE_OPTIONS} -GNinja && cmake --build . --config Release + - cd .. + From 1fb885d334a88c3176da234c38c937b220c449aa Mon Sep 17 00:00:00 2001 From: zerodefect Date: Tue, 23 Jan 2018 20:46:23 +0000 Subject: [PATCH 11/14] Fixed failed builds. Removed some os/compiler combinations that I couldn't get working. --- .travis.yml | 27 +-------------------------- 1 file changed, 1 insertion(+), 26 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4adc133..434fb28 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,31 +21,19 @@ matrix: # Linux / GCC ################ - - os: linux - compiler: gcc - env: - - COMPILER=g++-4.9 - - CXXFLAGS=-std=c++11 - addons: - apt: - sources: ['ubuntu-toolchain-r-test'] - packages: ['g++-4.9', 'ninja-build'] - - os: linux compiler: gcc env: - COMPILER=g++-5 - - CXXFLAGS=-std=c++11 addons: apt: - sources: ['ubuntu-toolchain-r-test'] + #sources: ['ubuntu-toolchain-r-test'] packages: ['g++-5', 'ninja-build'] - os: linux compiler: gcc env: - COMPILER=g++-6 - - CXXFLAGS=-std=c++11 addons: apt: sources: ['ubuntu-toolchain-r-test'] @@ -55,7 +43,6 @@ matrix: compiler: gcc env: - COMPILER=g++-7 - - CXXFLAGS=-std=c++11 addons: apt: sources: ['ubuntu-toolchain-r-test'] @@ -65,21 +52,10 @@ matrix: # Linux / Clang ################ - - os: linux - compiler: clang - env: - - COMPILER=clang++-3.9 - - CXXFLAGS=-std=c++11 - addons: - apt: - sources: ['ubuntu-toolchain-r-test'] - packages: ['g++-6', 'clang-3.9', 'ninja-build'] - - os: linux compiler: clang env: - COMPILER=clang++-4.0 - - CXXFLAGS=-std=c++11 addons: apt: sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-trusty-4.0'] @@ -89,7 +65,6 @@ matrix: compiler: clang env: - COMPILER=clang++-5.0 - - CXXFLAGS=-std=c++11 addons: apt: sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-trusty-5.0'] From e9307533fadd01605307a3edeb7976773f8fb27e Mon Sep 17 00:00:00 2001 From: zerodefect Date: Tue, 23 Jan 2018 21:12:31 +0000 Subject: [PATCH 12/14] Fixed failed builds. --- .travis.yml | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index 434fb28..06f69b0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,17 +23,15 @@ matrix: - os: linux compiler: gcc - env: - - COMPILER=g++-5 + env: COMPILER=g++-5 addons: apt: - #sources: ['ubuntu-toolchain-r-test'] + sources: ['ubuntu-toolchain-r-test'] packages: ['g++-5', 'ninja-build'] - os: linux compiler: gcc - env: - - COMPILER=g++-6 + env: COMPILER=g++-6 addons: apt: sources: ['ubuntu-toolchain-r-test'] @@ -41,8 +39,7 @@ matrix: - os: linux compiler: gcc - env: - - COMPILER=g++-7 + env: COMPILER=g++-7 addons: apt: sources: ['ubuntu-toolchain-r-test'] @@ -54,8 +51,7 @@ matrix: - os: linux compiler: clang - env: - - COMPILER=clang++-4.0 + env: COMPILER=clang++-4.0 addons: apt: sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-trusty-4.0'] @@ -63,8 +59,7 @@ matrix: - os: linux compiler: clang - env: - - COMPILER=clang++-5.0 + env: COMPILER=clang++-5.0 addons: apt: sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-trusty-5.0'] From 8477fbb2727db8447397e72529322c54f481df55 Mon Sep 17 00:00:00 2001 From: Steven Geddis Date: Wed, 24 Jan 2018 13:24:43 +0100 Subject: [PATCH 13/14] add PTR_FROM_THIS define; remove io_handler code duplication via read/write/dispatch wrapper functions. --- include/libboostasio.h | 231 ++++++++++++++++------------------------- 1 file changed, 92 insertions(+), 139 deletions(-) diff --git a/include/libboostasio.h b/include/libboostasio.h index dd229f9..10bac80 100644 --- a/include/libboostasio.h +++ b/include/libboostasio.h @@ -26,37 +26,12 @@ #include #include - -template -void strand_sock_dispatch_func( - Func fn, - std::weak_ptr strand, - const boost::system::error_code &ec, - const std::size_t bytes_transferred -) { - const std::shared_ptr apStrand = strand.lock(); - if (!apStrand) - { - fn(boost::system::errc::make_error_code(boost::system::errc::operation_canceled), std::size_t{0}); - return; - } - apStrand->dispatch(boost::bind(fn, ec, bytes_transferred)); -} - -template -void strand_timer_dispatch_func( - Func fn, - std::weak_ptr strand, - const boost::system::error_code &ec -) { - const std::shared_ptr apStrand = strand.lock(); - if (!apStrand) - { - fn(boost::system::errc::make_error_code(boost::system::errc::operation_canceled)); - return; - } - apStrand->dispatch(boost::bind(fn,ec)); -} +// C++17 has 'weak_from_this()' support. +#if __cplusplus >= 201701L +#define PTR_FROM_THIS weak_from_this +#else +#define PTR_FROM_THIS shared_from_this +#endif /** * Set up namespace @@ -122,6 +97,52 @@ private: */ bool _write_pending{false}; + using handler_cb = boost::function; + using io_handler = boost::function; + + /** + * Builds a io handler callback that executes the io callback in a strand. + * @param io_handler The handler callback to dispatch + * @return handler_cb A function wrapping the execution of the handler function in a io_service::strand. + */ + handler_cb get_dispatch_wrapper(io_handler fn) + { + return [fn, strand = _strand](const boost::system::error_code &ec, const std::size_t bytes_transferred) + { + const std::shared_ptr apStrand = strand.lock(); + if (!apStrand) + { + fn(boost::system::errc::make_error_code(boost::system::errc::operation_canceled), std::size_t{0}); + return; + } + apStrand->dispatch(boost::bind(fn, ec, bytes_transferred)); + }; + } + + /** + * Binds and returns a read handler for the io operation. + * @param connection The connection being watched. + * @param fd The file descripter being watched. + * @return handler callback + */ + handler_cb get_read_handler(TcpConnection *const connection, const int fd) + { + auto fn = boost::bind(&Watcher::read_handler, this, _1, _2, PTR_FROM_THIS(), connection, fd); + return get_dispatch_wrapper(fn); + } + + /** + * Binds and returns a read handler for the io operation. + * @param connection The connection being watched. + * @param fd The file descripter being watched. + * @return handler callback + */ + handler_cb get_write_handler(TcpConnection *const connection, const int fd) + { + auto fn = boost::bind(&Watcher::write_handler, this, _1, _2, PTR_FROM_THIS(), connection, fd); + return get_dispatch_wrapper(fn); + } + /** * Handler method that is called by boost's io_service when the socket pumps a read event. * @param ec The status of the callback. @@ -149,25 +170,8 @@ private: connection->process(fd, AMQP::readable); _read_pending = true; - - auto func = boost::bind(&Watcher::read_handler, - this, - boost::arg<1>(), - boost::arg<2>(), -// C++17 has 'weak_from_this()' support. -#if __cplusplus >= 201701L - weak_from_this(), -#else - shared_from_this(), -#endif - connection, - fd); - _socket.async_read_some( - boost::asio::null_buffers(), - [f = func, strand = _strand](const boost::system::error_code &ec, const std::size_t bytes_transferred) { - strand_sock_dispatch_func(f, strand, ec, bytes_transferred); - } - ); + + _socket.async_read_some(boost::asio::null_buffers(), get_read_handler(connection, fd)); } } @@ -199,24 +203,7 @@ private: _write_pending = true; - auto func = boost::bind(&Watcher::write_handler, - this, - boost::arg<1>(), - boost::arg<2>(), -// C++17 has 'weak_from_this()' support. -#if __cplusplus >= 201701L - weak_from_this(), -#else - shared_from_this(), -#endif - connection, - fd); - _socket.async_write_some( - boost::asio::null_buffers(), - [f = func, strand = _strand](const boost::system::error_code &ec, const std::size_t bytes_transferred) { - strand_sock_dispatch_func(f, strand, ec, bytes_transferred); - } - ); + _socket.async_write_some(boost::asio::null_buffers(), get_write_handler(connection, fd)); } } @@ -272,24 +259,7 @@ private: { _read_pending = true; - auto func = boost::bind(&Watcher::read_handler, - this, - boost::arg<1>(), - boost::arg<2>(), -// C++17 has 'weak_from_this()' support. -#if __cplusplus >= 201701L - weak_from_this(), -#else - shared_from_this(), -#endif - connection, - fd); - _socket.async_read_some( - boost::asio::null_buffers(), - [f = func, strand = _strand](const boost::system::error_code &ec, const std::size_t bytes_transferred) { - strand_sock_dispatch_func(f, strand, ec, bytes_transferred); - } - ); + _socket.async_read_some(boost::asio::null_buffers(), get_read_handler(connection, fd)); } // 2. Handle writes? @@ -300,24 +270,7 @@ private: { _write_pending = true; - auto func = boost::bind(&Watcher::write_handler, - this, - boost::arg<1>(), - boost::arg<2>(), -// C++17 has 'weak_from_this()' support. -#if __cplusplus >= 201701L - weak_from_this(), -#else - shared_from_this(), -#endif - connection, - fd); - _socket.async_write_some( - boost::asio::null_buffers(), - [f = func, strand = _strand](const boost::system::error_code &ec, const std::size_t bytes_transferred) { - strand_sock_dispatch_func(f, strand, ec, bytes_transferred); - } - ); + _socket.async_write_some(boost::asio::null_buffers(), get_write_handler(connection, fd)); } } }; @@ -347,11 +300,39 @@ private: */ boost::asio::deadline_timer _timer; + using handler_fn = boost::function; + /** + * Binds and returns a lamba function handler for the io operation. + * @param connection The connection being watched. + * @param timeout The file descripter being watched. + * @return handler callback + */ + handler_fn get_handler(TcpConnection *const connection, const uint16_t timeout) + { + auto fn = boost::bind(&Timer::timeout, + this, + _1, + PTR_FROM_THIS(), + connection, + timeout); + return [fn, this](const boost::system::error_code &ec) + { + const std::shared_ptr apStrand = _strand.lock(); + if (!apStrand) + { + fn(boost::system::errc::make_error_code(boost::system::errc::operation_canceled)); + return; + } + apStrand->dispatch(boost::bind(fn, ec)); + }; + } + /** * Callback method that is called by libev when the timer expires + * @param ec error code returned from loop * @param loop The loop in which the event was triggered - * @param timer Internal timer object - * @param revents The events that triggered this call + * @param connection + * @param timeout */ void timeout(const boost::system::error_code &ec, std::weak_ptr awpThis, @@ -371,27 +352,11 @@ private: connection->heartbeat(); } - auto func = boost::bind(&Timer::timeout, - this, - boost::arg<1>(), -// C++17 has 'weak_from_this()' support. -#if __cplusplus >= 201701L - weak_from_this(), -#else - shared_from_this(), -#endif - connection, - timeout); - // Reschedule the timer for the future: _timer.expires_at(_timer.expires_at() + boost::posix_time::seconds(timeout)); // Posts the timer event - _timer.async_wait( - [f = func, strand = _strand](const boost::system::error_code &ec) { - strand_timer_dispatch_func(f, strand, ec); - } - ); + _timer.async_wait(get_handler(connection, timeout)); } } @@ -446,23 +411,11 @@ private: // stop timer in case it was already set stop(); - auto func = boost::bind(&Timer::timeout, - this, - boost::arg<1>(), -// C++17 has 'weak_from_this()' support. -#if __cplusplus >= 201701L - weak_from_this(), -#else - shared_from_this(), -#endif - connection, - timeout); + // Reschedule the timer for the future: _timer.expires_from_now(boost::posix_time::seconds(timeout)); - _timer.async_wait( - [f = func, strand = _strand](const boost::system::error_code &ec) { - strand_timer_dispatch_func(f, strand, ec); - } - ); + + // Posts the timer event + _timer.async_wait(get_handler(connection, timeout)); } }; From 959e6238ae24820e0e5aca49f62bd5882616604b Mon Sep 17 00:00:00 2001 From: Steven Geddis Date: Wed, 24 Jan 2018 15:06:35 +0100 Subject: [PATCH 14/14] code formatting; capture strand by copy in timer handler --- include/libboostasio.h | 46 ++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/include/libboostasio.h b/include/libboostasio.h index 10bac80..3f80b89 100644 --- a/include/libboostasio.h +++ b/include/libboostasio.h @@ -127,7 +127,13 @@ private: */ handler_cb get_read_handler(TcpConnection *const connection, const int fd) { - auto fn = boost::bind(&Watcher::read_handler, this, _1, _2, PTR_FROM_THIS(), connection, fd); + auto fn = boost::bind(&Watcher::read_handler, + this, + _1, + _2, + PTR_FROM_THIS(), + connection, + fd); return get_dispatch_wrapper(fn); } @@ -139,7 +145,13 @@ private: */ handler_cb get_write_handler(TcpConnection *const connection, const int fd) { - auto fn = boost::bind(&Watcher::write_handler, this, _1, _2, PTR_FROM_THIS(), connection, fd); + auto fn = boost::bind(&Watcher::write_handler, + this, + _1, + _2, + PTR_FROM_THIS(), + connection, + fd); return get_dispatch_wrapper(fn); } @@ -171,7 +183,9 @@ private: _read_pending = true; - _socket.async_read_some(boost::asio::null_buffers(), get_read_handler(connection, fd)); + _socket.async_read_some( + boost::asio::null_buffers(), + get_read_handler(connection, fd)); } } @@ -203,7 +217,9 @@ private: _write_pending = true; - _socket.async_write_some(boost::asio::null_buffers(), get_write_handler(connection, fd)); + _socket.async_write_some( + boost::asio::null_buffers(), + get_write_handler(connection, fd)); } } @@ -259,7 +275,9 @@ private: { _read_pending = true; - _socket.async_read_some(boost::asio::null_buffers(), get_read_handler(connection, fd)); + _socket.async_read_some( + boost::asio::null_buffers(), + get_read_handler(connection, fd)); } // 2. Handle writes? @@ -270,7 +288,9 @@ private: { _write_pending = true; - _socket.async_write_some(boost::asio::null_buffers(), get_write_handler(connection, fd)); + _socket.async_write_some( + boost::asio::null_buffers(), + get_write_handler(connection, fd)); } } }; @@ -310,14 +330,14 @@ private: handler_fn get_handler(TcpConnection *const connection, const uint16_t timeout) { auto fn = boost::bind(&Timer::timeout, - this, - _1, - PTR_FROM_THIS(), - connection, - timeout); - return [fn, this](const boost::system::error_code &ec) + this, + _1, + PTR_FROM_THIS(), + connection, + timeout); + return [fn, strand = _strand](const boost::system::error_code &ec) { - const std::shared_ptr apStrand = _strand.lock(); + const std::shared_ptr apStrand = strand.lock(); if (!apStrand) { fn(boost::system::errc::make_error_code(boost::system::errc::operation_canceled));