Explorar o código

[Network] Resurrect additions for master server/client #1269

JimMarlowe %!s(int64=9) %!d(string=hai) anos
pai
achega
a205a770d0

+ 79 - 0
Source/Atomic/Network/Connection.cpp

@@ -212,6 +212,10 @@ void Connection::SetScene(Scene* newScene)
         scene_->StopAsyncLoading();
         scene_->StopAsyncLoading();
         SubscribeToEvent(scene_, E_ASYNCLOADFINISHED, ATOMIC_HANDLER(Connection, HandleAsyncLoadFinished));
         SubscribeToEvent(scene_, E_ASYNCLOADFINISHED, ATOMIC_HANDLER(Connection, HandleAsyncLoadFinished));
     }
     }
+
+    // ATOMIC BEGIN
+    SubscribeToEvent(scene_, E_COMPONENTREMOVED, ATOMIC_HANDLER(Connection, HandleComponentRemoved));
+    // ATOMIC END
 }
 }
 
 
 void Connection::SetIdentity(const VariantMap& identity)
 void Connection::SetIdentity(const VariantMap& identity)
@@ -450,6 +454,12 @@ bool Connection::ProcessMessage(int msgID, MemoryBuffer& msg)
         ProcessPackageInfo(msgID, msg);
         ProcessPackageInfo(msgID, msg);
         break;
         break;
 
 
+    // ATOMIC BEGIN
+    case MSG_STRING:
+        ProcessStringMessage(msgID, msg);
+        break;
+    // ATOMIC END
+
     default:
     default:
         processed = false;
         processed = false;
         break;
         break;
@@ -1104,6 +1114,29 @@ void Connection::ConfigureNetworkSimulator(int latencyMs, float packetLoss)
     }
     }
 }
 }
 
 
+// ATOMIC BEGIN
+void Connection::HandleComponentRemoved(StringHash eventType, VariantMap& eventData)
+{
+    using namespace ComponentRemoved;
+
+    Component* comp = static_cast<Component*>(eventData[P_COMPONENT].GetPtr());
+    Node* node = static_cast<Node*>(eventData[P_NODE].GetPtr());
+
+    unsigned nodeId = node->GetID();
+    unsigned compId = comp->GetID();
+
+    if (sceneState_.nodeStates_.Contains(node->GetID()))
+    {
+        NodeReplicationState& nodeState = sceneState_.nodeStates_[node->GetID()];
+        if (nodeState.componentStates_.Contains(comp->GetID()))
+        {
+            ComponentReplicationState& compState = nodeState.componentStates_[comp->GetID()];
+            compState.component_ = NULL;
+        }
+    }
+}
+// ATOMIC END
+
 void Connection::HandleAsyncLoadFinished(StringHash eventType, VariantMap& eventData)
 void Connection::HandleAsyncLoadFinished(StringHash eventType, VariantMap& eventData)
 {
 {
     sceneLoaded_ = true;
     sceneLoaded_ = true;
@@ -1558,4 +1591,50 @@ void Connection::ProcessPackageInfo(int msgID, MemoryBuffer& msg)
     RequestNeededPackages(1, msg);
     RequestNeededPackages(1, msg);
 }
 }
 
 
+// ATOMIC BEGIN
+
+// Expose control methods for current controls
+void Connection::SetControlButtons(unsigned buttons, bool down)
+{
+    controls_.Set(buttons,down);
+}
+
+/// Check if a button is held down.
+bool Connection::IsControlButtonDown(unsigned button) const
+{
+    return (controls_.IsDown(button));
+}
+
+void Connection::SetControlDataInt(const String &key, int value)
+{
+    controls_.extraData_[key] = value;
+}
+
+int Connection::GetControlDataInt(const String &key)
+{
+    return controls_.extraData_[key].GetInt();
+}
+
+void Connection::SendStringMessage(const String& message)
+{
+    // Send the identity map now
+    VectorBuffer msg;
+    msg.WriteString(message);
+    SendMessage(MSG_STRING, true, true, msg);
+}
+
+void Connection::ProcessStringMessage(int msgID, MemoryBuffer &msg) 
+{
+    using namespace NetworkMessage;
+
+    VariantMap &eventData = GetEventDataMap();
+    eventData[P_MESSAGEID] = (int) msgID;
+    eventData[P_CONNECTION] = this;
+    eventData[P_DATA] = msg.ReadString();
+    SendEvent(E_NETWORKSTRINGMESSAGE, eventData);
+}
+
+// ATOMIC END
+
+
 }
 }

+ 25 - 0
Source/Atomic/Network/Connection.h

@@ -234,6 +234,23 @@ public:
     unsigned char timeStamp_;
     unsigned char timeStamp_;
     /// Identity map.
     /// Identity map.
     VariantMap identity_;
     VariantMap identity_;
+    
+// ATOMIC BEGIN
+
+    /// Expose control methods for current controls
+    void SetControlButtons(unsigned buttons, bool down = true);
+
+    /// Check if a button is held down.
+    bool IsControlButtonDown(unsigned button) const;
+
+    void SetControlDataInt(const String& key, int value);
+
+    int GetControlDataInt(const String& key);
+
+    /// Send a message.
+    void SendStringMessage(const String& message);
+    
+// ATOMIC END
 
 
 private:
 private:
     /// Handle scene loaded event.
     /// Handle scene loaded event.
@@ -275,6 +292,14 @@ private:
     /// Handle all packages loaded successfully. Also called directly on MSG_LOADSCENE if there are none.
     /// Handle all packages loaded successfully. Also called directly on MSG_LOADSCENE if there are none.
     void OnPackagesReady();
     void OnPackagesReady();
 
 
+// ATOMIC BEGIN
+
+    void ProcessStringMessage(int msgID, MemoryBuffer& msg);
+
+    void HandleComponentRemoved(StringHash eventType, VariantMap& eventData);
+
+// ATOMIC END
+
     /// kNet message connection.
     /// kNet message connection.
     kNet::SharedPtr<kNet::MessageConnection> connection_;
     kNet::SharedPtr<kNet::MessageConnection> connection_;
     /// Scene.
     /// Scene.

+ 1 - 0
Source/Atomic/Network/Network.cpp

@@ -40,6 +40,7 @@
 
 
 // ATOMIC BEGIN
 // ATOMIC BEGIN
 #include <kNet/include/kNet.h>
 #include <kNet/include/kNet.h>
+#include <kNet/include/kNet/EndPoint.h>
 // ATOMIC END
 // ATOMIC END
 
 
 #include "../DebugNew.h"
 #include "../DebugNew.h"

+ 2 - 0
Source/Atomic/Network/Network.h

@@ -185,6 +185,8 @@ private:
     String packageCacheDir_;
     String packageCacheDir_;
 
 
     // ATOMIC BEGIN
     // ATOMIC BEGIN
+    
+    void HandleClientConnected(StringHash eventType, VariantMap& eventData);
 
 
     kNet::Network* GetKnetNetwork() { return network_; }
     kNet::Network* GetKnetNetwork() { return network_; }
 
 

+ 7 - 0
Source/Atomic/Network/NetworkEvents.h

@@ -115,6 +115,13 @@ ATOMIC_EVENT(E_MASTERMESSAGE, MasterServerMessage)
     ATOMIC_PARAM(P_DATA, Data);                    // Buffer
     ATOMIC_PARAM(P_DATA, Data);                    // Buffer
 }
 }
 
 
+/// Unhandled master message received.
+ATOMIC_EVENT(E_NETWORKSTRINGMESSAGE, NetworkStringMessage)
+{
+    ATOMIC_PARAM(P_CONNECTION, Connection);      // Connection pointer
+    ATOMIC_PARAM(P_DATA, Data);                  // Buffer
+}
+
 // ATOMIC END
 // ATOMIC END
 
 
 }
 }

+ 7 - 0
Source/Atomic/Network/Protocol.h

@@ -64,6 +64,13 @@ static const int MSG_REMOTENODEEVENT = 0x15;
 /// Server->client: info about package.
 /// Server->client: info about package.
 static const int MSG_PACKAGEINFO = 0x16;
 static const int MSG_PACKAGEINFO = 0x16;
 
 
+// ATOMIC BEGIN
+
+// Server->client, Client->server: string message
+static const int MSG_STRING = 0x17;
+
+// ATOMIC END
+
 /// Fixed content ID for client controls update.
 /// Fixed content ID for client controls update.
 static const unsigned CONTROLS_CONTENT_ID = 1;
 static const unsigned CONTROLS_CONTENT_ID = 1;
 /// Package file fragment size.
 /// Package file fragment size.