Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pickups2/src/network/synchronisable/Synchronisable.h @ 2961

Last change on this file since 2961 was 2917, checked in by landauf, 16 years ago

merged pickup into pickup2

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