Handle m_last_confirmed_item_ticks live in a thread safe way

This commit is contained in:
Benau
2019-01-08 02:17:35 +08:00
parent f6fefba343
commit 10dbac9149
2 changed files with 18 additions and 3 deletions

View File

@@ -191,7 +191,11 @@ void NetworkItemManager::setItemConfirmationTime(std::weak_ptr<STKPeer> peer,
int ticks)
{
assert(NetworkConfig::get()->isServer());
if (ticks > m_last_confirmed_item_ticks.at(peer))
std::unique_lock<std::mutex> ul(m_live_players_mutex);
// Peer may get removed earlier if peer request to go back to lobby
if (m_last_confirmed_item_ticks.find(peer) !=
m_last_confirmed_item_ticks.end() &&
ticks > m_last_confirmed_item_ticks.at(peer))
m_last_confirmed_item_ticks.at(peer) = ticks;
// Now discard unneeded events and expired (disconnected) peer, i.e. all
@@ -210,6 +214,7 @@ void NetworkItemManager::setItemConfirmationTime(std::weak_ptr<STKPeer> peer,
it++;
}
}
ul.unlock();
// Find the last entry before the minimal confirmed time.
// Since the event list is sorted, all events up to this

View File

@@ -27,6 +27,7 @@
#include <map>
#include <memory>
#include <mutex>
class STKPeer;
@@ -54,6 +55,9 @@ private:
/** Time at which m_confirmed_state was taken. */
int m_confirmed_state_time;
/** Allow remove or add peer live. */
std::mutex m_live_players_mutex;
/** Stores on the server the latest confirmed tick from each client. */
std::map<std::weak_ptr<STKPeer>, int32_t,
std::owner_less<std::weak_ptr<STKPeer> > > m_last_confirmed_item_ticks;
@@ -99,10 +103,16 @@ public:
virtual void undoEvent(BareNetworkString*) OVERRIDE {};
// ------------------------------------------------------------------------
void addLiveJoinPeer(std::weak_ptr<STKPeer> peer)
{ m_last_confirmed_item_ticks[peer] = 0; }
{
std::lock_guard<std::mutex> lock(m_live_players_mutex);
m_last_confirmed_item_ticks[peer] = 0;
}
// ------------------------------------------------------------------------
void erasePeerInGame(std::weak_ptr<STKPeer> peer)
{ m_last_confirmed_item_ticks.erase(peer); }
{
std::lock_guard<std::mutex> lock(m_live_players_mutex);
m_last_confirmed_item_ticks.erase(peer);
}
// ------------------------------------------------------------------------
void saveCompleteState(BareNetworkString* buffer) const;
// ------------------------------------------------------------------------