Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/network/synchronisable/Synchronisable.h @ 7163

Last change on this file since 7163 was 7163, checked in by dafrick, 14 years ago

Merged presentation3 branch into trunk.

  • Property svn:eol-style set to native
File size: 9.6 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 <cassert>
35#include <cstring>
36#include <vector>
37#include <map>
38#include <queue>
39#include <set>
40
41#include "util/mbool.h"
42#include "core/OrxonoxClass.h"
43#include "SynchronisableVariable.h"
44#include "NetworkCallback.h"
45
46
47namespace orxonox
48{
49
50  namespace ObjectDirection{
51    enum Value{
52      ToClient=0x1,
53      ToServer=0x2,
54      Bidirectional=0x3
55    };
56  }
57
58  namespace Priority{
59    enum Value{
60      VeryHigh    = -100,
61      High        = -15,
62      Normal      = 0,
63      Low         = 15,
64      VeryLow     = 100
65    };
66  }
67 
68  typedef uint8_t VariableID;
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 variables are stored in diff mode
77   * Byte 5 to 8: objectID_
78   * Byte 9 to 12: classID_
79   * Byte 13 to 16: creatorID_
80   */
81  class _NetworkExport SynchronisableHeader{
82    friend class SynchronisableHeaderLight;
83    private:
84      uint8_t* data_;
85    public:
86      SynchronisableHeader(uint8_t* data)
87        { data_ = data; }
88      inline static uint32_t getSize()
89        { return 14; }
90      inline uint16_t getDataSize() const
91        { return (*(uint16_t*)data_) & 0x7FFF; } //only use the first 15 bits
92      inline void setDataSize(uint16_t size)
93        { *(uint16_t*)(data_) = (size & 0x7FFF) | (*(uint16_t*)(data_) & 0x8000 ); }
94      inline bool isDiffed() const
95        { return ( (*(uint16_t*)data_) & 0x8000 ) == 0x8000; }
96      inline void setDiffed( bool b)
97        { *(uint16_t*)(data_) = (b << 15) | (*(uint16_t*)(data_) & 0x7FFF ); }
98      inline uint32_t getObjectID() const
99        { return *(uint32_t*)(data_+2); }
100      inline void setObjectID(uint32_t objectID_)
101        { *(uint32_t*)(data_+2) = objectID_; }
102      inline uint32_t getClassID() const
103        { return *(uint32_t*)(data_+6); }
104      inline void setClassID(uint32_t classID_)
105        { *(uint32_t*)(data_+6) = classID_; }
106      inline uint32_t getCreatorID() const
107        { return *(uint32_t*)(data_+10); }
108      inline void setCreatorID(uint32_t creatorID_)
109        { *(uint32_t*)(data_+10) = creatorID_; }
110      inline void operator=(SynchronisableHeader& h)
111        { memcpy(data_, h.data_, getSize()); }
112  };
113
114    /**
115   * @brief: stores information about a Synchronisable (light version)
116   *
117   * This class stores the information about a Synchronisable (objectID_, dataSize)
118   * in an emulated bitset.
119   * Bit 1 to 31 store the size of the Data the synchronisable consumes in the stream
120   * Bit 32 is a bool and defines whether the variables are stored in diff mode
121   * Byte 5 to 8: objectID_
122   */
123  class _NetworkExport SynchronisableHeaderLight{
124    private:
125      uint8_t* data_;
126    public:
127      SynchronisableHeaderLight(uint8_t* data)
128        { data_ = data; }
129      inline static uint32_t getSize()
130        { return 6; }
131      inline uint16_t getDataSize() const
132        { return (*(uint16_t*)data_) & 0x7FFF; } //only use the first 31 bits
133      inline void setDataSize(uint16_t size)
134        { *(uint16_t*)(data_) = (size & 0x7FFFFFFF) | (*(uint16_t*)(data_) & 0x8000 ); }
135      inline bool isDiffed() const
136        { return ( (*(uint16_t*)data_) & 0x8000 ) == 0x8000; }
137      inline void setDiffed( bool b)
138        { *(uint16_t*)(data_) = (b << 15) | (*(uint16_t*)(data_) & 0x7FFF ); }
139      inline uint32_t getObjectID() const
140        { return *(uint32_t*)(data_+2); }
141      inline void setObjectID(uint32_t objectID_)
142        { *(uint32_t*)(data_+2) = objectID_; }
143      inline void operator=(SynchronisableHeader& h)
144        { memcpy(data_, h.data_, getSize()); }
145  };
146
147  /**
148  * This class is the base class of all the Objects in the universe that need to be synchronised over the network
149  * Every class, that inherits from this class has to link the DATA THAT NEEDS TO BE SYNCHRONISED into the linked list.
150  * @author Oliver Scheuss
151  */
152  class _NetworkExport Synchronisable : virtual public OrxonoxClass{
153  public:
154    friend class packet::Gamestate;
155    virtual ~Synchronisable();
156
157    static void setClient(bool b);
158
159    static Synchronisable *fabricate(uint8_t*& mem, uint8_t mode=0x0);
160    static bool deleteObject(uint32_t objectID_);
161    static Synchronisable *getSynchronisable(uint32_t objectID_);
162    static unsigned int getNumberOfDeletedObject(){ return deletedObjects_.size(); }
163    static uint32_t popDeletedObject(){ uint32_t i = deletedObjects_.front(); deletedObjects_.pop(); return i; }
164
165    inline uint32_t getObjectID() const {return this->objectID_;}
166    inline unsigned int getCreatorID() const {return this->creatorID_;}
167    inline uint32_t getClassID() const {return this->classID_;}
168    inline unsigned int getPriority() const { return this->objectFrequency_;}
169    inline uint8_t getSyncMode() const { return this->objectMode_; }
170
171    void setSyncMode(uint8_t mode);
172   
173    inline uint32_t getNrOfVariables(){ return this->syncList_.size(); }
174    inline uint32_t getVarSize( VariableID ID )
175    { return this->syncList_[ID]->getSize(state_); }
176
177  protected:
178    Synchronisable(BaseObject* creator);
179    template <class T> void registerVariable(T& variable, uint8_t mode=0x1, NetworkCallbackBase *cb=0, bool bidirectional=false);
180    template <class T> void registerVariable(std::set<T>& variable, uint8_t mode=0x1, NetworkCallbackBase *cb=0, bool bidirectional=false);
181    template <class T> void unregisterVariable(T& var);
182
183    void setPriority(unsigned int freq){ objectFrequency_ = freq; }
184
185
186  private:
187    uint32_t getData(uint8_t*& mem, std::vector<uint32_t>& sizes, int32_t id, uint8_t mode);
188    uint32_t getSize(int32_t id, uint8_t mode=0x0);
189    bool updateData(uint8_t*& mem, uint8_t mode=0x0, bool forceCallback=false);
190    bool doSync(int32_t id, uint8_t mode=0x0);
191
192    inline void setObjectID(uint32_t id){ this->objectID_ = id; objectMap_[this->objectID_] = this; }
193    inline void setClassID(uint32_t id){ this->classID_ = id; }
194
195    uint32_t objectID_;
196    uint32_t creatorID_;
197    uint32_t classID_;
198
199    std::vector<SynchronisableVariableBase*> syncList_;
200    std::vector<SynchronisableVariableBase*> stringList_;
201    uint32_t dataSize_; //size of all variables except strings
202    static uint8_t state_; // detemines wheter we are server (default) or client
203    bool backsync_; // if true the variables with mode > 1 will be synchronised to server (client -> server)
204    unsigned int objectFrequency_;
205    int objectMode_;
206    static std::map<uint32_t, Synchronisable *> objectMap_;
207    static std::queue<uint32_t> deletedObjects_;
208  };
209
210  template <class T> void Synchronisable::registerVariable(T& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional)
211  {
212    if (bidirectional)
213    {
214      syncList_.push_back(new SynchronisableVariableBidirectional<T>(variable, mode, cb));
215      this->dataSize_ += syncList_.back()->getSize(state_);
216    }
217    else
218    {
219      syncList_.push_back(new SynchronisableVariable<T>(variable, mode, cb));
220      if ( this->state_ == mode )
221        this->dataSize_ += syncList_.back()->getSize(state_);
222    }
223  }
224 
225  template <class T> void Synchronisable::unregisterVariable(T& variable){
226    std::vector<SynchronisableVariableBase*>::iterator it = syncList_.begin();
227    while(it!=syncList_.end()){
228      if( ((*it)->getReference()) == &variable ){
229        this->dataSize_ -= (*it)->getSize(Synchronisable::state_);
230        delete (*it);
231        syncList_.erase(it);
232        return;
233      }
234      else
235        it++;
236    }
237    bool unregistered_nonexistent_variable = false;
238    assert(unregistered_nonexistent_variable); //if we reach this point something went wrong:
239    // the variable has not been registered before
240  }
241
242  template <class T> void Synchronisable::registerVariable( std::set<T>& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional)
243  {
244    SynchronisableVariableBase* sv;
245    if (bidirectional)
246      sv = new SynchronisableVariableBidirectional<std::set<T> >(variable, mode, cb);
247    else
248      sv = new SynchronisableVariable<std::set<T> >(variable, mode, cb);
249    syncList_.push_back(sv);
250    stringList_.push_back(sv);
251  }
252
253  template <> _NetworkExport void Synchronisable::registerVariable( std::string& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional);
254//   template <class T> _NetworkExport void Synchronisable::registerVariable<std::set<T> >( std::set<T>& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional);
255  template <> _NetworkExport void Synchronisable::unregisterVariable( std::string& variable );
256
257
258}
259
260#endif /* _Synchronisable_H__ */
Note: See TracBrowser for help on using the repository browser.