| [5523] | 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 |  | 
|---|
| [5547] | 11 |  | 
|---|
| [5523] | 12 | ### File Specific: | 
|---|
| [9406] | 13 |    main-programmer: Christoph Renner (rennerc@ee.ethz.ch) | 
|---|
 | 14 |    co-programmer: Patrick Boenzli (patrick@orxonox.ethz.ch) | 
|---|
| [5547] | 15 | */ | 
|---|
| [5523] | 16 |  | 
|---|
| [6139] | 17 | #define DEBUG_MODULE_NETWORK | 
|---|
 | 18 |  | 
|---|
| [6695] | 19 | #include "shared_network_data.h" | 
|---|
 | 20 | #include "network_stream.h" | 
|---|
| [5547] | 21 | #include "netdefs.h" | 
|---|
| [7954] | 22 | #include "network_log.h" | 
|---|
| [8068] | 23 | #include "network_game_manager.h" | 
|---|
| [5529] | 24 |  | 
|---|
| [6695] | 25 | #include "state.h" | 
|---|
| [5996] | 26 |  | 
|---|
| [6753] | 27 | #include <cassert> | 
|---|
| [6695] | 28 |  | 
|---|
 | 29 | #include "synchronizeable.h" | 
|---|
 | 30 |  | 
|---|
| [9406] | 31 | #include "converter.h" | 
|---|
| [6695] | 32 |  | 
|---|
 | 33 |  | 
|---|
| [9406] | 34 |  | 
|---|
| [5547] | 35 | /** | 
|---|
| [5807] | 36 |  *  default constructor | 
|---|
| [5547] | 37 |  */ | 
|---|
| [5996] | 38 | Synchronizeable::Synchronizeable() | 
|---|
| [5997] | 39 | { | 
|---|
| [6341] | 40 |   this->setClassID(CL_SYNCHRONIZEABLE, "Synchronizeable"); | 
|---|
| [8068] | 41 |   this->owner = 0; | 
|---|
| [9406] | 42 | //   this->setIsServer(SharedNetworkData::getInstance()->getHostID() == 0); | 
|---|
| [6695] | 43 |   this->uniqueID = NET_UID_UNASSIGNED; | 
|---|
| [6145] | 44 |   this->networkStream = NULL; | 
|---|
| [6695] | 45 |   this->bSynchronize = false; | 
|---|
| [9406] | 46 |  | 
|---|
| [6695] | 47 |   if( State::isOnline()) | 
|---|
 | 48 |   { | 
|---|
 | 49 |     NetworkStream* nd = SharedNetworkData::getInstance()->getDefaultSyncStream(); | 
|---|
 | 50 |     assert(nd != NULL); | 
|---|
 | 51 |     nd->connectSynchronizeable(*this); | 
|---|
 | 52 |     this->setUniqueID(SharedNetworkData::getInstance()->getNewUniqueID()); | 
|---|
 | 53 |   } | 
|---|
| [7954] | 54 |  | 
|---|
 | 55 |   /* make sure loadClassId is first synced var because this is read by networkStream */ | 
|---|
 | 56 |   assert( syncVarList.size() == 0 ); | 
|---|
 | 57 |   mLeafClassId = this->registerVarId( new SynchronizeableInt( (int*)&this->getLeafClassID(), (int*)&this->getLeafClassID(), "leafClassId" ) ); | 
|---|
| [9406] | 58 |  | 
|---|
| [7954] | 59 |   this->registerVar( new SynchronizeableInt( &this->owner, &this->owner, "owner" ) ); | 
|---|
 | 60 |   this->registerVar( new SynchronizeableString( &this->objectName, &this->objectName, "objectName" ) ); | 
|---|
| [5997] | 61 | } | 
|---|
 | 62 |  | 
|---|
| [5523] | 63 |  | 
|---|
| [5996] | 64 |  | 
|---|
| [5547] | 65 | /** | 
|---|
| [5807] | 66 |  *  default destructor deletes all unneded stuff | 
|---|
| [5547] | 67 |  */ | 
|---|
 | 68 | Synchronizeable::~Synchronizeable() | 
|---|
| [6139] | 69 | { | 
|---|
 | 70 |   if ( this->networkStream ) | 
|---|
| [9110] | 71 |   { | 
|---|
| [6139] | 72 |     this->networkStream->disconnectSynchronizeable(*this); | 
|---|
| [9406] | 73 |  | 
|---|
| [9494] | 74 |     // remove the message manager only by the server | 
|---|
 | 75 |     if ( (SharedNetworkData::getInstance()->isMasterServer() ) | 
|---|
| [9406] | 76 |            && this->beSynchronized() && this->getUniqueID() > 0 && !this->isA( CL_MESSAGE_MANAGER ) ) | 
|---|
| [9110] | 77 |       NetworkGameManager::getInstance()->removeSynchronizeable( this->getUniqueID() ); | 
|---|
 | 78 |   } | 
|---|
| [9406] | 79 |  | 
|---|
| [8623] | 80 |   for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ ) | 
|---|
 | 81 |   { | 
|---|
 | 82 |     delete *it; | 
|---|
 | 83 |   } | 
|---|
 | 84 |   syncVarList.clear(); | 
|---|
| [9406] | 85 |  | 
|---|
| [8623] | 86 |   for ( UserStateHistory::iterator it = recvStates.begin(); it != recvStates.end(); it++ ) | 
|---|
 | 87 |   { | 
|---|
 | 88 |     for ( StateHistory::iterator it2 = it->begin(); it2 != it->end(); it2++ ) | 
|---|
 | 89 |     { | 
|---|
 | 90 |       if ( (*it2)->data ) | 
|---|
 | 91 |       { | 
|---|
 | 92 |         delete [] (*it2)->data; | 
|---|
 | 93 |         (*it2)->data = NULL; | 
|---|
 | 94 |       } | 
|---|
 | 95 |       delete *it2; | 
|---|
 | 96 |     } | 
|---|
 | 97 |  | 
|---|
 | 98 |   } | 
|---|
| [9406] | 99 |  | 
|---|
| [8623] | 100 |   for ( UserStateHistory::iterator it = sentStates.begin(); it != sentStates.end(); it++ ) | 
|---|
 | 101 |   { | 
|---|
 | 102 |     for ( StateHistory::iterator it2 = it->begin(); it2 != it->end(); it2++ ) | 
|---|
 | 103 |     { | 
|---|
 | 104 |       if ( (*it2)->data ) | 
|---|
 | 105 |       { | 
|---|
 | 106 |         delete [] (*it2)->data; | 
|---|
 | 107 |         (*it2)->data = NULL; | 
|---|
 | 108 |       } | 
|---|
 | 109 |       delete *it2; | 
|---|
 | 110 |     } | 
|---|
 | 111 |   } | 
|---|
| [6139] | 112 | } | 
|---|
| [5523] | 113 |  | 
|---|
 | 114 |  | 
|---|
| [6695] | 115 |  | 
|---|
| [5547] | 116 | /** | 
|---|
| [9406] | 117 |  * creates a diff image from two states | 
|---|
 | 118 |  * @param userId: the userid of the user where the image will be sent to | 
|---|
 | 119 |  * @param data: the binary data array to write to | 
|---|
 | 120 |  * @param maxLength: maximal length of the data written (length of available space in the array) | 
|---|
 | 121 |  * @param stateId: the state id that this diff will represent | 
|---|
 | 122 |  * @param priorityTH: the priority threshold: all syncs below this threshold won't be synchronized | 
|---|
 | 123 |  * | 
|---|
 | 124 |  * @todo check for permissions | 
|---|
| [5547] | 125 |  */ | 
|---|
| [7954] | 126 | int Synchronizeable::getStateDiff( int userId, byte* data, int maxLength, int stateId, int fromStateId, int priorityTH ) | 
|---|
 | 127 | { | 
|---|
 | 128 |   //make sure this user has his history | 
|---|
 | 129 |   if ( sentStates.size() <= userId ) | 
|---|
 | 130 |     sentStates.resize( userId+1 ); | 
|---|
| [5547] | 131 |  | 
|---|
| [7954] | 132 |   //calculate needed memory | 
|---|
 | 133 |   int neededSize = 0; | 
|---|
| [5997] | 134 |  | 
|---|
| [7954] | 135 |   for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ ) | 
|---|
| [8147] | 136 |   { | 
|---|
 | 137 |     //PRINTF(0)("SIZE = %d %s\n", (*it)->getSize(), (*it)->getName().c_str()); | 
|---|
| [7954] | 138 |     neededSize += (*it)->getSize(); | 
|---|
| [8147] | 139 |   } | 
|---|
| [5997] | 140 |  | 
|---|
| [7954] | 141 |   if ( !( neededSize <= maxLength ) ) | 
|---|
 | 142 |   { | 
|---|
 | 143 |     PRINTF(0)( "%d > %d\n", neededSize, maxLength ); | 
|---|
 | 144 |     assert(false); | 
|---|
 | 145 |   } | 
|---|
 | 146 |  | 
|---|
 | 147 |   //remove older states from history than fromStateId | 
|---|
 | 148 |   StateHistory::iterator it = sentStates[userId].begin(); | 
|---|
 | 149 |  | 
|---|
 | 150 |   while ( it != sentStates[userId].end() && (*it)->stateId < fromStateId ) | 
|---|
 | 151 |     it++; | 
|---|
 | 152 |  | 
|---|
 | 153 |   if ( it != sentStates[userId].begin() ) | 
|---|
 | 154 |   { | 
|---|
 | 155 |     for ( StateHistory::iterator it2 = sentStates[userId].begin(); it2 != it; it2++ ) | 
|---|
 | 156 |     { | 
|---|
 | 157 |       if ( (*it2)->data != NULL ) | 
|---|
 | 158 |       { | 
|---|
 | 159 |         delete [] (*it2)->data; | 
|---|
 | 160 |         (*it2)->data = NULL; | 
|---|
 | 161 |       } | 
|---|
| [9406] | 162 |  | 
|---|
| [8623] | 163 |       delete *it2; | 
|---|
| [7954] | 164 |     } | 
|---|
 | 165 |     sentStates[userId].erase( sentStates[userId].begin(), it ); | 
|---|
 | 166 |   } | 
|---|
 | 167 |  | 
|---|
 | 168 |   //find state to create diff from | 
|---|
 | 169 |   StateHistoryEntry * stateFrom = NULL; | 
|---|
 | 170 |  | 
|---|
 | 171 |   it = sentStates[userId].begin(); | 
|---|
 | 172 |   while ( it != sentStates[userId].end() && (*it)->stateId != fromStateId ) | 
|---|
 | 173 |     it++; | 
|---|
 | 174 |  | 
|---|
 | 175 |   if ( it == sentStates[userId].end() ) | 
|---|
 | 176 |   { | 
|---|
 | 177 |     StateHistoryEntry * initialEntry = new StateHistoryEntry(); | 
|---|
 | 178 |  | 
|---|
 | 179 |     initialEntry->stateId = fromStateId; | 
|---|
 | 180 |     initialEntry->dataLength = 0; | 
|---|
 | 181 |     initialEntry->data = NULL; | 
|---|
 | 182 |  | 
|---|
 | 183 |     stateFrom = initialEntry; | 
|---|
| [9406] | 184 |  | 
|---|
| [8623] | 185 |     sentStates[userId].push_back( stateFrom ); | 
|---|
| [7954] | 186 |   } | 
|---|
 | 187 |   else | 
|---|
 | 188 |     stateFrom = (*it); | 
|---|
 | 189 |  | 
|---|
| [8623] | 190 |   StateHistoryEntry * stateTo = new StateHistoryEntry; | 
|---|
| [7954] | 191 |  | 
|---|
| [8623] | 192 |   sentStates[userId].push_back( stateTo ); | 
|---|
| [9406] | 193 |  | 
|---|
| [7954] | 194 |   stateTo->stateId = stateId; | 
|---|
 | 195 |   stateTo->dataLength = neededSize; | 
|---|
 | 196 |   stateTo->data = new byte[ neededSize ]; | 
|---|
 | 197 |  | 
|---|
 | 198 |   std::list<int>::iterator sizeIter = stateFrom->sizeList.begin(); | 
|---|
 | 199 |  | 
|---|
 | 200 |   int i = 0; | 
|---|
 | 201 |   int n; | 
|---|
| [9406] | 202 |  | 
|---|
 | 203 |   bool hasPermission = false; | 
|---|
| [8623] | 204 |   bool sizeChanged = false; | 
|---|
| [7954] | 205 |  | 
|---|
 | 206 |   // now do the actual synchronization: kick all variables to write into a common buffer | 
|---|
 | 207 |   for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ ) | 
|---|
 | 208 |   { | 
|---|
| [9406] | 209 |     // DATA PERMISSIONS | 
|---|
 | 210 |     // check if this synchronizeable has the permissions to write the data | 
|---|
 | 211 |  | 
|---|
| [9591] | 212 |     //  MASTER_SERVER |====> * | 
|---|
| [9406] | 213 |     if( SharedNetworkData::getInstance()->isMasterServer() && (*it)->checkPermission( PERMISSION_MASTER_SERVER )) | 
|---|
 | 214 |       hasPermission = true; | 
|---|
| [9591] | 215 |     else if( SharedNetworkData::getInstance()->isMasterServer() && this->owner != userId && (*it)->checkPermission( PERMISSION_OWNER )) | 
|---|
 | 216 |       hasPermission = true; | 
|---|
| [9592] | 217 |  | 
|---|
| [9591] | 218 |     // PROXY_SERVER  |====> * | 
|---|
| [9494] | 219 |     else if( SharedNetworkData::getInstance()->isProxyServerActive() && (*it)->checkPermission( PERMISSION_PROXY_SERVER )) | 
|---|
| [9406] | 220 |       hasPermission = true; | 
|---|
| [9591] | 221 |     else if( SharedNetworkData::getInstance()->isProxyServerActive() && this->owner != userId && (*it)->checkPermission( PERMISSION_OWNER )) | 
|---|
 | 222 |       hasPermission = true; | 
|---|
| [9592] | 223 |     // PROXY_SERVER  |====> MASTER_SERVER | 
|---|
 | 224 |     else if( SharedNetworkData::getInstance()->isProxyServerActive() && this->networkStream->isUserMasterServer(userId) | 
|---|
 | 225 |              && this->networkStream->isUserLocal(userId) && (*it)->checkPermission( PERMISSION_MASTER_SERVER)) | 
|---|
| [9406] | 226 |       hasPermission = true; | 
|---|
| [9592] | 227 |     // PROXY_SERVER  |====> CLIENT | 
|---|
 | 228 |     else if( SharedNetworkData::getInstance()->isProxyServerActive() && this->networkStream->isUserClient(userId) | 
|---|
 | 229 |              && this->owner != userId && (*it)->checkPermission( PERMISSION_OWNER ) ) | 
|---|
| [9406] | 230 |       hasPermission = true; | 
|---|
| [9494] | 231 |     else if( SharedNetworkData::getInstance()->isProxyServerActive() && this->networkStream->isUserClient(userId) | 
|---|
| [9581] | 232 |              && (*it)->checkPermission( PERMISSION_MASTER_SERVER) ) | 
|---|
 | 233 |       hasPermission = true; | 
|---|
| [9592] | 234 |  | 
|---|
 | 235 |     // OWNER         |====> * | 
|---|
 | 236 |     else if( this->owner == SharedNetworkData::getInstance()->getHostID() && (*it)->checkPermission( PERMISSION_OWNER )) | 
|---|
| [9588] | 237 |       hasPermission = true; | 
|---|
| [9592] | 238 |  | 
|---|
 | 239 |     // ALL           |====> * | 
|---|
 | 240 |     else if( (*it)->checkPermission( PERMISSION_ALL )) | 
|---|
| [9406] | 241 |       hasPermission = true; | 
|---|
| [9592] | 242 |  | 
|---|
| [9406] | 243 |     else | 
|---|
 | 244 |       hasPermission = false; | 
|---|
 | 245 |  | 
|---|
 | 246 |  | 
|---|
 | 247 |     if ( sizeIter == stateFrom->sizeList.end() || *sizeIter != (*it)->getSize() ) | 
|---|
| [8623] | 248 |       sizeChanged = true; | 
|---|
| [9406] | 249 |  | 
|---|
| [8623] | 250 |     if ( ( hasPermission && (*it)->getPriority() >= priorityTH ) || sizeChanged ) | 
|---|
| [7954] | 251 |     { | 
|---|
 | 252 |       n = (*it)->writeToBuf( stateTo->data+i, stateTo->dataLength - i ); | 
|---|
| [9579] | 253 |       //NETPRINTF(0)("getvar %s %d\n", (*it)->getName().c_str(), n); | 
|---|
| [9406] | 254 |       //PRINTF(0)("getvar %s %d\n", (*it)->getName().c_str(), n); | 
|---|
| [7954] | 255 |       stateTo->sizeList.push_back( n ); | 
|---|
| [9406] | 256 |       // this is only for very hardcore debug sessions | 
|---|
 | 257 |       // (*it)->debug(); | 
|---|
| [7954] | 258 |       i += n; | 
|---|
 | 259 |     } | 
|---|
 | 260 |     else | 
|---|
 | 261 |     { | 
|---|
 | 262 |       for ( int j = 0; j<(*sizeIter); j++ ) | 
|---|
 | 263 |       { | 
|---|
 | 264 |         assert( i < stateFrom->dataLength ); | 
|---|
 | 265 |         stateTo->data[i] = stateFrom->data[i]; | 
|---|
 | 266 |         i++; | 
|---|
 | 267 |       } | 
|---|
 | 268 |       //NETPRINTF(0)("getvar %s %d\n", (*it)->getName().c_str(), *sizeIter); | 
|---|
 | 269 |       stateTo->sizeList.push_back( (*sizeIter) ); | 
|---|
 | 270 |     } | 
|---|
 | 271 |  | 
|---|
 | 272 |     if ( sizeIter != stateFrom->sizeList.end() ) | 
|---|
 | 273 |       sizeIter++; | 
|---|
 | 274 |   } | 
|---|
 | 275 |  | 
|---|
| [8147] | 276 |   if ( i != neededSize ) | 
|---|
 | 277 |   { | 
|---|
| [9406] | 278 |     PRINTF(0)("strange error: (%s) %d != %d\n", this->getClassCName(), i, neededSize); | 
|---|
| [8147] | 279 |     assert(false); | 
|---|
 | 280 |   } | 
|---|
| [7954] | 281 |  | 
|---|
 | 282 |   //write diff to data | 
|---|
 | 283 |   for ( i = 0; i<neededSize; i++ ) | 
|---|
 | 284 |   { | 
|---|
 | 285 |     if ( i < stateFrom->dataLength ) | 
|---|
 | 286 |       data[i] = stateTo->data[i] - stateFrom->data[i]; | 
|---|
 | 287 |     else | 
|---|
 | 288 |       data[i] = stateTo->data[i]; | 
|---|
 | 289 |   } | 
|---|
 | 290 |  | 
|---|
 | 291 |   return neededSize; | 
|---|
 | 292 | } | 
|---|
 | 293 |  | 
|---|
| [5997] | 294 | /** | 
|---|
| [7954] | 295 |  * sets a new state out of a diff created on another host | 
|---|
 | 296 |  * @param userId hostId of user who send me that diff | 
|---|
 | 297 |  * @param data pointer to diff | 
|---|
 | 298 |  * @param length length of diff | 
|---|
 | 299 |  * @param stateId id of current state | 
|---|
 | 300 |  * @param fromStateId id of the base state id | 
|---|
 | 301 |  * @return number bytes read | 
|---|
| [9406] | 302 |  * | 
|---|
| [7954] | 303 |  * @todo check for permissions | 
|---|
| [5997] | 304 |  */ | 
|---|
| [7954] | 305 | int Synchronizeable::setStateDiff( int userId, byte* data, int length, int stateId, int fromStateId ) | 
|---|
| [5997] | 306 | { | 
|---|
| [7954] | 307 |   //make sure this user has his history | 
|---|
 | 308 |   if ( recvStates.size() <= userId ) | 
|---|
 | 309 |     recvStates.resize( userId+1 ); | 
|---|
 | 310 |  | 
|---|
 | 311 |   //create new state | 
|---|
 | 312 |   StateHistoryEntry * stateTo = new StateHistoryEntry(); | 
|---|
 | 313 |   stateTo->stateId = stateId; | 
|---|
 | 314 |   stateTo->dataLength = length; | 
|---|
 | 315 |   stateTo->data = new byte[ length ]; | 
|---|
 | 316 |  | 
|---|
 | 317 |  | 
|---|
 | 318 |   //find state to apply diff to | 
|---|
 | 319 |   StateHistoryEntry * stateFrom = NULL; | 
|---|
 | 320 |  | 
|---|
| [9406] | 321 |   // search the state from wich the diff is made of | 
|---|
| [7954] | 322 |   StateHistory::iterator it = recvStates[userId].begin(); | 
|---|
 | 323 |   while ( it != recvStates[userId].end() && (*it)->stateId != fromStateId ) | 
|---|
 | 324 |     it++; | 
|---|
 | 325 |  | 
|---|
| [9406] | 326 |   // if this is the first state to receive | 
|---|
| [7954] | 327 |   if ( it == recvStates[userId].end() ) | 
|---|
 | 328 |   { | 
|---|
 | 329 |     StateHistoryEntry * initialEntry = new StateHistoryEntry(); | 
|---|
 | 330 |  | 
|---|
 | 331 |     initialEntry->stateId = fromStateId; | 
|---|
 | 332 |     initialEntry->dataLength = 0; | 
|---|
 | 333 |     initialEntry->data = NULL; | 
|---|
 | 334 |  | 
|---|
 | 335 |     stateFrom = initialEntry; | 
|---|
| [9406] | 336 |  | 
|---|
| [8623] | 337 |     recvStates[userId].push_back( stateFrom ); | 
|---|
| [7954] | 338 |   } | 
|---|
| [5997] | 339 |   else | 
|---|
| [7954] | 340 |     stateFrom = (*it); | 
|---|
| [9406] | 341 |  | 
|---|
 | 342 |  | 
|---|
 | 343 |   // apply diff | 
|---|
| [7954] | 344 |   for ( int i = 0; i<length; i++ ) | 
|---|
 | 345 |   { | 
|---|
 | 346 |     if ( i < stateFrom->dataLength ) | 
|---|
 | 347 |       stateTo->data[i] = stateFrom->data[i] + data[i]; | 
|---|
 | 348 |     else | 
|---|
 | 349 |       stateTo->data[i] = data[i]; | 
|---|
 | 350 |   } | 
|---|
| [9406] | 351 |  | 
|---|
| [7954] | 352 |   //add state to state history | 
|---|
 | 353 |   recvStates[userId].push_back( stateTo ); | 
|---|
| [9406] | 354 |  | 
|---|
| [7954] | 355 |   int i = 0; | 
|---|
 | 356 |   int n = 0; | 
|---|
 | 357 |   std::list<int> changes; | 
|---|
| [9406] | 358 |   bool hasPermission = false; | 
|---|
 | 359 |  | 
|---|
 | 360 |   // extract the new state for every client | 
|---|
| [7954] | 361 |   for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ ) | 
|---|
 | 362 |   { | 
|---|
| [9406] | 363 |     // DATA PERMISSIONS | 
|---|
 | 364 |     // check if this synchronizeable has the permissions to write the data | 
|---|
 | 365 |  | 
|---|
| [9592] | 366 |  | 
|---|
 | 367 |     //  *              <====|  MASTER_SERVER | 
|---|
| [9406] | 368 |     if(  this->networkStream->isUserMasterServer( userId ) && (*it)->checkPermission( PERMISSION_MASTER_SERVER )) | 
|---|
 | 369 |       hasPermission = true; | 
|---|
| [9605] | 370 |     else if( this->networkStream->isUserMasterServer( userId ) && | 
|---|
 | 371 |              this->owner != SharedNetworkData::getInstance()->getHostID() && (*it)->checkPermission( PERMISSION_OWNER )) | 
|---|
| [9592] | 372 |       hasPermission = true; | 
|---|
 | 373 |  | 
|---|
 | 374 |     //  *              <====|  PROXY_SERVER | 
|---|
| [9605] | 375 |     else if( this->networkStream->isUserProxyServerActive( userId ) && | 
|---|
 | 376 |              this->owner != SharedNetworkData::getInstance()->getHostID() && (*it)->checkPermission( PERMISSION_OWNER )) | 
|---|
| [9592] | 377 |       hasPermission = true; | 
|---|
 | 378 |     //  CLIENT         <====|  PROXY_SERVER | 
|---|
| [9594] | 379 |     else if( this->networkStream->isUserProxyServerActive( userId )  && SharedNetworkData::getInstance()->isClient() | 
|---|
 | 380 |              && (*it)->checkPermission( PERMISSION_MASTER_SERVER )) | 
|---|
 | 381 |       hasPermission = true; | 
|---|
| [9592] | 382 |     // MASTER_SERVER   <====|  PROXY_SERVER | 
|---|
| [9590] | 383 |     else if( this->networkStream->isUserProxyServerActive( userId )  && SharedNetworkData::getInstance()->isMasterServer() && | 
|---|
 | 384 |              !this->networkStream->isUserLocal( userId )) | 
|---|
 | 385 |       hasPermission = true; | 
|---|
| [9592] | 386 |  | 
|---|
 | 387 |     //  *              <====|  OWNER | 
|---|
| [9406] | 388 |     else if( this->owner == userId && (*it)->checkPermission( PERMISSION_OWNER )) | 
|---|
 | 389 |       hasPermission = true; | 
|---|
| [9592] | 390 |  | 
|---|
 | 391 |     //  *              <====|  ALL | 
|---|
| [9406] | 392 |     else if( (*it)->checkPermission( PERMISSION_ALL )) | 
|---|
 | 393 |       hasPermission = true; | 
|---|
| [9592] | 394 |  | 
|---|
| [9406] | 395 |     else | 
|---|
 | 396 |       hasPermission = false; | 
|---|
 | 397 |  | 
|---|
 | 398 |  | 
|---|
 | 399 |  | 
|---|
 | 400 |     // if it has the permission to write do it | 
|---|
 | 401 |     if( hasPermission) | 
|---|
| [7954] | 402 |     { | 
|---|
 | 403 |       n = (*it)->readFromBuf( stateTo->data + i, stateTo->dataLength - i ); | 
|---|
 | 404 |       i += n; | 
|---|
| [9406] | 405 |       //NETPRINTF(0)("%s::setvar %s %d\n", getClassCName(), (*it)->getName().c_str(), n); | 
|---|
| [9579] | 406 |       //PRINTF(0)("%s::setvar %s %d\n", getClassCName(), (*it)->getName().c_str(), n); | 
|---|
| [7954] | 407 |       //(*it)->debug(); | 
|---|
 | 408 |       if ( (*it)->getHasChanged() ) | 
|---|
 | 409 |       { | 
|---|
 | 410 |         changes.push_back( (*it)->getVarId() ); | 
|---|
 | 411 |       } | 
|---|
 | 412 |     } | 
|---|
 | 413 |     else | 
|---|
 | 414 |     { | 
|---|
| [9494] | 415 | //       PRINTF(0)("DONT SET VAR BECAUSE OF PERMISSION: %s perm: %d %d %d - %d %d %d\n", (*it)->getName().c_str(), (*it)->checkPermission( PERMISSION_MASTER_SERVER ), (*it)->checkPermission( PERMISSION_OWNER ), (*it)->checkPermission( PERMISSION_ALL ), networkStream->isUserMasterServer( userId ), this->owner, userId ); | 
|---|
| [7954] | 416 |       n = (*it)->getSizeFromBuf( stateTo->data + i, stateTo->dataLength - i ); | 
|---|
| [9406] | 417 |       //NETPRINTF(0)("%s::setvar %s %d\n", getClassCName(), (*it)->getName().c_str(), n); | 
|---|
| [7954] | 418 |       //(*it)->debug(); | 
|---|
 | 419 |       i += n; | 
|---|
 | 420 |     } | 
|---|
 | 421 |   } | 
|---|
 | 422 |  | 
|---|
 | 423 |   this->varChangeHandler( changes ); | 
|---|
| [9406] | 424 |  | 
|---|
| [7954] | 425 |   return i; | 
|---|
| [5997] | 426 | } | 
|---|
 | 427 |  | 
|---|
| [7954] | 428 |  /** | 
|---|
 | 429 |  * override this function to be notified on change | 
|---|
 | 430 |  * of your registred variables. | 
|---|
 | 431 |  * @param id id's which have changed | 
|---|
 | 432 |  */ | 
|---|
 | 433 | void Synchronizeable::varChangeHandler( std::list<int> & id ) | 
|---|
 | 434 | { | 
|---|
 | 435 | } | 
|---|
| [6695] | 436 |  | 
|---|
| [5997] | 437 | /** | 
|---|
| [7954] | 438 |  * registers a varable to be synchronized over network | 
|---|
 | 439 |  * @param var see src/lib/network/synchronizeable_var/ for available classes | 
|---|
| [5997] | 440 |  */ | 
|---|
| [7954] | 441 | void Synchronizeable::registerVar( SynchronizeableVar * var ) | 
|---|
| [5997] | 442 | { | 
|---|
| [7954] | 443 |   syncVarList.push_back( var ); | 
|---|
| [5997] | 444 | } | 
|---|
 | 445 |  | 
|---|
 | 446 | /** | 
|---|
| [7954] | 447 |  * registers a varable to be synchronized over network | 
|---|
 | 448 |  * return value is passed to varChangeHandler on change | 
|---|
 | 449 |  * @param var see src/lib/network/synchronizeable_var/ for available classes | 
|---|
 | 450 |  * @return handle passed to varChangeHandler on changes | 
|---|
| [5997] | 451 |  */ | 
|---|
| [7954] | 452 | int Synchronizeable::registerVarId( SynchronizeableVar * var ) | 
|---|
| [5997] | 453 | { | 
|---|
| [7954] | 454 |   syncVarList.push_back( var ); | 
|---|
 | 455 |   var->setWatched( true ); | 
|---|
 | 456 |   var->setVarId( syncVarList.size()-1 ); | 
|---|
 | 457 |   return syncVarList.size()-1; | 
|---|
| [5997] | 458 | } | 
|---|
 | 459 |  | 
|---|
 | 460 | /** | 
|---|
| [7954] | 461 |  * removed user's states from memory | 
|---|
 | 462 |  * @param userId user to clean | 
|---|
| [5997] | 463 |  */ | 
|---|
| [7954] | 464 | void Synchronizeable::cleanUpUser( int userId ) | 
|---|
| [5997] | 465 | { | 
|---|
| [8228] | 466 |   if ( recvStates.size() > userId ) | 
|---|
| [7954] | 467 |   { | 
|---|
| [8228] | 468 |     for ( std::list<StateHistoryEntry*>::iterator it = recvStates[userId].begin(); it != recvStates[userId].end(); it++ ) | 
|---|
| [7954] | 469 |     { | 
|---|
| [8228] | 470 |       if ( (*it)->data ) | 
|---|
| [8623] | 471 |       { | 
|---|
| [8228] | 472 |         delete [] (*it)->data; | 
|---|
| [8623] | 473 |         (*it)->data = NULL; | 
|---|
 | 474 |       } | 
|---|
| [9406] | 475 |  | 
|---|
| [8228] | 476 |       delete *it; | 
|---|
| [7954] | 477 |     } | 
|---|
| [8228] | 478 |     recvStates[userId].clear(); | 
|---|
| [7954] | 479 |   } | 
|---|
| [9406] | 480 |  | 
|---|
| [8228] | 481 |   if ( sentStates.size() > userId ) | 
|---|
| [7954] | 482 |   { | 
|---|
| [9406] | 483 |  | 
|---|
| [8228] | 484 |     for ( std::list<StateHistoryEntry*>::iterator it = sentStates[userId].begin(); it != sentStates[userId].end(); it++ ) | 
|---|
| [7954] | 485 |     { | 
|---|
| [8228] | 486 |       if ( (*it)->data ) | 
|---|
| [8623] | 487 |       { | 
|---|
| [8228] | 488 |         delete [] (*it)->data; | 
|---|
| [8623] | 489 |         (*it)->data = NULL; | 
|---|
 | 490 |       } | 
|---|
| [9406] | 491 |  | 
|---|
| [8228] | 492 |       delete *it; | 
|---|
| [7954] | 493 |     } | 
|---|
| [8228] | 494 |     sentStates[userId].clear(); | 
|---|
| [7954] | 495 |   } | 
|---|
| [5997] | 496 | } | 
|---|
| [6139] | 497 |  | 
|---|
| [6341] | 498 | /** | 
|---|
| [7954] | 499 |  * this function is called after recieving a state. | 
|---|
| [9406] | 500 |  * @param userId | 
|---|
 | 501 |  * @param stateId | 
|---|
 | 502 |  * @param fromStateId | 
|---|
| [6341] | 503 |  */ | 
|---|
| [7954] | 504 | void Synchronizeable::handleRecvState( int userId, int stateId, int fromStateId ) | 
|---|
| [6341] | 505 | { | 
|---|
| [7954] | 506 |    //make sure this user has his history | 
|---|
 | 507 |   if ( recvStates.size() <= userId ) | 
|---|
 | 508 |     recvStates.resize( userId+1 ); | 
|---|
| [9406] | 509 |  | 
|---|
| [7954] | 510 |   //remove old states | 
|---|
 | 511 |   StateHistory::iterator it = recvStates[userId].begin(); | 
|---|
 | 512 |  | 
|---|
 | 513 | #if 0 | 
|---|
 | 514 |   while ( it != recvStates[userId].end() && (*it)->stateId < fromStateId ) | 
|---|
 | 515 |     it++; | 
|---|
 | 516 |  | 
|---|
 | 517 |   if ( it != recvStates[userId].begin() ) | 
|---|
 | 518 |   { | 
|---|
 | 519 |     for ( StateHistory::iterator it2 = recvStates[userId].begin(); it2 != it; it2++ ) | 
|---|
 | 520 |     { | 
|---|
 | 521 |       if ( (*it2)->data != NULL ) | 
|---|
 | 522 |       { | 
|---|
 | 523 |         delete [] (*it2)->data; | 
|---|
 | 524 |         (*it2)->data = NULL; | 
|---|
 | 525 |       } | 
|---|
 | 526 |     } | 
|---|
 | 527 |     recvStates[userId].erase( recvStates[userId].begin(), it ); | 
|---|
 | 528 |   } | 
|---|
 | 529 | #endif | 
|---|
 | 530 |  | 
|---|
 | 531 |   for ( it = recvStates[userId].begin(); it != recvStates[userId].end();  ) | 
|---|
 | 532 |   { | 
|---|
 | 533 |     if ( (*it)->stateId < fromStateId ) | 
|---|
 | 534 |     { | 
|---|
 | 535 |       StateHistory::iterator delIt = it; | 
|---|
 | 536 |       it ++; | 
|---|
| [9406] | 537 |  | 
|---|
| [7954] | 538 |       if ( (*delIt)->data ) | 
|---|
| [8623] | 539 |       { | 
|---|
| [7954] | 540 |         delete [] (*delIt)->data; | 
|---|
| [8623] | 541 |         (*delIt)->data = NULL; | 
|---|
 | 542 |       } | 
|---|
 | 543 |       delete *delIt; | 
|---|
| [7954] | 544 |       recvStates[userId].erase( delIt ); | 
|---|
| [9406] | 545 |  | 
|---|
| [7954] | 546 |       continue; | 
|---|
 | 547 |     } | 
|---|
 | 548 |     it++; | 
|---|
 | 549 |   } | 
|---|
| [9406] | 550 |  | 
|---|
| [7954] | 551 |   StateHistory::iterator fromState = recvStates[userId].end(); | 
|---|
 | 552 |   StateHistory::iterator toState = recvStates[userId].end(); | 
|---|
| [9406] | 553 |  | 
|---|
| [7954] | 554 |   for ( it = recvStates[userId].begin(); it != recvStates[userId].end(); it++ ) | 
|---|
 | 555 |   { | 
|---|
 | 556 |     if ( (*it)->stateId == stateId ) | 
|---|
 | 557 |       toState = it; | 
|---|
 | 558 |     if ( (*it)->stateId == fromStateId ) | 
|---|
 | 559 |       fromState = it; | 
|---|
| [9406] | 560 |  | 
|---|
| [7954] | 561 |     if ( fromState != recvStates[userId].end() && toState != recvStates[userId].end() ) | 
|---|
 | 562 |       break; | 
|---|
 | 563 |   } | 
|---|
| [9406] | 564 |  | 
|---|
| [7954] | 565 |   // setStateDiff was not called and i know fromStateId | 
|---|
 | 566 |   if ( fromState != recvStates[userId].end() && toState == recvStates[userId].end() ) | 
|---|
 | 567 |   { | 
|---|
 | 568 |     StateHistoryEntry * entry = new StateHistoryEntry; | 
|---|
| [9406] | 569 |  | 
|---|
| [7954] | 570 |     entry->dataLength = (*fromState)->dataLength; | 
|---|
 | 571 |     if ( entry->dataLength > 0 ) | 
|---|
 | 572 |     { | 
|---|
 | 573 |       entry->data = new byte[entry->dataLength]; | 
|---|
| [9406] | 574 |  | 
|---|
| [7954] | 575 |       assert( (*fromState)->data ); | 
|---|
 | 576 |       memcpy( entry->data, (*fromState)->data, entry->dataLength ); | 
|---|
 | 577 |     } | 
|---|
 | 578 |     else | 
|---|
 | 579 |       entry->data = NULL; | 
|---|
| [9406] | 580 |  | 
|---|
| [7954] | 581 |     entry->sizeList = (*fromState)->sizeList; | 
|---|
 | 582 |     entry->stateId = stateId; | 
|---|
| [9406] | 583 |  | 
|---|
| [7954] | 584 |     recvStates[userId].push_back(entry); | 
|---|
 | 585 |   } | 
|---|
| [6341] | 586 | } | 
|---|
| [6139] | 587 |  | 
|---|
| [6341] | 588 | /** | 
|---|
| [7954] | 589 |  * this function is called after sending a state | 
|---|
| [9406] | 590 |  * @param userId | 
|---|
 | 591 |  * @param stateId | 
|---|
 | 592 |  * @param fromStateId | 
|---|
| [6341] | 593 |  */ | 
|---|
| [7954] | 594 | void Synchronizeable::handleSentState( int userId, int stateId, int fromStateId ) | 
|---|
| [6341] | 595 | { | 
|---|
| [7954] | 596 |    //make sure this user has his history | 
|---|
 | 597 |   if ( sentStates.size() <= userId ) | 
|---|
 | 598 |     sentStates.resize( userId+1 ); | 
|---|
 | 599 |  | 
|---|
 | 600 |    //remove old states | 
|---|
 | 601 |   StateHistory::iterator it = sentStates[userId].begin(); | 
|---|
 | 602 |  | 
|---|
 | 603 |   for ( it = sentStates[userId].begin(); it != sentStates[userId].end();  ) | 
|---|
 | 604 |   { | 
|---|
 | 605 |     if ( (*it)->stateId < fromStateId ) | 
|---|
 | 606 |     { | 
|---|
 | 607 |       StateHistory::iterator delIt = it; | 
|---|
 | 608 |       it ++; | 
|---|
| [9406] | 609 |  | 
|---|
| [7954] | 610 |       if ( (*delIt)->data ) | 
|---|
| [8623] | 611 |       { | 
|---|
| [7954] | 612 |         delete [] (*delIt)->data; | 
|---|
| [8623] | 613 |         (*delIt)->data = NULL; | 
|---|
 | 614 |       } | 
|---|
 | 615 |       delete *delIt; | 
|---|
| [7954] | 616 |       sentStates[userId].erase( delIt ); | 
|---|
| [9406] | 617 |  | 
|---|
| [7954] | 618 |       continue; | 
|---|
 | 619 |     } | 
|---|
 | 620 |     it++; | 
|---|
 | 621 |   } | 
|---|
 | 622 |  | 
|---|
| [9406] | 623 |  | 
|---|
| [7954] | 624 |   StateHistory::iterator fromState = sentStates[userId].end(); | 
|---|
 | 625 |   StateHistory::iterator toState = sentStates[userId].end(); | 
|---|
| [9406] | 626 |  | 
|---|
| [7954] | 627 |   for ( it = sentStates[userId].begin(); it != sentStates[userId].end(); it++ ) | 
|---|
 | 628 |   { | 
|---|
 | 629 |     if ( (*it)->stateId == stateId ) | 
|---|
 | 630 |       toState = it; | 
|---|
 | 631 |     if ( (*it)->stateId == fromStateId ) | 
|---|
 | 632 |       fromState = it; | 
|---|
| [9406] | 633 |  | 
|---|
| [7954] | 634 |     if ( fromState != sentStates[userId].end() && toState != sentStates[userId].end() ) | 
|---|
 | 635 |       break; | 
|---|
 | 636 |   } | 
|---|
 | 637 |  | 
|---|
| [9406] | 638 |  | 
|---|
| [7954] | 639 |   // getStateDiff was not called and i know fromStateId | 
|---|
 | 640 |   if ( fromState != sentStates[userId].end() && toState == sentStates[userId].end() ) | 
|---|
 | 641 |   { | 
|---|
 | 642 |     StateHistoryEntry * entry = new StateHistoryEntry; | 
|---|
| [9406] | 643 |  | 
|---|
| [7954] | 644 |     entry->dataLength = (*fromState)->dataLength; | 
|---|
 | 645 |     if ( entry->dataLength > 0 ) | 
|---|
 | 646 |     { | 
|---|
 | 647 |       entry->data = new byte[entry->dataLength]; | 
|---|
| [9406] | 648 |  | 
|---|
| [7954] | 649 |       assert( (*fromState)->data ); | 
|---|
 | 650 |       memcpy( entry->data, (*fromState)->data, entry->dataLength ); | 
|---|
 | 651 |     } | 
|---|
 | 652 |     else | 
|---|
 | 653 |       entry->data = NULL; | 
|---|
| [9406] | 654 |  | 
|---|
| [7954] | 655 |     entry->sizeList = (*fromState)->sizeList; | 
|---|
 | 656 |     entry->stateId = stateId; | 
|---|
| [9406] | 657 |  | 
|---|
| [7954] | 658 |     sentStates[userId].push_back(entry); | 
|---|
 | 659 |   } | 
|---|
| [9406] | 660 |  | 
|---|
| [6341] | 661 | } | 
|---|
| [6139] | 662 |  | 
|---|
| [6341] | 663 |  | 
|---|
 | 664 |  | 
|---|