From b713d48bb97cd674ed145414733ee8cead54f90c Mon Sep 17 00:00:00 2001 From: Steven Geddis Date: Thu, 1 Feb 2018 12:44:48 +0100 Subject: [PATCH 1/5] add second constructor with heartbeat interval; apply modern cpp typedefs; Add comments; --- include/amqpcpp/libboostasio.h | 40 +++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/include/amqpcpp/libboostasio.h b/include/amqpcpp/libboostasio.h index 18b2a10..2ba7918 100644 --- a/include/amqpcpp/libboostasio.h +++ b/include/amqpcpp/libboostasio.h @@ -62,7 +62,7 @@ private: */ boost::asio::io_service & _ioservice; - typedef std::weak_ptr strand_weak_ptr; + using strand_weak_ptr = std::weak_ptr; /** * The boost asio io_service::strand managed pointer. @@ -77,7 +77,6 @@ private: */ boost::asio::posix::stream_descriptor _socket; - /** * A boolean that indicates if the watcher is monitoring for read events. * @var _read True if reads are being monitored else false. @@ -315,7 +314,7 @@ private: */ boost::asio::io_service & _ioservice; - typedef std::weak_ptr strand_weak_ptr; + using strand_weak_ptr = std::weak_ptr; /** * The boost asio io_service::strand managed pointer. @@ -330,6 +329,7 @@ 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. @@ -457,7 +457,7 @@ private: */ boost::asio::io_service & _ioservice; - typedef std::shared_ptr strand_shared_ptr; + using strand_shared_ptr = std::shared_ptr; /** * The boost asio io_service::strand managed pointer. @@ -465,16 +465,23 @@ private: */ strand_shared_ptr _strand; - /** * All I/O watchers that are active, indexed by their filedescriptor * @var std::map */ std::map > _watchers; - + /** + * The boost asio io_service::deadline_timer managed pointer. + * @var class std::shared_ptr + */ std::shared_ptr _timer; + /** + * The heartbeat timer interval (in seconds). + * @var uint16_t + */ + uint16_t _timer_interval; /** * Method that is called by AMQP-CPP to register a filedescriptor for readability or writability @@ -528,6 +535,9 @@ protected: // skip if no heartbeats are needed if (interval == 0) return 0; + // use the most frequent heartbeat interval (user-specified or rabbit server default). + interval = (_timer_interval > 0 && _timer_interval < interval) ? _timer_interval : interval; + // set the timer _timer->set(connection, interval); @@ -551,7 +561,22 @@ public: explicit LibBoostAsioHandler(boost::asio::io_service &io_service) : _ioservice(io_service), _strand(std::make_shared(_ioservice)), - _timer(std::make_shared(_ioservice,_strand)) + _timer(std::make_shared(_ioservice,_strand)), + _timer_interval(0) + { + + } + + /** + * Constructor + * @param io_service The boost io_service to wrap + * @param interval The interval to use when negotiating heartbeat with rabbit server. + */ + explicit LibBoostAsioHandler(boost::asio::io_service &io_service, uint16_t interval) : + _ioservice(io_service), + _strand(std::make_shared(_ioservice)), + _timer(std::make_shared(_ioservice,_strand)), + _timer_interval(interval) { } @@ -584,4 +609,3 @@ public: * End of namespace */ } - From ae3b94fe92b3e5b39b29ff57b33cc9ee7d3b5803 Mon Sep 17 00:00:00 2001 From: Steven Geddis Date: Thu, 1 Feb 2018 13:05:41 +0100 Subject: [PATCH 2/5] use custom heartbeat interval outright, if set. --- include/amqpcpp/libboostasio.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/amqpcpp/libboostasio.h b/include/amqpcpp/libboostasio.h index 2ba7918..fcc0705 100644 --- a/include/amqpcpp/libboostasio.h +++ b/include/amqpcpp/libboostasio.h @@ -536,7 +536,7 @@ protected: if (interval == 0) return 0; // use the most frequent heartbeat interval (user-specified or rabbit server default). - interval = (_timer_interval > 0 && _timer_interval < interval) ? _timer_interval : interval; + interval = (_timer_interval > 0) ? _timer_interval : interval; // set the timer _timer->set(connection, interval); From 33c8e76a7ea73a5595002e74ad4be80ccd3f5ba8 Mon Sep 17 00:00:00 2001 From: Steven Geddis Date: Thu, 1 Feb 2018 13:11:23 +0100 Subject: [PATCH 3/5] correct comment --- include/amqpcpp/libboostasio.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/amqpcpp/libboostasio.h b/include/amqpcpp/libboostasio.h index fcc0705..56f73d4 100644 --- a/include/amqpcpp/libboostasio.h +++ b/include/amqpcpp/libboostasio.h @@ -535,7 +535,7 @@ protected: // skip if no heartbeats are needed if (interval == 0) return 0; - // use the most frequent heartbeat interval (user-specified or rabbit server default). + // choose heartbeat interval to use (user-specified or rabbit server default). interval = (_timer_interval > 0) ? _timer_interval : interval; // set the timer From 5a853134aae2b26cc36618c641bcf655e1cb3448 Mon Sep 17 00:00:00 2001 From: Steven Geddis Date: Thu, 1 Feb 2018 15:27:59 +0100 Subject: [PATCH 4/5] remove second ctor with heartbeat interval (extend class instead). Change private to protected to allow LibBoostAsioHandler to be extended. --- include/amqpcpp/libboostasio.h | 25 +------------------------ 1 file changed, 1 insertion(+), 24 deletions(-) diff --git a/include/amqpcpp/libboostasio.h b/include/amqpcpp/libboostasio.h index 56f73d4..9c09645 100644 --- a/include/amqpcpp/libboostasio.h +++ b/include/amqpcpp/libboostasio.h @@ -47,7 +47,7 @@ namespace AMQP { */ class LibBoostAsioHandler : public virtual TcpHandler { -private: +protected: /** * Helper class that wraps a boost io_service socket monitor. @@ -477,12 +477,6 @@ private: */ std::shared_ptr _timer; - /** - * The heartbeat timer interval (in seconds). - * @var uint16_t - */ - uint16_t _timer_interval; - /** * Method that is called by AMQP-CPP to register a filedescriptor for readability or writability * @param connection The TCP connection object that is reporting @@ -535,9 +529,6 @@ protected: // skip if no heartbeats are needed if (interval == 0) return 0; - // choose heartbeat interval to use (user-specified or rabbit server default). - interval = (_timer_interval > 0) ? _timer_interval : interval; - // set the timer _timer->set(connection, interval); @@ -567,20 +558,6 @@ public: } - /** - * Constructor - * @param io_service The boost io_service to wrap - * @param interval The interval to use when negotiating heartbeat with rabbit server. - */ - explicit LibBoostAsioHandler(boost::asio::io_service &io_service, uint16_t interval) : - _ioservice(io_service), - _strand(std::make_shared(_ioservice)), - _timer(std::make_shared(_ioservice,_strand)), - _timer_interval(interval) - { - - } - /** * Handler cannot be copied or moved * From accc1810a07e9871cc1c3b299ab5e14b9e40a216 Mon Sep 17 00:00:00 2001 From: Steven Geddis Date: Thu, 1 Feb 2018 15:31:19 +0100 Subject: [PATCH 5/5] remove timer interval from ctor init list --- include/amqpcpp/libboostasio.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/amqpcpp/libboostasio.h b/include/amqpcpp/libboostasio.h index 9c09645..c6fb532 100644 --- a/include/amqpcpp/libboostasio.h +++ b/include/amqpcpp/libboostasio.h @@ -552,8 +552,7 @@ public: explicit LibBoostAsioHandler(boost::asio::io_service &io_service) : _ioservice(io_service), _strand(std::make_shared(_ioservice)), - _timer(std::make_shared(_ioservice,_strand)), - _timer_interval(0) + _timer(std::make_shared(_ioservice,_strand)) { }