[1505] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Oliver Scheuss, (C) 2007 |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #ifndef _Synchronisable_H__ |
---|
| 30 | #define _Synchronisable_H__ |
---|
| 31 | |
---|
| 32 | #include "NetworkPrereqs.h" |
---|
| 33 | |
---|
| 34 | #include <list> |
---|
[1907] | 35 | #include <map> |
---|
| 36 | #include <queue> |
---|
[1505] | 37 | #include "core/OrxonoxClass.h" |
---|
[1841] | 38 | #include "core/XMLIncludes.h" |
---|
[1534] | 39 | #include "NetworkCallback.h" |
---|
[1505] | 40 | |
---|
[2087] | 41 | #define REGISTERDATA(varname, ...) \ |
---|
[2171] | 42 | registerVariable((void*)&varname, sizeof(varname), DATA, __VA_ARGS__) |
---|
[2087] | 43 | #define REGISTERSTRING(stringname, ...) \ |
---|
[2171] | 44 | registerVariable(&stringname, stringname.length()+1, STRING, __VA_ARGS__) |
---|
[1907] | 45 | |
---|
[2171] | 46 | namespace orxonox |
---|
[1505] | 47 | { |
---|
[2087] | 48 | static const unsigned int OBJECTID_UNKNOWN = (unsigned int)-1; |
---|
| 49 | |
---|
[1907] | 50 | namespace direction{ |
---|
| 51 | enum syncdirection{ |
---|
| 52 | toclient=0x1, |
---|
| 53 | toserver=0x2, |
---|
[2087] | 54 | bidirectional=0x3, |
---|
| 55 | serverMaster=0x3, |
---|
| 56 | clientMaster=0x7 |
---|
[1907] | 57 | }; |
---|
| 58 | } |
---|
[2087] | 59 | |
---|
[1907] | 60 | namespace syncmode{ |
---|
| 61 | enum mode{ |
---|
[2171] | 62 | once=0, |
---|
[1907] | 63 | always=1 |
---|
| 64 | }; |
---|
| 65 | } |
---|
[2087] | 66 | |
---|
[1505] | 67 | enum variableType{ |
---|
| 68 | DATA, |
---|
| 69 | STRING, |
---|
| 70 | }; |
---|
| 71 | |
---|
[2087] | 72 | struct _NetworkExport synchronisableHeader{ |
---|
[1907] | 73 | uint32_t size:31; |
---|
| 74 | bool dataAvailable:1; |
---|
| 75 | uint32_t objectID; |
---|
[2087] | 76 | uint32_t creatorID; |
---|
[1907] | 77 | uint32_t classID; |
---|
[1505] | 78 | }; |
---|
| 79 | |
---|
[2087] | 80 | struct _NetworkExport synchronisableVariable{ |
---|
[2171] | 81 | size_t size; |
---|
| 82 | uint8_t mode; // this determines in which direction the variable gets synchronised |
---|
[1505] | 83 | void *var; |
---|
| 84 | variableType type; |
---|
[1534] | 85 | NetworkCallbackBase *callback; |
---|
[2087] | 86 | void *varBuffer; |
---|
| 87 | uint8_t varReference; |
---|
| 88 | }; |
---|
[1505] | 89 | |
---|
| 90 | /** |
---|
| 91 | * This class is the base class of all the Objects in the universe that need to be synchronised over the network |
---|
[1907] | 92 | * Every class, that inherits from this class has to link the DATA THAT NEEDS TO BE SYNCHRONISED into the linked list. |
---|
[1505] | 93 | * @author Oliver Scheuss |
---|
| 94 | */ |
---|
[2171] | 95 | class _NetworkExport Synchronisable : virtual public OrxonoxClass{ |
---|
[1505] | 96 | public: |
---|
[1907] | 97 | friend class packet::Gamestate; |
---|
[2171] | 98 | // friend class Server; |
---|
[1505] | 99 | virtual ~Synchronisable(); |
---|
| 100 | |
---|
[2087] | 101 | |
---|
[1505] | 102 | virtual bool create(); |
---|
| 103 | static void setClient(bool b); |
---|
[2087] | 104 | |
---|
[2171] | 105 | static Synchronisable *fabricate(uint8_t*& mem, uint8_t mode=0x0); |
---|
[1907] | 106 | static bool deleteObject(unsigned int objectID); |
---|
| 107 | static Synchronisable *getSynchronisable(unsigned int objectID); |
---|
| 108 | static unsigned int getNumberOfDeletedObject(){ return deletedObjects_.size(); } |
---|
| 109 | static unsigned int popDeletedObject(){ unsigned int i = deletedObjects_.front(); deletedObjects_.pop(); return i; } |
---|
[2087] | 110 | |
---|
[1907] | 111 | inline unsigned int getObjectID(){return objectID;} |
---|
| 112 | inline unsigned int getClassID(){return classID;} |
---|
[1505] | 113 | protected: |
---|
[2171] | 114 | Synchronisable(BaseObject* creator); |
---|
| 115 | void registerVariable(void *var, int size, variableType t, uint8_t mode=0x1, NetworkCallbackBase *cb=0); |
---|
| 116 | void unregisterVariable(void *var); |
---|
| 117 | void setObjectMode(uint8_t mode); |
---|
[1907] | 118 | void setObjectFrequency(unsigned int freq){ objectFrequency_ = freq; } |
---|
[2087] | 119 | |
---|
| 120 | |
---|
[1505] | 121 | private: |
---|
[2171] | 122 | bool getData(uint8_t*& men, unsigned int id, uint8_t mode=0x0); |
---|
| 123 | uint32_t getSize(unsigned int id, uint8_t mode=0x0); |
---|
| 124 | bool updateData(uint8_t*& mem, uint8_t mode=0x0, bool forceCallback=false); |
---|
[1907] | 125 | bool isMyData(uint8_t* mem); |
---|
| 126 | bool doSelection(unsigned int id); |
---|
[2171] | 127 | bool doSync(unsigned int id, uint8_t mode=0x0); |
---|
[2087] | 128 | |
---|
[1907] | 129 | unsigned int objectID; |
---|
[2087] | 130 | unsigned int creatorID; |
---|
[1907] | 131 | unsigned int classID; |
---|
[2087] | 132 | |
---|
[1505] | 133 | std::list<synchronisableVariable *> *syncList; |
---|
[2171] | 134 | static uint8_t state_; // detemines wheter we are server (default) or client |
---|
[1505] | 135 | bool backsync_; // if true the variables with mode > 1 will be synchronised to server (client -> server) |
---|
[1751] | 136 | unsigned int objectFrequency_; |
---|
| 137 | int objectMode_; |
---|
[1907] | 138 | static std::map<unsigned int, Synchronisable *> objectMap_; |
---|
| 139 | static std::queue<unsigned int> deletedObjects_; |
---|
[1505] | 140 | }; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | #endif /* _Synchronisable_H__ */ |
---|