Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7341 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
RevLine 
[1505]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
[2211]32#include "network/NetworkPrereqs.h"
[1505]33
[3214]34#include <cassert>
35#include <cstring>
[3084]36#include <vector>
[1907]37#include <map>
38#include <queue>
[7163]39#include <set>
[3214]40
[2245]41#include "util/mbool.h"
[1505]42#include "core/OrxonoxClass.h"
[3214]43#include "SynchronisableVariable.h"
[1534]44#include "NetworkCallback.h"
[1505]45
[1907]46
[2171]47namespace orxonox
[1505]48{
[2087]49
[3280]50  namespace ObjectDirection{
51    enum Value{
52      ToClient=0x1,
53      ToServer=0x2,
54      Bidirectional=0x3
[1907]55    };
56  }
[2485]57
[3280]58  namespace Priority{
59    enum Value{
60      VeryHigh    = -100,
61      High        = -15,
62      Normal      = 0,
63      Low         = 15,
64      VeryLow     = 100
[2415]65    };
66  }
[7163]67 
68  typedef uint8_t VariableID;
[2087]69
[2662]70  /**
[6417]71   * @brief: stores information about a Synchronisable
72   *
[5929]73   * This class stores the information about a Synchronisable (objectID_, classID_, creatorID_, dataSize)
[2662]74   * in an emulated bitset.
75   * Bit 1 to 31 store the size of the Data the synchronisable consumes in the stream
[7163]76   * Bit 32 is a bool and defines whether the variables are stored in diff mode
[5929]77   * Byte 5 to 8: objectID_
78   * Byte 9 to 12: classID_
79   * Byte 13 to 16: creatorID_
[2662]80   */
81  class _NetworkExport SynchronisableHeader{
[7163]82    friend class SynchronisableHeaderLight;
[2662]83    private:
[7163]84      uint8_t* data_;
[2662]85    public:
86      SynchronisableHeader(uint8_t* data)
87        { data_ = data; }
88      inline static uint32_t getSize()
[7163]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 ); }
[2662]98      inline uint32_t getObjectID() const
[7163]99        { return *(uint32_t*)(data_+2); }
[5929]100      inline void setObjectID(uint32_t objectID_)
[7163]101        { *(uint32_t*)(data_+2) = objectID_; }
[2662]102      inline uint32_t getClassID() const
[7163]103        { return *(uint32_t*)(data_+6); }
[5929]104      inline void setClassID(uint32_t classID_)
[7163]105        { *(uint32_t*)(data_+6) = classID_; }
[2662]106      inline uint32_t getCreatorID() const
[7163]107        { return *(uint32_t*)(data_+10); }
[5929]108      inline void setCreatorID(uint32_t creatorID_)
[7163]109        { *(uint32_t*)(data_+10) = creatorID_; }
[2662]110      inline void operator=(SynchronisableHeader& h)
111        { memcpy(data_, h.data_, getSize()); }
[1505]112  };
113
[7163]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  };
[1505]146
147  /**
148  * This class is the base class of all the Objects in the universe that need to be synchronised over the network
[2662]149  * Every class, that inherits from this class has to link the DATA THAT NEEDS TO BE SYNCHRONISED into the linked list.
[1505]150  * @author Oliver Scheuss
151  */
[2171]152  class _NetworkExport Synchronisable : virtual public OrxonoxClass{
[1505]153  public:
[1907]154    friend class packet::Gamestate;
[1505]155    virtual ~Synchronisable();
156
157    static void setClient(bool b);
[2087]158
[2171]159    static Synchronisable *fabricate(uint8_t*& mem, uint8_t mode=0x0);
[5929]160    static bool deleteObject(uint32_t objectID_);
161    static Synchronisable *getSynchronisable(uint32_t objectID_);
[1907]162    static unsigned int getNumberOfDeletedObject(){ return deletedObjects_.size(); }
[2309]163    static uint32_t popDeletedObject(){ uint32_t i = deletedObjects_.front(); deletedObjects_.pop(); return i; }
[2087]164
[5929]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_; }
[6417]170
[5929]171    void setSyncMode(uint8_t mode);
[7163]172   
173    inline uint32_t getNrOfVariables(){ return this->syncList_.size(); }
174    inline uint32_t getVarSize( VariableID ID )
175    { return this->syncList_[ID]->getSize(state_); }
[2355]176
[1505]177  protected:
[2171]178    Synchronisable(BaseObject* creator);
[2245]179    template <class T> void registerVariable(T& variable, uint8_t mode=0x1, NetworkCallbackBase *cb=0, bool bidirectional=false);
[7163]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);
[6417]182
[2415]183    void setPriority(unsigned int freq){ objectFrequency_ = freq; }
[2087]184
185
[1505]186  private:
[7163]187    uint32_t getData(uint8_t*& mem, std::vector<uint32_t>& sizes, int32_t id, uint8_t mode);
[2309]188    uint32_t getSize(int32_t id, uint8_t mode=0x0);
[2171]189    bool updateData(uint8_t*& mem, uint8_t mode=0x0, bool forceCallback=false);
[2710]190    bool doSync(int32_t id, uint8_t mode=0x0);
[6417]191
[5929]192    inline void setObjectID(uint32_t id){ this->objectID_ = id; objectMap_[this->objectID_] = this; }
193    inline void setClassID(uint32_t id){ this->classID_ = id; }
[2087]194
[5929]195    uint32_t objectID_;
196    uint32_t creatorID_;
197    uint32_t classID_;
[2087]198
[7163]199    std::vector<SynchronisableVariableBase*> syncList_;
200    std::vector<SynchronisableVariableBase*> stringList_;
[3084]201    uint32_t dataSize_; //size of all variables except strings
[2171]202    static uint8_t state_; // detemines wheter we are server (default) or client
[1505]203    bool backsync_; // if true the variables with mode > 1 will be synchronised to server (client -> server)
[1751]204    unsigned int objectFrequency_;
205    int objectMode_;
[2309]206    static std::map<uint32_t, Synchronisable *> objectMap_;
207    static std::queue<uint32_t> deletedObjects_;
[1505]208  };
[2485]209
[3084]210  template <class T> void Synchronisable::registerVariable(T& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional)
211  {
212    if (bidirectional)
213    {
[7163]214      syncList_.push_back(new SynchronisableVariableBidirectional<T>(variable, mode, cb));
215      this->dataSize_ += syncList_.back()->getSize(state_);
[3084]216    }
217    else
218    {
[7163]219      syncList_.push_back(new SynchronisableVariable<T>(variable, mode, cb));
[3084]220      if ( this->state_ == mode )
[7163]221        this->dataSize_ += syncList_.back()->getSize(state_);
[3084]222    }
223  }
[7163]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  }
[3084]241
[7163]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
[6417]253  template <> _NetworkExport void Synchronisable::registerVariable( std::string& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional);
[7163]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 );
[3084]256
257
[1505]258}
259
260#endif /* _Synchronisable_H__ */
Note: See TracBrowser for help on using the repository browser.