diff --git a/include/channelimpl.h b/include/channelimpl.h index 7ddf1e3..1ba6130 100644 --- a/include/channelimpl.h +++ b/include/channelimpl.h @@ -508,7 +508,7 @@ public: * @return bool */ bool reject(uint64_t deliveryTag, int flags); - + /** * Recover messages that were not yet ack'ed * @param flags optional flags diff --git a/include/connection.h b/include/connection.h index 18215f4..e7e1021 100644 --- a/include/connection.h +++ b/include/connection.h @@ -94,6 +94,15 @@ public: return _implementation.vhost(); } + /** + * Send a ping/heartbeat to the channel to keep it alive + * @return bool + */ + bool ping() + { + return _implementation.ping(); + } + /** * Parse data that was recevied from RabbitMQ * @@ -195,15 +204,6 @@ public: return _implementation.waiting(); } - /** - * Retrieve the heartbeat delay used by this connection - * @return uint16_t - */ - uint16_t heartbeat() const - { - return _implementation.heartbeat(); - } - /** * Some classes have access to private properties */ diff --git a/include/connectionhandler.h b/include/connectionhandler.h index e46fa8d..d2c4786 100644 --- a/include/connectionhandler.h +++ b/include/connectionhandler.h @@ -51,6 +51,10 @@ public: * alternative heartbeat interval, you can override this method * to use an other interval. You should return 0 if you want to * disable heartbeats. + * + * If heartbeats are enabled, you yourself are responsible to send + * out a heartbeat every *interval* number of seconds by calling + * the Connection::heartbeat() method. * * @param connection The connection that suggested a heartbeat interval * @param interval The suggested interval from the server diff --git a/include/connectionimpl.h b/include/connectionimpl.h index aaa8efa..b01486f 100644 --- a/include/connectionimpl.h +++ b/include/connectionimpl.h @@ -124,12 +124,6 @@ protected: */ std::queue _queue; - /** - * Heartbeat delay - * @var uint16_t - */ - uint16_t _heartbeat = 0; - /** * Helper method to send the close frame * Return value tells if the connection is still valid @@ -413,15 +407,6 @@ public: return _channels.size(); } - /** - * Heartbeat delay - * @return uint16_t - */ - uint16_t heartbeat() const - { - return _heartbeat; - } - /** * Set the heartbeat delay * @param heartbeat suggested heartbeat by server @@ -430,7 +415,7 @@ public: uint16_t setHeartbeat(uint16_t heartbeat) { // pass to the handler - return _heartbeat = _handler->onNegotiate(_parent, heartbeat); + return _handler->onNegotiate(_parent, heartbeat); } /** @@ -441,6 +426,12 @@ public: // pass to handler _handler->onHeartbeat(_parent); } + + /** + * Send a heartbeat to keep the connection alive + * @return bool + */ + bool heartbeat(); /** * The actual connection is a friend and can construct this class diff --git a/include/tcphandler.h b/include/tcphandler.h index 7c86a4a..5549425 100644 --- a/include/tcphandler.h +++ b/include/tcphandler.h @@ -33,7 +33,7 @@ public: /** * Destructor */ - virtual ~TcpHandler() {} + virtual ~TcpHandler() = default; /** * Method that is called when the heartbeat frequency is negotiated @@ -96,8 +96,6 @@ public: * @param flags Should the object be monitored for readability or writability? */ virtual void monitor(TcpConnection *connection, int fd, int flags) = 0; - - }; /** diff --git a/src/channelimpl.cpp b/src/channelimpl.cpp index 1ddf54f..73471ab 100644 --- a/src/channelimpl.cpp +++ b/src/channelimpl.cpp @@ -39,7 +39,6 @@ #include "basicrejectframe.h" #include "basicgetframe.h" - /** * Set up namespace */ diff --git a/src/connectionimpl.cpp b/src/connectionimpl.cpp index ca81a1f..4be9464 100644 --- a/src/connectionimpl.cpp +++ b/src/connectionimpl.cpp @@ -3,7 +3,7 @@ * * Implementation of an AMQP connection * - * @copyright 2014 - 2016 Copernica BV + * @copyright 2014 - 2017 Copernica BV */ #include "includes.h" #include "protocolheaderframe.h" @@ -11,6 +11,7 @@ #include "connectioncloseframe.h" #include "reducedbuffer.h" #include "passthroughbuffer.h" +#include "heartbeatframe.h" /** * set namespace @@ -374,6 +375,16 @@ bool ConnectionImpl::send(CopiedBuffer &&buffer) return true; } +/** + * Send a ping / heartbeat frame to keep the connection alive + * @return bool + */ +bool ConnectionImpl::heartbeat() +{ + // send a frame + return send(HeartbeatFrame()); +} + /** * End of namspace */ diff --git a/src/heartbeatframe.h b/src/heartbeatframe.h index 955c57d..cd4320b 100644 --- a/src/heartbeatframe.h +++ b/src/heartbeatframe.h @@ -59,9 +59,6 @@ public: { // notify the connection-handler connection->reportHeartbeat(); - - // send back the same frame - connection->send(*this); // done return true;