Merge branch 'master' of github.com:CopernicaMarketingSoftware/AMQP-CPP

This commit is contained in:
Emiel Bruijntjes 2018-06-15 09:19:37 +02:00
commit e665916b85
1 changed files with 20 additions and 10 deletions

View File

@ -66,19 +66,29 @@ void TcpConnection::process(int fd, int flags)
// monitor the object for destruction, because you never know what the user // monitor the object for destruction, because you never know what the user
Monitor monitor(this); Monitor monitor(this);
// store the old state
auto *oldstate = _state.get();
// pass on the the state, that returns a new impl // pass on the the state, that returns a new impl
auto *result = _state->process(monitor, fd, flags); auto *newstate = _state->process(monitor, fd, flags);
// are we still valid // if the state did not change, we do not have to update a member,
if (!monitor.valid()) return; // when the newstate is nullptr, the object is (being) destructed
// and we do not have to do anything else either
if (oldstate == newstate || newstate == nullptr) return;
// skip if the same state is continued to be used, or when the process() // in a bizarre set of circumstances, the user may have implemented the
// method returns nullptr (which only happens when the object is destructed, // handler in such a way that the connection object was destructed
// and "this" is no longer valid) if (!monitor.valid())
if (!result || result == _state.get()) return; {
// ok, user code is weird, connection object no longer exist, get rid of the state too
// replace it with the new implementation delete newstate;
_state.reset(result); }
else
{
// replace it with the new implementation
_state.reset(newstate);
}
} }
/** /**