Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/mergeFS18/src/libraries/network/synchronisable/Synchronisable.h @ 12067

Last change on this file since 12067 was 12027, checked in by merholzl, 6 years ago

Merged Masterserver, refresh button had to be removed

  • Property svn:eol-style set to native
File size: 10.0 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  };
[1505]136
137  /**
138  * This class is the base class of all the Objects in the universe that need to be synchronised over the network
[2662]139  * Every class, that inherits from this class has to link the DATA THAT NEEDS TO BE SYNCHRONISED into the linked list.
[1505]140  * @author Oliver Scheuss
141  */
[9667]142  class _NetworkExport Synchronisable : virtual public OrxonoxInterface {
[1505]143  public:
[1907]144    friend class packet::Gamestate;
[1505]145    virtual ~Synchronisable();
146
147    static void setClient(bool b);
[2087]148
[2171]149    static Synchronisable *fabricate(uint8_t*& mem, uint8_t mode=0x0);
[5929]150    static bool deleteObject(uint32_t objectID_);
151    static Synchronisable *getSynchronisable(uint32_t objectID_);
[1907]152    static unsigned int getNumberOfDeletedObject(){ return deletedObjects_.size(); }
[2309]153    static uint32_t popDeletedObject(){ uint32_t i = deletedObjects_.front(); deletedObjects_.pop(); return i; }
[2087]154
[5929]155    inline uint32_t getObjectID() const {return this->objectID_;}
[9667]156    inline unsigned int getContextID() const {return this->contextID_;}
[5929]157    inline uint32_t getClassID() const {return this->classID_;}
158    inline unsigned int getPriority() const { return this->objectFrequency_;}
159    inline uint8_t getSyncMode() const { return this->objectMode_; }
[6417]160
[5929]161    void setSyncMode(uint8_t mode);
[7163]162   
163    inline uint32_t getNrOfVariables(){ return this->syncList_.size(); }
164    inline uint32_t getVarSize( VariableID ID )
165    { return this->syncList_[ID]->getSize(state_); }
[2355]166
[1505]167  protected:
[9667]168    Synchronisable(Context* context);
[11071]169    template <class T> void registerVariable(T& variable, uint8_t mode=0x1, NetworkCallbackBase *cb=nullptr, bool bidirectional=false);
170    template <class T> void registerVariable(std::set<T>& variable, uint8_t mode=0x1, NetworkCallbackBase *cb=nullptr, bool bidirectional=false);
[7163]171    template <class T> void unregisterVariable(T& var);
[6417]172
[2415]173    void setPriority(unsigned int freq){ objectFrequency_ = freq; }
[9667]174    uint32_t findContextID(Context* context);
[2087]175
[1505]176  private:
[7163]177    uint32_t getData(uint8_t*& mem, std::vector<uint32_t>& sizes, int32_t id, uint8_t mode);
[2309]178    uint32_t getSize(int32_t id, uint8_t mode=0x0);
[2171]179    bool updateData(uint8_t*& mem, uint8_t mode=0x0, bool forceCallback=false);
[8329]180    bool doSync(/*int32_t id,*/ uint8_t mode=0x0);
181    bool doReceive( uint8_t mode );
[6417]182
[5929]183    inline void setObjectID(uint32_t id){ this->objectID_ = id; objectMap_[this->objectID_] = this; }
184    inline void setClassID(uint32_t id){ this->classID_ = id; }
[2087]185
[5929]186    uint32_t objectID_;
[9667]187    uint32_t contextID_;
[5929]188    uint32_t classID_;
[2087]189
[7163]190    std::vector<SynchronisableVariableBase*> syncList_;
191    std::vector<SynchronisableVariableBase*> stringList_;
[3084]192    uint32_t dataSize_; //size of all variables except strings
[2171]193    static uint8_t state_; // detemines wheter we are server (default) or client
[1505]194    bool backsync_; // if true the variables with mode > 1 will be synchronised to server (client -> server)
[1751]195    unsigned int objectFrequency_;
196    int objectMode_;
[2309]197    static std::map<uint32_t, Synchronisable *> objectMap_;
198    static std::queue<uint32_t> deletedObjects_;
[1505]199  };
[2485]200
[11071]201  namespace detail
[3084]202  {
[11071]203    template <class T, bool = std::is_enum<T>::value>
204    struct UnderlyingType;
205    template <class T>
206    struct UnderlyingType<T, true> { typedef typename std::underlying_type<T>::type type; };
207    template <class T>
208    struct UnderlyingType<T, false> { typedef T type; };
209  }
210
211  template <class T>
212  void Synchronisable::registerVariable(T& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional)
213  {
214    typedef typename detail::UnderlyingType<T>::type UnderlyingType;
[3084]215    if (bidirectional)
216    {
[11071]217      syncList_.push_back(new SynchronisableVariableBidirectional<UnderlyingType>(reinterpret_cast<UnderlyingType&>(variable), mode, cb));
[7163]218      this->dataSize_ += syncList_.back()->getSize(state_);
[3084]219    }
220    else
221    {
[11071]222      syncList_.push_back(new SynchronisableVariable<UnderlyingType>(reinterpret_cast<UnderlyingType&>(variable), mode, cb));
[3084]223      if ( this->state_ == mode )
[7163]224        this->dataSize_ += syncList_.back()->getSize(state_);
[3084]225    }
226  }
[7163]227 
[11071]228  template <class T>
229  void Synchronisable::unregisterVariable(T& variable)
[8314]230  {
[7163]231    std::vector<SynchronisableVariableBase*>::iterator it = syncList_.begin();
[8314]232    while(it!=syncList_.end())
233    {
234      if( ((*it)->getReference()) == &variable )
235      {
[7163]236        this->dataSize_ -= (*it)->getSize(Synchronisable::state_);
237        delete (*it);
238        syncList_.erase(it);
239        return;
240      }
241      else
242        it++;
243    }
[8858]244    orxout(internal_error, context::network) << "Tried to unregister not registered variable" << endl;
[8314]245    assert(false); //if we reach this point something went wrong:
[7163]246    // the variable has not been registered before
247  }
[3084]248
[11071]249  template <class T>
250  void Synchronisable::registerVariable( std::set<T>& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional)
[7163]251  {
[11071]252    typedef typename detail::UnderlyingType<T>::type UnderlyingType;
[7163]253    SynchronisableVariableBase* sv;
254    if (bidirectional)
[11071]255      sv = new SynchronisableVariableBidirectional<std::set<UnderlyingType>>(reinterpret_cast<std::set<UnderlyingType>&>(variable), mode, cb);
[7163]256    else
[11071]257      sv = new SynchronisableVariable<std::set<UnderlyingType>>(reinterpret_cast<std::set<UnderlyingType>&>(variable), mode, cb);
[7163]258    syncList_.push_back(sv);
259    stringList_.push_back(sv);
260  }
261
[6417]262  template <> _NetworkExport void Synchronisable::registerVariable( std::string& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional);
[11071]263//   template <class T> _NetworkExport void Synchronisable::registerVariable<std::set<T>>( std::set<T>& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional);
[7163]264  template <> _NetworkExport void Synchronisable::unregisterVariable( std::string& variable );
[3084]265
266
[1505]267}
268
269#endif /* _Synchronisable_H__ */
Note: See TracBrowser for help on using the repository browser.