make sure onLost and onDetached are always called

This commit is contained in:
Michael van der Werve 2020-09-28 16:37:12 +02:00
parent 0d42234a3d
commit 2496dbbd4e
2 changed files with 13 additions and 4 deletions

View File

@ -231,7 +231,8 @@ public:
* pending operations are completed, and then an AMQP closing-handshake is * pending operations are completed, and then an AMQP closing-handshake is
* performed. If you pass a parameter "immediate=true" the connection is * performed. If you pass a parameter "immediate=true" the connection is
* immediately closed, without waiting for earlier commands (and your handler's * immediately closed, without waiting for earlier commands (and your handler's
* onError() method is called about the premature close) * onError() method is called about the premature close, including the onLost() and
* onDetached()).
* @return bool * @return bool
*/ */
bool close(bool immediate = false); bool close(bool immediate = false);

View File

@ -118,7 +118,8 @@ void TcpConnection::process(int fd, int flags)
} }
/** /**
* Close the connection * Close the connection.
* Warning: this potentially directly calls several handlers (onError, onLost, onDetached)
* @return bool * @return bool
*/ */
bool TcpConnection::close(bool immediate) bool TcpConnection::close(bool immediate)
@ -140,10 +141,17 @@ bool TcpConnection::close(bool immediate)
// stop if object was destructed // stop if object was destructed
if (!monitor.valid()) return true; if (!monitor.valid()) return true;
// also call the lost handler, we have now lost the connection from this state (since we force-closed).
// this makes sure the onLost and onDetached is properly called.
onLost(_state.get());
// stop if object was destructed
if (!monitor.valid()) return true;
// change the state // change the state
_state.reset(new TcpClosed(this)); _state.reset(new TcpClosed(this));
// done, we return true because the connection is closed // done, we return true because the connection is closed
return true; return true;
} }