浏览代码

Code cleanup, reorganization of network profiling blocks.

Lasse Öörni 13 年之前
父节点
当前提交
41c2efde36
共有 4 个文件被更改,包括 40 次插入31 次删除
  1. 5 8
      Engine/Network/Connection.cpp
  2. 23 17
      Engine/Network/Network.cpp
  3. 10 6
      Engine/Scene/Scene.cpp
  4. 2 0
      Engine/Scene/Scene.h

+ 5 - 8
Engine/Network/Connection.cpp

@@ -221,10 +221,8 @@ void Connection::SendServerUpdate()
     if (!scene_ || !sceneLoaded_)
         return;
     
-    PROFILE(SendServerUpdate);
-    
-    // Always check the root node (scene) first so that the scene-wide components get sent first
-    nodesToProcess_.Clear();
+    // Always check the root node (scene) first so that the scene-wide components get sent first,
+    // and all other replicated nodes get added to the dirty set for sending the initial state
     nodesToProcess_.Insert(scene_->GetID());
     ProcessNode(scene_);
     
@@ -243,15 +241,14 @@ void Connection::SendServerUpdate()
             nodesToProcess_.Erase(id);
             
             // If node is dirty, but is no longer found, it has been removed
-            HashMap<unsigned, NodeReplicationState>::Iterator j = sceneState_.nodeStates_.Find(id);
-            if (j != sceneState_.nodeStates_.End())
+            if (sceneState_.nodeStates_.Contains(id))
             {
                 msg_.Clear();
                 msg_.WriteNetID(id);
                 
                 // Note: we will send MSG_REMOVENODE redundantly for each node in the hierarchy, even if removing the root node
-                // would be enough. However, this may be better due to the client not possibly having updated parenting information
-                // at the time of receiving this message
+                // would be enough. However, this may be better due to the client not possibly having updated parenting
+                // information at the time of receiving this message
                 SendMessage(MSG_REMOVENODE, true, true, msg_, NET_HIGH_PRIORITY);
                 sceneState_.nodeStates_.Erase(id);
             }

+ 23 - 17
Engine/Network/Network.cpp

@@ -423,28 +423,34 @@ void Network::PostUpdate(float timeStep)
         
         if (IsServerRunning())
         {
-            // Collect and update all networked scenes
-            networkScenes_.Clear();
-            for (Map<kNet::MessageConnection*, SharedPtr<Connection> >::ConstIterator i = clientConnections_.Begin();
-                i != clientConnections_.End(); ++i)
-            {
-                Scene* scene = i->second_->GetScene();
-                if (scene)
-                    networkScenes_.Insert(scene);
-            }
-            for (HashSet<Scene*>::ConstIterator i = networkScenes_.Begin(); i != networkScenes_.End(); ++i)
+            // Collect and prepare all networked scenes
             {
                 PROFILE(PrepareServerUpdate);
-                (*i)->PrepareNetworkUpdate();
+                
+                networkScenes_.Clear();
+                for (Map<kNet::MessageConnection*, SharedPtr<Connection> >::ConstIterator i = clientConnections_.Begin();
+                    i != clientConnections_.End(); ++i)
+                {
+                    Scene* scene = i->second_->GetScene();
+                    if (scene)
+                        networkScenes_.Insert(scene);
+                }
+                
+                for (HashSet<Scene*>::ConstIterator i = networkScenes_.Begin(); i != networkScenes_.End(); ++i)
+                    (*i)->PrepareNetworkUpdate();
             }
             
-            // Send server updates for each client connection
-            for (Map<kNet::MessageConnection*, SharedPtr<Connection> >::ConstIterator i = clientConnections_.Begin();
-                i != clientConnections_.End(); ++i)
             {
-                i->second_->SendServerUpdate();
-                i->second_->SendRemoteEvents();
-                i->second_->SendPackages();
+                PROFILE(SendServerUpdate);
+                
+                // Then send server updates for each client connection
+                for (Map<kNet::MessageConnection*, SharedPtr<Connection> >::ConstIterator i = clientConnections_.Begin();
+                    i != clientConnections_.End(); ++i)
+                {
+                    i->second_->SendServerUpdate();
+                    i->second_->SendRemoteEvents();
+                    i->second_->SendPackages();
+                }
             }
         }
         

+ 10 - 6
Engine/Scene/Scene.cpp

@@ -612,9 +612,7 @@ void Scene::NodeAdded(Node* node)
         }
         
         replicatedNodes_[id] = node;
-        
-        for (PODVector<NodeReplicationState*>::Iterator i = replicationStates_.Begin(); i != replicationStates_.End(); ++i)
-            (*i)->sceneState_->dirtyNodes_.Insert(id);
+        MarkReplicationDirty(node);
     }
     else
     {
@@ -639,9 +637,7 @@ void Scene::NodeRemoved(Node* node)
     if (id < FIRST_LOCAL_ID)
     {
         replicatedNodes_.Erase(id);
-        
-        for (PODVector<NodeReplicationState*>::Iterator i = replicationStates_.Begin(); i != replicationStates_.End(); ++i)
-            (*i)->sceneState_->dirtyNodes_.Insert(id);
+        MarkReplicationDirty(node);
     }
     else
         localNodes_.Erase(id);
@@ -733,6 +729,14 @@ void Scene::CleanupConnection(Connection* connection)
         i->second_->CleanupConnection(connection);
 }
 
+void Scene::MarkReplicationDirty(Node* node)
+{
+    unsigned id = node->GetID();
+    
+    for (PODVector<NodeReplicationState*>::Iterator i = replicationStates_.Begin(); i != replicationStates_.End(); ++i)
+        (*i)->sceneState_->dirtyNodes_.Insert(id);
+}
+
 void Scene::HandleUpdate(StringHash eventType, VariantMap& eventData)
 {
     using namespace Update;

+ 2 - 0
Engine/Scene/Scene.h

@@ -164,6 +164,8 @@ public:
     void PrepareNetworkUpdate();
     /// Clean up all references to a network connection that is about to be removed.
     void CleanupConnection(Connection* connection);
+    /// Mark a node dirty in scene replication states. The node does not need to have own replication state yet.
+    void MarkReplicationDirty(Node* node);
     
 private:
     /// Handle the logic update event to update the scene, if active.