void Chat::HandleNetworkMessage(StringHash eventType, VariantMap& eventData) { Network* network = GetSubsystem<Network>(); using namespace NetworkMessage; int msgID = eventData[P_MESSAGEID].GetInt(); if (msgID == MSG_CHAT) { const PODVector<unsigned char>& data = eventData[P_DATA].GetBuffer(); // Use a MemoryBuffer to read the message data so that there is no unnecessary copying MemoryBuffer msg(data); String text = msg.ReadString(); // If we are the server, prepend the sender's IP address and port and echo to everyone // If we are a client, just display the message if (network->IsServerRunning()) { Connection* sender = static_cast<Connection*>(eventData[P_CONNECTION].GetPtr()); text = sender->ToString() + " " + text; VectorBuffer sendMsg; sendMsg.WriteString(text); // Broadcast as in-order and reliable network->BroadcastMessage(MSG_CHAT, true, true, sendMsg); } ShowChatText(text); } }
bool Node::Save(Serializer& dest) { // Write node ID if (!dest.WriteUInt(id_)) return false; // Write attributes if (!Serializable::Save(dest)) return false; // Write components dest.WriteVLE(components_.Size()); for (unsigned i = 0; i < components_.Size(); ++i) { Component* component = components_[i]; // Create a separate buffer to be able to skip unknown components during deserialization VectorBuffer compBuffer; if (!component->Save(compBuffer)) return false; dest.WriteVLE(compBuffer.GetSize()); dest.Write(compBuffer.GetData(), compBuffer.GetSize()); } // Write child nodes dest.WriteVLE(children_.Size()); for (unsigned i = 0; i < children_.Size(); ++i) { Node* node = children_[i]; if (!node->Save(dest)) return false; } return true; }
// // write_node // void ClientSidePrediction::write_node(VectorBuffer& message, Node& node) { // Write node ID message.WriteUInt(node.GetID()); // Write attributes write_network_attributes(node, message); // Write user variables const auto& vars = node.GetVars(); message.WriteVLE(vars.Size()); for (auto i = vars.Begin(); i != vars.End(); ++i) { message.WriteStringHash(i->first_); message.WriteVariant(i->second_); } // Write number of components message.WriteVLE(node.GetNumComponents()); // Write components const auto& components = node.GetComponents(); for (unsigned i = 0; i < components.Size(); ++i) { auto component = components[i]; write_component(message, *component); } }
String XMLFile::ToString(const String& indentation) const { VectorBuffer dest; XMLWriter writer(dest); document_->save(writer, indentation.CString()); return String((const char*)dest.GetData(), dest.GetSize()); }
VectorBuffer DecompressVectorBuffer(VectorBuffer& src) { VectorBuffer ret; src.Seek(0); DecompressStream(ret, src); ret.Seek(0); return ret; }
// // write_component // void ClientSidePrediction::write_component(VectorBuffer& message, Component& component) { // Write ID message.WriteUInt(component.GetID()); // Write type message.WriteStringHash(component.GetType()); // Write attributes write_network_attributes(component, message); }
std::vector<unsigned char> CollisionChain2D::GetVerticesAttr() const { VectorBuffer ret; for (unsigned i = 0; i < vertices_.size(); ++i) ret.WriteVector2(vertices_[i]); return ret.GetBuffer(); }
PODVector<unsigned char> CrowdAgent::GetAgentDataAttr() const { const dtCrowdAgent* agent = GetDetourCrowdAgent(); if (!agent) return Variant::emptyBuffer; // Reading it back in isn't this simple, see SetAgentDataAttr VectorBuffer ret; ret.Write(agent, sizeof(dtCrowdAgent)); return ret.GetBuffer(); }
PODVector<unsigned char> ScriptInstance::GetScriptDataAttr() const { if (!scriptObject_ || !methods_[METHOD_SAVE]) return PODVector<unsigned char>(); else { VectorBuffer buf; VariantVector parameters; parameters.Push(Variant((void*)static_cast<Serializer*>(&buf))); scriptFile_->Execute(scriptObject_, methods_[METHOD_SAVE], parameters); return buf.GetBuffer(); } }
PODVector<unsigned char> CrowdAgent::GetAgentDataAttr() const { if (!inCrowd_ || !crowdManager_ || !IsEnabled()) return Variant::emptyBuffer; dtCrowd* crowd = crowdManager_->GetCrowd(); const dtCrowdAgent* agent = crowd->getAgent(agentCrowdId_); // Reading it back in isn't this simple, see SetAgentDataAttr VectorBuffer ret; ret.Write(agent, sizeof(dtCrowdAgent)); return ret.GetBuffer(); }
void Network::OnServerConnected() { serverConnection_->SetConnectPending(false); LOGINFO("Connected to server"); // Send the identity map now VectorBuffer msg; msg.WriteVariantMap(serverConnection_->GetIdentity()); serverConnection_->SendMessage(MSG_IDENTITY, true, true, msg); SendEvent(E_SERVERCONNECTED); }
// // write_scene_state // void ClientSidePrediction::write_scene_state(VectorBuffer& message, Scene* scene) { // Write placeholder last input ID, which will be set per connection before sending message.WriteUInt(0); auto& nodes = scene_nodes[scene]; // Write number of nodes message.WriteVLE(nodes.size()); // Write nodes for (auto node : nodes) write_node(message, *node); }
void Material::RefreshShaderParameterHash() { VectorBuffer temp; for (HashMap<StringHash, MaterialShaderParameter>::ConstIterator i = shaderParameters_.Begin(); i != shaderParameters_.End(); ++i) { temp.WriteStringHash(i->first_); temp.WriteVariant(i->second_.value_); } shaderParameterHash_ = 0; const unsigned char* data = temp.GetData(); unsigned dataSize = temp.GetSize(); for (unsigned i = 0; i < dataSize; ++i) shaderParameterHash_ = SDBMHash(shaderParameterHash_, data[i]); }
bool Variant::operator ==(const VectorBuffer& rhs) const { const PODVector<unsigned char>& buffer = *(reinterpret_cast<const PODVector<unsigned char>*>(&value_)); return type_ == VAR_BUFFER && buffer.Size() == rhs.GetSize() ? strncmp(reinterpret_cast<const char*>(&buffer[0]), reinterpret_cast<const char*>(rhs.GetData()), buffer.Size()) == 0 : false; }
PODVector<unsigned char> LuaScriptInstance::GetScriptNetworkDataAttr() const { if (scriptObjectRef_ == LUA_REFNIL) return PODVector<unsigned char>(); VectorBuffer buf; WeakPtr<LuaFunction> function = scriptObjectMethods_[LSOM_WRITENETWORKUPDATE]; if (function && function->BeginCall(this)) { function->PushUserType((Serializer&)buf, "Serializer"); function->EndCall(); } return buf.GetBuffer(); }
void Chat::HandleSend(StringHash eventType, VariantMap& eventData) { String text = textEdit_->GetText(); if (text.Empty()) return; // Do not send an empty message Network* network = GetSubsystem<Network>(); Connection* serverConnection = network->GetServerConnection(); if (serverConnection) { // A VectorBuffer object is convenient for constructing a message to send VectorBuffer msg; msg.WriteString(text); // Send the chat message as in-order and reliable serverConnection->SendMessage(MSG_CHAT, true, true, msg); // Empty the text edit after sending textEdit_->SetText(String::EMPTY); } }
PODVector<unsigned char> ScriptInstance::GetDelayedMethodCallsAttr() const { VectorBuffer buf; buf.WriteVLE(delayedMethodCalls_.Size()); for (Vector<DelayedMethodCall>::ConstIterator i = delayedMethodCalls_.Begin(); i != delayedMethodCalls_.End(); ++i) { buf.WriteFloat(i->period_); buf.WriteFloat(i->delay_); buf.WriteBool(i->repeat_); buf.WriteString(i->declaration_); buf.WriteVariantVector(i->parameters_); } return buf.GetBuffer(); }
bool Node::Save(Serializer& dest) const { // Write node ID if (!dest.WriteUInt(id_)) return false; // Write attributes if (!Animatable::Save(dest)) return false; // Write components dest.WriteVLE(GetNumPersistentComponents()); for (unsigned i = 0; i < components_.Size(); ++i) { Component* component = components_[i]; if (component->IsTemporary()) continue; // Create a separate buffer to be able to skip failing components during deserialization VectorBuffer compBuffer; if (!component->Save(compBuffer)) return false; dest.WriteVLE(compBuffer.GetSize()); dest.Write(compBuffer.GetData(), compBuffer.GetSize()); } // Write child nodes dest.WriteVLE(GetNumPersistentChildren()); for (unsigned i = 0; i < children_.Size(); ++i) { Node* node = children_[i]; if (node->IsTemporary()) continue; if (!node->Save(dest)) return false; } return true; }
void GameEconomicServerClientConsole::SendMessage(String Message) { /// If sending message is empty if (Message.Empty()) { return; // Do not send an empty message } /// Get connection Network* network = GetSubsystem<Network>(); Connection* serverConnection = network->GetServerConnection(); /// Send message only if serverconnection and connection is true if (serverConnection&&serverconnection) { // A VectorBuffer object is convenient for constructing a message to send VectorBuffer msg; msg.WriteString(Message); // Send the chat message as in-order and reliable serverConnection->SendMessage(NetMessageAdminClientSend, true, true, msg); } return; }
void PvPGM::RequestMechanicExecution(Node* receiver, VectorBuffer& mechanicParams) { const PODVector<unsigned char>& data = mechanicParams.GetBuffer(); MemoryBuffer msg(data); for (int x = 0; x < players_.Size(); x++) { if (players_[x]->player_ == receiver) { ((GameMechanic*)(players_[x]))->message_ = &msg; ((GameMechanic*)(players_[x]))->clientID_ = ((GameMechanic*)(players_[x]))->clientIDSelf_; ((GameMechanic*)(players_[x]))->lagTime_ = 0.0f; players_[x]->RequestMechanicExecute(); break; } } }
PODVector<unsigned char> DynamicNavigationMesh::GetNavigationDataAttr() const { VectorBuffer ret; if (navMesh_ && tileCache_) { ret.WriteBoundingBox(boundingBox_); ret.WriteInt(numTilesX_); ret.WriteInt(numTilesZ_); const dtNavMeshParams* params = navMesh_->getParams(); ret.Write(params, sizeof(dtNavMeshParams)); const dtTileCacheParams* tcParams = tileCache_->getParams(); ret.Write(tcParams, sizeof(dtTileCacheParams)); for (int z = 0; z < numTilesZ_; ++z) { for (int x = 0; x < numTilesX_; ++x) { dtCompressedTileRef tiles[TILECACHE_MAXLAYERS]; const int ct = tileCache_->getTilesAt(x, z, tiles, TILECACHE_MAXLAYERS); for (int i = 0; i < ct; ++i) { const dtCompressedTile* tile = tileCache_->getTileByRef(tiles[i]); if (!tile || !tile->header || !tile->dataSize) continue; // Don't write "void-space" tiles // The header conveniently has the majority of the information required ret.Write(tile->header, sizeof(dtTileCacheLayerHeader)); ret.WriteInt(tile->dataSize); ret.Write(tile->data, tile->dataSize); } } } } return ret.GetBuffer(); }
PODVector<unsigned char> DynamicNavigationMesh::GetNavigationDataAttr() const { VectorBuffer ret; if (navMesh_ && tileCache_) { ret.WriteBoundingBox(boundingBox_); ret.WriteInt(numTilesX_); ret.WriteInt(numTilesZ_); const dtNavMeshParams* params = navMesh_->getParams(); ret.Write(params, sizeof(dtNavMeshParams)); const dtTileCacheParams* tcParams = tileCache_->getParams(); ret.Write(tcParams, sizeof(dtTileCacheParams)); for (int z = 0; z < numTilesZ_; ++z) for (int x = 0; x < numTilesX_; ++x) WriteTiles(ret, x, z); } return ret.GetBuffer(); }
PODVector<unsigned char> DynamicNavigationMesh::GetTileData(const IntVector2& tile) const { VectorBuffer ret; WriteTiles(ret, tile.x_, tile.y_); return ret.GetBuffer(); }
PODVector<unsigned char> CustomGeometry::GetGeometryDataAttr() const { VectorBuffer ret; ret.WriteVLE(geometries_.Size()); ret.WriteUInt(elementMask_); for (unsigned i = 0; i < geometries_.Size(); ++i) { unsigned numVertices = vertices_[i].Size(); ret.WriteVLE(numVertices); ret.WriteUByte(primitiveTypes_[i]); for (unsigned j = 0; j < numVertices; ++j) { if (elementMask_ & MASK_POSITION) ret.WriteVector3(vertices_[i][j].position_); if (elementMask_ & MASK_NORMAL) ret.WriteVector3(vertices_[i][j].normal_); if (elementMask_ & MASK_COLOR) ret.WriteUInt(vertices_[i][j].color_); if (elementMask_ & MASK_TEXCOORD1) ret.WriteVector2(vertices_[i][j].texCoord_); if (elementMask_ & MASK_TANGENT) ret.WriteVector4(vertices_[i][j].tangent_); } } return ret.GetBuffer(); }
PODVector<unsigned char> DecalSet::GetDecalsAttr() const { VectorBuffer ret; ret.WriteBool(skinned_); ret.WriteVLE(decals_.Size()); for (List<Decal>::ConstIterator i = decals_.Begin(); i != decals_.End(); ++i) { ret.WriteFloat(i->timer_); ret.WriteFloat(i->timeToLive_); ret.WriteVLE(i->vertices_.Size()); ret.WriteVLE(i->indices_.Size()); for (PODVector<DecalVertex>::ConstIterator j = i->vertices_.Begin(); j != i->vertices_.End(); ++j) { ret.WriteVector3(j->position_); ret.WriteVector3(j->normal_); ret.WriteVector2(j->texCoord_); ret.WriteVector4(j->tangent_); if (skinned_) { for (unsigned k = 0; k < 4; ++k) ret.WriteFloat(j->blendWeights_[k]); for (unsigned k = 0; k < 4; ++k) ret.WriteUByte(j->blendIndices_[k]); } } for (PODVector<unsigned short>::ConstIterator j = i->indices_.Begin(); j != i->indices_.End(); ++j) ret.WriteUShort(*j); } if (skinned_) { ret.WriteVLE(bones_.Size()); for (Vector<Bone>::ConstIterator i = bones_.Begin(); i != bones_.End(); ++i) { ret.WriteString(i->name_); ret.WriteUByte(i->collisionMask_); if (i->collisionMask_ & BONECOLLISION_SPHERE) ret.WriteFloat(i->radius_); if (i->collisionMask_ & BONECOLLISION_BOX) ret.WriteBoundingBox(i->boundingBox_); ret.Write(i->offsetMatrix_.Data(), sizeof(Matrix3x4)); } } return ret.GetBuffer(); }
PODVector<unsigned char> NavigationMesh::GetNavigationDataAttr() const { VectorBuffer ret; if (navMesh_) { ret.WriteBoundingBox(boundingBox_); ret.WriteInt(numTilesX_); ret.WriteInt(numTilesZ_); const dtNavMeshParams* params = navMesh_->getParams(); ret.WriteFloat(params->tileWidth); ret.WriteFloat(params->tileHeight); ret.WriteInt(params->maxTiles); ret.WriteInt(params->maxPolys); const dtNavMesh* navMesh = navMesh_; for (int z = 0; z < numTilesZ_; ++z) { for (int x = 0; x < numTilesX_; ++x) { const dtMeshTile* tile = navMesh->getTileAt(x, z, 0); if (!tile) continue; ret.WriteInt(x); ret.WriteInt(z); ret.WriteUInt(navMesh->getTileRef(tile)); ret.WriteUInt((unsigned)tile->dataSize); ret.Write(tile->data, (unsigned)tile->dataSize); } } } return ret.GetBuffer(); }
void Network::BroadcastMessage(int msgID, bool reliable, bool inOrder, const VectorBuffer& msg, unsigned contentID) { BroadcastMessage(msgID, reliable, inOrder, msg.GetData(), msg.GetSize(), contentID); }