Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8474 was 8329, checked in by scheusso, 14 years ago

preparing countermeasures against clients sending unwanted data

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