Browse Source

Network code cleanup.
Enabled kNet flow control.
Fixed kNet uninitialized variables.

Lasse Öörni 14 years ago
parent
commit
db453e642d

+ 12 - 12
Bin/Data/Scripts/TestScene.as

@@ -86,6 +86,18 @@ void InitScene()
     zone.fogEnd = 300.0;
     zone.boundingBox = BoundingBox(-1000.0, 1000.0);
 
+    {
+        Node@ lightNode = testScene.CreateChild("Light");
+        lightNode.direction = Vector3(0.5, -0.5, 0.5);
+
+        Light@ light = lightNode.CreateComponent("Light");
+        light.lightType = LIGHT_DIRECTIONAL;
+        light.castShadows = true;
+        light.shadowBias = BiasParameters(0.0001, 0.5);
+        light.shadowCascade = CascadeParameters(3, 0.90, 0.2, 200.0);
+        light.specularIntensity = 0.5f;
+    }
+
     {
         Node@ objectNode = testScene.CreateChild("Floor");
         objectNode.position = Vector3(0.0, -0.5, 0.0);
@@ -169,18 +181,6 @@ void InitScene()
         ctrl.Play("Models/Jack_Walk.ani", 0, true, 0.0f);
     }
 
-    {
-        Node@ lightNode = testScene.CreateChild("Light");
-        lightNode.direction = Vector3(0.5, -0.5, 0.5);
-
-        Light@ light = lightNode.CreateComponent("Light");
-        light.lightType = LIGHT_DIRECTIONAL;
-        light.castShadows = true;
-        light.shadowBias = BiasParameters(0.0001, 0.5);
-        light.shadowCascade = CascadeParameters(3, 0.90, 0.2, 200.0);
-        light.specularIntensity = 0.5f;
-    }
-
     // Enable access to this script file & scene from the console
     script.defaultScene = testScene;
     script.defaultScriptFile = scriptFile;

+ 2 - 8
Engine/Core/Object.h

@@ -228,14 +228,8 @@ private:
     public: \
         virtual ShortStringHash GetType() const { return GetTypeStatic(); } \
         virtual const String& GetTypeName() const { return GetTypeNameStatic(); } \
-        static ShortStringHash GetTypeStatic() \
-        { \
-            return typeStatic; \
-        } \
-        static const String& GetTypeNameStatic() \
-        { \
-            return typeNameStatic; \
-        } \
+        static ShortStringHash GetTypeStatic() { return typeStatic; } \
+        static const String& GetTypeNameStatic() { return typeNameStatic; } \
 
 #define OBJECTTYPESTATIC(typeName) \
     const ShortStringHash typeName::typeStatic(#typeName); \

+ 12 - 21
Engine/Graphics/View.cpp

@@ -665,7 +665,7 @@ void View::GetLitBatches(Drawable* drawable, Light* light, Light* splitLight, Li
             }
         }
         
-        // If no first light pass, get ordinary light pass
+        // If no lit base pass, get ordinary light pass
         if (!pass)
             pass = tech->GetPass(PASS_LIGHT);
         // Skip if material does not receive light at all
@@ -2005,9 +2005,12 @@ void View::DrawSplitLightToStencil(Camera& camera, Light* light, bool clear)
         break;
         
     case LIGHT_DIRECTIONAL:
-        // If light encompasses whole frustum, no drawing to frustum necessary
+        // If light encompasses whole frustum, no drawing to stencil necessary
         if (light->GetNearSplit() <= camera.GetNearClip() && light->GetFarSplit() >= camera.GetFarClip())
+        {
+            graphics_->SetStencilTest(false);
             return;
+        }
         else
         {
             if (!clear)
@@ -2022,25 +2025,13 @@ void View::DrawSplitLightToStencil(Camera& camera, Light* light, bool clear)
                 graphics_->SetDepthWrite(false);
                 graphics_->SetCullMode(CULL_NONE);
                 
-                // If the split begins at the near plane (first split), draw at split far plane
-                if (light->GetNearSplit() <= camera.GetNearClip())
-                {
-                    graphics_->SetDepthTest(CMP_GREATEREQUAL);
-                    graphics_->SetShaders(renderer_->stencilVS_, renderer_->stencilPS_);
-                    graphics_->SetShaderParameter(VSP_MODEL, farTransform);
-                    graphics_->SetShaderParameter(VSP_VIEWPROJ, projection);
-                    graphics_->SetStencilTest(true, CMP_ALWAYS, OP_REF, OP_ZERO, OP_ZERO, 1);
-                }
-                // Otherwise draw at split near plane
-                else
-                {
-                    graphics_->SetDepthTest(CMP_LESSEQUAL);
-                    graphics_->SetShaders(renderer_->stencilVS_, renderer_->stencilPS_);
-                    graphics_->SetShaderParameter(VSP_MODEL, nearTransform);
-                    graphics_->SetShaderParameter(VSP_VIEWPROJ, projection);
-                    graphics_->SetStencilTest(true, CMP_ALWAYS, OP_REF, OP_ZERO, OP_ZERO, 1);
-                }
-                
+                // If the split begins at the near plane (first split), draw at split far plane, otherwise at near plane
+                bool firstSplit = light->GetNearSplit() <= camera.GetNearClip();
+                graphics_->SetDepthTest(firstSplit ? CMP_GREATEREQUAL : CMP_LESSEQUAL);
+                graphics_->SetShaders(renderer_->stencilVS_, renderer_->stencilPS_);
+                graphics_->SetShaderParameter(VSP_MODEL, firstSplit ? farTransform : nearTransform);
+                graphics_->SetShaderParameter(VSP_VIEWPROJ, projection);
+                graphics_->SetStencilTest(true, CMP_ALWAYS, OP_REF, OP_ZERO, OP_ZERO, 1);
                 graphics_->ClearTransformSources();
                 
                 renderer_->dirLightGeometry_->Draw(graphics_);

+ 9 - 16
Engine/Network/Connection.cpp

@@ -57,21 +57,19 @@ Connection::~Connection()
     SetScene(0);
 }
 
+void Connection::SendMessage(int msgID, bool reliable, bool inOrder, const VectorBuffer& msg)
+{
+    SendMessage(msgID, 0, reliable, inOrder, msg.GetData(), msg.GetSize());
+}
+
 void Connection::SendMessage(int msgID, bool reliable, bool inOrder, const unsigned char* data, unsigned numBytes)
 {
-    // Make sure not to use kNet internal message ID's
-    if (msgID <= 0x4 || msgID >= 0x3ffffffe)
-    {
-        LOGERROR("Can not send message with reserved ID");
-        return;
-    }
-    
-    connection_->SendMessage(msgID, reliable, inOrder, 0, 0, (const char*)data, numBytes);
+    SendMessage(msgID, 0, reliable, inOrder, data, numBytes);
 }
 
-void Connection::SendMessage(int msgID, bool reliable, bool inOrder, const VectorBuffer& msg)
+void Connection::SendMessage(int msgID, unsigned contentID, bool reliable, bool inOrder, const VectorBuffer& msg)
 {
-    SendMessage(msgID, reliable, inOrder, msg.GetData(), msg.GetSize());
+    SendMessage(msgID, contentID, reliable, inOrder, msg.GetData(), msg.GetSize());
 }
 
 void Connection::SendMessage(int msgID, unsigned contentID, bool reliable, bool inOrder, const unsigned char* data, unsigned numBytes)
@@ -83,12 +81,7 @@ void Connection::SendMessage(int msgID, unsigned contentID, bool reliable, bool
         return;
     }
     
-    connection_->SendMessage(msgID, reliable, inOrder, 0, contentID, (const char*)data, numBytes);
-}
-
-void Connection::SendMessage(int msgID, unsigned contentID, bool reliable, bool inOrder, const VectorBuffer& msg)
-{
-    SendMessage(msgID, contentID, reliable, inOrder, msg.GetData(), msg.GetSize());
+    connection_->SendMessage(msgID, reliable, inOrder, DEFAULT_MSG_PRIORITY, contentID, (const char*)data, numBytes);
 }
 
 void Connection::SendRemoteEvent(StringHash eventType, bool inOrder, const VariantMap& eventData)

+ 7 - 4
Engine/Network/Connection.h

@@ -41,6 +41,9 @@ class Node;
 class Scene;
 class Serializable;
 
+/// Message priority for kNet. For now all messages have same priority
+static const int DEFAULT_MSG_PRIORITY = 100;
+
 /// Queued remote event
 struct RemoteEvent
 {
@@ -65,14 +68,14 @@ public:
     /// Destruct
     ~Connection();
     
-    /// Send a message
-    void SendMessage(int msgID, bool reliable, bool inOrder, const unsigned char* data, unsigned numBytes);
     /// Send a message
     void SendMessage(int msgID, bool reliable, bool inOrder, const VectorBuffer& msg);
-    /// Send a message with content ID
-    void SendMessage(int msgID, unsigned contentID, bool reliable, bool inOrder, const unsigned char* data, unsigned numBytes);
+    /// Send a message
+    void SendMessage(int msgID, bool reliable, bool inOrder, const unsigned char* data, unsigned numBytes);
     /// Send a message with content ID
     void SendMessage(int msgID, unsigned contentID, bool reliable, bool inOrder, const VectorBuffer& msg);
+    /// Send a message with content ID
+    void SendMessage(int msgID, unsigned contentID, bool reliable, bool inOrder, const unsigned char* data, unsigned numBytes);
     /// Send a remote event
     void SendRemoteEvent(StringHash eventType, bool inOrder, const VariantMap& eventData = VariantMap());
     /// Send a remote node event

+ 17 - 22
Engine/Network/Network.cpp

@@ -254,23 +254,12 @@ void Network::StopServer()
 
 void Network::BroadcastMessage(int msgID, bool reliable, bool inOrder, const VectorBuffer& msg)
 {
-    BroadcastMessage(msgID, reliable, inOrder, msg.GetData(), msg.GetSize());
+    BroadcastMessage(msgID, 0, reliable, inOrder, msg.GetData(), msg.GetSize());
 }
 
 void Network::BroadcastMessage(int msgID, bool reliable, bool inOrder, const unsigned char* data, unsigned numBytes)
 {
-   // Make sure not to use kNet internal message ID's
-    if (msgID <= 0x4 || msgID >= 0x3ffffffe)
-    {
-        LOGERROR("Can not send message with reserved ID");
-        return;
-    }
-    
-    kNet::NetworkServer* server = network_->GetServer();
-    if (server)
-        server->BroadcastMessage(msgID, reliable, inOrder, 0, 0, (const char*)data, numBytes);
-    else
-        LOGERROR("Server not running, can not broadcast messages");
+    BroadcastMessage(msgID, 0, reliable, inOrder, data, numBytes);
 }
 
 void Network::BroadcastMessage(int msgID, unsigned contentID, bool reliable, bool inOrder, const VectorBuffer& msg)
@@ -289,7 +278,7 @@ void Network::BroadcastMessage(int msgID, unsigned contentID, bool reliable, boo
     
     kNet::NetworkServer* server = network_->GetServer();
     if (server)
-        server->BroadcastMessage(msgID, reliable, inOrder, 0, contentID, (const char*)data, numBytes);
+        server->BroadcastMessage(msgID, reliable, inOrder, DEFAULT_MSG_PRIORITY, contentID, (const char*)data, numBytes);
     else
         LOGERROR("Server not running, can not broadcast messages");
 }
@@ -400,10 +389,6 @@ void Network::Update(float timeStep)
     if (serverConnection_)
     {
         kNet::MessageConnection* connection = serverConnection_->GetMessageConnection();
-        connection->Process();
-        
-        // Process latest data messages waiting for the correct nodes or components to be created
-        serverConnection_->ProcessPendingLatestData();
         
         // Check for state transitions
         kNet::ConnectionState state = connection->GetConnectionState();
@@ -414,11 +399,20 @@ void Network::Update(float timeStep)
         else if (state == kNet::ConnectionClosed)
             OnServerDisconnected();
         
-        // Send the client update
-        if (updateNow && serverConnection_)
+        if (serverConnection_)
         {
-            serverConnection_->SendClientUpdate();
-            serverConnection_->SendQueuedRemoteEvents();
+            // Receive new messages
+            connection->Process();
+            
+            // Process latest data messages waiting for the correct nodes or components to be created
+            serverConnection_->ProcessPendingLatestData();
+            
+            // Send the client update
+            if (updateNow)
+            {
+                serverConnection_->SendClientUpdate();
+                serverConnection_->SendQueuedRemoteEvents();
+            }
         }
     }
     
@@ -451,6 +445,7 @@ void Network::HandleBeginFrame(StringHash eventType, VariantMap& eventData)
 void Network::OnServerConnected()
 {
     serverConnection_->SetConnectPending(false);
+    
     LOGINFO("Connected to server");
     
     // Send the identity map now

+ 1 - 1
ThirdParty/kNet/CMakeLists.txt

@@ -54,7 +54,7 @@ AddCompilationDefine(KNET_ENABLE_WINXP_SUPPORT)
 
 # Enable internal LOG messaging if this flag is enabled. Comment this out to squeeze the last bit of
 # extra performance by avoiding all logging-related string operations.
-AddCompilationDefine(KNET_LOGGING_SUPPORT_ENABLED)
+#AddCompilationDefine(KNET_LOGGING_SUPPORT_ENABLED)
 
 # Enable storing profiling data from different network level events.
 #AddCompilationDefine(KNET_NETWORK_PROFILING)

+ 2 - 6
ThirdParty/kNet/src/UDPMessageConnection.cpp

@@ -58,7 +58,7 @@ static const u32 cMaxUDPMessageFragmentSize = 470;
 
 UDPMessageConnection::UDPMessageConnection(Network *owner, NetworkServer *ownerServer, Socket *socket, ConnectionState startingState)
 :MessageConnection(owner, ownerServer, socket, startingState),
-retransmissionTimeout(3.f), smoothedRTT(3.f), rttVariation(0.f), rttCleared(true), // Set RTT initial values as per RFC 2988.
+retransmissionTimeout(3.f), numAcksLastFrame(0), numLossesLastFrame(0), smoothedRTT(3.f), rttVariation(0.f), rttCleared(true), // Set RTT initial values as per RFC 2988.
 lastReceivedInOrderPacketID(0), 
 lastSentInOrderPacketID(0), datagramPacketIDCounter(1),
 packetLossRate(0.f), packetLossCount(0.f), datagramOutRatePerSecond(initialDatagramRatePerSecond), 
@@ -268,7 +268,7 @@ void UDPMessageConnection::HandleFlowControl()
 	AssertInWorkerThreadContext();
 
 	// In packets/second.
-	const float totalEstimatedBandwidth = 50; ///\todo Make this estimation dynamic as in UDT or similar.
+	const float totalEstimatedBandwidth = 100; ///\todo Make this estimation dynamic as in UDT or similar.
 	const float additiveIncreaseAggressiveness = 5e-2f;
 
 	const tick_t frameLength = Clock::TicksPerSec() / 100; // in ticks
@@ -301,9 +301,6 @@ void UDPMessageConnection::HandleFlowControl()
 		else
 			lastFrameTime = Clock::Tick();
 	}
-
-	// Do a fixed flow control for testing.
-	datagramSendRate = 100; ///\todo Remove.
 }
 
 void UDPMessageConnection::SendOutPackets()
@@ -569,7 +566,6 @@ MessageConnection::PacketSendResult UDPMessageConnection::SendOutPacket()
 		const tick_t now = Clock::Tick();
 		ack.sendCount = 1;
 		ack.sentTick = now;
-		retransmissionTimeout = 5000.f; ///\todo Remove this.
 		ack.timeoutTick = now + (tick_t)((double)retransmissionTimeout * Clock::TicksPerMillisecond());
 		ack.datagramSendRate = datagramSendRate;