From afca5440f0c7b5e70966ea1b86422311e773b1a5 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Wed, 15 Apr 2015 10:58:56 -0700 Subject: [PATCH] entity-server will automatically clear simulation-owner ID if it doesn't get an update for 2 seconds --- libraries/entities/src/EntityItem.h | 2 +- .../entities/src/SimpleEntitySimulation.cpp | 28 +++++++++++++++++++ .../entities/src/SimpleEntitySimulation.h | 1 + 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/libraries/entities/src/EntityItem.h b/libraries/entities/src/EntityItem.h index 8f7feb2ed8..946efea39c 100644 --- a/libraries/entities/src/EntityItem.h +++ b/libraries/entities/src/EntityItem.h @@ -323,7 +323,7 @@ protected: quint64 _lastEdited; // last official local or remote edit time quint64 _lastEditedFromRemote; // last time we received and edit from the server - quint64 _lastEditedFromRemoteInRemoteTime; // last time we received and edit from the server (in server-time-frame) + quint64 _lastEditedFromRemoteInRemoteTime; // last time we received an edit from the server (in server-time-frame) quint64 _created; quint64 _changedOnServer; diff --git a/libraries/entities/src/SimpleEntitySimulation.cpp b/libraries/entities/src/SimpleEntitySimulation.cpp index 6d45768c26..6846a38cb0 100644 --- a/libraries/entities/src/SimpleEntitySimulation.cpp +++ b/libraries/entities/src/SimpleEntitySimulation.cpp @@ -13,8 +13,12 @@ #include "EntityItem.h" #include "SimpleEntitySimulation.h" +#include "EntitiesLogging.h" + +const quint64 AUTO_REMOVE_SIMULATION_OWNER_USEC = 2000000; void SimpleEntitySimulation::updateEntitiesInternal(const quint64& now) { + // now is usecTimestampNow() QSet::iterator itemItr = _movingEntities.begin(); while (itemItr != _movingEntities.end()) { EntityItem* entity = *itemItr; @@ -27,6 +31,22 @@ void SimpleEntitySimulation::updateEntitiesInternal(const quint64& now) { ++itemItr; } } + + // If an Entity has a simulation owner and we don't get an update for some amount of time, + // clear the owner. This guards against an interface failing to release the Entity when it + // has finished simulating it. + itemItr = _hasSimulationOwnerEntities.begin(); + while (itemItr != _hasSimulationOwnerEntities.end()) { + EntityItem* entity = *itemItr; + if (!entity->getSimulatorID().isEmpty() && + usecTimestampNow() - entity->getLastChangedOnServer() >= AUTO_REMOVE_SIMULATION_OWNER_USEC) { + qCDebug(entities) << "auto-removing simulation owner" << entity->getSimulatorID(); + entity->setSimulatorID(""); + itemItr = _hasSimulationOwnerEntities.erase(itemItr); + } else { + ++itemItr; + } + } } void SimpleEntitySimulation::addEntityInternal(EntityItem* entity) { @@ -35,11 +55,15 @@ void SimpleEntitySimulation::addEntityInternal(EntityItem* entity) { } else if (entity->getCollisionsWillMove()) { _movableButStoppedEntities.insert(entity); } + if (!entity->getSimulatorID().isEmpty()) { + _hasSimulationOwnerEntities.insert(entity); + } } void SimpleEntitySimulation::removeEntityInternal(EntityItem* entity) { _movingEntities.remove(entity); _movableButStoppedEntities.remove(entity); + _hasSimulationOwnerEntities.remove(entity); } const int SIMPLE_SIMULATION_DIRTY_FLAGS = EntityItem::DIRTY_VELOCITY | EntityItem::DIRTY_MOTION_TYPE; @@ -55,6 +79,9 @@ void SimpleEntitySimulation::entityChangedInternal(EntityItem* entity) { _movingEntities.remove(entity); _movableButStoppedEntities.remove(entity); } + if (!entity->getSimulatorID().isEmpty()) { + _hasSimulationOwnerEntities.insert(entity); + } } entity->clearDirtyFlags(); } @@ -62,5 +89,6 @@ void SimpleEntitySimulation::entityChangedInternal(EntityItem* entity) { void SimpleEntitySimulation::clearEntitiesInternal() { _movingEntities.clear(); _movableButStoppedEntities.clear(); + _hasSimulationOwnerEntities.clear(); } diff --git a/libraries/entities/src/SimpleEntitySimulation.h b/libraries/entities/src/SimpleEntitySimulation.h index 92b6a28215..af79ec0131 100644 --- a/libraries/entities/src/SimpleEntitySimulation.h +++ b/libraries/entities/src/SimpleEntitySimulation.h @@ -30,6 +30,7 @@ protected: QSet _movingEntities; QSet _movableButStoppedEntities; + QSet _hasSimulationOwnerEntities; }; #endif // hifi_SimpleEntitySimulation_h