Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp5/src/network/synchronisable/SynchronisableVariable.h @ 3209

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

Cleanup in network plus a few dependency reductions (no enet-function inlines, using enum PacketFlag instead of the enet version)

  • Property svn:eol-style set to native
File size: 8.0 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
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29
30#ifndef _SynchronisableVariable_H__
31#define _SynchronisableVariable_H__
32
33#include "network/NetworkPrereqs.h"
34
35#include <cassert>
36#include "util/Serialise.h"
37#include "core/GameMode.h"
38#include "network/synchronisable/NetworkCallbackManager.h"
39
40namespace orxonox{
41 
42  namespace variableDirection{
43    enum syncdirection{
44      toclient=0x1,
45      toserver=0x2
46    };
47    enum bidirectional{
48      serverMaster=0x1,
49      clientMaster=0x2
50    };
51  }
52 
53  class _NetworkExport SynchronisableVariableBase
54  {
55    public:
56      virtual uint32_t getData(uint8_t*& mem, uint8_t mode)=0;
57      virtual void putData(uint8_t*& mem, uint8_t mode, bool forceCallback = false)=0;
58      virtual uint32_t getSize(uint8_t mode)=0;
59      virtual void* getReference()=0;
60      virtual uint8_t getMode()=0;
61      virtual ~SynchronisableVariableBase() {}
62    protected:
63      static uint8_t state_;
64  };
65
66  template <class T>
67  class SynchronisableVariable: public SynchronisableVariableBase
68  {
69    public:
70      SynchronisableVariable(T& variable, uint8_t syncDirection=variableDirection::toclient, NetworkCallbackBase *cb=0);
71      virtual ~SynchronisableVariable();
72
73      virtual inline uint8_t getMode(){ return mode_; }
74      virtual inline uint32_t getData(uint8_t*& mem, uint8_t mode);
75      virtual inline void putData(uint8_t*& mem, uint8_t mode, bool forceCallback = false);
76      virtual inline uint32_t getSize(uint8_t mode);
77      virtual inline void* getReference(){ return (void *)&this->variable_; }
78    protected:
79     
80      T& variable_;
81      uint8_t mode_;
82      NetworkCallbackBase *callback_;
83  };
84 
85  template <class T>
86  class SynchronisableVariableBidirectional: public SynchronisableVariable<T>
87  {
88    public:
89      SynchronisableVariableBidirectional(T& variable, uint8_t master=variableDirection::serverMaster, NetworkCallbackBase *cb=0);
90      virtual ~SynchronisableVariableBidirectional();
91     
92      virtual inline uint8_t getMode(){ return 0x3; } //this basically is a hack ^^
93      virtual inline uint32_t getData(uint8_t*& mem, uint8_t mode);
94      virtual void putData(uint8_t*& mem, uint8_t mode, bool forceCallback = false);
95      virtual inline uint32_t getSize(uint8_t mode);
96    private:
97      T varBuffer_;
98      uint8_t varReference_;
99  };
100
101  // ================= Unidirectional Part ===============
102
103  template <class T> SynchronisableVariable<T>::SynchronisableVariable(T& variable, uint8_t syncDirection, NetworkCallbackBase *cb):
104      variable_( variable ), mode_( syncDirection ), callback_( cb )
105  {
106    if ( state_ == 0x0 )
107    {
108      state_ = GameMode::isMaster() ? 0x1 : 0x2;  // set the appropriate mode here
109    }
110  }
111 
112  template <class T> SynchronisableVariable<T>::~SynchronisableVariable()
113  {
114    if (this->callback_ != 0)
115      NetworkCallbackManager::deleteCallback(this->callback_); //safe call for deletion
116  }
117
118  template <class T> inline uint32_t SynchronisableVariable<T>::getData(uint8_t*& mem, uint8_t mode)
119  {
120    if ( mode == this->mode_ )
121    {
122      saveAndIncrease( this->variable_, mem );
123      return returnSize( this->variable_ );
124    }
125    else
126      return 0;
127  }
128
129  template <class T> void SynchronisableVariable<T>::putData(uint8_t*& mem, uint8_t mode, bool forceCallback)
130  {
131    assert ( mode == 0x1 || mode == 0x2 );
132    bool callback = false;
133    if ( mode == this->mode_ ) //don't do anything
134      return;
135  // check whether we need to consider a callback
136    if ( this->callback_ != 0 )
137    {
138      if( forceCallback || !checkEquality( this->variable_, mem ) )
139        callback = true;
140    }
141  // write the data
142    loadAndIncrease( this->variable_, mem );
143  // now do a callback if neccessary
144    if ( callback )
145      NetworkCallbackManager::triggerCallback( this->callback_ );
146  }
147
148  template <class T> inline uint32_t SynchronisableVariable<T>::getSize(uint8_t mode)
149  {
150    if ( mode == this->mode_ )
151      return returnSize( this->variable_ );
152    else
153      return 0;
154  }
155
156
157
158
159// ================= Bidirectional Part ================
160
161    template <class T> SynchronisableVariableBidirectional<T>::SynchronisableVariableBidirectional(T& variable, uint8_t master, NetworkCallbackBase *cb):
162    SynchronisableVariable<T>( variable, master, cb ), varBuffer_( variable ), varReference_( 0 )
163    {
164    }
165
166    template <class T> SynchronisableVariableBidirectional<T>::~SynchronisableVariableBidirectional()
167    {
168    }
169
170    template <class T> uint32_t SynchronisableVariableBidirectional<T>::getData(uint8_t*& mem, uint8_t mode)
171    {
172      if ( this->mode_ == mode )
173      {   // we are master for this variable and have to check whether to change the varReference
174        if( this->varBuffer_ != this->variable_ )
175        {
176          this->varReference_++;
177          memcpy((void*)&this->varBuffer_, &this->variable_, sizeof(this->variable_));
178        }
179      }
180  // write the reference number to the stream
181      *static_cast<uint8_t*>(mem) = varReference_;
182      mem += sizeof(this->varReference_);
183  // now write the content
184      saveAndIncrease( this->variable_, mem );
185      return SynchronisableVariableBidirectional::getSize(mode);
186    }
187
188    template <class T> void SynchronisableVariableBidirectional<T>::putData(uint8_t*& mem, uint8_t mode, bool forceCallback)
189    {
190      bool callback = false;
191      if ( this->mode_ == mode )
192      {   //        MASTER
193        // check that the client (source of the data) has a recent version of this variable
194        if ( *static_cast<uint8_t*>(mem) != this->varReference_ )
195        { // wrong reference number, so discard the data
196//           COUT(0) << "discharding data" << endl;
197          mem += getSize( mode ); // SynchronisableVariableBidirectional::getSize returns size of variable + reference
198          return;
199        }
200        else{
201          // apply data
202          if ( checkEquality( this->variable_, mem+sizeof(varReference_) )==true )
203          {
204            mem += getSize( mode );
205            return;
206          }
207          else
208          {
209            mem += sizeof(varReference_);
210            memcpy((void*)&this->varBuffer_, &this->variable_, sizeof(T));
211            if ( this->callback_ != 0 )
212              callback = true;
213          }
214        }
215      }
216      else
217      {   // we are slave for this variable
218        if (*static_cast<uint8_t*>(mem) == this->varReference_ && !forceCallback)
219        {
220          mem += getSize( mode ); //just skip the variable because nothing changed
221          return;
222        }
223        else
224        {
225          this->varReference_ = *static_cast<uint8_t*>(mem);
226          mem += sizeof(varReference_);
227          if ( checkEquality( this->variable_, mem ) == false )
228          {
229            // value changed so remark for callback
230            if ( this->callback_ != 0 )
231              callback = true;
232          }
233        }
234      }
235  // now write the data
236      loadAndIncrease(this->variable_, mem);
237  // now do a callback if neccessary
238      if ( callback )
239        NetworkCallbackManager::triggerCallback( this->callback_ );
240    }
241
242    template <class T> inline uint32_t SynchronisableVariableBidirectional<T>::getSize(uint8_t mode)
243    {
244      return returnSize( this->variable_ ) + sizeof(varReference_);
245    }
246 
247
248}
249
250
251#endif /* _SynchronisableVariable_H__ */
Note: See TracBrowser for help on using the repository browser.