From 18e85ab18a30d8b3528984c0b06240451ec50fd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lasse=20=C3=96=C3=B6rni?= Date: Tue, 14 Oct 2014 22:07:01 +0300 Subject: [PATCH] Changed Connection::SendPackageToClient() to not broadcast to all clients. Instead Network::SendPackageToClients() will send to all clients in a specific scene. Updated Network Lua bindings. --- .../LuaScript/pkgs/Network/Connection.pkg | 7 +------ .../Engine/LuaScript/pkgs/Network/Network.pkg | 3 ++- Source/Engine/Network/Connection.cpp | 14 ++++++++++--- Source/Engine/Network/Connection.h | 4 ++-- Source/Engine/Network/Network.cpp | 21 +++++++++++++++++++ Source/Engine/Network/Network.h | 2 ++ Source/Engine/Script/NetworkAPI.cpp | 3 ++- 7 files changed, 41 insertions(+), 13 deletions(-) diff --git a/Source/Engine/LuaScript/pkgs/Network/Connection.pkg b/Source/Engine/LuaScript/pkgs/Network/Connection.pkg index d82caa6ec..89d7065de 100644 --- a/Source/Engine/LuaScript/pkgs/Network/Connection.pkg +++ b/Source/Engine/LuaScript/pkgs/Network/Connection.pkg @@ -27,12 +27,7 @@ class Connection : public Object void SetConnectPending(bool connectPending); void SetLogStatistics(bool enable); void Disconnect(int waitMSec = 0); - void SendServerUpdate(); - void SendClientUpdate(); - void SendRemoteEvents(); - void SendPackages(); - void ProcessPendingLatestData(); - bool ProcessMessage(int msgID, MemoryBuffer& msg); + void SendPackageToClient(PackageFile* package); VariantMap& GetIdentity(); Scene* GetScene() const; diff --git a/Source/Engine/LuaScript/pkgs/Network/Network.pkg b/Source/Engine/LuaScript/pkgs/Network/Network.pkg index 19279a502..801370f94 100644 --- a/Source/Engine/LuaScript/pkgs/Network/Network.pkg +++ b/Source/Engine/LuaScript/pkgs/Network/Network.pkg @@ -29,7 +29,8 @@ class Network void UnregisterAllRemoteEvents(); void SetPackageCacheDir(const String path); - + void SendPackageToClients(Scene* scene, PackageFile* package); + // SharedPtr MakeHttpRequest(const String url, const String verb = String::EMPTY, const Vector& headers = Vector(), const String postData = String::EMPTY); tolua_outside HttpRequest* NetworkMakeHttpRequest @ MakeHttpRequest(const String url, const String verb = String::EMPTY, const Vector& headers = Vector(), const String postData = String::EMPTY); diff --git a/Source/Engine/Network/Connection.cpp b/Source/Engine/Network/Connection.cpp index 576fc6bd8..292f807b9 100644 --- a/Source/Engine/Network/Connection.cpp +++ b/Source/Engine/Network/Connection.cpp @@ -1432,21 +1432,29 @@ void Connection::OnPackagesReady() } } -void Connection::SendPackageToClients(PackageFile* package) +void Connection::SendPackageToClient(PackageFile* package) { if (!scene_) return; if (!IsClient()) + { + LOGERROR("SendPackageToClient can be called on the server only"); return; - + } + if (!package) + { + LOGERROR("Null package specified for SendPackageToClient"); + return; + } + msg_.Clear(); String filename = GetFileNameAndExtension(package->GetName()); msg_.WriteString(filename); msg_.WriteUInt(package->GetTotalSize()); msg_.WriteUInt(package->GetChecksum()); - GetSubsystem()->BroadcastMessage(MSG_PACKAGEINFO, true, true, msg_); + SendMessage(MSG_PACKAGEINFO, true, true, msg_); } void Connection::ProcessPackageInfo(int msgID, MemoryBuffer& msg) diff --git a/Source/Engine/Network/Connection.h b/Source/Engine/Network/Connection.h index 5fb23b427..668958099 100644 --- a/Source/Engine/Network/Connection.h +++ b/Source/Engine/Network/Connection.h @@ -183,8 +183,8 @@ public: const String& GetDownloadName() const; /// Return progress of current package download, or 1.0 if no downloads. float GetDownloadProgress() const; - /// Trigger client connections to download a package file from the server. Can be used to download additional resource packages when clients are already joined in a scene. - void SendPackageToClients(PackageFile* package); + /// Trigger client connection to download a package file from the server. Can be used to download additional resource packages when client is already joined in a scene. The package must have been added as a requirement to the scene the client is joined in, or else the eventual download will fail. + void SendPackageToClient(PackageFile* package); /// Current controls. Controls controls_; diff --git a/Source/Engine/Network/Network.cpp b/Source/Engine/Network/Network.cpp index 9f29fb6e5..75c0d6ea8 100644 --- a/Source/Engine/Network/Network.cpp +++ b/Source/Engine/Network/Network.cpp @@ -361,6 +361,27 @@ void Network::SetPackageCacheDir(const String& path) packageCacheDir_ = AddTrailingSlash(path); } +void Network::SendPackageToClients(Scene* scene, PackageFile* package) +{ + if (!scene) + { + LOGERROR("Null scene specified for SendPackageToClients"); + return; + } + if (!package) + { + LOGERROR("Null package specified for SendPackageToClients"); + return; + } + + for (HashMap >::Iterator i = clientConnections_.Begin(); + i != clientConnections_.End(); ++i) + { + if (i->second_->GetScene() == scene) + i->second_->SendPackageToClient(package); + } +} + SharedPtr Network::MakeHttpRequest(const String& url, const String& verb, const Vector& headers, const String& postData) { PROFILE(MakeHttpRequest); diff --git a/Source/Engine/Network/Network.h b/Source/Engine/Network/Network.h index 6732cea27..793b463b5 100644 --- a/Source/Engine/Network/Network.h +++ b/Source/Engine/Network/Network.h @@ -91,6 +91,8 @@ public: void UnregisterAllRemoteEvents(); /// Set the package download cache directory. void SetPackageCacheDir(const String& path); + /// Trigger all client connections in the specified scene to download a package file from the server. Can be used to download additional resource packages when clients are already joined in the scene. The package must have been added as a requirement to the scene, or else the eventual download will fail. + void SendPackageToClients(Scene* scene, PackageFile* package); /// Perform an HTTP request to the specified URL. Empty verb defaults to a GET request. Return a request object which can be used to read the response data. SharedPtr MakeHttpRequest(const String& url, const String& verb = String::EMPTY, const Vector& headers = Vector(), const String& postData = String::EMPTY); diff --git a/Source/Engine/Script/NetworkAPI.cpp b/Source/Engine/Script/NetworkAPI.cpp index eb8443fe1..cce297391 100644 --- a/Source/Engine/Script/NetworkAPI.cpp +++ b/Source/Engine/Script/NetworkAPI.cpp @@ -112,7 +112,7 @@ static void RegisterConnection(asIScriptEngine* engine) engine->RegisterObjectMethod("Connection", "const Vector3& get_position() const", asMETHOD(Connection, GetPosition), asCALL_THISCALL); engine->RegisterObjectMethod("Connection", "void set_rotation(const Quaternion&in)", asMETHOD(Connection, SetRotation), asCALL_THISCALL); engine->RegisterObjectMethod("Connection", "const Quaternion& get_rotation() const", asMETHOD(Connection, GetRotation), asCALL_THISCALL); - engine->RegisterObjectMethod("Connection", "void sendPackageToClients(PackageFile@+)", asMETHOD(Connection, SendPackageToClients), asCALL_THISCALL); + engine->RegisterObjectMethod("Connection", "void SendPackageToClient(PackageFile@+)", asMETHOD(Connection, SendPackageToClient), asCALL_THISCALL); engine->RegisterObjectProperty("Connection", "Controls controls", offsetof(Connection, controls_)); engine->RegisterObjectProperty("Connection", "VariantMap identity", offsetof(Connection, identity_)); @@ -206,6 +206,7 @@ void RegisterNetwork(asIScriptEngine* engine) engine->RegisterObjectMethod("Network", "void UnregisterAllRemoteEvents()", asMETHOD(Network, UnregisterAllRemoteEvents), asCALL_THISCALL); engine->RegisterObjectMethod("Network", "bool CheckRemoteEvent(const String&in) const", asFUNCTION(NetworkCheckRemoteEvent), asCALL_CDECL_OBJLAST); engine->RegisterObjectMethod("Network", "HttpRequest@ MakeHttpRequest(const String&in, const String&in verb = String(), Array@+ headers = null, const String&in postData = String())", asFUNCTION(NetworkMakeHttpRequest), asCALL_CDECL_OBJLAST); + engine->RegisterObjectMethod("Network", "void SendPackageToClients(Scene@+, PackageFile@+)", asMETHOD(Network, SendPackageToClients), asCALL_THISCALL); engine->RegisterObjectMethod("Network", "void set_updateFps(int)", asMETHOD(Network, SetUpdateFps), asCALL_THISCALL); engine->RegisterObjectMethod("Network", "int get_updateFps() const", asMETHOD(Network, GetUpdateFps), asCALL_THISCALL); engine->RegisterObjectMethod("Network", "void set_packageCacheDir(const String&in)", asMETHOD(Network, SetPackageCacheDir), asCALL_THISCALL);