[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 | |
---|
[2211] | 32 | #include "network/NetworkPrereqs.h" |
---|
[1505] | 33 | |
---|
| 34 | #include <list> |
---|
[1907] | 35 | #include <map> |
---|
| 36 | #include <queue> |
---|
[2245] | 37 | #include <cassert> |
---|
| 38 | #include "util/Math.h" |
---|
| 39 | #include "util/mbool.h" |
---|
[1505] | 40 | #include "core/OrxonoxClass.h" |
---|
[2245] | 41 | // TODO: this has to be removed |
---|
| 42 | // #include <OgreLight.h> |
---|
| 43 | // #include "OrxonoxPrereqs.h" |
---|
| 44 | // ============================ |
---|
[1534] | 45 | #include "NetworkCallback.h" |
---|
[2245] | 46 | #include "SynchronisableVariable.h" |
---|
[1505] | 47 | |
---|
[2245] | 48 | /*#define REGISTERDATA(varname, ...) \ |
---|
[2171] | 49 | registerVariable((void*)&varname, sizeof(varname), DATA, __VA_ARGS__) |
---|
[2087] | 50 | #define REGISTERSTRING(stringname, ...) \ |
---|
[2245] | 51 | registerVariable(&stringname, stringname.length()+1, STRING, __VA_ARGS__)*/ |
---|
[1907] | 52 | |
---|
[2171] | 53 | namespace orxonox |
---|
[1505] | 54 | { |
---|
[2087] | 55 | |
---|
[2245] | 56 | namespace objectDirection{ |
---|
| 57 | enum objectdirection{ |
---|
[1907] | 58 | toclient=0x1, |
---|
| 59 | toserver=0x2, |
---|
[2245] | 60 | bidirectional=0x3 |
---|
[1907] | 61 | }; |
---|
| 62 | } |
---|
[2485] | 63 | |
---|
[2415] | 64 | namespace priority{ |
---|
| 65 | enum prio{ |
---|
[2419] | 66 | very_high = -100, |
---|
[2415] | 67 | high = -15, |
---|
| 68 | normal = 0, |
---|
| 69 | low = 15, |
---|
[2419] | 70 | very_low = 100 |
---|
[2415] | 71 | }; |
---|
| 72 | } |
---|
[2087] | 73 | |
---|
[2662] | 74 | /** |
---|
| 75 | * @brief: stores information about a Synchronisable |
---|
| 76 | * |
---|
| 77 | * This class stores the information about a Synchronisable (objectID, classID, creatorID, dataSize) |
---|
| 78 | * in an emulated bitset. |
---|
| 79 | * Bit 1 to 31 store the size of the Data the synchronisable consumes in the stream |
---|
| 80 | * Bit 32 is a bool and defines whether the data is actually stored or is just filled up with 0 |
---|
| 81 | * Byte 5 to 8: objectID |
---|
| 82 | * Byte 9 to 12: classID |
---|
| 83 | * Byte 13 to 16: creatorID |
---|
| 84 | */ |
---|
| 85 | class _NetworkExport SynchronisableHeader{ |
---|
| 86 | private: |
---|
| 87 | uint8_t *data_; |
---|
| 88 | public: |
---|
| 89 | SynchronisableHeader(uint8_t* data) |
---|
| 90 | { data_ = data; } |
---|
| 91 | inline static uint32_t getSize() |
---|
| 92 | { return 16; } |
---|
| 93 | inline uint32_t getDataSize() const |
---|
| 94 | { return (*(uint32_t*)data_) & 0x7FFFFFFF; } //only use the first 31 bits |
---|
| 95 | inline void setDataSize(uint32_t size) |
---|
| 96 | { *(uint32_t*)(data_) = (size & 0x7FFFFFFF) | (*(uint32_t*)(data_) & 0x80000000 ); } |
---|
| 97 | inline bool isDataAvailable() const |
---|
| 98 | { return ( (*(uint32_t*)data_) & 0x80000000 ) == 0x80000000; } |
---|
| 99 | inline void setDataAvailable( bool b) |
---|
| 100 | { *(uint32_t*)(data_) = (b << 31) | (*(uint32_t*)(data_) & 0x7FFFFFFF ); } |
---|
| 101 | inline uint32_t getObjectID() const |
---|
| 102 | { return *(uint32_t*)(data_+4); } |
---|
| 103 | inline void setObjectID(uint32_t objectID) |
---|
| 104 | { *(uint32_t*)(data_+4) = objectID; } |
---|
| 105 | inline uint32_t getClassID() const |
---|
| 106 | { return *(uint32_t*)(data_+8); } |
---|
| 107 | inline void setClassID(uint32_t classID) |
---|
| 108 | { *(uint32_t*)(data_+8) = classID; } |
---|
| 109 | inline uint32_t getCreatorID() const |
---|
| 110 | { return *(uint32_t*)(data_+12); } |
---|
| 111 | inline void setCreatorID(uint32_t creatorID) |
---|
| 112 | { *(uint32_t*)(data_+12) = creatorID; } |
---|
| 113 | inline void operator=(SynchronisableHeader& h) |
---|
| 114 | { memcpy(data_, h.data_, getSize()); } |
---|
[1505] | 115 | }; |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | /** |
---|
| 119 | * This class is the base class of all the Objects in the universe that need to be synchronised over the network |
---|
[2662] | 120 | * Every class, that inherits from this class has to link the DATA THAT NEEDS TO BE SYNCHRONISED into the linked list. |
---|
[1505] | 121 | * @author Oliver Scheuss |
---|
| 122 | */ |
---|
[2171] | 123 | class _NetworkExport Synchronisable : virtual public OrxonoxClass{ |
---|
[1505] | 124 | public: |
---|
[1907] | 125 | friend class packet::Gamestate; |
---|
[2171] | 126 | // friend class Server; |
---|
[1505] | 127 | virtual ~Synchronisable(); |
---|
| 128 | |
---|
| 129 | static void setClient(bool b); |
---|
[2087] | 130 | |
---|
[2171] | 131 | static Synchronisable *fabricate(uint8_t*& mem, uint8_t mode=0x0); |
---|
[2309] | 132 | static bool deleteObject(uint32_t objectID); |
---|
| 133 | static Synchronisable *getSynchronisable(uint32_t objectID); |
---|
[1907] | 134 | static unsigned int getNumberOfDeletedObject(){ return deletedObjects_.size(); } |
---|
[2309] | 135 | static uint32_t popDeletedObject(){ uint32_t i = deletedObjects_.front(); deletedObjects_.pop(); return i; } |
---|
[2087] | 136 | |
---|
[2485] | 137 | inline uint32_t getObjectID() const {return objectID;} |
---|
| 138 | inline unsigned int getCreatorID() const {return creatorID;} |
---|
| 139 | inline uint32_t getClassID() const {return classID;} |
---|
| 140 | inline unsigned int getPriority() const { return objectFrequency_;} |
---|
[2355] | 141 | |
---|
[1505] | 142 | protected: |
---|
[2171] | 143 | Synchronisable(BaseObject* creator); |
---|
[2245] | 144 | // void registerVariable(void *var, int size, variableType t, uint8_t mode=0x1, NetworkCallbackBase *cb=0); |
---|
| 145 | template <class T> void registerVariable(T& variable, uint8_t mode=0x1, NetworkCallbackBase *cb=0, bool bidirectional=false); |
---|
| 146 | template <class T> void unregisterVariable(T& var); |
---|
[2171] | 147 | void setObjectMode(uint8_t mode); |
---|
[2415] | 148 | void setPriority(unsigned int freq){ objectFrequency_ = freq; } |
---|
[2087] | 149 | |
---|
| 150 | |
---|
[1505] | 151 | private: |
---|
[2309] | 152 | bool getData(uint8_t*& men, int32_t id, uint8_t mode=0x0); |
---|
| 153 | uint32_t getSize(int32_t id, uint8_t mode=0x0); |
---|
[2171] | 154 | bool updateData(uint8_t*& mem, uint8_t mode=0x0, bool forceCallback=false); |
---|
[2575] | 155 | inline bool isMyData(uint8_t* mem); |
---|
| 156 | inline bool doSync(int32_t id, uint8_t mode=0x0); |
---|
[2087] | 157 | |
---|
[2309] | 158 | uint32_t objectID; |
---|
| 159 | uint32_t creatorID; |
---|
| 160 | uint32_t classID; |
---|
[2087] | 161 | |
---|
[2245] | 162 | std::list<SynchronisableVariableBase*> syncList; |
---|
[2171] | 163 | static uint8_t state_; // detemines wheter we are server (default) or client |
---|
[1505] | 164 | bool backsync_; // if true the variables with mode > 1 will be synchronised to server (client -> server) |
---|
[1751] | 165 | unsigned int objectFrequency_; |
---|
| 166 | int objectMode_; |
---|
[2309] | 167 | static std::map<uint32_t, Synchronisable *> objectMap_; |
---|
| 168 | static std::queue<uint32_t> deletedObjects_; |
---|
[1505] | 169 | }; |
---|
[2485] | 170 | |
---|
[2245] | 171 | template <class T> void Synchronisable::registerVariable(T& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional) |
---|
| 172 | { |
---|
| 173 | if (bidirectional) |
---|
| 174 | syncList.push_back(new SynchronisableVariableBidirectional<const T>(variable, mode, cb)); |
---|
| 175 | else |
---|
| 176 | syncList.push_back(new SynchronisableVariable<const T>(variable, mode, cb)); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | template <class T> void Synchronisable::unregisterVariable(T& var){ |
---|
| 180 | std::list<SynchronisableVariableBase*>::iterator it = syncList.begin(); |
---|
| 181 | while(it!=syncList.end()){ |
---|
| 182 | if( ((*it)->getReference()) == &var ){ |
---|
| 183 | delete (*it); |
---|
| 184 | syncList.erase(it); |
---|
| 185 | return; |
---|
| 186 | } |
---|
| 187 | else |
---|
| 188 | it++; |
---|
| 189 | } |
---|
| 190 | bool unregistered_nonexistent_variable = false; |
---|
| 191 | assert(unregistered_nonexistent_variable); //if we reach this point something went wrong: |
---|
| 192 | // the variable has not been registered before |
---|
| 193 | } |
---|
[2485] | 194 | |
---|
[2245] | 195 | // ================= Specialisation declarations |
---|
[2307] | 196 | template <> _NetworkExport void Synchronisable::registerVariable( const ColourValue& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional); |
---|
| 197 | template <> _NetworkExport void Synchronisable::registerVariable( ColourValue& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional); |
---|
| 198 | template <> _NetworkExport void Synchronisable::registerVariable( const Vector2& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional); |
---|
| 199 | template <> _NetworkExport void Synchronisable::registerVariable( Vector2& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional); |
---|
| 200 | template <> _NetworkExport void Synchronisable::registerVariable( const Vector3& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional); |
---|
| 201 | template <> _NetworkExport void Synchronisable::registerVariable( Vector3& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional); |
---|
| 202 | template <> _NetworkExport void Synchronisable::registerVariable( const Vector4& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional); |
---|
| 203 | template <> _NetworkExport void Synchronisable::registerVariable( Vector4& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional); |
---|
| 204 | template <> _NetworkExport void Synchronisable::registerVariable( mbool& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional); |
---|
| 205 | template <> _NetworkExport void Synchronisable::registerVariable( const Quaternion& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional); |
---|
| 206 | template <> _NetworkExport void Synchronisable::registerVariable( Quaternion& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional); |
---|
| 207 | // template <> _NetworkExport void Synchronisable::registerVariable( LODParticle::LOD& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional); |
---|
| 208 | // template <> _NetworkExport void Synchronisable::registerVariable( Ogre::Light::LightTypes& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional); |
---|
[1505] | 209 | } |
---|
| 210 | |
---|
| 211 | #endif /* _Synchronisable_H__ */ |
---|