diff --git a/include/deferred.h b/include/deferred.h index e2fb2d0..df4f9ed 100644 --- a/include/deferred.h +++ b/include/deferred.h @@ -27,17 +27,17 @@ private: /** * Callback to execute on success */ - std::function _successCallback; + std::function _successCallback; /** * Callback to execute on failure */ - std::function _errorCallback; + std::function _errorCallback; /** * Callback to execute either way */ - std::function _finalizeCallback; + std::function _finalizeCallback; /** * Indicate success @@ -47,8 +47,8 @@ private: void success(Arguments ...parameters) const { // execute callbacks if registered - if (_successCallback) _successCallback(_channel, parameters...); - if (_finalizeCallback) _finalizeCallback(_channel, ""); + if (_successCallback) _successCallback(parameters...); + if (_finalizeCallback) _finalizeCallback(""); } /** @@ -59,8 +59,8 @@ private: void error(const std::string& error) const { // execute callbacks if registered - if (_errorCallback) _errorCallback(_channel, error); - if (_finalizeCallback) _finalizeCallback(_channel, error); + if (_errorCallback) _errorCallback(error); + if (_finalizeCallback) _finalizeCallback(error); } /** @@ -69,14 +69,11 @@ private: */ friend class ChannelImpl; friend class Callbacks; + protected: - /** - * The channel we operate under - */ - Channel *_channel; - /** * Do we already know we failed? + * @var bool */ bool _failed; @@ -84,13 +81,10 @@ protected: * Protected constructor that can only be called * from within the channel implementation * - * @param channel the channel we operate under - * @param boolea are we already failed? + * @param failed are we already failed? */ - Deferred(Channel *channel, bool failed = false) : - _channel(channel), - _failed(failed) - {} + Deferred(bool failed = false) : _failed(failed) {} + public: /** * Deleted copy constructor @@ -125,7 +119,7 @@ public: * * @param callback the callback to execute */ - Deferred& onSuccess(const std::function& callback) + Deferred& onSuccess(const std::function& callback) { // store callback _successCallback = callback; @@ -142,7 +136,7 @@ public: * * @param callback the callback to execute */ - Deferred& onError(const std::function& callback) + Deferred& onError(const std::function& callback) { // store callback _errorCallback = callback; @@ -164,7 +158,7 @@ public: * * @param callback the callback to execute */ - Deferred& onFinalize(const std::function& callback) + Deferred& onFinalize(const std::function& callback) { // store callback _finalizeCallback = callback; diff --git a/include/deferredconsumer.h b/include/deferredconsumer.h index 3610bd7..8de39bd 100644 --- a/include/deferredconsumer.h +++ b/include/deferredconsumer.h @@ -20,7 +20,7 @@ private: /** * Callback to execute when a message arrives */ - std::function _messageCallback; + std::function _messageCallback; /** * Process a message @@ -33,7 +33,7 @@ private: void message(const Message &message, uint64_t deliveryTag, const std::string &consumerTag, bool redelivered) const { // do we have a valid callback - if (_messageCallback) _messageCallback(_channel, message, deliveryTag, consumerTag, redelivered); + if (_messageCallback) _messageCallback(message, deliveryTag, consumerTag, redelivered); } /** @@ -42,6 +42,7 @@ private: */ friend class ChannelImpl; friend class ConsumedMessage; + protected: /** * Protected constructor that can only be called @@ -50,9 +51,8 @@ protected: * @param channel the channel we operate under * @param boolea are we already failed? */ - DeferredConsumer(Channel *channel, bool failed = false) : - Deferred(channel, failed) - {} + DeferredConsumer(bool failed = false) : Deferred(failed) {} + public: /** * Register a function to be called when a message arrives @@ -62,7 +62,7 @@ public: * * @param callback the callback to execute */ - DeferredConsumer& onReceived(const std::function& callback) + DeferredConsumer& onReceived(const std::function& callback) { // store callback _messageCallback = callback; diff --git a/src/Makefile b/src/Makefile index 3a9b4e9..4f0baa2 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,6 +1,6 @@ CPP = g++ RM = rm -f -CPPFLAGS = -Wall -c -I. -O2 -flto -std=c++11 -g +CPPFLAGS = -Wall -c -I. -g -flto -std=c++11 -g LD = g++ LD_FLAGS = -Wall -shared -O2 SHARED_LIB = libamqpcpp.so diff --git a/src/channelimpl.cpp b/src/channelimpl.cpp index 4609d0d..a478dff 100644 --- a/src/channelimpl.cpp +++ b/src/channelimpl.cpp @@ -439,7 +439,7 @@ Deferred<>& ChannelImpl::setQos(uint16_t prefetchCount) DeferredConsumer& ChannelImpl::consume(const std::string &queue, const std::string &tag, int flags, const Table &arguments) { // create the deferred consumer - _consumer = std::unique_ptr(new DeferredConsumer(_parent, false)); + _consumer = std::unique_ptr(new DeferredConsumer(false)); // can we send the basic consume frame? if (!send(BasicConsumeFrame(_id, queue, tag, flags & nolocal, flags & noack, flags & exclusive, flags & nowait, arguments))) @@ -542,7 +542,7 @@ Deferred& ChannelImpl::send(const Frame &frame, const char *messag // create a new deferred handler and get a pointer to it // note: cannot use auto here or the lambda below chokes // when compiling under gcc 4.8 - Deferred *handler = &_callbacks.push_back(Deferred(_parent)); + Deferred *handler = &_callbacks.push_back(Deferred()); // send the frame over the channel if (!send(frame)) @@ -554,6 +554,7 @@ Deferred& ChannelImpl::send(const Frame &frame, const char *messag // after a timeout, so it gets called only // after a possible handler was installed. _connection->_handler->setTimeout(_connection->_parent, 0, [handler, message]() { + // emit an error on the handler handler->error(message); });