Skip to content

Commit

Permalink
the TcpConnection class no longer calls back to userspace / to the us…
Browse files Browse the repository at this point in the history
…er-supplied handler if user-space explicitly destructs the object
  • Loading branch information
EmielBruijntjes committed Apr 30, 2020
1 parent a75b3d5 commit e7f76bc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
10 changes: 5 additions & 5 deletions include/amqpcpp/linux_tcp/tcpconnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class TcpConnection :
virtual void onHeartbeat(Connection *connection) override
{
// pass on to tcp handler
_handler->onHeartbeat(this);
if (_handler) _handler->onHeartbeat(this);
}

/**
Expand All @@ -108,7 +108,7 @@ class TcpConnection :
virtual void onReady(Connection *connection) override
{
// pass on to the handler
_handler->onReady(this);
if (_handler) _handler->onReady(this);
}

/**
Expand All @@ -124,7 +124,7 @@ class TcpConnection :
virtual void onConnected(TcpState *state) override
{
// pass on to the handler
_handler->onConnected(this);
if (_handler) _handler->onConnected(this);
}

/**
Expand All @@ -136,7 +136,7 @@ class TcpConnection :
virtual bool onSecured(TcpState *state, const SSL *ssl) override
{
// pass on to user-space
return _handler->onSecured(this, ssl);
return _handler && _handler->onSecured(this, ssl);
}

/**
Expand All @@ -160,7 +160,7 @@ class TcpConnection :
virtual void onIdle(TcpState *state, int socket, int events) override
{
// pass on to user-space
return _handler->monitor(this, socket, events);
if (_handler) _handler->monitor(this, socket, events);
}

/**
Expand Down
27 changes: 21 additions & 6 deletions src/linux_tcp/tcpconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Implementation file for the TCP connection
*
* @author Emiel Bruijntjes <[email protected]>
* @copyright 2015 - 2018 Copernica BV
* @copyright 2015 - 2020 Copernica BV
*/

/**
Expand Down Expand Up @@ -36,7 +36,16 @@ TcpConnection::TcpConnection(TcpHandler *handler, const Address &address) :
/**
* Destructor
*/
TcpConnection::~TcpConnection() noexcept = default;
TcpConnection::~TcpConnection() noexcept
{
// When the object is destructed, the _state-pointer will also destruct, resulting
// in some final calls back to us to inform that the connection has indeed been closed.
// This normally results in calls back to user-space (via the _handler pointer) but
// since user-space is apparently no longer interested in the TcpConnection (why would
// it otherwise prematurely destruct the object?) we reset the handler-pointer to
// prevent that such calls back to userspace take place
_handler = nullptr;
}

/**
* The filedescriptor that is used for this connection
Expand Down Expand Up @@ -121,7 +130,7 @@ bool TcpConnection::close(bool immediate)
if (!monitor.valid()) return true;

// tell the handler that the connection was closed
if (failed) _handler->onError(this, "connection prematurely closed by client");
if (failed && _handler) _handler->onError(this, "connection prematurely closed by client");

// stop if object was destructed
if (!monitor.valid()) return true;
Expand All @@ -143,7 +152,7 @@ bool TcpConnection::close(bool immediate)
void TcpConnection::onProperties(Connection *connection, const Table &server, Table &client)
{
// tell the handler
return _handler->onProperties(this, server, client);
if (_handler) _handler->onProperties(this, server, client);
}

/**
Expand All @@ -158,7 +167,7 @@ uint16_t TcpConnection::onNegotiate(Connection *connection, uint16_t interval)
_state->maxframe(connection->maxFrame());

// tell the handler
return _handler->onNegotiate(this, interval);
return _handler ? _handler->onNegotiate(this, interval) : interval;
}

/**
Expand All @@ -184,7 +193,7 @@ void TcpConnection::onError(Connection *connection, const char *message)
Monitor monitor(this);

// tell this to the user
_handler->onError(this, message);
if (_handler) _handler->onError(this, message);

// object could be destructed by user-space
if (!monitor.valid()) return;
Expand Down Expand Up @@ -214,6 +223,9 @@ void TcpConnection::onClosed(Connection *connection)
*/
void TcpConnection::onError(TcpState *state, const char *message, bool connected)
{
// if user-space is no longer interested in this object, the rest of the code is pointless here
if (_handler == nullptr) return;

// monitor to check if all operations are active
Monitor monitor(this);

Expand All @@ -240,6 +252,9 @@ void TcpConnection::onError(TcpState *state, const char *message, bool connected
*/
void TcpConnection::onLost(TcpState *state)
{
// if user-space is no longer interested in this object, the rest of the code is pointless here
if (_handler == nullptr) return;

// monitor to check if "this" is destructed
Monitor monitor(this);

Expand Down

0 comments on commit e7f76bc

Please sign in to comment.