blender/source/gameengine/Network/NG_NetworkScene.cpp

272 lines
7.7 KiB
C++
Raw Normal View History

/*
* ***** BEGIN GPL LICENSE BLOCK *****
2002-10-12 11:37:38 +00:00
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
2002-10-12 11:37:38 +00:00
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
2010-02-12 13:34:04 +00:00
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2002-10-12 11:37:38 +00:00
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL LICENSE BLOCK *****
2002-10-12 11:37:38 +00:00
* NetworkSceneManagement generic implementation
*/
2011-02-25 13:36:49 +00:00
/** \file gameengine/Network/NG_NetworkScene.cpp
* \ingroup bgenet
*/
2002-10-12 11:37:38 +00:00
#include <stdio.h>
#include <MT_assert.h>
2002-10-12 11:37:38 +00:00
#include <algorithm>
#include "NG_NetworkScene.h"
#include "NG_NetworkDeviceInterface.h"
#include "NG_NetworkMessage.h"
#include "NG_NetworkObject.h"
NG_NetworkScene::NG_NetworkScene(NG_NetworkDeviceInterface* nic)
{
m_networkdevice = nic;
}
NG_NetworkScene::~NG_NetworkScene()
{
ClearAllMessageMaps();
}
/**
* progress one frame, handle all network traffic
*/
void NG_NetworkScene::proceed(double curtime)
2002-10-12 11:37:38 +00:00
{
if (!m_networkdevice) return;
if (!m_networkdevice->IsOnline()) return;
ClearAllMessageMaps();
// read all NetworkMessages from the device
vector<NG_NetworkMessage*> messages =
m_networkdevice->RetrieveNetworkMessages();
vector<NG_NetworkMessage*>::iterator mesit = messages.begin();
for (; !(mesit == messages.end()); mesit++) {
NG_NetworkMessage* message = (*mesit);
vector<NG_NetworkMessage*>* tmplist=NULL;
vector<NG_NetworkMessage*>** tmplistptr =
m_messagesByDestinationName[message->GetDestinationName()];
// if there is already a vector of messages, append, else create
// a new vector and insert into map
if (!tmplistptr) {
tmplist = new vector<NG_NetworkMessage*>;
m_messagesByDestinationName.insert(message->GetDestinationName(),
tmplist);
} else {
tmplist = *tmplistptr;
}
message->AddRef();
tmplist->push_back(message);
tmplist = NULL;
tmplistptr = m_messagesBySenderName[message->GetSenderName()];
// if there is already a vector of messages, append, else create
// a new vector and insert into map
if (!tmplistptr) {
tmplist = new vector<NG_NetworkMessage*>;
m_messagesBySenderName.insert(message->GetSenderName(), tmplist);
} else {
tmplist = *tmplistptr;
}
message->AddRef();
tmplist->push_back(message);
tmplist = NULL;
tmplistptr = m_messagesBySubject[message->GetSubject()];
// if there is already a vector of messages, append, else create
// a new vector and insert into map
if (!tmplistptr) {
tmplist = new vector<NG_NetworkMessage*>;
m_messagesBySubject.insert(message->GetSubject(), tmplist);
} else {
tmplist = *tmplistptr;
}
message->AddRef();
tmplist->push_back(message);
tmplist = NULL;
}
}
/**
* add a network object to the network scene
*/
void NG_NetworkScene::AddObject(NG_NetworkObject* object)
{
if (! m_networkdevice->IsOnline()) return;
BGE performance, 4th round: logic This commit extends the technique of dynamic linked list to the logic system to eliminate as much as possible temporaries, map lookup or full scan. The logic engine is now free of memory allocation, which is an important stability factor. The overhead of the logic system is reduced by a factor between 3 and 6 depending on the logic setup. This is the speed-up you can expect on a logic setup using simple bricks. Heavy bricks like python controllers and ray sensors will still take about the same time to execute so the speed up will be less important. The core of the logic engine has been much reworked but the functionality is still the same except for one thing: the priority system on the execution of controllers. The exact same remark applies to actuators but I'll explain for controllers only: Previously, it was possible, with the "executePriority" attribute to set a controller to run before any other controllers in the game. Other than that, the sequential execution of controllers, as defined in Blender was guaranteed by default. With the new system, the sequential execution of controllers is still guaranteed but only within the controllers of one object. the user can no longer set a controller to run before any other controllers in the game. The "executePriority" attribute controls the execution of controllers within one object. The priority is a small number starting from 0 for the first controller and incrementing for each controller. If this missing feature is a must, a special method can be implemented to set a controller to run before all other controllers. Other improvements: - Systematic use of reference in parameter passing to avoid unnecessary data copy - Use pre increment in iterator instead of post increment to avoid temporary allocation - Use const char* instead of STR_String whenever possible to avoid temporary allocation - Fix reference counting bugs (memory leak) - Fix a crash in certain cases of state switching and object deletion - Minor speed up in property sensor - Removal of objects during the game is a lot faster
2009-05-10 20:53:58 +00:00
const STR_String& name = object->GetName();
2002-10-12 11:37:38 +00:00
m_networkObjects.insert(name, object);
}
/**
* remove a network object from the network scene
*/
void NG_NetworkScene::RemoveObject(NG_NetworkObject* object)
{
if (! m_networkdevice->IsOnline()) return;
BGE performance, 4th round: logic This commit extends the technique of dynamic linked list to the logic system to eliminate as much as possible temporaries, map lookup or full scan. The logic engine is now free of memory allocation, which is an important stability factor. The overhead of the logic system is reduced by a factor between 3 and 6 depending on the logic setup. This is the speed-up you can expect on a logic setup using simple bricks. Heavy bricks like python controllers and ray sensors will still take about the same time to execute so the speed up will be less important. The core of the logic engine has been much reworked but the functionality is still the same except for one thing: the priority system on the execution of controllers. The exact same remark applies to actuators but I'll explain for controllers only: Previously, it was possible, with the "executePriority" attribute to set a controller to run before any other controllers in the game. Other than that, the sequential execution of controllers, as defined in Blender was guaranteed by default. With the new system, the sequential execution of controllers is still guaranteed but only within the controllers of one object. the user can no longer set a controller to run before any other controllers in the game. The "executePriority" attribute controls the execution of controllers within one object. The priority is a small number starting from 0 for the first controller and incrementing for each controller. If this missing feature is a must, a special method can be implemented to set a controller to run before all other controllers. Other improvements: - Systematic use of reference in parameter passing to avoid unnecessary data copy - Use pre increment in iterator instead of post increment to avoid temporary allocation - Use const char* instead of STR_String whenever possible to avoid temporary allocation - Fix reference counting bugs (memory leak) - Fix a crash in certain cases of state switching and object deletion - Minor speed up in property sensor - Removal of objects during the game is a lot faster
2009-05-10 20:53:58 +00:00
const STR_String& name = object->GetName();
2002-10-12 11:37:38 +00:00
m_networkObjects.remove(name);
}
/**
* remove all network scene objects at once
*/
void NG_NetworkScene::RemoveAllObjects()
{
m_networkObjects.clear();
}
/**
* get a single network object given its name
*/
NG_NetworkObject* NG_NetworkScene::FindNetworkObject(const STR_String& objname) {
NG_NetworkObject *nwobj = NULL;
if (! m_networkdevice->IsOnline()) return nwobj;
NG_NetworkObject **nwobjptr = m_networkObjects[objname];
if (nwobjptr) {
nwobj = *nwobjptr;
}
return nwobj;
}
bool NG_NetworkScene::ConstraintsAreValid(
const STR_String& from,
const STR_String& subject,
NG_NetworkMessage* message)
{
vector<NG_NetworkMessage*>** fromlistptr = m_messagesBySenderName[from];
vector<NG_NetworkMessage*>** subjectlistptr = m_messagesBySubject[subject];
vector<NG_NetworkMessage*>* fromlist = (fromlistptr ? *fromlistptr : NULL);
vector<NG_NetworkMessage*>* subjectlist = (subjectlistptr ? *subjectlistptr : NULL);
return (
( from.IsEmpty() || (!fromlist ? false : (!(std::find(fromlist->begin(), fromlist->end(), message) == fromlist->end())))
) &&
( subject.IsEmpty() || (!subjectlist ? false : (!(std::find(subjectlist->begin(), subjectlist->end(), message) == subjectlist->end())))
));
}
vector<NG_NetworkMessage*> NG_NetworkScene::FindMessages(
const STR_String& to,
const STR_String& from,
const STR_String& subject,
bool spamallowed)
{
vector<NG_NetworkMessage*> foundmessages;
bool notfound = false;
// broad phase
notfound = ((to.IsEmpty() || spamallowed) ? notfound : m_messagesByDestinationName[to] == NULL);
if (!notfound)
notfound = (from.IsEmpty() ? notfound : m_messagesBySenderName[from] == NULL);
if (!notfound)
notfound = (subject.IsEmpty() ? notfound : m_messagesBySubject[subject] == NULL);
if (notfound) {
// it's definitely NOT in the scene, so stop looking
2002-10-12 11:37:38 +00:00
} else { // narrow phase
// possibly it's there, but maybe not (false hit)
if (to.IsEmpty()) {
// take all messages, and check other fields
MT_assert(!"objectnames that are empty are not valid, so make it a hobby project :)\n");
2002-10-12 11:37:38 +00:00
} else {
//todo: find intersection of messages (that are in other 2 maps)
vector<NG_NetworkMessage*>** tolistptr = m_messagesByDestinationName[to];
if (tolistptr) {
vector<NG_NetworkMessage*>* tolist = *tolistptr;
vector<NG_NetworkMessage*>::iterator listit;
for (listit=tolist->begin();!(listit==tolist->end());listit++) {
NG_NetworkMessage* message = *listit;
if (ConstraintsAreValid(from, subject, message)) {
message->AddRef();
foundmessages.push_back(message);
}
}
}
// TODO find intersection of messages (that are in other 2 maps)
if (spamallowed) {
tolistptr = m_messagesByDestinationName[""];
if (tolistptr) {
vector<NG_NetworkMessage*>* tolist = *tolistptr;
vector<NG_NetworkMessage*>::iterator listit;
for (listit=tolist->begin();!(listit==tolist->end());listit++) {
NG_NetworkMessage* message = *listit;
if (ConstraintsAreValid(from, subject, message)) {
message->AddRef();
foundmessages.push_back(message);
}
}
}
}
}
}
return foundmessages;
}
void NG_NetworkScene::SendMessage(
const STR_String& to,
const STR_String& from,
const STR_String& subject,
const STR_String& message)
{
NG_NetworkMessage* msg = new NG_NetworkMessage(to, from, subject, message);
m_networkdevice->SendNetworkMessage(msg);
msg->Release();
}
void NG_NetworkScene::ClearAllMessageMaps(void)
{
ClearMessageMap(m_messagesByDestinationName);
ClearMessageMap(m_messagesBySenderName);
ClearMessageMap(m_messagesBySubject);
}
void NG_NetworkScene::ClearMessageMap(TMessageMap& map)
{
// Release the messages in the map
for (int i = 0; i < map.size(); i++) {
vector<NG_NetworkMessage*>* msglist;
msglist = *(map.at(i));
// Iterate through the current vector and release all it's messages
vector<NG_NetworkMessage*>::iterator msgit;
for (msgit = msglist->begin(); msgit != msglist->end(); msgit++) {
(*msgit)->Release();
}
// Delete the actual vector
delete (msglist);
}
// Empty the map
map.clear();
}