1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | |
---|
12 | ### File Specific: |
---|
13 | main-programmer: Silvan Nellen |
---|
14 | co-programmer: Benjamin Wuest |
---|
15 | */ |
---|
16 | |
---|
17 | #define DEBUG_MODULE_NETWORK |
---|
18 | |
---|
19 | #include "shared_network_data.h" |
---|
20 | #include "network_stream.h" |
---|
21 | #include "netdefs.h" |
---|
22 | |
---|
23 | #include "state.h" |
---|
24 | |
---|
25 | #include <cassert> |
---|
26 | |
---|
27 | #include "synchronizeable.h" |
---|
28 | |
---|
29 | |
---|
30 | |
---|
31 | /** |
---|
32 | * default constructor |
---|
33 | */ |
---|
34 | Synchronizeable::Synchronizeable() |
---|
35 | { |
---|
36 | this->setClassID(CL_SYNCHRONIZEABLE, "Synchronizeable"); |
---|
37 | this->owner = -1; |
---|
38 | this->hostID = SharedNetworkData::getInstance()->getHostID(); |
---|
39 | this->setIsServer(this->hostID == 0); |
---|
40 | this->uniqueID = NET_UID_UNASSIGNED; |
---|
41 | this->networkStream = NULL; |
---|
42 | this->bSynchronize = false; |
---|
43 | |
---|
44 | if( State::isOnline()) |
---|
45 | { |
---|
46 | NetworkStream* nd = SharedNetworkData::getInstance()->getDefaultSyncStream(); |
---|
47 | assert(nd != NULL); |
---|
48 | nd->connectSynchronizeable(*this); |
---|
49 | this->setUniqueID(SharedNetworkData::getInstance()->getNewUniqueID()); |
---|
50 | } |
---|
51 | |
---|
52 | /* make sure loadClassId is first synced var because this is read by networkStream */ |
---|
53 | assert( syncVarList.size() == 0 ); |
---|
54 | mLeafClassId = this->registerVarId( new SynchronizeableInt( (int*)&this->getLeafClassID(), (int*)&this->getLeafClassID(), "leafClassId" ) ); |
---|
55 | |
---|
56 | this->registerVar( new SynchronizeableInt( &this->owner, &this->owner, "owner" ) ); |
---|
57 | this->registerVar( new SynchronizeableString( &this->objectName, &this->objectName, "objectName" ) ); |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | |
---|
62 | /** |
---|
63 | * default destructor deletes all unneded stuff |
---|
64 | */ |
---|
65 | Synchronizeable::~Synchronizeable() |
---|
66 | { |
---|
67 | if ( this->networkStream ) |
---|
68 | this->networkStream->disconnectSynchronizeable(*this); |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * Sets the server flag to a given value |
---|
73 | * @param isServer: the boolean value which the server flag is to set to |
---|
74 | */ |
---|
75 | void Synchronizeable::setIsServer(bool isServer) |
---|
76 | { |
---|
77 | if( isServer ) |
---|
78 | this->state = this->state | STATE_SERVER; |
---|
79 | else |
---|
80 | this->state = this->state & (~STATE_SERVER); |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | /** |
---|
85 | * Determines if the server flag is set |
---|
86 | * @return true, if the server flag is true, false else |
---|
87 | */ |
---|
88 | bool Synchronizeable::isServer() |
---|
89 | { |
---|
90 | return (this->state & STATE_SERVER) >0; |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | |
---|
95 | int Synchronizeable::getStateDiff( int userId, byte* data, int maxLength, int stateId, int fromStateId, int priorityTH ) |
---|
96 | { |
---|
97 | //make sure this user has his history |
---|
98 | if ( sentStates.size() <= userId ) |
---|
99 | sentStates.resize( userId+1 ); |
---|
100 | |
---|
101 | //calculate needed memory |
---|
102 | int neededSize = 0; |
---|
103 | |
---|
104 | for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ ) |
---|
105 | neededSize += (*it)->getSize(); |
---|
106 | |
---|
107 | if ( !( neededSize <= maxLength ) ) |
---|
108 | { |
---|
109 | PRINTF(0)( "%d > %d\n", neededSize, maxLength ); |
---|
110 | assert(false); |
---|
111 | } |
---|
112 | |
---|
113 | //remove older states from history than fromStateId |
---|
114 | StateHistory::iterator it = sentStates[userId].begin(); |
---|
115 | |
---|
116 | while ( it != sentStates[userId].end() && (*it)->stateId < fromStateId ) |
---|
117 | it++; |
---|
118 | |
---|
119 | if ( it != sentStates[userId].begin() ) |
---|
120 | { |
---|
121 | for ( StateHistory::iterator it2 = sentStates[userId].begin(); it2 != it; it2++ ) |
---|
122 | { |
---|
123 | if ( (*it2)->data != NULL ) |
---|
124 | { |
---|
125 | delete [] (*it2)->data; |
---|
126 | (*it2)->data = NULL; |
---|
127 | } |
---|
128 | } |
---|
129 | sentStates[userId].erase( sentStates[userId].begin(), it ); |
---|
130 | } |
---|
131 | |
---|
132 | //find state to create diff from |
---|
133 | StateHistoryEntry * stateFrom = NULL; |
---|
134 | |
---|
135 | it = sentStates[userId].begin(); |
---|
136 | while ( it != sentStates[userId].end() && (*it)->stateId != fromStateId ) |
---|
137 | it++; |
---|
138 | |
---|
139 | if ( it == sentStates[userId].end() ) |
---|
140 | { |
---|
141 | StateHistoryEntry * initialEntry = new StateHistoryEntry(); |
---|
142 | |
---|
143 | initialEntry->stateId = fromStateId; |
---|
144 | initialEntry->dataLength = 0; |
---|
145 | initialEntry->data = NULL; |
---|
146 | |
---|
147 | stateFrom = initialEntry; |
---|
148 | } |
---|
149 | else |
---|
150 | stateFrom = (*it); |
---|
151 | |
---|
152 | StateHistoryEntry * stateTo = new StateHistoryEntry(); |
---|
153 | |
---|
154 | stateTo->stateId = stateId; |
---|
155 | stateTo->dataLength = neededSize; |
---|
156 | stateTo->data = new byte[ neededSize ]; |
---|
157 | |
---|
158 | std::list<int>::iterator sizeIter = stateFrom->sizeList.begin(); |
---|
159 | |
---|
160 | int i = 0; |
---|
161 | int n; |
---|
162 | |
---|
163 | bool hasPermission; |
---|
164 | |
---|
165 | // now do the actual synchronization: kick all variables to write into a common buffer |
---|
166 | for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ ) |
---|
167 | { |
---|
168 | hasPermission = ( |
---|
169 | this->isServer() && (*it)->checkPermission( PERMISSION_SERVER ) || |
---|
170 | this->owner == this->hostID && (*it)->checkPermission( PERMISSION_OWNER ) || |
---|
171 | (*it)->checkPermission( PERMISSION_ALL ) |
---|
172 | ); |
---|
173 | |
---|
174 | if ( ( hasPermission && (*it)->getPriority() >= priorityTH ) || sizeIter == stateFrom->sizeList.end() ) |
---|
175 | { |
---|
176 | //(*it)->debug(); |
---|
177 | n = (*it)->writeToBuf( stateTo->data+i, stateTo->dataLength - i ); |
---|
178 | stateTo->sizeList.push_back( n ); |
---|
179 | i += n; |
---|
180 | } |
---|
181 | else |
---|
182 | { |
---|
183 | for ( int j = 0; j<(*sizeIter); j++ ) |
---|
184 | { |
---|
185 | assert( i < stateFrom->dataLength ); |
---|
186 | stateTo->data[i] = stateFrom->data[i]; |
---|
187 | i++; |
---|
188 | } |
---|
189 | stateTo->sizeList.push_back( (*sizeIter) ); |
---|
190 | } |
---|
191 | |
---|
192 | if ( sizeIter != stateFrom->sizeList.end() ) |
---|
193 | sizeIter++; |
---|
194 | } |
---|
195 | |
---|
196 | sentStates[userId].push_back( stateTo ); |
---|
197 | |
---|
198 | assert( i == neededSize ); |
---|
199 | |
---|
200 | //write diff to data |
---|
201 | for ( i = 0; i<neededSize; i++ ) |
---|
202 | { |
---|
203 | if ( i < stateFrom->dataLength ) |
---|
204 | data[i] = stateTo->data[i] - stateFrom->data[i]; |
---|
205 | else |
---|
206 | data[i] = stateTo->data[i]; |
---|
207 | } |
---|
208 | |
---|
209 | return neededSize; |
---|
210 | } |
---|
211 | |
---|
212 | /** |
---|
213 | * sets a new state out of a diff created on another host |
---|
214 | * @param userId hostId of user who send me that diff |
---|
215 | * @param data pointer to diff |
---|
216 | * @param length length of diff |
---|
217 | * @param stateId id of current state |
---|
218 | * @param fromStateId id of the base state id |
---|
219 | * @return number bytes read |
---|
220 | * @todo check for permissions |
---|
221 | */ |
---|
222 | int Synchronizeable::setStateDiff( int userId, byte* data, int length, int stateId, int fromStateId ) |
---|
223 | { |
---|
224 | //make sure this user has his history |
---|
225 | if ( recvStates.size() <= userId ) |
---|
226 | recvStates.resize( userId+1 ); |
---|
227 | |
---|
228 | //create new state |
---|
229 | StateHistoryEntry * stateTo = new StateHistoryEntry(); |
---|
230 | stateTo->stateId = stateId; |
---|
231 | stateTo->dataLength = length; |
---|
232 | stateTo->data = new byte[ length ]; |
---|
233 | |
---|
234 | //remove old states |
---|
235 | StateHistory::iterator it = recvStates[userId].begin(); |
---|
236 | |
---|
237 | while ( it != recvStates[userId].end() && (*it)->stateId < fromStateId ) |
---|
238 | it++; |
---|
239 | |
---|
240 | if ( it != recvStates[userId].begin() ) |
---|
241 | { |
---|
242 | for ( StateHistory::iterator it2 = recvStates[userId].begin(); it2 != it; it2++ ) |
---|
243 | { |
---|
244 | if ( (*it2)->data != NULL ) |
---|
245 | { |
---|
246 | delete [] (*it2)->data; |
---|
247 | (*it2)->data = NULL; |
---|
248 | } |
---|
249 | } |
---|
250 | recvStates[userId].erase( recvStates[userId].begin(), it ); |
---|
251 | } |
---|
252 | |
---|
253 | //find state to apply diff to |
---|
254 | StateHistoryEntry * stateFrom = NULL; |
---|
255 | |
---|
256 | it = recvStates[userId].begin(); |
---|
257 | while ( it != recvStates[userId].end() && (*it)->stateId != fromStateId ) |
---|
258 | it++; |
---|
259 | |
---|
260 | if ( it == recvStates[userId].end() ) |
---|
261 | { |
---|
262 | StateHistoryEntry * initialEntry = new StateHistoryEntry(); |
---|
263 | |
---|
264 | initialEntry->stateId = fromStateId; |
---|
265 | initialEntry->dataLength = 0; |
---|
266 | initialEntry->data = NULL; |
---|
267 | |
---|
268 | stateFrom = initialEntry; |
---|
269 | } |
---|
270 | else |
---|
271 | stateFrom = (*it); |
---|
272 | |
---|
273 | //apply diff |
---|
274 | for ( int i = 0; i<length; i++ ) |
---|
275 | { |
---|
276 | if ( i < stateFrom->dataLength ) |
---|
277 | stateTo->data[i] = stateFrom->data[i] + data[i]; |
---|
278 | else |
---|
279 | stateTo->data[i] = data[i]; |
---|
280 | |
---|
281 | } |
---|
282 | |
---|
283 | //add state to state history |
---|
284 | recvStates[userId].push_back( stateTo ); |
---|
285 | |
---|
286 | int i = 0; |
---|
287 | std::list<int> changes; |
---|
288 | |
---|
289 | for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ ) |
---|
290 | { |
---|
291 | if ( |
---|
292 | (*it)->checkPermission( PERMISSION_SERVER ) && networkStream->isUserServer( userId ) || |
---|
293 | (*it)->checkPermission( PERMISSION_OWNER ) && this->owner == userId || |
---|
294 | (*it)->checkPermission( PERMISSION_ALL ) |
---|
295 | ) |
---|
296 | { |
---|
297 | i += (*it)->readFromBuf( stateTo->data + i, stateTo->dataLength - i ); |
---|
298 | if ( (*it)->getHasChanged() ) |
---|
299 | changes.push_back( (*it)->getVarId() ); |
---|
300 | } |
---|
301 | else |
---|
302 | { |
---|
303 | // PRINTF(0)("DONT SET VAR BECAUSE OF PERMISSION: %s %d %d %d %d %d %d\n", (*it)->getName().c_str(), (*it)->checkPermission( PERMISSION_SERVER ), (*it)->checkPermission( PERMISSION_OWNER ), (*it)->checkPermission( PERMISSION_ALL ), networkStream->isUserServer( userId ), this->owner, userId ); |
---|
304 | i += (*it)->getSizeFromBuf( stateTo->data + i, stateTo->dataLength - i ); |
---|
305 | } |
---|
306 | } |
---|
307 | |
---|
308 | this->varChangeHandler( changes ); |
---|
309 | |
---|
310 | return i; |
---|
311 | } |
---|
312 | |
---|
313 | /** |
---|
314 | * override this function to be notified on change |
---|
315 | * of your registred variables. |
---|
316 | * @param id id's which have changed |
---|
317 | */ |
---|
318 | void Synchronizeable::varChangeHandler( std::list<int> & id ) |
---|
319 | { |
---|
320 | } |
---|
321 | |
---|
322 | /** |
---|
323 | * registers a varable to be synchronized over network |
---|
324 | * @param var see src/lib/network/synchronizeable_var/ for available classes |
---|
325 | */ |
---|
326 | void Synchronizeable::registerVar( SynchronizeableVar * var ) |
---|
327 | { |
---|
328 | syncVarList.push_back( var ); |
---|
329 | } |
---|
330 | |
---|
331 | /** |
---|
332 | * registers a varable to be synchronized over network |
---|
333 | * return value is passed to varChangeHandler on change |
---|
334 | * @param var see src/lib/network/synchronizeable_var/ for available classes |
---|
335 | * @return handle passed to varChangeHandler on changes |
---|
336 | */ |
---|
337 | int Synchronizeable::registerVarId( SynchronizeableVar * var ) |
---|
338 | { |
---|
339 | syncVarList.push_back( var ); |
---|
340 | var->setWatched( true ); |
---|
341 | var->setVarId( syncVarList.size()-1 ); |
---|
342 | return syncVarList.size()-1; |
---|
343 | } |
---|
344 | |
---|
345 | /** |
---|
346 | * removed user's states from memory |
---|
347 | * @param userId user to clean |
---|
348 | */ |
---|
349 | void Synchronizeable::cleanUpUser( int userId ) |
---|
350 | { |
---|
351 | for ( UserStateHistory::iterator it = sentStates.begin(); it != sentStates.end(); it++ ) |
---|
352 | { |
---|
353 | for ( StateHistory::iterator it2 = it->begin(); it2 != it->end(); it2++ ) |
---|
354 | { |
---|
355 | if ( (*it2)->data ) |
---|
356 | delete [] (*it2)->data; |
---|
357 | (*it2)->data = NULL; |
---|
358 | |
---|
359 | delete *it2; |
---|
360 | } |
---|
361 | } |
---|
362 | |
---|
363 | sentStates.clear(); |
---|
364 | |
---|
365 | for ( UserStateHistory::iterator it = recvStates.begin(); it != recvStates.end(); it++ ) |
---|
366 | { |
---|
367 | for ( StateHistory::iterator it2 = it->begin(); it2 != it->end(); it2++ ) |
---|
368 | { |
---|
369 | if ( (*it2)->data ) |
---|
370 | delete [] (*it2)->data; |
---|
371 | (*it2)->data = NULL; |
---|
372 | |
---|
373 | delete *it2; |
---|
374 | } |
---|
375 | } |
---|
376 | |
---|
377 | recvStates.clear(); |
---|
378 | } |
---|
379 | |
---|
380 | |
---|
381 | |
---|