Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10134 was 9667, checked in by landauf, 11 years ago

merged core6 back to trunk

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