2
0
Эх сурвалжийг харах

Added common script code for parsing network-related command line arguments.

Lasse Öörni 14 жил өмнө
parent
commit
4d0e922e3f

+ 1 - 1
Bin/ChatServer.bat

@@ -1 +1 @@
-Urho3D.exe Scripts/Chat.as -headless %1 %2 %3 %4 %5 %6 %7 %8
+Urho3D.exe Scripts/Chat.as server -headless %1 %2 %3 %4 %5 %6 %7 %8

+ 21 - 15
Bin/Data/Scripts/Chat.as

@@ -1,3 +1,5 @@
+#include "Scripts/Network.as"
+
 Text@ chatHistoryText;
 UIElement@ buttonLayout;
 LineEdit@ textEdit;
@@ -18,15 +20,25 @@ void Start()
     SubscribeToEvent("NetworkMessage", "HandleNetworkMessage");
 
     if (engine.headless)
-    {
         OpenConsoleWindow();
-        HandleStartServer();
-        return;
+    else
+    {
+        InitUI();
+
+        SubscribeToEvent("LogMessage", "HandleLogMessage");
+        SubscribeToEvent("KeyDown", "HandleKeyDown");
+        SubscribeToEvent(textEdit, "TextFinished", "HandleTextFinished");
+        SubscribeToEvent(sendButton, "Pressed", "HandleSend");
+        SubscribeToEvent(connectButton, "Pressed", "HandleConnect");
+        SubscribeToEvent(disconnectButton, "Pressed", "HandleDisconnect");
+        SubscribeToEvent(startServerButton, "Pressed", "HandleStartServer");
     }
-
-    SubscribeToEvent("LogMessage", "HandleLogMessage");
-    SubscribeToEvent("KeyDown", "HandleKeyDown");
-    InitUI();
+    
+    ParseNetworkArguments();
+    if (startServer)
+        network.StartServer(serverPort);
+    if (startClient)
+        network.Connect(serverAddress, serverPort, null);
 }
 
 void InitUI()
@@ -63,12 +75,6 @@ void InitUI()
     disconnectButton = AddUIButton("Disconnect", 100);
     startServerButton = AddUIButton("Start Server", 110);
 
-    SubscribeToEvent(textEdit, "TextFinished", "HandleTextFinished");
-    SubscribeToEvent(sendButton, "Pressed", "HandleSend");
-    SubscribeToEvent(connectButton, "Pressed", "HandleConnect");
-    SubscribeToEvent(disconnectButton, "Pressed", "HandleDisconnect");
-    SubscribeToEvent(startServerButton, "Pressed", "HandleStartServer");
-
     chatHistory.Resize((graphics.height - 20) / chatHistoryText.rowHeight);
 }
 
@@ -136,7 +142,7 @@ void HandleConnect()
     String address = textEdit.text.Trimmed();
     if (!address.empty)
     {
-        network.Connect(address, 1234, null);
+        network.Connect(address, serverPort, null);
         textEdit.text = "";
     }
 }
@@ -149,7 +155,7 @@ void HandleDisconnect()
 void HandleStartServer()
 {
     if (!network.serverRunning)
-        network.StartServer(1234);
+        network.StartServer(serverPort);
 }
 
 void HandleNetworkMessage(StringHash eventType, VariantMap& eventData)

+ 29 - 0
Bin/Data/Scripts/Network.as

@@ -0,0 +1,29 @@
+bool startServer = false;
+bool startClient = false;
+String serverAddress;
+uint16 serverPort = 1234;
+
+void ParseNetworkArguments()
+{
+    uint index = 0;
+
+    for (uint i = 0; i < arguments.length; ++i)
+    {
+        if (arguments[i][0] != '-')
+        {
+            if (arguments[i] == "server")
+            {
+                startServer = true;
+                startClient = false;
+                return;
+            }
+            else if (index > 0) // First parameter is script name, so skip that
+            {
+                startClient = true;
+                serverAddress = arguments[i];
+            }
+            
+            ++index;
+        }
+    }
+}

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

@@ -1,3 +1,5 @@
+#include "Scripts/Network.as"
+
 Scene@ testScene;
 Camera@ camera;
 Node@ cameraNode;
@@ -5,7 +7,6 @@ Node@ cameraNode;
 float yaw = 0.0;
 float pitch = 0.0;
 int drawDebug = 0;
-bool clientMode = false;
 
 void Start()
 {
@@ -27,19 +28,18 @@ void Start()
     SubscribeToEvent("PostRenderUpdate", "HandlePostRenderUpdate");
     SubscribeToEvent("SpawnBox", "HandleSpawnBox");
 
-    for (uint i = 0; i < arguments.length; ++i)
+    network.RegisterRemoteEvent("SpawnBox");
+    
+    ParseNetworkArguments();
+    if (startServer)
     {
-        if (arguments[i] == "server")
-        {
-            network.StartServer(1234);
-            SubscribeToEvent("ClientConnected", "HandleClientConnected");
-        }
-        else if (arguments[i] == "client" && i < arguments.length - 1)
-        {
-            testScene.Clear();
-            network.Connect(arguments[i + 1], 1234, testScene);
-            clientMode = true;
-        }
+        network.StartServer(serverPort);
+        SubscribeToEvent("ClientConnected", "HandleClientConnected");
+    }
+    if (startClient)
+    {
+        testScene.Clear();
+        network.Connect(serverAddress, serverPort, testScene);
     }
 }
 
@@ -354,7 +354,7 @@ void HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
         eventData["Rot"] = cameraNode.rotation;
 
         // If we are the client, send the spawn command as a remote event, else send locally
-        if (clientMode)
+        if (startClient)
         {
             if (network.serverConnection !is null)
                 network.serverConnection.SendRemoteEvent("SpawnBox", true, eventData);

+ 6 - 2
Docs/GettingStarted.dox

@@ -55,6 +55,8 @@ F7          Load scene
 T           Toggle profiling display
 \endverbatim
 
+TestScene also includes a network replication test, where clients can connect, move around as invisible cameras, and create new physics objects. For this, a server needs to be started with the command Urho3D.exe Scripts/TestScene.as server (-headless switch can optionally given so that the server will not open a graphics window) and clients can connect by specifying the server address on the command line, eg. Urho3D.exe Scripts/TestScene.as 127.0.0.1
+
 \section Running_NinjaSnowWar NinjaSnowWar
 
 A third-person action game. To start, run NinjaSnowWar.bat in the Bin directory, or use the command Urho3D.exe Scripts/NinjaSnowWar.as
@@ -75,8 +77,10 @@ F4          Toggle octree debug geometry
 
 Simple client-server chat test application. To start, run Chat.bat or ChatServer.bat in the Bin directory, or use the command Urho3D.exe Scripts/Chat.as
 
-Starting in headless mode (ChatServer.bat) automatically starts the server. On the client, first type the server address to the text edit box and click
-"Connect." After connecting successfully you can start typing messages; either press return or click "Send" to send them. Press ESC to exit.
+On the client, first type the server address to the text edit box and click "Connect." After connecting successfully you can start typing messages; 
+either press return or click "Send" to send them. Press ESC to exit.
+
+To connect automatically, the server address can also be given on the command line, for example Urho3D.exe Scripts/Chat.as 127.0.0.1
 
 \section Running_Commandline Command line options
 

+ 3 - 5
Engine/Network/Connection.cpp

@@ -743,9 +743,6 @@ void Connection::HandleAsyncLoadFinished(StringHash eventType, VariantMap& event
 
 void Connection::ProcessNode(Node* node)
 {
-    if (!node)
-        return;
-    
     processedNodes_.Insert(node);
     
     // Process depended upon nodes first
@@ -753,11 +750,12 @@ void Connection::ProcessNode(Node* node)
     node->GetDependencyNodes(depends);
     for (PODVector<Node*>::ConstIterator i = depends.Begin(); i != depends.End(); ++i)
     {
-        if (!processedNodes_.Contains(*i))
+        // Paranoid null check: a component might supply a null dependency
+        if (*i && !processedNodes_.Contains(*i))
             ProcessNode(*i);
     }
     
-    // Check if the client's scene state already has this node
+    // Check if the client's replication state already has this node
     if (sceneState_.Find(node->GetID()) != sceneState_.End())
         ProcessExistingNode(node);
     else

+ 1 - 1
Engine/Scene/Node.cpp

@@ -894,7 +894,7 @@ bool Node::LoadXML(const XMLElement& source, bool readChildren)
 
 Component* Node::CreateComponent(ShortStringHash type, unsigned id, CreateMode mode)
 {
-    // Make sure the object in question is a component
+    // Check that creation succeeds and that the object in fact is a component
     SharedPtr<Component> newComponent = DynamicCast<Component>(context_->CreateObject(type));
     if (!newComponent)
     {