Skip to content

Commit

Permalink
Merge branch 'master' of github.com:CopernicaMarketingSoftware/AMQP-CPP
Browse files Browse the repository at this point in the history
  • Loading branch information
EmielBruijntjes committed Jun 15, 2018
2 parents 80ce632 + 2430da7 commit e665916
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/linux_tcp/tcpconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,29 @@ void TcpConnection::process(int fd, int flags)
// monitor the object for destruction, because you never know what the user
Monitor monitor(this);

// pass on the the state, that returns a new impl
auto *result = _state->process(monitor, fd, flags);
// store the old state
auto *oldstate = _state.get();

// are we still valid
if (!monitor.valid()) return;
// pass on the the state, that returns a new impl
auto *newstate = _state->process(monitor, fd, flags);

// skip if the same state is continued to be used, or when the process()
// method returns nullptr (which only happens when the object is destructed,
// and "this" is no longer valid)
if (!result || result == _state.get()) return;
// if the state did not change, we do not have to update a member,
// 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;

// replace it with the new implementation
_state.reset(result);
// in a bizarre set of circumstances, the user may have implemented the
// handler in such a way that the connection object was destructed
if (!monitor.valid())
{
// ok, user code is weird, connection object no longer exist, get rid of the state too
delete newstate;
}
else
{
// replace it with the new implementation
_state.reset(newstate);
}
}

/**
Expand Down

0 comments on commit e665916

Please sign in to comment.