diff --git a/include/channel.h b/include/channel.h index 94a7b99..39a2229 100644 --- a/include/channel.h +++ b/include/channel.h @@ -52,8 +52,7 @@ public: */ void onReady(const SuccessCallback &callback) { - // store callback in implementation - _implementation->_readyCallback = callback; + _implementation->onReady(callback); } /** @@ -66,8 +65,7 @@ public: */ void onError(const ErrorCallback &callback) { - // store callback in implementation - _implementation->_errorCallback = callback; + _implementation->onError(callback); } /** diff --git a/include/channelimpl.h b/include/channelimpl.h index f03930d..b9c51db 100644 --- a/include/channelimpl.h +++ b/include/channelimpl.h @@ -144,9 +144,40 @@ public: */ void detach() { + // connection is gone _connection = nullptr; } + /** + * Callback that is called when the channel was succesfully created. + * @param callback the callback to execute + */ + void onReady(const SuccessCallback &callback) + { + // store callback + _readyCallback = callback; + + // direct call if channel is already ready + if (_state == state_connected) callback(); + } + + /** + * Callback that is called when an error occurs. + * + * Only one error callback can be registered. Calling this function + * multiple times will remove the old callback. + * + * @param callback the callback to execute + */ + void onError(const ErrorCallback &callback) + { + // store callback + _errorCallback = callback; + + // direct call if channel is already in error state + if (_state != state_connected) callback("Channel is in error state"); + } + /** * Pause deliveries on a channel * @@ -480,7 +511,7 @@ public: */ bool waiting() const { - return _synchronous; + return _synchronous || !_queue.empty(); } /** diff --git a/src/channelimpl.cpp b/src/channelimpl.cpp index 5fa756f..4b49942 100644 --- a/src/channelimpl.cpp +++ b/src/channelimpl.cpp @@ -80,13 +80,16 @@ void ChannelImpl::attach(Connection *connection) // this is invalid _state = state_closed; } - else + else { - // busy connecting + // assume channel is connected _state = state_connected; - - // valid id, send a channel open frame - send(ChannelOpenFrame(_id)); + + // send the open frame + if (send(ChannelOpenFrame(_id))) return; + + // report an error + reportError("Channel could not be initialized", true); } } @@ -663,11 +666,14 @@ bool ChannelImpl::send(const Frame &frame) return true; } - // enter synchronous mode if necessary - _synchronous = frame.synchronous(); - // send to tcp connection - return _connection->send(frame); + if (!_connection->send(frame)) return false; + + // frame was sent, if this was a synchronous frame, we now have to wait + _synchronous = frame.synchronous(); + + // done + return true; } /**