Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11948 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

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