The Channel constructor now throws an exception if the max number of channels has been reached

This commit is contained in:
Emiel Bruijntjes 2018-11-23 11:15:51 +01:00
parent 45ca61cc43
commit ee60aeb025
4 changed files with 22 additions and 12 deletions

View File

@ -31,15 +31,14 @@ public:
* Construct a channel object
*
* The passed in connection pointer must remain valid for the
* lifetime of the channel.
* lifetime of the channel. Watch out: this method throws an error
* if the channel could not be constructed (for example because the
* max number of AMQP channels has been reached)
*
* @param connection
* @throws std::runtime_error
*/
Channel(Connection *connection) : _implementation(new ChannelImpl())
{
// attach the connection to the channel
_implementation->attach(connection);
}
Channel(Connection *connection);
/**
* Copy'ing of channel objects is not supported

View File

@ -151,8 +151,9 @@ private:
/**
* Attach the connection
* @param connection
* @return bool
*/
void attach(Connection *connection);
bool attach(Connection *connection);
/**
* Push a deferred result

View File

@ -27,9 +27,12 @@ public:
* Constructor
*
* The passed in connection pointer must remain valid for the
* lifetime of the channel.
* lifetime of the channel. A constructor is thrown if the channel
* cannot be connected (because the connection is already closed or
* because max number of channels has been reached)
*
* @param connection
* @throws std::runtime_error
*/
TcpChannel(TcpConnection *connection) :
Channel(&connection->_connection) {}

View File

@ -96,8 +96,9 @@ void ChannelImpl::onError(const ErrorCallback &callback)
/**
* Initialize the object with an connection
* @param connection
* @return bool
*/
void ChannelImpl::attach(Connection *connection)
bool ChannelImpl::attach(Connection *connection)
{
// get connection impl
_connection = &connection->_implementation;
@ -110,6 +111,9 @@ void ChannelImpl::attach(Connection *connection)
{
// this is invalid
_state = state_closed;
// failure
return false;
}
else
{
@ -117,10 +121,13 @@ void ChannelImpl::attach(Connection *connection)
_state = state_connected;
// send the open frame
if (send(ChannelOpenFrame(_id))) return;
if (send(ChannelOpenFrame(_id))) return true;
// report an error
reportError("Channel could not be initialized", true);
// this is an error
_state = state_closed;
// report failure
return false;
}
}