Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network2/src/libraries/network/synchronisable/Synchronisable.h @ 6455

Last change on this file since 6455 was 6455, checked in by scheusso, 14 years ago

unregisterVariable reintroduced
some style changes

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