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) 2008 |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | |
---|
30 | #ifndef _NETWORK_SYNCHRONISABLEVARIABLE__ |
---|
31 | #define _NETWORK_SYNCHRONISABLEVARIABLE__ |
---|
32 | |
---|
33 | #include "network/NetworkPrereqs.h" |
---|
34 | |
---|
35 | #include <string> |
---|
36 | #include <cassert> |
---|
37 | #include "util/Math.h" |
---|
38 | #include "network/synchronisable/NetworkCallback.h" |
---|
39 | #include "network/synchronisable/NetworkCallbackManager.h" |
---|
40 | |
---|
41 | namespace orxonox{ |
---|
42 | |
---|
43 | namespace variableDirection{ |
---|
44 | enum syncdirection{ |
---|
45 | toclient=0x1, |
---|
46 | toserver=0x2 |
---|
47 | }; |
---|
48 | enum bidirectional{ |
---|
49 | serverMaster=0x1, |
---|
50 | clientMaster=0x2 |
---|
51 | }; |
---|
52 | } |
---|
53 | |
---|
54 | class _NetworkExport SynchronisableVariableBase |
---|
55 | { |
---|
56 | public: |
---|
57 | virtual void getData(uint8_t*& mem, uint8_t mode)=0; |
---|
58 | virtual void putData(uint8_t*& mem, uint8_t mode, bool forceCallback = false)=0; |
---|
59 | virtual uint32_t getSize(uint8_t mode)=0; |
---|
60 | virtual void* getReference()=0; |
---|
61 | virtual uint8_t getMode()=0; |
---|
62 | virtual ~SynchronisableVariableBase() {} |
---|
63 | protected: |
---|
64 | static void setState(); |
---|
65 | static uint8_t state_; |
---|
66 | }; |
---|
67 | |
---|
68 | template <class T> |
---|
69 | class SynchronisableVariable: public SynchronisableVariableBase |
---|
70 | { |
---|
71 | public: |
---|
72 | SynchronisableVariable(T& variable, uint8_t syncDirection=variableDirection::toclient, NetworkCallbackBase *cb=0); |
---|
73 | virtual ~SynchronisableVariable(); |
---|
74 | |
---|
75 | virtual inline uint8_t getMode(){ return mode_; } |
---|
76 | virtual inline void getData(uint8_t*& mem, uint8_t mode); |
---|
77 | virtual inline void putData(uint8_t*& mem, uint8_t mode, bool forceCallback = false); |
---|
78 | virtual inline uint32_t getSize(uint8_t mode); |
---|
79 | virtual inline void* getReference(){ return (void *)&this->variable_; } |
---|
80 | protected: |
---|
81 | bool checkEquality(uint8_t* mem); |
---|
82 | void setAndIncrease(uint8_t*& mem); |
---|
83 | void getAndIncrease(uint8_t*& mem); |
---|
84 | uint32_t returnSize(); |
---|
85 | |
---|
86 | T& variable_; |
---|
87 | uint8_t mode_; |
---|
88 | NetworkCallbackBase *callback_; |
---|
89 | }; |
---|
90 | |
---|
91 | template <class T> |
---|
92 | class SynchronisableVariableBidirectional: public SynchronisableVariable<T> |
---|
93 | { |
---|
94 | public: |
---|
95 | SynchronisableVariableBidirectional(T& variable, uint8_t master=variableDirection::serverMaster, NetworkCallbackBase *cb=0); |
---|
96 | virtual ~SynchronisableVariableBidirectional(); |
---|
97 | |
---|
98 | virtual inline uint8_t getMode(){ return 0x3; } //this basically is a hack ^^ |
---|
99 | virtual void getData(uint8_t*& mem, uint8_t mode); |
---|
100 | virtual void putData(uint8_t*& mem, uint8_t mode, bool forceCallback = false); |
---|
101 | virtual uint32_t getSize(uint8_t mode); |
---|
102 | private: |
---|
103 | T varBuffer_; |
---|
104 | uint8_t varReference_; |
---|
105 | }; |
---|
106 | |
---|
107 | // ================= Unidirectional Part =============== |
---|
108 | |
---|
109 | template <class T> SynchronisableVariable<T>::SynchronisableVariable(T& variable, uint8_t syncDirection, NetworkCallbackBase *cb): |
---|
110 | variable_( variable ), mode_( syncDirection ), callback_( cb ) |
---|
111 | { |
---|
112 | setState(); |
---|
113 | } |
---|
114 | |
---|
115 | template <class T> SynchronisableVariable<T>::~SynchronisableVariable() |
---|
116 | { |
---|
117 | if (this->callback_ != 0) |
---|
118 | NetworkCallbackManager::deleteCallback(this->callback_); //safe call for deletion |
---|
119 | } |
---|
120 | |
---|
121 | template <class T> void SynchronisableVariable<T>::getData(uint8_t*& mem, uint8_t mode) |
---|
122 | { |
---|
123 | if ( state_ == this->mode_ ) |
---|
124 | getAndIncrease( mem ); |
---|
125 | // mem += SynchronisableVariable<T>::getSize(); |
---|
126 | } |
---|
127 | |
---|
128 | template <class T> void SynchronisableVariable<T>::putData(uint8_t*& mem, uint8_t mode, bool forceCallback) |
---|
129 | { |
---|
130 | assert ( mode == 0x1 || mode == 0x2 ); |
---|
131 | bool callback = false; |
---|
132 | if ( mode == this->mode_ ) //don't do anything |
---|
133 | return; |
---|
134 | // check whether we need to consider a callback |
---|
135 | if ( this->callback_ != 0 ) |
---|
136 | { |
---|
137 | if( forceCallback || !checkEquality( mem ) ) |
---|
138 | callback = true; |
---|
139 | } |
---|
140 | // write the data |
---|
141 | setAndIncrease( mem ); |
---|
142 | // mem += SynchronisableVariable<T>::getSize(); |
---|
143 | // now do a callback if neccessary |
---|
144 | if ( callback ) |
---|
145 | NetworkCallbackManager::triggerCallback( this->callback_ ); |
---|
146 | //this->callback_->call(); |
---|
147 | } |
---|
148 | |
---|
149 | template <class T> uint32_t SynchronisableVariable<T>::getSize(uint8_t mode) |
---|
150 | { |
---|
151 | if ( mode == this->mode_ ) |
---|
152 | return returnSize(); |
---|
153 | else |
---|
154 | return 0; |
---|
155 | } |
---|
156 | |
---|
157 | template <> _NetworkExport uint32_t SynchronisableVariable<const bool>::returnSize(); |
---|
158 | template <> _NetworkExport void SynchronisableVariable<const bool>::setAndIncrease(uint8_t*& mem); |
---|
159 | template <> _NetworkExport void SynchronisableVariable<const bool>::getAndIncrease(uint8_t*& mem); |
---|
160 | template <> _NetworkExport bool SynchronisableVariable<const bool>::checkEquality(uint8_t* mem); |
---|
161 | template <> _NetworkExport uint32_t SynchronisableVariable<const unsigned char>::returnSize(); |
---|
162 | template <> _NetworkExport void SynchronisableVariable<const unsigned char>::setAndIncrease(uint8_t*& mem); |
---|
163 | template <> _NetworkExport void SynchronisableVariable<const unsigned char>::getAndIncrease(uint8_t*& mem); |
---|
164 | template <> _NetworkExport bool SynchronisableVariable<const unsigned char>::checkEquality(uint8_t* mem); |
---|
165 | template <> _NetworkExport uint32_t SynchronisableVariable<const short>::returnSize(); |
---|
166 | template <> _NetworkExport void SynchronisableVariable<const short>::setAndIncrease(uint8_t*& mem); |
---|
167 | template <> _NetworkExport void SynchronisableVariable<const short>::getAndIncrease(uint8_t*& mem); |
---|
168 | template <> _NetworkExport bool SynchronisableVariable<const short>::checkEquality(uint8_t* mem); |
---|
169 | template <> _NetworkExport uint32_t SynchronisableVariable<const unsigned short>::returnSize(); |
---|
170 | template <> _NetworkExport void SynchronisableVariable<const unsigned short>::setAndIncrease(uint8_t*& mem); |
---|
171 | template <> _NetworkExport void SynchronisableVariable<const unsigned short>::getAndIncrease(uint8_t*& mem); |
---|
172 | template <> _NetworkExport bool SynchronisableVariable<const unsigned short>::checkEquality(uint8_t* mem); |
---|
173 | template <> _NetworkExport uint32_t SynchronisableVariable<const int>::returnSize(); |
---|
174 | template <> _NetworkExport void SynchronisableVariable<const int>::setAndIncrease(uint8_t*& mem); |
---|
175 | template <> _NetworkExport void SynchronisableVariable<const int>::getAndIncrease(uint8_t*& mem); |
---|
176 | template <> _NetworkExport bool SynchronisableVariable<const int>::checkEquality(uint8_t* mem); |
---|
177 | template <> _NetworkExport uint32_t SynchronisableVariable<const unsigned int>::returnSize(); |
---|
178 | template <> _NetworkExport void SynchronisableVariable<const unsigned int>::setAndIncrease(uint8_t*& mem); |
---|
179 | template <> _NetworkExport void SynchronisableVariable<const unsigned int>::getAndIncrease(uint8_t*& mem); |
---|
180 | template <> _NetworkExport bool SynchronisableVariable<const unsigned int>::checkEquality(uint8_t* mem); |
---|
181 | template <> _NetworkExport uint32_t SynchronisableVariable<const long>::returnSize(); |
---|
182 | template <> _NetworkExport void SynchronisableVariable<const long>::setAndIncrease(uint8_t*& mem); |
---|
183 | template <> _NetworkExport void SynchronisableVariable<const long>::getAndIncrease(uint8_t*& mem); |
---|
184 | template <> _NetworkExport bool SynchronisableVariable<const long>::checkEquality(uint8_t* mem); |
---|
185 | template <> _NetworkExport uint32_t SynchronisableVariable<const unsigned long>::returnSize(); |
---|
186 | template <> _NetworkExport void SynchronisableVariable<const unsigned long>::setAndIncrease(uint8_t*& mem); |
---|
187 | template <> _NetworkExport void SynchronisableVariable<const unsigned long>::getAndIncrease(uint8_t*& mem); |
---|
188 | template <> _NetworkExport bool SynchronisableVariable<const unsigned long>::checkEquality(uint8_t* mem); |
---|
189 | template <> _NetworkExport uint32_t SynchronisableVariable<const long long>::returnSize(); |
---|
190 | template <> _NetworkExport void SynchronisableVariable<const long long>::setAndIncrease(uint8_t*& mem); |
---|
191 | template <> _NetworkExport void SynchronisableVariable<const long long>::getAndIncrease(uint8_t*& mem); |
---|
192 | template <> _NetworkExport bool SynchronisableVariable<const long long>::checkEquality(uint8_t* mem); |
---|
193 | template <> _NetworkExport uint32_t SynchronisableVariable<const unsigned long long>::returnSize(); |
---|
194 | template <> _NetworkExport void SynchronisableVariable<const unsigned long long>::setAndIncrease(uint8_t*& mem); |
---|
195 | template <> _NetworkExport void SynchronisableVariable<const unsigned long long>::getAndIncrease(uint8_t*& mem); |
---|
196 | template <> _NetworkExport bool SynchronisableVariable<const unsigned long long>::checkEquality(uint8_t* mem); |
---|
197 | template <> _NetworkExport uint32_t SynchronisableVariable<const float>::returnSize(); |
---|
198 | template <> _NetworkExport void SynchronisableVariable<const float>::setAndIncrease(uint8_t*& mem); |
---|
199 | template <> _NetworkExport void SynchronisableVariable<const float>::getAndIncrease(uint8_t*& mem); |
---|
200 | template <> _NetworkExport bool SynchronisableVariable<const float>::checkEquality(uint8_t* mem); |
---|
201 | template <> _NetworkExport uint32_t SynchronisableVariable<const double>::returnSize(); |
---|
202 | template <> _NetworkExport void SynchronisableVariable<const double>::setAndIncrease(uint8_t*& mem); |
---|
203 | template <> _NetworkExport void SynchronisableVariable<const double>::getAndIncrease(uint8_t*& mem); |
---|
204 | template <> _NetworkExport bool SynchronisableVariable<const double>::checkEquality(uint8_t* mem); |
---|
205 | template <> _NetworkExport uint32_t SynchronisableVariable<const long double>::returnSize(); |
---|
206 | template <> _NetworkExport void SynchronisableVariable<const long double>::setAndIncrease(uint8_t*& mem); |
---|
207 | template <> _NetworkExport void SynchronisableVariable<const long double>::getAndIncrease(uint8_t*& mem); |
---|
208 | template <> _NetworkExport bool SynchronisableVariable<const long double>::checkEquality(uint8_t* mem); |
---|
209 | template <> _NetworkExport uint32_t SynchronisableVariable<const std::string>::returnSize(); |
---|
210 | template <> _NetworkExport void SynchronisableVariable<const std::string>::setAndIncrease(uint8_t*& mem); |
---|
211 | template <> _NetworkExport void SynchronisableVariable<const std::string>::getAndIncrease(uint8_t*& mem); |
---|
212 | template <> _NetworkExport bool SynchronisableVariable<const std::string>::checkEquality(uint8_t* mem); |
---|
213 | template <> _NetworkExport uint32_t SynchronisableVariable<const Degree>::returnSize(); |
---|
214 | template <> _NetworkExport void SynchronisableVariable<const Degree>::setAndIncrease(uint8_t*& mem); |
---|
215 | template <> _NetworkExport void SynchronisableVariable<const Degree>::getAndIncrease(uint8_t*& mem); |
---|
216 | template <> _NetworkExport bool SynchronisableVariable<const Degree>::checkEquality(uint8_t* mem); |
---|
217 | |
---|
218 | |
---|
219 | |
---|
220 | // ================= Bidirectional Part ================ |
---|
221 | |
---|
222 | template <class T> SynchronisableVariableBidirectional<T>::SynchronisableVariableBidirectional(T& variable, uint8_t master, NetworkCallbackBase *cb): |
---|
223 | SynchronisableVariable<T>( variable, master, cb ), varBuffer_( variable ), varReference_( 0 ) |
---|
224 | { |
---|
225 | } |
---|
226 | |
---|
227 | template <class T> SynchronisableVariableBidirectional<T>::~SynchronisableVariableBidirectional() |
---|
228 | { |
---|
229 | } |
---|
230 | |
---|
231 | template <class T> void SynchronisableVariableBidirectional<T>::getData(uint8_t*& mem, uint8_t mode) |
---|
232 | { |
---|
233 | if ( this->mode_ == mode ) |
---|
234 | { // we are master for this variable and have to check whether to change the varReference |
---|
235 | if( this->varBuffer_ != this->variable_ ) |
---|
236 | { |
---|
237 | this->varReference_++; |
---|
238 | memcpy((void*)&this->varBuffer_, &this->variable_, sizeof(this->variable_)); |
---|
239 | } |
---|
240 | } |
---|
241 | // write the reference number to the stream |
---|
242 | *static_cast<uint8_t*>(mem) = varReference_; |
---|
243 | mem += sizeof(this->varReference_); |
---|
244 | // now write the content |
---|
245 | SynchronisableVariable<T>::getAndIncrease( mem ); |
---|
246 | // mem += SynchronisableVariable<T>::getSize(); |
---|
247 | } |
---|
248 | |
---|
249 | template <class T> void SynchronisableVariableBidirectional<T>::putData(uint8_t*& mem, uint8_t mode, bool forceCallback) |
---|
250 | { |
---|
251 | bool callback = false; |
---|
252 | if ( this->mode_ == mode ) |
---|
253 | { // MASTER |
---|
254 | // check that the client (source of the data) has a recent version of this variable |
---|
255 | if ( *static_cast<uint8_t*>(mem) != this->varReference_ ) |
---|
256 | { // wrong reference number, so discard the data |
---|
257 | mem += getSize( mode ); // SynchronisableVariableBidirectional::getSize returns size of variable + reference |
---|
258 | return; |
---|
259 | } |
---|
260 | else{ |
---|
261 | // apply data |
---|
262 | mem += sizeof(varReference_); |
---|
263 | if ( SynchronisableVariableBidirectional<T>::checkEquality( mem )==true ) |
---|
264 | { |
---|
265 | mem += SynchronisableVariable<T>::getSize( mode ); |
---|
266 | return; |
---|
267 | } |
---|
268 | else |
---|
269 | { |
---|
270 | memcpy((void*)&this->varBuffer_, &this->variable_, sizeof(T)); |
---|
271 | if ( this->callback_ != 0 ) |
---|
272 | callback = true; |
---|
273 | } |
---|
274 | } |
---|
275 | } |
---|
276 | else |
---|
277 | { // we are slave for this variable |
---|
278 | if (*static_cast<uint8_t*>(mem) == this->varReference_ && !forceCallback) |
---|
279 | { |
---|
280 | mem += getSize( mode ); //just skip the variable because nothing changed |
---|
281 | return; |
---|
282 | } |
---|
283 | else |
---|
284 | { |
---|
285 | this->varReference_ = *static_cast<uint8_t*>(mem); |
---|
286 | mem += sizeof(varReference_); |
---|
287 | if ( SynchronisableVariable<T>::checkEquality( mem ) == false ) |
---|
288 | { |
---|
289 | // value changed so remark for callback |
---|
290 | if ( this->callback_ != 0 ) |
---|
291 | callback = true; |
---|
292 | } |
---|
293 | } |
---|
294 | } |
---|
295 | // now write the data |
---|
296 | SynchronisableVariable<T>::setAndIncrease(mem); |
---|
297 | // now do a callback if neccessary |
---|
298 | if ( callback ) |
---|
299 | NetworkCallbackManager::triggerCallback( this->callback_ ); |
---|
300 | //this->callback_->call(); |
---|
301 | } |
---|
302 | |
---|
303 | template <class T> uint32_t SynchronisableVariableBidirectional<T>::getSize(uint8_t mode) |
---|
304 | { |
---|
305 | return SynchronisableVariable<T>::returnSize() + sizeof(varReference_); |
---|
306 | } |
---|
307 | |
---|
308 | |
---|
309 | } |
---|
310 | |
---|
311 | |
---|
312 | #endif |
---|