| 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> |
|---|
| 35 | #include <map> |
|---|
| 36 | #include <queue> |
|---|
| 37 | #include "util/Integers.h" |
|---|
| 38 | #include "core/OrxonoxClass.h" |
|---|
| 39 | #include "core/XMLIncludes.h" |
|---|
| 40 | #include "NetworkCallback.h" |
|---|
| 41 | #include "util/Integers.h" |
|---|
| 42 | |
|---|
| 43 | #define REGISTERDATA(varname, ...) \ |
|---|
| 44 | registerVariable((void*)&varname, sizeof(varname), DATA, __VA_ARGS__) |
|---|
| 45 | #define REGISTERSTRING(stringname, ...) \ |
|---|
| 46 | registerVariable(&stringname, stringname.length()+1, STRING, __VA_ARGS__) |
|---|
| 47 | |
|---|
| 48 | namespace orxonox |
|---|
| 49 | { |
|---|
| 50 | static const unsigned int OBJECTID_UNKNOWN = (unsigned int)-1; |
|---|
| 51 | |
|---|
| 52 | namespace direction{ |
|---|
| 53 | enum syncdirection{ |
|---|
| 54 | toclient=0x1, |
|---|
| 55 | toserver=0x2, |
|---|
| 56 | bidirectional=0x3, |
|---|
| 57 | serverMaster=0x3, |
|---|
| 58 | clientMaster=0x7 |
|---|
| 59 | }; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | namespace syncmode{ |
|---|
| 63 | enum mode{ |
|---|
| 64 | once=0, |
|---|
| 65 | always=1 |
|---|
| 66 | }; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | enum variableType{ |
|---|
| 70 | DATA, |
|---|
| 71 | STRING, |
|---|
| 72 | }; |
|---|
| 73 | |
|---|
| 74 | struct _NetworkExport synchronisableHeader{ |
|---|
| 75 | uint32_t size:31; |
|---|
| 76 | bool dataAvailable:1; |
|---|
| 77 | uint32_t objectID; |
|---|
| 78 | uint32_t creatorID; |
|---|
| 79 | uint32_t classID; |
|---|
| 80 | }; |
|---|
| 81 | |
|---|
| 82 | struct _NetworkExport synchronisableVariable{ |
|---|
| 83 | size_t size; |
|---|
| 84 | uint8_t mode; // this determines in which direction the variable gets synchronised |
|---|
| 85 | void *var; |
|---|
| 86 | variableType type; |
|---|
| 87 | NetworkCallbackBase *callback; |
|---|
| 88 | void *varBuffer; |
|---|
| 89 | uint8_t varReference; |
|---|
| 90 | }; |
|---|
| 91 | |
|---|
| 92 | /** |
|---|
| 93 | * This class is the base class of all the Objects in the universe that need to be synchronised over the network |
|---|
| 94 | * Every class, that inherits from this class has to link the DATA THAT NEEDS TO BE SYNCHRONISED into the linked list. |
|---|
| 95 | * @author Oliver Scheuss |
|---|
| 96 | */ |
|---|
| 97 | class _NetworkExport Synchronisable : virtual public OrxonoxClass{ |
|---|
| 98 | public: |
|---|
| 99 | friend class packet::Gamestate; |
|---|
| 100 | // friend class Server; |
|---|
| 101 | virtual ~Synchronisable(); |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | virtual bool create(); |
|---|
| 105 | static void setClient(bool b); |
|---|
| 106 | |
|---|
| 107 | static Synchronisable *fabricate(uint8_t*& mem, uint8_t mode=0x0); |
|---|
| 108 | static bool deleteObject(unsigned int objectID); |
|---|
| 109 | static Synchronisable *getSynchronisable(unsigned int objectID); |
|---|
| 110 | static unsigned int getNumberOfDeletedObject(){ return deletedObjects_.size(); } |
|---|
| 111 | static unsigned int popDeletedObject(){ unsigned int i = deletedObjects_.front(); deletedObjects_.pop(); return i; } |
|---|
| 112 | |
|---|
| 113 | inline unsigned int getObjectID(){return objectID;} |
|---|
| 114 | inline unsigned int getClassID(){return classID;} |
|---|
| 115 | protected: |
|---|
| 116 | Synchronisable(BaseObject* creator); |
|---|
| 117 | void registerVariable(void *var, int size, variableType t, uint8_t mode=0x1, NetworkCallbackBase *cb=0); |
|---|
| 118 | void unregisterVariable(void *var); |
|---|
| 119 | void setObjectMode(uint8_t mode); |
|---|
| 120 | void setObjectFrequency(unsigned int freq){ objectFrequency_ = freq; } |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | private: |
|---|
| 124 | bool getData(uint8_t*& men, unsigned int id, uint8_t mode=0x0); |
|---|
| 125 | uint32_t getSize(unsigned int id, uint8_t mode=0x0); |
|---|
| 126 | bool updateData(uint8_t*& mem, uint8_t mode=0x0, bool forceCallback=false); |
|---|
| 127 | bool isMyData(uint8_t* mem); |
|---|
| 128 | bool doSelection(unsigned int id); |
|---|
| 129 | bool doSync(unsigned int id, uint8_t mode=0x0); |
|---|
| 130 | |
|---|
| 131 | unsigned int objectID; |
|---|
| 132 | unsigned int creatorID; |
|---|
| 133 | unsigned int classID; |
|---|
| 134 | |
|---|
| 135 | std::list<synchronisableVariable *> *syncList; |
|---|
| 136 | static uint8_t state_; // detemines wheter we are server (default) or client |
|---|
| 137 | bool backsync_; // if true the variables with mode > 1 will be synchronised to server (client -> server) |
|---|
| 138 | unsigned int objectFrequency_; |
|---|
| 139 | int objectMode_; |
|---|
| 140 | static std::map<unsigned int, Synchronisable *> objectMap_; |
|---|
| 141 | static std::queue<unsigned int> deletedObjects_; |
|---|
| 142 | }; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | #endif /* _Synchronisable_H__ */ |
|---|