Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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