Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4862 was 3304, checked in by rgrieder, 15 years ago

Merged netp6 branch back to the trunk.

  • Property svn:eol-style set to native
File size: 8.7 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>
[3214]39
[2245]40#include "util/mbool.h"
[1505]41#include "core/OrxonoxClass.h"
[3214]42#include "SynchronisableVariable.h"
[1534]43#include "NetworkCallback.h"
[1505]44
[1907]45
[2171]46namespace orxonox
[1505]47{
[2087]48
[3280]49  namespace ObjectDirection{
50    enum Value{
51      ToClient=0x1,
52      ToServer=0x2,
53      Bidirectional=0x3
[1907]54    };
55  }
[2485]56
[3280]57  namespace Priority{
58    enum Value{
59      VeryHigh    = -100,
60      High        = -15,
61      Normal      = 0,
62      Low         = 15,
63      VeryLow     = 100
[2415]64    };
65  }
[2087]66
[2662]67  /**
68   * @brief: stores information about a Synchronisable
69   *
70   * This class stores the information about a Synchronisable (objectID, classID, creatorID, dataSize)
71   * in an emulated bitset.
72   * Bit 1 to 31 store the size of the Data the synchronisable consumes in the stream
73   * Bit 32 is a bool and defines whether the data is actually stored or is just filled up with 0
74   * Byte 5 to 8: objectID
75   * Byte 9 to 12: classID
76   * Byte 13 to 16: creatorID
77   */
78  class _NetworkExport SynchronisableHeader{
79    private:
80      uint8_t *data_;
81    public:
82      SynchronisableHeader(uint8_t* data)
83        { data_ = data; }
84      inline static uint32_t getSize()
85        { return 16; }
86      inline uint32_t getDataSize() const
87        { return (*(uint32_t*)data_) & 0x7FFFFFFF; } //only use the first 31 bits
88      inline void setDataSize(uint32_t size)
89        { *(uint32_t*)(data_) = (size & 0x7FFFFFFF) | (*(uint32_t*)(data_) & 0x80000000 ); }
90      inline bool isDataAvailable() const
91        { return ( (*(uint32_t*)data_) & 0x80000000 ) == 0x80000000; }
92      inline void setDataAvailable( bool b)
93        { *(uint32_t*)(data_) = (b << 31) | (*(uint32_t*)(data_) & 0x7FFFFFFF ); }
94      inline uint32_t getObjectID() const
95        { return *(uint32_t*)(data_+4); }
96      inline void setObjectID(uint32_t objectID)
97        { *(uint32_t*)(data_+4) = objectID; }
98      inline uint32_t getClassID() const
99        { return *(uint32_t*)(data_+8); }
100      inline void setClassID(uint32_t classID)
101        { *(uint32_t*)(data_+8) = classID; }
102      inline uint32_t getCreatorID() const
103        { return *(uint32_t*)(data_+12); }
104      inline void setCreatorID(uint32_t creatorID)
105        { *(uint32_t*)(data_+12) = creatorID; }
106      inline void operator=(SynchronisableHeader& h)
107        { memcpy(data_, h.data_, getSize()); }
[1505]108  };
109
110
111  /**
112  * This class is the base class of all the Objects in the universe that need to be synchronised over the network
[2662]113  * Every class, that inherits from this class has to link the DATA THAT NEEDS TO BE SYNCHRONISED into the linked list.
[1505]114  * @author Oliver Scheuss
115  */
[2171]116  class _NetworkExport Synchronisable : virtual public OrxonoxClass{
[1505]117  public:
[1907]118    friend class packet::Gamestate;
[1505]119    virtual ~Synchronisable();
120
121    static void setClient(bool b);
[2087]122
[2171]123    static Synchronisable *fabricate(uint8_t*& mem, uint8_t mode=0x0);
[2309]124    static bool deleteObject(uint32_t objectID);
125    static Synchronisable *getSynchronisable(uint32_t objectID);
[1907]126    static unsigned int getNumberOfDeletedObject(){ return deletedObjects_.size(); }
[2309]127    static uint32_t popDeletedObject(){ uint32_t i = deletedObjects_.front(); deletedObjects_.pop(); return i; }
[2087]128
[2485]129    inline uint32_t getObjectID() const {return objectID;}
130    inline unsigned int getCreatorID() const {return creatorID;}
131    inline uint32_t getClassID() const {return classID;}
132    inline unsigned int getPriority() const { return objectFrequency_;}
[2355]133
[1505]134  protected:
[2171]135    Synchronisable(BaseObject* creator);
[2245]136    template <class T> void registerVariable(T& variable, uint8_t mode=0x1, NetworkCallbackBase *cb=0, bool bidirectional=false);
[3084]137    //template <class T> void unregisterVariable(T& var);
[2171]138    void setObjectMode(uint8_t mode);
[2415]139    void setPriority(unsigned int freq){ objectFrequency_ = freq; }
[2087]140
141
[1505]142  private:
[3084]143    uint32_t getData(uint8_t*& men, int32_t id, uint8_t mode=0x0);
[2309]144    uint32_t getSize(int32_t id, uint8_t mode=0x0);
[2171]145    bool updateData(uint8_t*& mem, uint8_t mode=0x0, bool forceCallback=false);
[2710]146    bool isMyData(uint8_t* mem);
147    bool doSync(int32_t id, uint8_t mode=0x0);
[2087]148
[2309]149    uint32_t objectID;
150    uint32_t creatorID;
151    uint32_t classID;
[2087]152
[3084]153    std::vector<SynchronisableVariableBase*> syncList;
154    std::vector<SynchronisableVariableBase*> stringList;
155    uint32_t dataSize_; //size of all variables except strings
[2171]156    static uint8_t state_; // detemines wheter we are server (default) or client
[1505]157    bool backsync_; // if true the variables with mode > 1 will be synchronised to server (client -> server)
[1751]158    unsigned int objectFrequency_;
159    int objectMode_;
[2309]160    static std::map<uint32_t, Synchronisable *> objectMap_;
161    static std::queue<uint32_t> deletedObjects_;
[1505]162  };
[2485]163
[2245]164  // ================= Specialisation declarations
[3084]165 
166//   template <> _NetworkExport void Synchronisable::registerVariable( const std::string& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional);
167  template <> _NetworkExport void Synchronisable::registerVariable( std::string& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional);
[2307]168  template <> _NetworkExport void Synchronisable::registerVariable( const ColourValue& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional);
169  template <> _NetworkExport void Synchronisable::registerVariable( ColourValue& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional);
170  template <> _NetworkExport void Synchronisable::registerVariable( const Vector2& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional);
171  template <> _NetworkExport void Synchronisable::registerVariable( Vector2& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional);
172  template <> _NetworkExport void Synchronisable::registerVariable( const Vector3& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional);
173  template <> _NetworkExport void Synchronisable::registerVariable( Vector3& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional);
174  template <> _NetworkExport void Synchronisable::registerVariable( const Vector4& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional);
175  template <> _NetworkExport void Synchronisable::registerVariable( Vector4& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional);
176  template <> _NetworkExport void Synchronisable::registerVariable( mbool& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional);
177  template <> _NetworkExport void Synchronisable::registerVariable( const Quaternion& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional);
178  template <> _NetworkExport void Synchronisable::registerVariable( Quaternion& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional);
[3084]179 
180  template <class T> void Synchronisable::registerVariable(T& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional)
181  {
182    if (bidirectional)
183    {
184      syncList.push_back(new SynchronisableVariableBidirectional<const T>(variable, mode, cb));
185      this->dataSize_ += syncList.back()->getSize(state_);
186    }
187    else
188    {
189      syncList.push_back(new SynchronisableVariable<const T>(variable, mode, cb));
190      if ( this->state_ == mode )
191        this->dataSize_ += syncList.back()->getSize(state_);
192    }
193  }
194 
195
196
197//   template <class T> void Synchronisable::unregisterVariable(T& var){
198//     std::vector<SynchronisableVariableBase*>::iterator it = syncList.begin();
199//     while(it!=syncList.end()){
200//       if( ((*it)->getReference()) == &var ){
201//         delete (*it);
202//         syncList.erase(it);
203//         return;
204//       }
205//       else
206//         it++;
207//     }
208//     bool unregistered_nonexistent_variable = false;
209//     assert(unregistered_nonexistent_variable); //if we reach this point something went wrong:
210//     // the variable has not been registered before
211//   }
212
213 
[1505]214}
215
216#endif /* _Synchronisable_H__ */
Note: See TracBrowser for help on using the repository browser.