From 4652a9cbdfee3b49c7cc64e8efdb93dfc9c4a386 Mon Sep 17 00:00:00 2001 From: Steven Geddis Date: Tue, 23 Jan 2018 18:55:46 +0100 Subject: [PATCH 1/4] 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 2/4] 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 8477fbb2727db8447397e72529322c54f481df55 Mon Sep 17 00:00:00 2001 From: Steven Geddis Date: Wed, 24 Jan 2018 13:24:43 +0100 Subject: [PATCH 3/4] 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 4/4] 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));