Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation3/src/libraries/network/synchronisable/Synchronisable.h @ 7144

Last change on this file since 7144 was 7127, checked in by rgrieder, 15 years ago

Removed excess white space at the end of lines.

  • Property svn:eol-style set to native
File size: 7.3 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>
[7105]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  }
[2087]67
[2662]68  /**
[6417]69   * @brief: stores information about a Synchronisable
70   *
[5929]71   * This class stores the information about a Synchronisable (objectID_, classID_, creatorID_, dataSize)
[2662]72   * in an emulated bitset.
73   * Bit 1 to 31 store the size of the Data the synchronisable consumes in the stream
74   * Bit 32 is a bool and defines whether the data is actually stored or is just filled up with 0
[5929]75   * Byte 5 to 8: objectID_
76   * Byte 9 to 12: classID_
77   * Byte 13 to 16: creatorID_
[2662]78   */
79  class _NetworkExport SynchronisableHeader{
80    private:
81      uint8_t *data_;
82    public:
83      SynchronisableHeader(uint8_t* data)
84        { data_ = data; }
85      inline static uint32_t getSize()
86        { return 16; }
87      inline uint32_t getDataSize() const
88        { return (*(uint32_t*)data_) & 0x7FFFFFFF; } //only use the first 31 bits
89      inline void setDataSize(uint32_t size)
90        { *(uint32_t*)(data_) = (size & 0x7FFFFFFF) | (*(uint32_t*)(data_) & 0x80000000 ); }
91      inline bool isDataAvailable() const
92        { return ( (*(uint32_t*)data_) & 0x80000000 ) == 0x80000000; }
93      inline void setDataAvailable( bool b)
94        { *(uint32_t*)(data_) = (b << 31) | (*(uint32_t*)(data_) & 0x7FFFFFFF ); }
95      inline uint32_t getObjectID() const
96        { return *(uint32_t*)(data_+4); }
[5929]97      inline void setObjectID(uint32_t objectID_)
98        { *(uint32_t*)(data_+4) = objectID_; }
[2662]99      inline uint32_t getClassID() const
100        { return *(uint32_t*)(data_+8); }
[5929]101      inline void setClassID(uint32_t classID_)
102        { *(uint32_t*)(data_+8) = classID_; }
[2662]103      inline uint32_t getCreatorID() const
104        { return *(uint32_t*)(data_+12); }
[5929]105      inline void setCreatorID(uint32_t creatorID_)
106        { *(uint32_t*)(data_+12) = creatorID_; }
[2662]107      inline void operator=(SynchronisableHeader& h)
108        { memcpy(data_, h.data_, getSize()); }
[1505]109  };
110
111
112  /**
113  * This class is the base class of all the Objects in the universe that need to be synchronised over the network
[2662]114  * Every class, that inherits from this class has to link the DATA THAT NEEDS TO BE SYNCHRONISED into the linked list.
[1505]115  * @author Oliver Scheuss
116  */
[2171]117  class _NetworkExport Synchronisable : virtual public OrxonoxClass{
[1505]118  public:
[1907]119    friend class packet::Gamestate;
[1505]120    virtual ~Synchronisable();
121
122    static void setClient(bool b);
[2087]123
[2171]124    static Synchronisable *fabricate(uint8_t*& mem, uint8_t mode=0x0);
[5929]125    static bool deleteObject(uint32_t objectID_);
126    static Synchronisable *getSynchronisable(uint32_t objectID_);
[1907]127    static unsigned int getNumberOfDeletedObject(){ return deletedObjects_.size(); }
[2309]128    static uint32_t popDeletedObject(){ uint32_t i = deletedObjects_.front(); deletedObjects_.pop(); return i; }
[2087]129
[5929]130    inline uint32_t getObjectID() const {return this->objectID_;}
131    inline unsigned int getCreatorID() const {return this->creatorID_;}
132    inline uint32_t getClassID() const {return this->classID_;}
133    inline unsigned int getPriority() const { return this->objectFrequency_;}
134    inline uint8_t getSyncMode() const { return this->objectMode_; }
[6417]135
[5929]136    void setSyncMode(uint8_t mode);
[2355]137
[1505]138  protected:
[2171]139    Synchronisable(BaseObject* creator);
[2245]140    template <class T> void registerVariable(T& variable, uint8_t mode=0x1, NetworkCallbackBase *cb=0, bool bidirectional=false);
[7105]141    template <class T> void registerVariable(std::set<T>& variable, uint8_t mode=0x1, NetworkCallbackBase *cb=0, bool bidirectional=false);
[6417]142
[2415]143    void setPriority(unsigned int freq){ objectFrequency_ = freq; }
[2087]144
145
[1505]146  private:
[3084]147    uint32_t getData(uint8_t*& men, int32_t id, uint8_t mode=0x0);
[2309]148    uint32_t getSize(int32_t id, uint8_t mode=0x0);
[2171]149    bool updateData(uint8_t*& mem, uint8_t mode=0x0, bool forceCallback=false);
[2710]150    bool isMyData(uint8_t* mem);
151    bool doSync(int32_t id, uint8_t mode=0x0);
[6417]152
[5929]153    inline void setObjectID(uint32_t id){ this->objectID_ = id; objectMap_[this->objectID_] = this; }
154    inline void setClassID(uint32_t id){ this->classID_ = id; }
[2087]155
[5929]156    uint32_t objectID_;
157    uint32_t creatorID_;
158    uint32_t classID_;
[2087]159
[3084]160    std::vector<SynchronisableVariableBase*> syncList;
161    std::vector<SynchronisableVariableBase*> stringList;
162    uint32_t dataSize_; //size of all variables except strings
[2171]163    static uint8_t state_; // detemines wheter we are server (default) or client
[1505]164    bool backsync_; // if true the variables with mode > 1 will be synchronised to server (client -> server)
[1751]165    unsigned int objectFrequency_;
166    int objectMode_;
[2309]167    static std::map<uint32_t, Synchronisable *> objectMap_;
168    static std::queue<uint32_t> deletedObjects_;
[1505]169  };
[2485]170
[3084]171  template <class T> void Synchronisable::registerVariable(T& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional)
172  {
173    if (bidirectional)
174    {
[6417]175      syncList.push_back(new SynchronisableVariableBidirectional<T>(variable, mode, cb));
[3084]176      this->dataSize_ += syncList.back()->getSize(state_);
177    }
178    else
179    {
[6417]180      syncList.push_back(new SynchronisableVariable<T>(variable, mode, cb));
[3084]181      if ( this->state_ == mode )
182        this->dataSize_ += syncList.back()->getSize(state_);
183    }
184  }
[7127]185
[7105]186  template <class T> void Synchronisable::registerVariable( std::set<T>& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional)
187  {
188    SynchronisableVariableBase* sv;
189    if (bidirectional)
190      sv = new SynchronisableVariableBidirectional<std::set<T> >(variable, mode, cb);
191    else
192      sv = new SynchronisableVariable<std::set<T> >(variable, mode, cb);
193    syncList.push_back(sv);
194    stringList.push_back(sv);
195  }
[3084]196
[6417]197  template <> _NetworkExport void Synchronisable::registerVariable( std::string& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional);
[7105]198//   template <class T> _NetworkExport void Synchronisable::registerVariable<std::set<T> >( std::set<T>& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional);
[3084]199
200
[1505]201}
202
203#endif /* _Synchronisable_H__ */
Note: See TracBrowser for help on using the repository browser.