Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Merging presentation2 branch back to trunk.

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