Removed the nowait option from the public interface, because the deferred would never be called and implemented a queue to wait for synchronous methods to complete before sending the next frame

This commit is contained in:
Martijn Otto 2014-04-29 15:51:33 +02:00
parent e0b709fa63
commit a9570277b7
43 changed files with 524 additions and 237 deletions

View File

@ -174,42 +174,30 @@ public:
/** /**
* Bind two exchanges to each other * Bind two exchanges to each other
* *
* The following flags can be used for the exchange
*
* - nowait do not wait on response
*
* @param source the source exchange * @param source the source exchange
* @param target the target exchange * @param target the target exchange
* @param routingkey the routing key * @param routingkey the routing key
* @param flags optional flags
* @param arguments additional bind arguments * @param arguments additional bind arguments
* *
* This function returns a deferred handler. Callbacks can be installed * This function returns a deferred handler. Callbacks can be installed
* using onSuccess(), onError() and onFinalize() methods. * using onSuccess(), onError() and onFinalize() methods.
*/ */
Deferred &bindExchange(const std::string &source, const std::string &target, const std::string &routingkey, int flags, const Table &arguments) { return _implementation.bindExchange(source, target, routingkey, flags, arguments); } Deferred &bindExchange(const std::string &source, const std::string &target, const std::string &routingkey, const Table &arguments) { return _implementation.bindExchange(source, target, routingkey, arguments); }
Deferred &bindExchange(const std::string &source, const std::string &target, const std::string &routingkey, const Table &arguments) { return _implementation.bindExchange(source, target, routingkey, 0, arguments); } Deferred &bindExchange(const std::string &source, const std::string &target, const std::string &routingkey) { return _implementation.bindExchange(source, target, routingkey, Table()); }
Deferred &bindExchange(const std::string &source, const std::string &target, const std::string &routingkey, int flags = 0) { return _implementation.bindExchange(source, target, routingkey, flags, Table()); }
/** /**
* Unbind two exchanges from one another * Unbind two exchanges from one another
* *
* The following flags can be used for the exchange
*
* - nowait do not wait on response
*
* @param target the target exchange * @param target the target exchange
* @param source the source exchange * @param source the source exchange
* @param routingkey the routing key * @param routingkey the routing key
* @param flags optional flags
* @param arguments additional unbind arguments * @param arguments additional unbind arguments
* *
* This function returns a deferred handler. Callbacks can be installed * This function returns a deferred handler. Callbacks can be installed
* using onSuccess(), onError() and onFinalize() methods. * using onSuccess(), onError() and onFinalize() methods.
*/ */
Deferred &unbindExchange(const std::string &target, const std::string &source, const std::string &routingkey, int flags, const Table &arguments) { return _implementation.unbindExchange(target, source, routingkey, flags, arguments); } Deferred &unbindExchange(const std::string &target, const std::string &source, const std::string &routingkey, const Table &arguments) { return _implementation.unbindExchange(target, source, routingkey, arguments); }
Deferred &unbindExchange(const std::string &target, const std::string &source, const std::string &routingkey, const Table &arguments) { return _implementation.unbindExchange(target, source, routingkey, 0, arguments); } Deferred &unbindExchange(const std::string &target, const std::string &source, const std::string &routingkey) { return _implementation.unbindExchange(target, source, routingkey, Table()); }
Deferred &unbindExchange(const std::string &target, const std::string &source, const std::string &routingkey, int flags = 0) { return _implementation.unbindExchange(target, source, routingkey, flags, Table()); }
/** /**
* Declare a queue * Declare a queue
@ -250,22 +238,16 @@ public:
/** /**
* Bind a queue to an exchange * Bind a queue to an exchange
* *
* The following flags can be used for the exchange
*
* - nowait do not wait on response
*
* @param exchange the source exchange * @param exchange the source exchange
* @param queue the target queue * @param queue the target queue
* @param routingkey the routing key * @param routingkey the routing key
* @param flags additional flags
* @param arguments additional bind arguments * @param arguments additional bind arguments
* *
* This function returns a deferred handler. Callbacks can be installed * This function returns a deferred handler. Callbacks can be installed
* using onSuccess(), onError() and onFinalize() methods. * using onSuccess(), onError() and onFinalize() methods.
*/ */
Deferred &bindQueue(const std::string &exchange, const std::string &queue, const std::string &routingkey, int flags, const Table &arguments) { return _implementation.bindQueue(exchange, queue, routingkey, flags, arguments); } Deferred &bindQueue(const std::string &exchange, const std::string &queue, const std::string &routingkey, const Table &arguments) { return _implementation.bindQueue(exchange, queue, routingkey, arguments); }
Deferred &bindQueue(const std::string &exchange, const std::string &queue, const std::string &routingkey, const Table &arguments) { return _implementation.bindQueue(exchange, queue, routingkey, 0, arguments); } Deferred &bindQueue(const std::string &exchange, const std::string &queue, const std::string &routingkey) { return _implementation.bindQueue(exchange, queue, routingkey, Table()); }
Deferred &bindQueue(const std::string &exchange, const std::string &queue, const std::string &routingkey, int flags = 0) { return _implementation.bindQueue(exchange, queue, routingkey, flags, Table()); }
/** /**
* Unbind a queue from an exchange * Unbind a queue from an exchange
@ -283,12 +265,7 @@ public:
/** /**
* Purge a queue * Purge a queue
* *
* The following flags can be used for the exchange
*
* - nowait do not wait on response
*
* @param name name of the queue * @param name name of the queue
* @param flags additional flags
* *
* This function returns a deferred handler. Callbacks can be installed * This function returns a deferred handler. Callbacks can be installed
* using onSuccess(), onError() and onFinalize() methods. * using onSuccess(), onError() and onFinalize() methods.
@ -303,7 +280,7 @@ public:
* *
* }); * });
*/ */
DeferredDelete &purgeQueue(const std::string &name, int flags = 0){ return _implementation.purgeQueue(name, flags); } DeferredDelete &purgeQueue(const std::string &name){ return _implementation.purgeQueue(name); }
/** /**
* Remove a queue * Remove a queue
@ -375,11 +352,6 @@ public:
* - nolocal if set, messages published on this channel are not also consumed * - nolocal if set, messages published on this channel are not also consumed
* - noack if set, consumed messages do not have to be acked, this happens automatically * - noack if set, consumed messages do not have to be acked, this happens automatically
* - exclusive request exclusive access, only this consumer can access the queue * - exclusive request exclusive access, only this consumer can access the queue
* - nowait the server does not have to send a response back that consuming is active
*
* The method Deferred::onSuccess() will be called when the
* consumer has started (unless the nowait option was set, in which case
* no confirmation method is called)
* *
* @param queue the queue from which you want to consume * @param queue the queue from which you want to consume
* @param tag a consumer tag that will be associated with this consume operation * @param tag a consumer tag that will be associated with this consume operation
@ -411,16 +383,7 @@ public:
* *
* If you want to stop a running consumer, you can use this method with the consumer tag * If you want to stop a running consumer, you can use this method with the consumer tag
* *
* The following flags are supported:
*
* - nowait the server does not have to send a response back that the consumer has been cancelled
*
* The method Deferred::onSuccess() will be called when the consumer
* was succesfully stopped (unless the nowait option was used, in which case no
* confirmation method is called)
*
* @param tag the consumer tag * @param tag the consumer tag
* @param flags optional additional flags
* *
* This function returns a deferred handler. Callbacks can be installed * This function returns a deferred handler. Callbacks can be installed
* using onSuccess(), onError() and onFinalize() methods. * using onSuccess(), onError() and onFinalize() methods.
@ -435,7 +398,7 @@ public:
* *
* }); * });
*/ */
DeferredCancel &cancel(const std::string &tag, int flags = 0) { return _implementation.cancel(tag, flags); } DeferredCancel &cancel(const std::string &tag) { return _implementation.cancel(tag); }
/** /**
* Acknoldge a received message * Acknoldge a received message

View File

@ -86,6 +86,19 @@ private:
state_closed state_closed
} _state = state_connected; } _state = state_connected;
/**
* The frames that still need to be send out
*
* We store the data as well as whether they
* should be handled synchronously.
*/
std::queue<std::pair<bool, OutBuffer>> _queue;
/**
* Are we currently operating in synchronous mode?
*/
bool _synchronous = false;
/** /**
* The message that is now being received * The message that is now being received
* @var ConsumedMessage * @var ConsumedMessage
@ -203,13 +216,12 @@ public:
* @param source exchange which binds to target * @param source exchange which binds to target
* @param target exchange to bind to * @param target exchange to bind to
* @param routingKey routing key * @param routingKey routing key
* @param glags additional flags
* @param arguments additional arguments for binding * @param arguments additional arguments for binding
* *
* This function returns a deferred handler. Callbacks can be installed * This function returns a deferred handler. Callbacks can be installed
* using onSuccess(), onError() and onFinalize() methods. * using onSuccess(), onError() and onFinalize() methods.
*/ */
Deferred &bindExchange(const std::string &source, const std::string &target, const std::string &routingkey, int flags, const Table &arguments); Deferred &bindExchange(const std::string &source, const std::string &target, const std::string &routingkey, const Table &arguments);
/** /**
* unbind two exchanges * unbind two exchanges
@ -217,13 +229,12 @@ public:
* @param source the source exchange * @param source the source exchange
* @param target the target exchange * @param target the target exchange
* @param routingkey the routing key * @param routingkey the routing key
* @param flags optional flags
* @param arguments additional unbind arguments * @param arguments additional unbind arguments
* *
* This function returns a deferred handler. Callbacks can be installed * This function returns a deferred handler. Callbacks can be installed
* using onSuccess(), onError() and onFinalize() methods. * using onSuccess(), onError() and onFinalize() methods.
*/ */
Deferred &unbindExchange(const std::string &source, const std::string &target, const std::string &routingkey, int flags, const Table &arguments); Deferred &unbindExchange(const std::string &source, const std::string &target, const std::string &routingkey, const Table &arguments);
/** /**
* remove an exchange * remove an exchange
@ -253,13 +264,12 @@ public:
* @param exchangeName name of the exchange to bind to * @param exchangeName name of the exchange to bind to
* @param queueName name of the queue * @param queueName name of the queue
* @param routingkey routingkey * @param routingkey routingkey
* @param flags additional flags
* @param arguments additional arguments * @param arguments additional arguments
* *
* This function returns a deferred handler. Callbacks can be installed * This function returns a deferred handler. Callbacks can be installed
* using onSuccess(), onError() and onFinalize() methods. * using onSuccess(), onError() and onFinalize() methods.
*/ */
Deferred &bindQueue(const std::string &exchangeName, const std::string &queueName, const std::string &routingkey, int flags, const Table &arguments); Deferred &bindQueue(const std::string &exchangeName, const std::string &queueName, const std::string &routingkey, const Table &arguments);
/** /**
* Unbind a queue from an exchange * Unbind a queue from an exchange
@ -277,7 +287,6 @@ public:
/** /**
* Purge a queue * Purge a queue
* @param queue queue to purge * @param queue queue to purge
* @param flags additional flags
* *
* This function returns a deferred handler. Callbacks can be installed * This function returns a deferred handler. Callbacks can be installed
* using onSuccess(), onError() and onFinalize() methods. * using onSuccess(), onError() and onFinalize() methods.
@ -292,7 +301,7 @@ public:
* *
* }); * });
*/ */
DeferredDelete &purgeQueue(const std::string &name, int flags); DeferredDelete &purgeQueue(const std::string &name);
/** /**
* Remove a queue * Remove a queue
@ -363,7 +372,6 @@ public:
/** /**
* Cancel a running consumer * Cancel a running consumer
* @param tag the consumer tag * @param tag the consumer tag
* @param flags optional flags
* *
* This function returns a deferred handler. Callbacks can be installed * This function returns a deferred handler. Callbacks can be installed
* using onSuccess(), onError() and onFinalize() methods. * using onSuccess(), onError() and onFinalize() methods.
@ -378,7 +386,7 @@ public:
* *
* }); * });
*/ */
DeferredCancel &cancel(const std::string &tag, int flags); DeferredCancel &cancel(const std::string &tag);
/** /**
* Acknowledge a message * Acknowledge a message
@ -429,39 +437,61 @@ public:
*/ */
bool send(const Frame &frame); bool send(const Frame &frame);
/**
* Signal the channel that a synchronous operation
* was completed. After this operation, waiting
* frames can be sent out.
*/
void synchronized();
/** /**
* Report to the handler that the channel is opened * Report to the handler that the channel is opened
*/ */
void reportReady() void reportReady()
{ {
// callbacks could destroy us, so monitor it
Monitor monitor(this);
// inform handler // inform handler
if (_readyCallback) _readyCallback(); if (_readyCallback) _readyCallback();
// if the monitor is still valid, we exit synchronous mode now
if (monitor.valid()) synchronized();
} }
/** /**
* Report to the handler that the channel is closed * Report to the handler that the channel is closed
*
* Returns whether the channel object is still valid
*/ */
void reportClosed() bool reportClosed()
{ {
// change state // change state
_state = state_closed; _state = state_closed;
// and pass on to the reportSuccess() method which will call the // and pass on to the reportSuccess() method which will call the
// appropriate deferred object to report the successful operation // appropriate deferred object to report the successful operation
reportSuccess(); return reportSuccess();
// technically, we should exit synchronous method now
// since the synchronous channel close frame has been
// acknowledged by the server.
//
// but since the channel was just closed, there is no
// real point in doing this, as we cannot send frames
// out anymore.
} }
/** /**
* Report success * Report success
* *
* This function is called to report success for all * Returns whether the channel object is still valid
* cases where the callback does not receive any parameters
*/ */
template <typename... Arguments> template <typename... Arguments>
void reportSuccess(Arguments ...parameters) bool reportSuccess(Arguments ...parameters)
{ {
// skip if there is no oldest callback // skip if there is no oldest callback
if (!_oldestCallback) return; if (!_oldestCallback) return true;
// we are going to call callbacks that could destruct the channel // we are going to call callbacks that could destruct the channel
Monitor monitor(this); Monitor monitor(this);
@ -470,13 +500,16 @@ public:
auto *next = _oldestCallback->reportSuccess(std::forward<Arguments>(parameters)...); auto *next = _oldestCallback->reportSuccess(std::forward<Arguments>(parameters)...);
// leap out if channel no longer exists // leap out if channel no longer exists
if (!monitor.valid()) return; if (!monitor.valid()) return false;
// set the oldest callback // set the oldest callback
_oldestCallback.reset(next); _oldestCallback.reset(next);
// if there was no next callback, the newest callback was just used // if there was no next callback, the newest callback was just used
if (!next) _newestCallback = nullptr; if (!next) _newestCallback = nullptr;
// we are still valid
return true;
} }
/** /**

View File

@ -253,6 +253,13 @@ public:
*/ */
bool send(const Frame &frame); bool send(const Frame &frame);
/**
* Send buffered data over the connection
*
* @param buffer the buffer with data to send
*/
bool send(OutBuffer &&buffer);
/** /**
* Get a channel by its identifier * Get a channel by its identifier
* *

View File

@ -26,7 +26,6 @@ extern const int global;
extern const int nolocal; extern const int nolocal;
extern const int noack; extern const int noack;
extern const int exclusive; extern const int exclusive;
extern const int nowait;
extern const int mandatory; extern const int mandatory;
extern const int immediate; extern const int immediate;
extern const int redelivered; extern const int redelivered;

View File

@ -104,7 +104,7 @@ public:
* Get access to the internal buffer * Get access to the internal buffer
* @return const char* * @return const char*
*/ */
const char *data() const char *data() const
{ {
return _buffer; return _buffer;
} }
@ -113,7 +113,7 @@ public:
* Current size of the output buffer * Current size of the output buffer
* @return size_t * @return size_t
*/ */
size_t size() size_t size() const
{ {
return _size; return _size;
} }

View File

@ -73,6 +73,17 @@ public:
*/ */
virtual ~BasicAckFrame() {} virtual ~BasicAckFrame() {}
/**
* Is this a synchronous frame?
*
* After a synchronous frame no more frames may be
* sent until the accompanying -ok frame arrives
*/
virtual bool synchronous() const override
{
return false;
}
/** /**
* Return the method ID * Return the method ID
* @return uint16_t * @return uint16_t

View File

@ -68,6 +68,18 @@ public:
*/ */
virtual ~BasicCancelFrame() {} virtual ~BasicCancelFrame() {}
/**
* Is this a synchronous frame?
*
* After a synchronous frame no more frames may be
* sent until the accompanying -ok frame arrives
*/
bool synchronous() const override
{
// we are synchronous when the nowait option is not used
return !noWait();
}
/** /**
* Return the consumertag, which is specified by the client or provided by the server * Return the consumertag, which is specified by the client or provided by the server
* @return string * @return string
@ -90,7 +102,7 @@ public:
* Return whether to wait for a response * Return whether to wait for a response
* @return boolean * @return boolean
*/ */
const bool noWait() const bool noWait() const
{ {
return _noWait.get(0); return _noWait.get(0);
} }

View File

@ -94,7 +94,7 @@ public:
if (!channel) return false; if (!channel) return false;
// report // report
channel->reportSuccess<const std::string&>(consumerTag()); if (channel->reportSuccess<const std::string&>(consumerTag())) channel->synchronized();
// done // done
return true; return true;

View File

@ -111,6 +111,18 @@ public:
*/ */
virtual ~BasicConsumeFrame() {} virtual ~BasicConsumeFrame() {}
/**
* Is this a synchronous frame?
*
* After a synchronous frame no more frames may be
* sent until the accompanying -ok frame arrives
*/
bool synchronous() const override
{
// we are synchronous when the nowait option is not set
return !noWait();
}
/** /**
* Return the method ID * Return the method ID
* @return uint16_t * @return uint16_t

View File

@ -94,7 +94,7 @@ public:
if (!channel) return false; if (!channel) return false;
// report // report
channel->reportSuccess(consumerTag()); if (channel->reportSuccess(consumerTag())) channel->synchronized();
// done // done
return true; return true;

View File

@ -120,6 +120,17 @@ public:
return _routingKey; return _routingKey;
} }
/**
* Is this a synchronous frame?
*
* After a synchronous frame no more frames may be
* sent until the accompanying -ok frame arrives
*/
virtual bool synchronous() const override
{
return false;
}
/** /**
* Return the method ID * Return the method ID
* @return uint16_t * @return uint16_t

View File

@ -94,6 +94,17 @@ public:
*/ */
virtual ~BasicPublishFrame() {} virtual ~BasicPublishFrame() {}
/**
* Is this a synchronous frame?
*
* After a synchronous frame no more frames may be
* sent until the accompanying -ok frame arrives
*/
bool synchronous() const override
{
return false;
}
/** /**
* Return the name of the exchange to publish to * Return the name of the exchange to publish to
* @return string * @return string

View File

@ -67,7 +67,7 @@ public:
if (!channel) return false; if (!channel) return false;
// report // report
channel->reportSuccess(); if (channel->reportSuccess()) channel->synchronized();
// done // done
return true; return true;

View File

@ -62,6 +62,17 @@ public:
*/ */
virtual ~BasicRecoverAsyncFrame() {} virtual ~BasicRecoverAsyncFrame() {}
/**
* Is this a synchronous frame?
*
* After a synchronous frame no more frames may be
* sent until the accompanying -ok frame arrives
*/
virtual bool synchronous() const override
{
return false;
}
/** /**
* Return the method ID * Return the method ID
* @return uint16_t * @return uint16_t

View File

@ -62,6 +62,17 @@ public:
*/ */
virtual ~BasicRecoverFrame() {} virtual ~BasicRecoverFrame() {}
/**
* Is this a synchronous frame?
*
* After a synchronous frame no more frames may be
* sent until the accompanying -ok frame arrives
*/
bool synchronous() const override
{
return false;
}
/** /**
* Return the method ID * Return the method ID
* @return uint16_t * @return uint16_t

View File

@ -73,6 +73,17 @@ public:
*/ */
virtual ~BasicRejectFrame() {} virtual ~BasicRejectFrame() {}
/**
* Is this a synchronous frame?
*
* After a synchronous frame no more frames may be
* sent until the accompanying -ok frame arrives
*/
bool synchronous() const override
{
return false;
}
/** /**
* Return the method ID * Return the method ID
* @return uint16_t * @return uint16_t

View File

@ -92,6 +92,17 @@ public:
*/ */
virtual ~BasicReturnFrame() {} virtual ~BasicReturnFrame() {}
/**
* Is this a synchronous frame?
*
* After a synchronous frame no more frames may be
* sent until the accompanying -ok frame arrives
*/
virtual bool synchronous() const override
{
return false;
}
/** /**
* Return the name of the exchange to publish to * Return the name of the exchange to publish to
* @return string * @return string

View File

@ -72,7 +72,7 @@ public:
if (!channel) return false; if (!channel) return false;
// report that the channel is closed // report that the channel is closed
channel->reportClosed(); if (channel->reportClosed()) channel->synchronized();
// done // done
return true; return true;

View File

@ -93,7 +93,7 @@ public:
if (!channel) return false; if (!channel) return false;
// report success for the call // report success for the call
channel->reportSuccess(); if (channel->reportSuccess()) channel->synchronized();
// done // done
return true; return true;

View File

@ -222,7 +222,7 @@ Deferred &ChannelImpl::declareExchange(const std::string &name, ExchangeType typ
if (type == ExchangeType::headers)exchangeType = "headers"; if (type == ExchangeType::headers)exchangeType = "headers";
// send declare exchange frame // send declare exchange frame
return push(ExchangeDeclareFrame(_id, name, exchangeType, flags & passive, flags & durable, flags & nowait, arguments)); return push(ExchangeDeclareFrame(_id, name, exchangeType, flags & passive, flags & durable, false, arguments));
} }
/** /**
@ -231,16 +231,15 @@ Deferred &ChannelImpl::declareExchange(const std::string &name, ExchangeType typ
* @param source exchange which binds to target * @param source exchange which binds to target
* @param target exchange to bind to * @param target exchange to bind to
* @param routingKey routing key * @param routingKey routing key
* @param flags additional flags
* @param arguments additional arguments for binding * @param arguments additional arguments for binding
* *
* This function returns a deferred handler. Callbacks can be installed * This function returns a deferred handler. Callbacks can be installed
* using onSuccess(), onError() and onFinalize() methods. * using onSuccess(), onError() and onFinalize() methods.
*/ */
Deferred &ChannelImpl::bindExchange(const std::string &source, const std::string &target, const std::string &routingkey, int flags, const Table &arguments) Deferred &ChannelImpl::bindExchange(const std::string &source, const std::string &target, const std::string &routingkey, const Table &arguments)
{ {
// send exchange bind frame // send exchange bind frame
return push(ExchangeBindFrame(_id, target, source, routingkey, flags & nowait, arguments)); return push(ExchangeBindFrame(_id, target, source, routingkey, false, arguments));
} }
/** /**
@ -249,16 +248,15 @@ Deferred &ChannelImpl::bindExchange(const std::string &source, const std::string
* @param source the source exchange * @param source the source exchange
* @param target the target exchange * @param target the target exchange
* @param routingkey the routing key * @param routingkey the routing key
* @param flags optional flags
* @param arguments additional unbind arguments * @param arguments additional unbind arguments
* *
* This function returns a deferred handler. Callbacks can be installed * This function returns a deferred handler. Callbacks can be installed
* using onSuccess(), onError() and onFinalize() methods. * using onSuccess(), onError() and onFinalize() methods.
*/ */
Deferred &ChannelImpl::unbindExchange(const std::string &source, const std::string &target, const std::string &routingkey, int flags, const Table &arguments) Deferred &ChannelImpl::unbindExchange(const std::string &source, const std::string &target, const std::string &routingkey, const Table &arguments)
{ {
// send exchange unbind frame // send exchange unbind frame
return push(ExchangeUnbindFrame(_id, target, source, routingkey, flags & nowait, arguments)); return push(ExchangeUnbindFrame(_id, target, source, routingkey, false, arguments));
} }
/** /**
@ -273,7 +271,7 @@ Deferred &ChannelImpl::unbindExchange(const std::string &source, const std::stri
Deferred &ChannelImpl::removeExchange(const std::string &name, int flags) Deferred &ChannelImpl::removeExchange(const std::string &name, int flags)
{ {
// send delete exchange frame // send delete exchange frame
return push(ExchangeDeleteFrame(_id, name, flags & ifunused, flags & nowait)); return push(ExchangeDeleteFrame(_id, name, flags & ifunused, false));
} }
/** /**
@ -288,7 +286,7 @@ Deferred &ChannelImpl::removeExchange(const std::string &name, int flags)
DeferredQueue &ChannelImpl::declareQueue(const std::string &name, int flags, const Table &arguments) DeferredQueue &ChannelImpl::declareQueue(const std::string &name, int flags, const Table &arguments)
{ {
// the frame to send // the frame to send
QueueDeclareFrame frame(_id, name, flags & passive, flags & durable, flags & exclusive, flags & autodelete, flags & nowait, arguments); QueueDeclareFrame frame(_id, name, flags & passive, flags & durable, flags & exclusive, flags & autodelete, false, arguments);
// send the queuedeclareframe // send the queuedeclareframe
auto *result = new DeferredQueue(send(frame)); auto *result = new DeferredQueue(send(frame));
@ -306,16 +304,15 @@ DeferredQueue &ChannelImpl::declareQueue(const std::string &name, int flags, con
* @param exchangeName name of the exchange to bind to * @param exchangeName name of the exchange to bind to
* @param queueName name of the queue * @param queueName name of the queue
* @param routingkey routingkey * @param routingkey routingkey
* @param flags additional flags
* @param arguments additional arguments * @param arguments additional arguments
* *
* This function returns a deferred handler. Callbacks can be installed * This function returns a deferred handler. Callbacks can be installed
* using onSuccess(), onError() and onFinalize() methods. * using onSuccess(), onError() and onFinalize() methods.
*/ */
Deferred &ChannelImpl::bindQueue(const std::string &exchangeName, const std::string &queueName, const std::string &routingkey, int flags, const Table &arguments) Deferred &ChannelImpl::bindQueue(const std::string &exchangeName, const std::string &queueName, const std::string &routingkey, const Table &arguments)
{ {
// send the bind queue frame // send the bind queue frame
return push(QueueBindFrame(_id, queueName, exchangeName, routingkey, flags & nowait, arguments)); return push(QueueBindFrame(_id, queueName, exchangeName, routingkey, false, arguments));
} }
/** /**
@ -338,7 +335,6 @@ Deferred &ChannelImpl::unbindQueue(const std::string &exchange, const std::strin
/** /**
* Purge a queue * Purge a queue
* @param queue queue to purge * @param queue queue to purge
* @param flags additional flags
* *
* This function returns a deferred handler. Callbacks can be installed * This function returns a deferred handler. Callbacks can be installed
* using onSuccess(), onError() and onFinalize() methods. * using onSuccess(), onError() and onFinalize() methods.
@ -353,10 +349,10 @@ Deferred &ChannelImpl::unbindQueue(const std::string &exchange, const std::strin
* *
* }); * });
*/ */
DeferredDelete &ChannelImpl::purgeQueue(const std::string &name, int flags) DeferredDelete &ChannelImpl::purgeQueue(const std::string &name)
{ {
// the frame to send // the frame to send
QueuePurgeFrame frame(_id, name, flags & nowait); QueuePurgeFrame frame(_id, name, false);
// send the frame, and create deferred object // send the frame, and create deferred object
auto *deferred = new DeferredDelete(send(frame)); auto *deferred = new DeferredDelete(send(frame));
@ -389,7 +385,7 @@ DeferredDelete &ChannelImpl::purgeQueue(const std::string &name, int flags)
DeferredDelete &ChannelImpl::removeQueue(const std::string &name, int flags) DeferredDelete &ChannelImpl::removeQueue(const std::string &name, int flags)
{ {
// the frame to send // the frame to send
QueueDeleteFrame frame(_id, name, flags & ifunused, flags & ifempty, flags & nowait); QueueDeleteFrame frame(_id, name, flags & ifunused, flags & ifempty, false);
// send the frame, and create deferred object // send the frame, and create deferred object
auto *deferred = new DeferredDelete(send(frame)); auto *deferred = new DeferredDelete(send(frame));
@ -495,7 +491,7 @@ Deferred &ChannelImpl::setQos(uint16_t prefetchCount)
DeferredConsumer& ChannelImpl::consume(const std::string &queue, const std::string &tag, int flags, const Table &arguments) DeferredConsumer& ChannelImpl::consume(const std::string &queue, const std::string &tag, int flags, const Table &arguments)
{ {
// the frame to send // the frame to send
BasicConsumeFrame frame(_id, queue, tag, flags & nolocal, flags & noack, flags & exclusive, flags & nowait, arguments); BasicConsumeFrame frame(_id, queue, tag, flags & nolocal, flags & noack, flags & exclusive, false, arguments);
// send the frame, and create deferred object // send the frame, and create deferred object
auto *deferred = new DeferredConsumer(this, send(frame)); auto *deferred = new DeferredConsumer(this, send(frame));
@ -510,7 +506,6 @@ DeferredConsumer& ChannelImpl::consume(const std::string &queue, const std::stri
/** /**
* Cancel a running consumer * Cancel a running consumer
* @param tag the consumer tag * @param tag the consumer tag
* @param flags optional flags
* *
* This function returns a deferred handler. Callbacks can be installed * This function returns a deferred handler. Callbacks can be installed
* using onSuccess(), onError() and onFinalize() methods. * using onSuccess(), onError() and onFinalize() methods.
@ -525,10 +520,10 @@ DeferredConsumer& ChannelImpl::consume(const std::string &queue, const std::stri
* *
* }); * });
*/ */
DeferredCancel &ChannelImpl::cancel(const std::string &tag, int flags) DeferredCancel &ChannelImpl::cancel(const std::string &tag)
{ {
// the cancel frame to send // the cancel frame to send
BasicCancelFrame frame(_id, tag, flags & nowait); BasicCancelFrame frame(_id, tag, false);
// send the frame, and create deferred object // send the frame, and create deferred object
auto *deferred = new DeferredCancel(this, send(frame)); auto *deferred = new DeferredCancel(this, send(frame));
@ -587,10 +582,56 @@ bool ChannelImpl::send(const Frame &frame)
// skip if channel is not connected // skip if channel is not connected
if (_state != state_connected || !_connection) return false; if (_state != state_connected || !_connection) return false;
// are we currently in synchronous mode or are there
// other frames waiting for their turn to be sent?
if (_synchronous || !_queue.empty())
{
// we need to wait until the synchronous frame has
// been processed, so queue the frame until it was
_queue.emplace(frame.synchronous(), frame.buffer());
// it was of course not actually sent but we pretend
// that it was, because no error occured
return true;
}
// enter synchronous mode if necessary
_synchronous = frame.synchronous();
// send to tcp connection // send to tcp connection
return _connection->send(frame); return _connection->send(frame);
} }
/**
* Signal the channel that a synchronous operation
* was completed. After this operation, waiting
* frames can be sent out.
*/
void ChannelImpl::synchronized()
{
// we are no longer waiting for synchronous operations
_synchronous = false;
// we need to monitor the channel for validity
Monitor monitor(this);
// send all frames while not in synchronous mode
while (monitor.valid() && !_synchronous && !_queue.empty())
{
// retrieve the first buffer and synchronous
auto pair = std::move(_queue.front());
// remove from the list
_queue.pop();
// mark as synchronous if necessary
_synchronous = pair.first;
// send it over the connection
_connection->send(std::move(pair.second));
}
}
/** /**
* Report the received message * Report the received message
*/ */

View File

@ -213,9 +213,9 @@ void ConnectionImpl::setConnected()
// store connected state // store connected state
_state = state_connected; _state = state_connected;
// if the close operation was already called, we do that again now again // if the close method was called before, the frame was not
// so that the actual messages to close down the connection and the channel // sent. append it to the end of the queue to make sure we
// are appended to the queue // are correctly closed down.
if (_closed && !sendClose()) return; if (_closed && !sendClose()) return;
// we're going to call the handler, which can destruct the connection, // we're going to call the handler, which can destruct the connection,
@ -225,11 +225,8 @@ void ConnectionImpl::setConnected()
// inform handler // inform handler
_handler->onConnected(_parent); _handler->onConnected(_parent);
// leap out if the connection no longer exists
if (!monitor.valid()) return;
// empty the queue of messages // empty the queue of messages
while (!_queue.empty()) while (monitor.valid() && !_queue.empty())
{ {
// get the next message // get the next message
OutBuffer buffer(std::move(_queue.front())); OutBuffer buffer(std::move(_queue.front()));
@ -239,9 +236,6 @@ void ConnectionImpl::setConnected()
// send it // send it
_handler->onData(_parent, buffer.data(), buffer.size()); _handler->onData(_parent, buffer.data(), buffer.size());
// leap out if the connection was destructed
if (!monitor.valid()) return;
} }
} }
@ -256,16 +250,10 @@ bool ConnectionImpl::send(const Frame &frame)
if (_state == state_closing || _state == state_closed) return false; if (_state == state_closing || _state == state_closed) return false;
// we need an output buffer // we need an output buffer
OutBuffer buffer(frame.totalSize()); OutBuffer buffer(frame.buffer());
// fill the buffer
frame.fill(buffer);
// append an end of frame byte (but not when still negotiating the protocol)
if (frame.needsSeparator()) buffer.add((uint8_t)206);
// are we still setting up the connection? // are we still setting up the connection?
if ((_state == state_connected && _queue.size() == 0) || frame.partOfHandshake()) if ((_state == state_connected && _queue.empty()) || frame.partOfHandshake())
{ {
// send the buffer // send the buffer
_handler->onData(_parent, buffer.data(), buffer.size()); _handler->onData(_parent, buffer.data(), buffer.size());
@ -280,6 +268,32 @@ bool ConnectionImpl::send(const Frame &frame)
return true; return true;
} }
/**
* Send buffered data over the connection
*
* @param buffer the buffer with data to send
*/
bool ConnectionImpl::send(OutBuffer &&buffer)
{
// this only works when we are already connected
if (_state != state_connected) return false;
// are we waiting for other frames to be sent before us?
if (_queue.empty())
{
// send it directly
_handler->onData(_parent, buffer.data(), buffer.size());
}
else
{
// add to the list of waiting buffers
_queue.push(std::move(buffer));
}
// done
return true;
}
/** /**
* End of namspace * End of namspace
*/ */

View File

@ -103,6 +103,17 @@ public:
_arguments(arguments) _arguments(arguments)
{} {}
/**
* Is this a synchronous frame?
*
* After a synchronous frame no more frames may be
* sent until the accompanying -ok frame arrives
*/
bool synchronous() const override
{
// we are synchronous when the nowait option has not been set
return !noWait();
}
/** /**
* Get the destination exchange * Get the destination exchange
@ -153,7 +164,7 @@ public:
* Get the nowait bool * Get the nowait bool
* @return bool * @return bool
*/ */
bool noWait() bool noWait() const
{ {
return _bools.get(0); return _bools.get(0);
} }

View File

@ -67,7 +67,7 @@ public:
if(!channel) return false; if(!channel) return false;
// report to handler // report to handler
channel->reportSuccess(); if (channel->reportSuccess()) channel->synchronized();
// done // done
return true; return true;

View File

@ -106,6 +106,18 @@ public:
*/ */
virtual ~ExchangeDeclareFrame() {} virtual ~ExchangeDeclareFrame() {}
/**
* Is this a synchronous frame?
*
* After a synchronous frame no more frames may be
* sent until the accompanying -ok frame arrives
*/
bool synchronous() const override
{
// we are synchronous without the nowait option
return !noWait();
}
/** /**
* Method id * Method id
* @return uint16_t * @return uint16_t

View File

@ -70,7 +70,7 @@ public:
if(!channel) return false; if(!channel) return false;
// report exchange declare ok // report exchange declare ok
channel->reportSuccess(); if (channel->reportSuccess()) channel->synchronized();
// done // done
return true; return true;

View File

@ -83,6 +83,18 @@ public:
*/ */
virtual ~ExchangeDeleteFrame() {} virtual ~ExchangeDeleteFrame() {}
/**
* Is this a synchronous frame?
*
* After a synchronous frame no more frames may be
* sent until the accompanying -ok frame arrives
*/
bool synchronous() const override
{
// we are synchronous without the nowait option
return !noWait();
}
/** /**
* returns the method id * returns the method id
* @return uint16_t * @return uint16_t

View File

@ -71,7 +71,7 @@ public:
if(!channel) return false; if(!channel) return false;
// report to handler // report to handler
channel->reportSuccess(); if (channel->reportSuccess()) channel->synchronized();
// done // done
return true; return true;

View File

@ -103,6 +103,17 @@ public:
_arguments(arguments) _arguments(arguments)
{} {}
/**
* Is this a synchronous frame?
*
* After a synchronous frame no more frames may be
* sent until the accompanying -ok frame arrives
*/
bool synchronous() const override
{
// we are synchronous without the nowait option
return !noWait();
}
/** /**
* Get the destination exchange * Get the destination exchange
@ -153,7 +164,7 @@ public:
* Get the nowait bool * Get the nowait bool
* @return bool * @return bool
*/ */
bool noWait() bool noWait() const
{ {
return _bools.get(0); return _bools.get(0);
} }

View File

@ -68,7 +68,7 @@ public:
if(!channel) return false; if(!channel) return false;
// report to handler // report to handler
channel->reportSuccess(); if (channel->reportSuccess()) channel->synchronized();
// done // done
return true; return true;

View File

@ -54,6 +54,33 @@ public:
*/ */
virtual bool needsSeparator() const { return true; } virtual bool needsSeparator() const { return true; }
/**
* Is this a synchronous frame?
*
* After a synchronous frame no more frames may be
* sent until the accompanying -ok frame arrives
*/
virtual bool synchronous() const { return false; }
/**
* Retrieve the buffer in AMQP wire-format for
* sending over the socket connection
*/
OutBuffer buffer() const
{
// we need an output buffer
OutBuffer buffer(totalSize());
// fill the buffer
fill(buffer);
// append an end of frame byte (but not when still negotiating the protocol)
if (needsSeparator()) buffer.add((uint8_t)206);
// return the created buffer
return buffer;
}
/** /**
* Process the frame * Process the frame
* @param connection The connection over which it was received * @param connection The connection over which it was received

View File

@ -49,6 +49,14 @@ public:
*/ */
virtual ~MethodFrame() {} virtual ~MethodFrame() {}
/**
* Is this a synchronous frame?
*
* After a synchronous frame no more frames may be
* sent until the accompanying -ok frame arrives
*/
bool synchronous() const override { return true; }
/** /**
* Get the message type * Get the message type
* @return uint8_t * @return uint8_t

View File

@ -110,6 +110,18 @@ public:
_arguments(frame) _arguments(frame)
{} {}
/**
* Is this a synchronous frame?
*
* After a synchronous frame no more frames may be
* sent until the accompanying -ok frame arrives
*/
bool synchronous() const override
{
// we are synchronous without the nowait option
return !noWait();
}
/** /**
* Returns the method id * Returns the method id
* @return uint16_t * @return uint16_t

View File

@ -69,7 +69,7 @@ public:
if(!channel) return false; if(!channel) return false;
// report to handler // report to handler
channel->reportSuccess(); if (channel->reportSuccess()) channel->synchronized();
// done // done
return true; return true;

View File

@ -98,6 +98,18 @@ public:
_arguments(frame) _arguments(frame)
{} {}
/**
* Is this a synchronous frame?
*
* After a synchronous frame no more frames may be
* sent until the accompanying -ok frame arrives
*/
bool synchronous() const override
{
// we are synchronous without the nowait option
return !noWait();
}
/** /**
* returns the method id * returns the method id
* @return string * @return string
@ -156,7 +168,7 @@ public:
* returns whether to wait for a response * returns whether to wait for a response
* @return bool * @return bool
*/ */
bool noWait() bool noWait() const
{ {
return _bools.get(4); return _bools.get(4);
} }

View File

@ -133,7 +133,7 @@ public:
if (!channel) return false; if (!channel) return false;
// report success // report success
channel->reportSuccess(name(), messageCount(), consumerCount()); if (channel->reportSuccess(name(), messageCount(), consumerCount())) channel->synchronized();
// done // done
return true; return true;

View File

@ -85,6 +85,18 @@ public:
_bools(frame) _bools(frame)
{} {}
/**
* Is this a synchronous frame?
*
* After a synchronous frame no more frames may be
* sent until the accompanying -ok frame arrives
*/
bool synchronous() const override
{
// we are synchronous without the nowait option
return !noWait();
}
/** /**
* returns the method id * returns the method id
* @returns uint16_t * @returns uint16_t

View File

@ -94,7 +94,7 @@ public:
if(!channel) return false; if(!channel) return false;
// report queue deletion success // report queue deletion success
channel->reportSuccess(this->messageCount()); if (channel->reportSuccess(this->messageCount())) channel->synchronized();
// done // done
return true; return true;

View File

@ -81,6 +81,18 @@ public:
_noWait(frame) _noWait(frame)
{} {}
/**
* Is this a synchronous frame?
*
* After a synchronous frame no more frames may be
* sent until the accompanying -ok frame arrives
*/
bool synchronous() const override
{
// we are synchronous without the nowait option
return !noWait();
}
/** /**
* The method ID * The method ID
* @return method id * @return method id

View File

@ -94,7 +94,7 @@ public:
if(!channel) return false; if(!channel) return false;
// report queue purge success // report queue purge success
channel->reportSuccess(this->messageCount()); if (channel->reportSuccess(this->messageCount())) channel->synchronized();
// done // done
return true; return true;

View File

@ -73,7 +73,7 @@ public:
if(!channel) return false; if(!channel) return false;
// report queue unbind success // report queue unbind success
channel->reportSuccess(); if (channel->reportSuccess()) channel->synchronized();
// done // done
return true; return true;

View File

@ -74,7 +74,7 @@ public:
if(!channel) return false; if(!channel) return false;
// report that the channel is open // report that the channel is open
channel->reportSuccess(); if (channel->reportSuccess()) channel->synchronized();
// done // done
return true; return true;

View File

@ -74,7 +74,7 @@ public:
if(!channel) return false; if(!channel) return false;
// report that the channel is open // report that the channel is open
channel->reportSuccess(); if (channel->reportSuccess()) channel->synchronized();
// done // done
return true; return true;

View File

@ -74,7 +74,7 @@ public:
if(!channel) return false; if(!channel) return false;
// report that the channel is open // report that the channel is open
channel->reportSuccess(); if (channel->reportSuccess()) channel->synchronized();
// done // done
return true; return true;