diff options
Diffstat (limited to 'source/gameengine/Network')
14 files changed, 1315 insertions, 0 deletions
diff --git a/source/gameengine/Network/LoopBackNetwork/Makefile b/source/gameengine/Network/LoopBackNetwork/Makefile new file mode 100644 index 00000000000..19fe362b8f7 --- /dev/null +++ b/source/gameengine/Network/LoopBackNetwork/Makefile @@ -0,0 +1,44 @@ +# +# $Id$ +# +# ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** +# +# 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. The Blender +# Foundation also sells licenses for use in proprietary software under +# the Blender License. See http://www.blender.org/BL/ for information +# about this. +# +# 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, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# 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/BL DUAL LICENSE BLOCK ***** +# +# + +LIBNAME = LoopBackNetwork +DIR = $(OCGDIR)/gameengine/Network/$(LIBNAME) + +include nan_compile.mk + +CCFLAGS += $(LEVEL_1_CPP_WARNINGS) + +CPPFLAGS += -I$(NAN_STRING)/include +CPPFLAGS += -I.. +CPPFLAGS += -I../../../kernel/gen_system + diff --git a/source/gameengine/Network/LoopBackNetwork/NG_LoopBackNetworkDeviceInterface.cpp b/source/gameengine/Network/LoopBackNetwork/NG_LoopBackNetworkDeviceInterface.cpp new file mode 100644 index 00000000000..9d4069bcac6 --- /dev/null +++ b/source/gameengine/Network/LoopBackNetwork/NG_LoopBackNetworkDeviceInterface.cpp @@ -0,0 +1,110 @@ +/* + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * 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. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * 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, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * 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/BL DUAL LICENSE BLOCK ***** + * LoopbackNetworkDeviceInterface derived from NG_NetworkDeviceInterface + */ + +#include "NG_LoopBackNetworkDeviceInterface.h" +#include "NG_NetworkMessage.h" + +// temporary debugging printf's +#ifdef NAN_NET_DEBUG + #include <stdio.h> +#endif + +NG_LoopBackNetworkDeviceInterface::NG_LoopBackNetworkDeviceInterface() +{ + m_currentQueue=0; + Online(); // LoopBackdevices are 'online' immediately +} + +NG_LoopBackNetworkDeviceInterface::~NG_LoopBackNetworkDeviceInterface() +{ +} + +// perhaps this should go to the shared/common implementation too +void NG_LoopBackNetworkDeviceInterface::NextFrame() +{ + // Release reference to the messages while emptying the queue + while (m_messages[m_currentQueue].size() > 0) { + // Should do assert(m_events[0]); + m_messages[m_currentQueue][0]->Release(); + m_messages[m_currentQueue].pop_front(); + } + //m_messages[m_currentQueue].clear(); + + m_currentQueue=1-m_currentQueue; +} + +STR_String NG_LoopBackNetworkDeviceInterface::GetNetworkVersion() +{ + return LOOPBACK_NETWORK_VERSION; +} + +void NG_LoopBackNetworkDeviceInterface::SendNetworkMessage(NG_NetworkMessage* nwmsg) +{ +#ifdef NAN_NET_DEBUG + printf("NG_LBNDI::SendNetworkMessage '%s'->'%s' '%s' '%s'\n", + nwmsg->GetDestinationName().ReadPtr(), + nwmsg->GetSenderName().ReadPtr(), + nwmsg->GetSubject().ReadPtr(), + nwmsg->GetMessageText().ReadPtr()); +#endif + int backqueue = 1-m_currentQueue; + + nwmsg->AddRef(); + m_messages[backqueue].push_back(nwmsg); +} + +vector<NG_NetworkMessage*> NG_LoopBackNetworkDeviceInterface::RetrieveNetworkMessages() +{ + vector<NG_NetworkMessage*> messages; + + std::deque<NG_NetworkMessage*>::iterator mesit=m_messages[m_currentQueue].begin(); + for (; !(mesit == m_messages[m_currentQueue].end());mesit++) + { + + // We don't increase the reference count for these messages. We + // are passing a vector of messages in the interface and not + // explicitily storing the messgaes for long term usage + + messages.push_back(*mesit); + +#ifdef NAN_NET_DEBUG + printf("NG_LBNDI::RetrieveNetworkMessages '%s'->'%s' '%s' '%s'\n", + (*mesit)->GetDestinationName().ReadPtr(), + (*mesit)->GetSenderName().ReadPtr(), + (*mesit)->GetSubject().ReadPtr(), + (*mesit)->GetMessageText().ReadPtr()); +#endif + } + return messages; +} + diff --git a/source/gameengine/Network/LoopBackNetwork/NG_LoopBackNetworkDeviceInterface.h b/source/gameengine/Network/LoopBackNetwork/NG_LoopBackNetworkDeviceInterface.h new file mode 100644 index 00000000000..ad87b232794 --- /dev/null +++ b/source/gameengine/Network/LoopBackNetwork/NG_LoopBackNetworkDeviceInterface.h @@ -0,0 +1,68 @@ +/* + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * 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. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * 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, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * 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/BL DUAL LICENSE BLOCK ***** + * LoopbackNetworkDeviceInterface derived from NG_NetworkDeviceInterface + */ +#ifndef NG_LOOPBACKNETWORKDEVICEINTERFACE_H +#define NG_LOOPBACKNETWORKDEVICEINTERFACE_H + +#include <deque> +#include "NG_NetworkDeviceInterface.h" + +class NG_LoopBackNetworkDeviceInterface : public NG_NetworkDeviceInterface +{ + enum { + LOOPBACK_NETWORK_VERSION=28022001 + }; + + std::deque<NG_NetworkMessage*> m_messages[2]; + int m_currentQueue; + +public: + NG_LoopBackNetworkDeviceInterface(); + virtual ~NG_LoopBackNetworkDeviceInterface(); + + /** + * Clear message buffer + */ + virtual void NextFrame(); + + bool Connect(char *address, unsigned int port, char *password, + unsigned int localport, unsigned int timeout) { + return true;} + bool Disconnect(void) {return true;} + + virtual void SendNetworkMessage(class NG_NetworkMessage* msg); + virtual vector<NG_NetworkMessage*> RetrieveNetworkMessages(); + + STR_String GetNetworkVersion(); +}; + +#endif //NG_LOOPBACKNETWORKDEVICEINTERFACE_H diff --git a/source/gameengine/Network/Makefile b/source/gameengine/Network/Makefile new file mode 100644 index 00000000000..3c5ed35e539 --- /dev/null +++ b/source/gameengine/Network/Makefile @@ -0,0 +1,50 @@ +# +# $Id$ +# +# ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** +# +# 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. The Blender +# Foundation also sells licenses for use in proprietary software under +# the Blender License. See http://www.blender.org/BL/ for information +# about this. +# +# 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, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# 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/BL DUAL LICENSE BLOCK ***** +# +# + +LIBNAME = Network +DIR = $(OCGDIR)/gameengine/$(LIBNAME) + +include nan_compile.mk + +CCFLAGS += $(LEVEL_1_CPP_WARNINGS) + +CPPFLAGS += -I$(NAN_STRING)/include +CPPFLAGS += -I../GameLogic +CPPFLAGS += -I../../kernel/gen_system + +############## + +DIRS = LoopBackNetwork +SOURCEDIR = source/gameengine/Network + +include nan_subdirs.mk diff --git a/source/gameengine/Network/NG_NetworkDeviceInterface.h b/source/gameengine/Network/NG_NetworkDeviceInterface.h new file mode 100644 index 00000000000..f6f8b0e8631 --- /dev/null +++ b/source/gameengine/Network/NG_NetworkDeviceInterface.h @@ -0,0 +1,85 @@ +/* + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * 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. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * 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, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * 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/BL DUAL LICENSE BLOCK ***** + * NetworkGameengine_NetworkDeviceInterface + * Functions like (de)initialize network, get library version + * To be derived by loopback and network libraries + */ +#ifndef NG_NETWORKDEVICEINTERFACE_H +#define NG_NETWORKDEVICEINTERFACE_H + +#include "NG_NetworkMessage.h" + +#include <vector> + +class NG_NetworkDeviceInterface +{ +private: + // candidates for shared/common implementation class + bool m_online; +public: + NG_NetworkDeviceInterface() {}; + virtual ~NG_NetworkDeviceInterface() {}; + + virtual void NextFrame()=0; + + /** + * Mark network connection online + */ + void Online(void) { m_online = true; } + /** + * Mark network connection offline + */ + void Offline(void) { m_online = false; } + /** + * Is the network connection established ? + */ + bool IsOnline(void) { return m_online; } + + virtual bool Connect(char *address, unsigned int port, char *password, + unsigned int localport, unsigned int timeout)=0; + virtual bool Disconnect(void)=0; + + virtual void SendNetworkMessage(NG_NetworkMessage* msg)=0; + /** + * read NG_NetworkMessage from library buffer, may be + * irrelevant for loopbackdevices + */ + + virtual std::vector<NG_NetworkMessage*> RetrieveNetworkMessages()=0; + + /** + * number of messages in device hash for this frame + */ + + virtual STR_String GetNetworkVersion(void)=0; +}; + +#endif //NG_NETWORKDEVICEINTERFACE_H diff --git a/source/gameengine/Network/NG_NetworkMessage.cpp b/source/gameengine/Network/NG_NetworkMessage.cpp new file mode 100644 index 00000000000..d000dd4b4cc --- /dev/null +++ b/source/gameengine/Network/NG_NetworkMessage.cpp @@ -0,0 +1,55 @@ +/* + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * 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. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * 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, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * 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/BL DUAL LICENSE BLOCK ***** + * generic Network Message implementation + */ +#include "NG_NetworkMessage.h" +#include <assert.h> + +int NG_NetworkMessage::s_nextID = 3; // just some number to start with + +NG_NetworkMessage::NG_NetworkMessage( + const STR_String& to, + const STR_String& from, + const STR_String& subject, + const STR_String& body) : + m_uniqueMessageID(s_nextID++), + m_refcount(1), + m_to(to), + m_from(from), + m_subject(subject), + m_message(body) +{ +} + +NG_NetworkMessage::~NG_NetworkMessage() +{ + assert(m_refcount==0); +} diff --git a/source/gameengine/Network/NG_NetworkMessage.h b/source/gameengine/Network/NG_NetworkMessage.h new file mode 100644 index 00000000000..72abb3e4a71 --- /dev/null +++ b/source/gameengine/Network/NG_NetworkMessage.h @@ -0,0 +1,130 @@ +/* + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * 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. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * 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, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * 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/BL DUAL LICENSE BLOCK ***** + * NetworkGame_NetworkMessage generic Network Message class + */ +#ifndef NG_NETWORKMESSAGE_H +#define NG_NETWORKMESSAGE_H + +#include "STR_HashedString.h" + +class NG_NetworkMessage +{ + static int s_nextID; + int m_uniqueMessageID; // intern counting MessageID + unsigned int m_ClientId; + int m_refcount; + + STR_String m_to; // receiver + STR_String m_from; // sender + STR_String m_subject; // empty or propName + STR_String m_message; // message or propValue + +protected: + ~NG_NetworkMessage(); + +public: + NG_NetworkMessage( + const STR_String& to, + const STR_String& from, + const STR_String& subject, + const STR_String& body); + + void AddRef() { + m_refcount++; + } + + // This is not nice code you should'nt need to resort to + // delete this. + void Release() + { + if (! --m_refcount) + { + delete this; + } + } + + /** + * set the content of this message + */ + void SetMessageText(const STR_String& msgtext) { + m_message = msgtext; + } + + /** + * get the (read-only) To part of this message + */ + const STR_String& GetDestinationName() { return m_to;}; + + /** + * get the (read-only) From part of this message + */ + const STR_String& GetSenderName() { return m_from;}; + + /** + * get the (read-only) Subject part of this message + */ + const STR_String& GetSubject() { return m_subject;}; + + /** + * get the (read-only) Body part of this message + */ + const STR_String& GetMessageText() { +//cout << "GetMessageText " << m_message << "\n"; + return m_message; + } + const STR_String& GetMessageText() const { +//cout << "GetMessageText " << m_message << "\n"; + return m_message; + } + + /** + * Set the NetworkMessage sender identifier + */ + void SetSender(unsigned int ClientId) { + m_ClientId = ClientId; + } + + /** + * Get the NetworkMessage sender identifier + */ + unsigned int GetSender(void) { + return m_ClientId; + } + + /** + * get the unique Network Message ID + */ + int GetMessageID() { + return m_uniqueMessageID; + } +}; + +#endif //NG_NETWORKMESSAGE_H diff --git a/source/gameengine/Network/NG_NetworkObject.cpp b/source/gameengine/Network/NG_NetworkObject.cpp new file mode 100644 index 00000000000..54db7c4f4b8 --- /dev/null +++ b/source/gameengine/Network/NG_NetworkObject.cpp @@ -0,0 +1,46 @@ +/* + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * 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. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * 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, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * 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/BL DUAL LICENSE BLOCK ***** + * NetworkGame_NetworkObject generic Network Object implementation + */ +#include "NG_NetworkObject.h" + +NG_NetworkObject::NG_NetworkObject() +{ +} + +NG_NetworkObject::~NG_NetworkObject() +{ +} + +const STR_String& NG_NetworkObject::GetName() +{ + return m_name; +} diff --git a/source/gameengine/Network/NG_NetworkObject.h b/source/gameengine/Network/NG_NetworkObject.h new file mode 100644 index 00000000000..d276ab6a25a --- /dev/null +++ b/source/gameengine/Network/NG_NetworkObject.h @@ -0,0 +1,47 @@ +/* + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * 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. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * 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, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * 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/BL DUAL LICENSE BLOCK ***** + * NetworkGame_NetworkObject generic Network Object class + */ +#ifndef NG_NETWORKOBJECT_H +#define NG_NETWORKOBJECT_H + +#include "STR_String.h" + +class NG_NetworkObject +{ + STR_String m_name; +public: + NG_NetworkObject(); + ~NG_NetworkObject(); + const STR_String& GetName(); +}; + +#endif //NG_NETWORKOBJECT_H diff --git a/source/gameengine/Network/NG_NetworkScene.cpp b/source/gameengine/Network/NG_NetworkScene.cpp new file mode 100644 index 00000000000..30bc1aac122 --- /dev/null +++ b/source/gameengine/Network/NG_NetworkScene.cpp @@ -0,0 +1,271 @@ +/** + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * 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. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * 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, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * 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/BL DUAL LICENSE BLOCK ***** + * NetworkSceneManagement generic implementation + */ +#include <stdio.h> +#include <assert.h> +#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, double deltatime) +{ + 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; + + STR_String name = object->GetName(); + 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; + + STR_String name = object->GetName(); + 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 definately NOT in the scene, so stop looking + } else { // narrow phase + // possibly it's there, but maybe not (false hit) + if (to.IsEmpty()) { + // take all messages, and check other fields + assert("objectnames that are empty are not valid, so make it a hobby project :)\n"); + } 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(); +} + diff --git a/source/gameengine/Network/NG_NetworkScene.h b/source/gameengine/Network/NG_NetworkScene.h new file mode 100644 index 00000000000..487934205ec --- /dev/null +++ b/source/gameengine/Network/NG_NetworkScene.h @@ -0,0 +1,102 @@ +/* + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * 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. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * 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, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * 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/BL DUAL LICENSE BLOCK ***** + * NetworkSceneManagement generic class + */ +#ifndef __NG_NETWORKSCENE_H +#define __NG_NETWORKSCENE_H + +#include "GEN_Map.h" +#include "STR_HashedString.h" +#include <vector> +class NG_NetworkDeviceInterface; + +class NG_NetworkScene +{ + class NG_NetworkDeviceInterface *m_networkdevice; + GEN_Map<STR_HashedString, class NG_NetworkObject *> m_networkObjects; + + // GEN_Maps used as a 'Bloom' filter + typedef GEN_Map<STR_HashedString, std::vector<class NG_NetworkMessage*>* > TMessageMap; + TMessageMap m_messagesByDestinationName; + TMessageMap m_messagesBySenderName; + TMessageMap m_messagesBySubject; + +public: + NG_NetworkScene(NG_NetworkDeviceInterface *nic); + ~NG_NetworkScene(); + + /** + * progress one frame, handle all network traffic + */ + void proceed(double curtime, double deltatime); + + /** + * add a networkobject to the scene + */ + void AddObject(NG_NetworkObject* object); + + /** + * remove a networkobject to the scene + */ + void RemoveObject(NG_NetworkObject* object); + + /** + * remove all objects at once + */ + void RemoveAllObjects(); + + /** + * send a message (ascii text) over the network + */ + void SendMessage(const STR_String& to,const STR_String& from,const STR_String& subject,const STR_String& message); + + /** + * find an object by name + */ + NG_NetworkObject* FindNetworkObject(const STR_String& objname); + + bool ConstraintsAreValid(const STR_String& from,const STR_String& subject,class NG_NetworkMessage* message); + vector<NG_NetworkMessage*> FindMessages(const STR_String& to,const STR_String& from,const STR_String& subject,bool spamallowed); + +protected: + /** + * Releases messages in message map members. + */ + void ClearAllMessageMaps(void); + + /** + * Releases messages for the given message map. + * @param map Message map with messages. + */ + void ClearMessageMap(TMessageMap& map); +}; + +#endif //__NG_NETWORKSCENE_H diff --git a/source/gameengine/Network/TerraplayNetwork/Makefile b/source/gameengine/Network/TerraplayNetwork/Makefile new file mode 100644 index 00000000000..6fad0e0ca43 --- /dev/null +++ b/source/gameengine/Network/TerraplayNetwork/Makefile @@ -0,0 +1,45 @@ +# +# $Id$ +# +# ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** +# +# 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. The Blender +# Foundation also sells licenses for use in proprietary software under +# the Blender License. See http://www.blender.org/BL/ for information +# about this. +# +# 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, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# 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/BL DUAL LICENSE BLOCK ***** +# +# + +LIBNAME = TerraPlayNetwork +DIR = $(OCGDIR)/gameengine/Network/$(LIBNAME) + +include nan_compile.mk + +CCFLAGS += $(LEVEL_1_CPP_WARNINGS) + +CPPFLAGS += -I$(NAN_STRING)/include +CPPFLAGS += -I$(LCGDIR)/terraplay/include +CPPFLAGS += -I../../../kernel/gen_system +CPPFLAGS += -I.. + diff --git a/source/gameengine/Network/TerraplayNetwork/NG_TerraplayNetworkDeviceInterface.cpp b/source/gameengine/Network/TerraplayNetwork/NG_TerraplayNetworkDeviceInterface.cpp new file mode 100644 index 00000000000..55710e0f8ad --- /dev/null +++ b/source/gameengine/Network/TerraplayNetwork/NG_TerraplayNetworkDeviceInterface.cpp @@ -0,0 +1,192 @@ +/* + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * 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. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * 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, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * 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/BL DUAL LICENSE BLOCK ***** + * TerraplayNetworkDeviceInterface derived from NG_NetworkDeviceInterface + */ + +#include "NG_TerraplayNetworkDeviceInterface.h" +#include "NG_NetworkMessage.h" + +//---- relocate these +void NG_TerraplayNetworkDeviceInterface::interface_error(char *str, GASResult error) { + GASRString err_str = GAS->ErrorTranslate(error); + if (err_str.result == GASOK) + printf("%s: %s\n",str,err_str.ptr); + else + printf("%s: UNKNOWN (Error code %d)", error); +} +//---- END relocate these + +NG_TerraplayNetworkDeviceInterface::NG_TerraplayNetworkDeviceInterface() +{ + group_id = GASCLIENTIDNULL; + group_id_request_valid = false; + this->Offline(); + + if ((GAS = new GASInterface()) == NULL) { + // terror + printf("ERROR GAS Common Network Interface NOT created\n"); + // do something useful + } else { + printf("GAS Common Network Interface created\n"); + } +} + +NG_TerraplayNetworkDeviceInterface::~NG_TerraplayNetworkDeviceInterface() +{ + if (GAS != NULL) { + delete GAS; + printf("GAS Common Network Interface deleted\n"); + } +} + +bool NG_TerraplayNetworkDeviceInterface::Connect(char *GAS_address, + unsigned int GAS_port, char *GAS_password, unsigned int localport, + unsigned int timeout) +{ + GASResult result; + printf("Establishing connection to GAS...\n"); + result = GAS->ConnectionRequest(GAS_address, GAS_port, + GAS_password,localport, timeout); + if (result == GASOK) { + this->Online(); + GASRClientId client_id = GAS->Connected(); + if (client_id.result != GASOK) { + printf("... connected, but no client ID\n"); + return false; + } else { + printf("Connected with client ID %d\n", + client_id.clientid); + return true; + } + } else { + interface_error("Connection", result); + return false; + } +} + +bool NG_TerraplayNetworkDeviceInterface::Disconnect(void) +{ + int i = 0; + printf("Disconnecting...\n"); + if (! this->IsOnline()) { + printf("ehh... /me was not connected\n"); + return false; + } + + GASRRequestId req = GAS->ConnectionClose(); + if (req.result != GASWAITING) { + interface_error("ConnectionClose",req.result); + this->~NG_TerraplayNetworkDeviceInterface(); + } + this->Offline(); +// dit is erg fout :( ik wil helemaal geen ~NG_ hier + + while (true) { + GASRMessage gas_message; + GASResult result = GAS->GasActivity(GASBLOCK, 100); + if (++i>5000) { + printf("\nGiving up on waiting for connection close\n"); + this->~NG_TerraplayNetworkDeviceInterface(); + } + switch (result) { + case GASCONNECTIONOK: + break; + case GASGASMESSAGE: + gas_message = GAS->GasMessageGetNext(); + if (gas_message.type == GASRCONNECTIONCLOSE) { + if (gas_message.result == GASOK || + gas_message.result == GASALREADYDONE) { + return true; + } else { + interface_error("GasMessageGetNext", + gas_message.result); + return false; + } + } + // no break ... + default: + interface_error("GasActivity",result); + } + } + return true; +} + +STR_String NG_TerraplayNetworkDeviceInterface::GetNetworkVersion() +{ + GASRString version = GAS->Version(); + if (version.result != GASOK) { + interface_error("GetNetworkVersion", version.result); + return NULL; + } else { + return version.ptr; + } +} + +int NG_TerraplayNetworkDeviceInterface::mytest() { + return (3); +} + +void NG_TerraplayNetworkDeviceInterface::SendNetworkMessage(NG_NetworkMessage* nwmsg) +{ + GASPayload payload; + GASResult result; + STR_String mystring; + + if (group_id == GASCLIENTIDNULL) { + printf("Oops, no group to send to yet\n"); + return; + } + + mystring = nwmsg->GetMessageText().ReadPtr(); + payload.ptr = (void *) mystring.Ptr(); + payload.size = mystring.Length() + 1; + + result = GAS->ClientMessageSend(group_id, payload, GASBESTEFFORT); + + switch (result) { + case GASOK: + break; + default: + interface_error("ClientMessageSend",result); + } + // NOTE. You shall NOT free the payload with PayloadFree(). + // This is your own payload, allocated and freed by yourself + // anyway you want. +} + +vector <NG_NetworkMessage*> NG_TerraplayNetworkDeviceInterface::RetrieveNetworkMessages() +{ + + vector <NG_NetworkMessage*> messages; + //todo: spend your expensive time here! + + return messages; +} diff --git a/source/gameengine/Network/TerraplayNetwork/NG_TerraplayNetworkDeviceInterface.h b/source/gameengine/Network/TerraplayNetwork/NG_TerraplayNetworkDeviceInterface.h new file mode 100644 index 00000000000..b430b72d985 --- /dev/null +++ b/source/gameengine/Network/TerraplayNetwork/NG_TerraplayNetworkDeviceInterface.h @@ -0,0 +1,70 @@ +/* + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * 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. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * 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, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * 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/BL DUAL LICENSE BLOCK ***** + * TerraplayNetworkDeviceInterface derived from NG_NetworkDeviceInterface + */ +#ifndef NG_TERRAPLAYNETWORKDEVICEINTERFACE_H +#define NG_TERRAPLAYNETWORKDEVICEINTERFACE_H + +#include <deque> + +#include "GASInterface.h" + +#include "NG_NetworkDeviceInterface.h" + +class NG_TerraplayNetworkDeviceInterface : public NG_NetworkDeviceInterface +{ + std::deque<NG_NetworkMessage*> m_messages; + + // Terraplay GAS stuff + GASInterface *GAS; + GASClientId group_id; + GASRequestId group_id_request; + int group_id_request_valid; + + void interface_error(char *str, GASResult error); +public: + NG_TerraplayNetworkDeviceInterface(); + ~NG_TerraplayNetworkDeviceInterface(); + + bool Connect(char *GAS_address, unsigned int GAS_port, + char *GAS_password, unsigned int localport, + unsigned int timeout); + bool Disconnect(void); + + void SendNetworkMessage(NG_NetworkMessage* nwmsg); + vector<NG_NetworkMessage*> RetrieveNetworkMessages(void); + + STR_String GetNetworkVersion(void); + + int mytest(void); +}; + +#endif //NG_TERRAPLAYNETWORKDEVICEINTERFACE_H |