Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8429 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
Line 
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
32#include "network/NetworkPrereqs.h"
33
34#include <cassert>
35#include <cstring>
36#include <vector>
37#include <map>
38#include <queue>
39#include <set>
40
41#include "util/mbool.h"
42#include "core/OrxonoxClass.h"
43#include "SynchronisableVariable.h"
44#include "NetworkCallback.h"
45
46
47namespace orxonox
48{
49
50  namespace ObjectDirection{
51    enum Value{
52      ToClient=0x1,
53      ToServer=0x2,
54      Bidirectional=0x3
55    };
56  }
57
58  namespace Priority{
59    enum Value{
60      VeryHigh    = -100,
61      High        = -15,
62      Normal      = 0,
63      Low         = 15,
64      VeryLow     = 100
65    };
66  }
67
68    /**
69   * @brief: stores information about a Synchronisable (light version)
70   *
71   * This class stores the information about a Synchronisable (objectID_, dataSize)
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 variables are stored in diff mode
75   * Byte 5 to 8: objectID_
76   */
77  class _NetworkExport SynchronisableHeaderLight
78  {
79    protected:
80      uint8_t* data_;
81    public:
82      SynchronisableHeaderLight(uint8_t* data)
83        { data_ = data; }
84      inline static uint32_t getSize()
85        { return 6; }
86      inline uint16_t getDataSize() const
87        { return (*(uint16_t*)data_) & 0x7FFF; } //only use the first 31 bits
88      inline void setDataSize(uint16_t size)
89        { *(uint16_t*)(data_) = (size & 0x7FFFFFFF) | (*(uint16_t*)(data_) & 0x8000 ); }
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 ); }
94      inline uint32_t getObjectID() const
95        { return *(uint32_t*)(data_+2); }
96      inline void setObjectID(uint32_t objectID_)
97        { *(uint32_t*)(data_+2) = objectID_; }
98      inline void operator=(SynchronisableHeaderLight& h)
99        { memcpy(data_, h.data_, SynchronisableHeaderLight::getSize()); }
100  };
101 
102  typedef uint8_t VariableID;
103 
104  /**
105   * @brief: stores information about a Synchronisable
106   *
107   * This class stores the information about a Synchronisable (objectID_, classID_, creatorID_, dataSize)
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_
112   * Byte 9 to 12: classID_
113   * Byte 13 to 16: creatorID_
114   */
115  class _NetworkExport SynchronisableHeader: public SynchronisableHeaderLight
116  {
117    public:
118      SynchronisableHeader(uint8_t* data): SynchronisableHeaderLight(data)
119        {}
120      inline static uint32_t getSize()
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_; }
130      inline void operator=(SynchronisableHeader& h)
131        { memcpy(data_, h.data_, getSize()); }
132  };
133 
134//   inline void operator=(SynchronisableHeaderLight& h1, SynchronisableHeader& h2)
135//   {
136//     memcpy(h1.data_, h2.data_, h1.getSize());
137//   }
138
139  /**
140  * This class is the base class of all the Objects in the universe that need to be synchronised over the network
141  * Every class, that inherits from this class has to link the DATA THAT NEEDS TO BE SYNCHRONISED into the linked list.
142  * @author Oliver Scheuss
143  */
144  class _NetworkExport Synchronisable : virtual public OrxonoxClass{
145  public:
146    friend class packet::Gamestate;
147    virtual ~Synchronisable();
148
149    static void setClient(bool b);
150
151    static Synchronisable *fabricate(uint8_t*& mem, uint8_t mode=0x0);
152    static bool deleteObject(uint32_t objectID_);
153    static Synchronisable *getSynchronisable(uint32_t objectID_);
154    static unsigned int getNumberOfDeletedObject(){ return deletedObjects_.size(); }
155    static uint32_t popDeletedObject(){ uint32_t i = deletedObjects_.front(); deletedObjects_.pop(); return i; }
156
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_; }
162
163    void setSyncMode(uint8_t mode);
164   
165    inline uint32_t getNrOfVariables(){ return this->syncList_.size(); }
166    inline uint32_t getVarSize( VariableID ID )
167    { return this->syncList_[ID]->getSize(state_); }
168
169  protected:
170    Synchronisable(BaseObject* creator);
171    template <class T> void registerVariable(T& variable, uint8_t mode=0x1, NetworkCallbackBase *cb=0, bool bidirectional=false);
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);
174
175    void setPriority(unsigned int freq){ objectFrequency_ = freq; }
176
177
178  private:
179    uint32_t getData(uint8_t*& mem, std::vector<uint32_t>& sizes, int32_t id, uint8_t mode);
180    uint32_t getSize(int32_t id, uint8_t mode=0x0);
181    bool updateData(uint8_t*& mem, uint8_t mode=0x0, bool forceCallback=false);
182    bool doSync(/*int32_t id,*/ uint8_t mode=0x0);
183    bool doReceive( uint8_t mode );
184
185    inline void setObjectID(uint32_t id){ this->objectID_ = id; objectMap_[this->objectID_] = this; }
186    inline void setClassID(uint32_t id){ this->classID_ = id; }
187
188    uint32_t objectID_;
189    uint32_t creatorID_;
190    uint32_t classID_;
191
192    std::vector<SynchronisableVariableBase*> syncList_;
193    std::vector<SynchronisableVariableBase*> stringList_;
194    uint32_t dataSize_; //size of all variables except strings
195    static uint8_t state_; // detemines wheter we are server (default) or client
196    bool backsync_; // if true the variables with mode > 1 will be synchronised to server (client -> server)
197    unsigned int objectFrequency_;
198    int objectMode_;
199    static std::map<uint32_t, Synchronisable *> objectMap_;
200    static std::queue<uint32_t> deletedObjects_;
201  };
202
203  template <class T> void Synchronisable::registerVariable(T& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional)
204  {
205    if (bidirectional)
206    {
207      syncList_.push_back(new SynchronisableVariableBidirectional<T>(variable, mode, cb));
208      this->dataSize_ += syncList_.back()->getSize(state_);
209    }
210    else
211    {
212      syncList_.push_back(new SynchronisableVariable<T>(variable, mode, cb));
213      if ( this->state_ == mode )
214        this->dataSize_ += syncList_.back()->getSize(state_);
215    }
216  }
217 
218  template <class T> void Synchronisable::unregisterVariable(T& variable)
219  {
220    std::vector<SynchronisableVariableBase*>::iterator it = syncList_.begin();
221    while(it!=syncList_.end())
222    {
223      if( ((*it)->getReference()) == &variable )
224      {
225        this->dataSize_ -= (*it)->getSize(Synchronisable::state_);
226        delete (*it);
227        syncList_.erase(it);
228        return;
229      }
230      else
231        it++;
232    }
233    COUT(1) << "Tried to unregister not registered variable" << endl;
234    assert(false); //if we reach this point something went wrong:
235    // the variable has not been registered before
236  }
237
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
249  template <> _NetworkExport void Synchronisable::registerVariable( std::string& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional);
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 );
252
253
254}
255
256#endif /* _Synchronisable_H__ */
Note: See TracBrowser for help on using the repository browser.