| 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: Christoph Renner (rennerc@ee.ethz.ch) | 
|---|
| 14 |    co-programmer: Patrick Boenzli (patrick@orxonox.ethz.ch) | 
|---|
| 15 | */ | 
|---|
| 16 |  | 
|---|
| 17 | #define DEBUG_MODULE_NETWORK | 
|---|
| 18 |  | 
|---|
| 19 | #include "shared_network_data.h" | 
|---|
| 20 | #include "network_stream.h" | 
|---|
| 21 | #include "netdefs.h" | 
|---|
| 22 | #include "network_log.h" | 
|---|
| 23 | #include "network_game_manager.h" | 
|---|
| 24 |  | 
|---|
| 25 | #include "state.h" | 
|---|
| 26 |  | 
|---|
| 27 | #include <cassert> | 
|---|
| 28 |  | 
|---|
| 29 | #include "synchronizeable.h" | 
|---|
| 30 |  | 
|---|
| 31 | #include "converter.h" | 
|---|
| 32 |  | 
|---|
| 33 |  | 
|---|
| 34 |  | 
|---|
| 35 | /** | 
|---|
| 36 |  *  default constructor | 
|---|
| 37 |  */ | 
|---|
| 38 | Synchronizeable::Synchronizeable() | 
|---|
| 39 | { | 
|---|
| 40 |   this->setClassID(CL_SYNCHRONIZEABLE, "Synchronizeable"); | 
|---|
| 41 |   this->owner = 0; | 
|---|
| 42 | //   this->setIsServer(SharedNetworkData::getInstance()->getHostID() == 0); | 
|---|
| 43 |   this->uniqueID = NET_UID_UNASSIGNED; | 
|---|
| 44 |   this->networkStream = NULL; | 
|---|
| 45 |   this->bSynchronize = false; | 
|---|
| 46 |  | 
|---|
| 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 |   } | 
|---|
| 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", PERMISSION_MASTER_SERVER) ); | 
|---|
| 58 |  | 
|---|
| 59 |   this->registerVar( new SynchronizeableInt( &this->owner, &this->owner, "owner", PERMISSION_MASTER_SERVER ) ); | 
|---|
| 60 |   this->registerVar( new SynchronizeableString( &this->objectName, &this->objectName, "objectName", PERMISSION_MASTER_SERVER ) ); | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 |  | 
|---|
| 64 |  | 
|---|
| 65 | /** | 
|---|
| 66 |  *  default destructor deletes all unneded stuff | 
|---|
| 67 |  */ | 
|---|
| 68 | Synchronizeable::~Synchronizeable() | 
|---|
| 69 | { | 
|---|
| 70 |   if ( this->networkStream ) | 
|---|
| 71 |   { | 
|---|
| 72 |     this->networkStream->disconnectSynchronizeable(*this); | 
|---|
| 73 |  | 
|---|
| 74 |     // remove the message manager only by the server | 
|---|
| 75 |     if ( (SharedNetworkData::getInstance()->isMasterServer() ) | 
|---|
| 76 |            && this->beSynchronized() && this->getUniqueID() > 0 && !this->isA( CL_MESSAGE_MANAGER ) ) | 
|---|
| 77 |       NetworkGameManager::getInstance()->removeSynchronizeable( this->getUniqueID() ); | 
|---|
| 78 |   } | 
|---|
| 79 |  | 
|---|
| 80 |   for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ ) | 
|---|
| 81 |   { | 
|---|
| 82 |     delete *it; | 
|---|
| 83 |   } | 
|---|
| 84 |   syncVarList.clear(); | 
|---|
| 85 |  | 
|---|
| 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 |   } | 
|---|
| 99 |  | 
|---|
| 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 |   } | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 |  | 
|---|
| 115 |  | 
|---|
| 116 | /** | 
|---|
| 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 | 
|---|
| 125 |  */ | 
|---|
| 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 or resize for new clients | 
|---|
| 129 |   if ( (int)sentStates.size() <= userId ) | 
|---|
| 130 |     sentStates.resize( userId+1 ); | 
|---|
| 131 |  | 
|---|
| 132 |   //calculate needed memory | 
|---|
| 133 |   int neededSize = 0; | 
|---|
| 134 |  | 
|---|
| 135 |   // calculate the needed space for network packet by summing up | 
|---|
| 136 |   for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ ) | 
|---|
| 137 |   { | 
|---|
| 138 |     //PRINTF(0)("SIZE = %d %s\n", (*it)->getSize(), (*it)->getName().c_str()); | 
|---|
| 139 |     neededSize += (*it)->getSize(); | 
|---|
| 140 |   } | 
|---|
| 141 |  | 
|---|
| 142 |   if ( !( neededSize <= maxLength ) ) | 
|---|
| 143 |   { | 
|---|
| 144 |     PRINTF(0)( "%d > %d\n", neededSize, maxLength ); | 
|---|
| 145 |     assert(false); | 
|---|
| 146 |   } | 
|---|
| 147 |  | 
|---|
| 148 |   //remove older states from history than fromStateId | 
|---|
| 149 |   StateHistory::iterator it = sentStates[userId].begin(); | 
|---|
| 150 |  | 
|---|
| 151 |   while ( it != sentStates[userId].end() && (*it)->stateId < fromStateId ) | 
|---|
| 152 |     it++; | 
|---|
| 153 |  | 
|---|
| 154 |   if ( it != sentStates[userId].begin() ) | 
|---|
| 155 |   { | 
|---|
| 156 |     for ( StateHistory::iterator it2 = sentStates[userId].begin(); it2 != it; it2++ ) | 
|---|
| 157 |     { | 
|---|
| 158 |       if ( (*it2)->data != NULL ) | 
|---|
| 159 |       { | 
|---|
| 160 |         delete [] (*it2)->data; | 
|---|
| 161 |         (*it2)->data = NULL; | 
|---|
| 162 |       } | 
|---|
| 163 |  | 
|---|
| 164 |       delete *it2; | 
|---|
| 165 |     } | 
|---|
| 166 |     sentStates[userId].erase( sentStates[userId].begin(), it ); | 
|---|
| 167 |   } | 
|---|
| 168 |  | 
|---|
| 169 |   //find state to create diff from | 
|---|
| 170 |   StateHistoryEntry * stateFrom = NULL; | 
|---|
| 171 |  | 
|---|
| 172 |   it = sentStates[userId].begin(); | 
|---|
| 173 |   while ( it != sentStates[userId].end() && (*it)->stateId != fromStateId ) | 
|---|
| 174 |     it++; | 
|---|
| 175 |  | 
|---|
| 176 |   if ( it == sentStates[userId].end() ) | 
|---|
| 177 |   { | 
|---|
| 178 |     StateHistoryEntry * initialEntry = new StateHistoryEntry(); | 
|---|
| 179 |  | 
|---|
| 180 |     initialEntry->stateId = fromStateId; | 
|---|
| 181 |     initialEntry->dataLength = 0; | 
|---|
| 182 |     initialEntry->data = NULL; | 
|---|
| 183 |  | 
|---|
| 184 |     stateFrom = initialEntry; | 
|---|
| 185 |  | 
|---|
| 186 |     sentStates[userId].push_back( stateFrom ); | 
|---|
| 187 |   } | 
|---|
| 188 |   else | 
|---|
| 189 |     stateFrom = (*it); | 
|---|
| 190 |  | 
|---|
| 191 |   StateHistoryEntry * stateTo = new StateHistoryEntry; | 
|---|
| 192 |  | 
|---|
| 193 |   sentStates[userId].push_back( stateTo ); | 
|---|
| 194 |  | 
|---|
| 195 |   stateTo->stateId = stateId; | 
|---|
| 196 |   stateTo->dataLength = neededSize; | 
|---|
| 197 |   stateTo->data = new byte[ neededSize ]; | 
|---|
| 198 |  | 
|---|
| 199 |   std::list<int>::iterator sizeIter = stateFrom->sizeList.begin(); | 
|---|
| 200 |  | 
|---|
| 201 |   int i = 0; | 
|---|
| 202 |   int n; | 
|---|
| 203 |  | 
|---|
| 204 |   bool sizeChanged = false; | 
|---|
| 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 |   { | 
|---|
| 209 |  | 
|---|
| 210 |     //////////////////////////////// | 
|---|
| 211 |     // Data SENDING Permissions | 
|---|
| 212 |     //////////////////////////////// | 
|---|
| 213 |     bool hasPermission = false; | 
|---|
| 214 |     bool b1, b2, b3, b4, b5, b6, b7, b8, b9; | 
|---|
| 215 |     b1 = b2 = b3 = b4 = b5 = b6 = b7 = b8 = b9 = false; | 
|---|
| 216 |  | 
|---|
| 217 |  | 
|---|
| 218 |     // Permission   OWNER accept if: | 
|---|
| 219 |     // I am the owner | 
|---|
| 220 |     if(       (*it)->checkPermission( PERMISSION_OWNER ) && this->owner == SharedNetworkData::getInstance()->getHostID()) { | 
|---|
| 221 |       hasPermission = true; b1 = true; } | 
|---|
| 222 |     // reciever != owner && owner is local | 
|---|
| 223 |     else if(  (*it)->checkPermission( PERMISSION_OWNER ) && userId != this->owner && | 
|---|
| 224 |                 (SharedNetworkData::getInstance()->isUserLocal(this->owner) || this->owner == SharedNetworkData::getInstance()->getHostID())) { | 
|---|
| 225 |       hasPermission = true; b2 = true; } | 
|---|
| 226 |  | 
|---|
| 227 |  | 
|---|
| 228 |     // Permission   MASTER_SERVER accept if: | 
|---|
| 229 |     // im MASTER_SERVER | 
|---|
| 230 |     else if( (*it)->checkPermission( PERMISSION_MASTER_SERVER ) && SharedNetworkData::getInstance()->isMasterServer()) { | 
|---|
| 231 |       hasPermission = true; b3 = true; } | 
|---|
| 232 |     // im PROXY_SERVER && reciever == CLIENT | 
|---|
| 233 |     else if( (*it)->checkPermission( PERMISSION_MASTER_SERVER ) && SharedNetworkData::getInstance()->isProxyServerActive() && | 
|---|
| 234 |                SharedNetworkData::getInstance()->isUserClient( userId)) { | 
|---|
| 235 |       hasPermission = true;  b4 = true; } | 
|---|
| 236 |  | 
|---|
| 237 |  | 
|---|
| 238 |     // Pemission    SERVER accept if: | 
|---|
| 239 |     // i am server && reciever == CLIENT | 
|---|
| 240 |     else if( (*it)->checkPermission( PERMISSION_SERVER ) && !SharedNetworkData::getInstance()->isClient() && | 
|---|
| 241 |                SharedNetworkData::getInstance()->isUserClient( userId)) { | 
|---|
| 242 |       hasPermission = true; b5 = true; } | 
|---|
| 243 |     // i am SERVER && reciever == SERVER && reciever != owner && ( owner is local || i am owner) | 
|---|
| 244 |     else if( (*it)->checkPermission( PERMISSION_SERVER ) && !SharedNetworkData::getInstance()->isClient() && | 
|---|
| 245 |                userId != this->owner && | 
|---|
| 246 |                ( SharedNetworkData::getInstance()->isUserLocal( this->owner) || this->owner ==  SharedNetworkData::getInstance()->getHostID())) { | 
|---|
| 247 |       hasPermission = true; b6 = true; } | 
|---|
| 248 |  | 
|---|
| 249 |  | 
|---|
| 250 |     // Permission   ALL accept if: | 
|---|
| 251 |     else if( (*it)->checkPermission( PERMISSION_ALL )) { | 
|---|
| 252 |       hasPermission = true; b7 = true; } | 
|---|
| 253 |     // or else refuse sending data | 
|---|
| 254 |     else | 
|---|
| 255 |       hasPermission = false; | 
|---|
| 256 |  | 
|---|
| 257 |  | 
|---|
| 258 |  | 
|---|
| 259 |     if ( sizeIter == stateFrom->sizeList.end() || *sizeIter != (*it)->getSize() ) | 
|---|
| 260 |       sizeChanged = true; | 
|---|
| 261 |  | 
|---|
| 262 |     if ( ( hasPermission && (*it)->getPriority() >= priorityTH ) || sizeChanged ) | 
|---|
| 263 |     { | 
|---|
| 264 |       n = (*it)->writeToBuf( stateTo->data+i, stateTo->dataLength - i ); | 
|---|
| 265 |       //NETPRINTF(0)("getvar %s %d\n", (*it)->getName().c_str(), n); | 
|---|
| 266 | //       PRINTF(0)("sending %s %d\n", (*it)->getName().c_str(), n); | 
|---|
| 267 |  | 
|---|
| 268 | //       if( this->isA(CL_PLAYABLE)) | 
|---|
| 269 | //       { | 
|---|
| 270 | //         PRINTF(0)("ms: %i, ps: %i, c: %i, sender: %i, reciever: %i, owner: %i, perm: (ow %i, ms %i, s %i, a %i)\n", | 
|---|
| 271 | //         SharedNetworkData::getInstance()->isMasterServer(), SharedNetworkData::getInstance()->isProxyServerActive(), SharedNetworkData::getInstance()->isClient(), | 
|---|
| 272 | //         SharedNetworkData::getInstance()->getHostID(), userId, this->owner, | 
|---|
| 273 | //         (*it)->checkPermission( PERMISSION_OWNER ), (*it)->checkPermission( PERMISSION_MASTER_SERVER ), | 
|---|
| 274 | //         (*it)->checkPermission( PERMISSION_SERVER ), (*it)->checkPermission( PERMISSION_ALL )); | 
|---|
| 275 | //         PRINTF(0)("hasPermission: %i, sizeChanged: %i, eval: %i, %i, %i, %i, %i, %i, %i\n", hasPermission, sizeChanged, b1, b2, b3, b4, b5, b6, b7); | 
|---|
| 276 | //         PRINTF(0)("sending %s %s %d\n", this->getClassCName(), (*it)->getName().c_str(), n); | 
|---|
| 277 | //       } | 
|---|
| 278 |  | 
|---|
| 279 |  | 
|---|
| 280 |       stateTo->sizeList.push_back( n ); | 
|---|
| 281 |       // this is only for very hardcore debug sessions | 
|---|
| 282 |       // (*it)->debug(); | 
|---|
| 283 |       i += n; | 
|---|
| 284 |     } | 
|---|
| 285 |     else | 
|---|
| 286 |     { | 
|---|
| 287 |       for ( int j = 0; j<(*sizeIter); j++ ) | 
|---|
| 288 |       { | 
|---|
| 289 |         assert( i < stateFrom->dataLength ); | 
|---|
| 290 |         stateTo->data[i] = stateFrom->data[i]; | 
|---|
| 291 |         i++; | 
|---|
| 292 |       } | 
|---|
| 293 |       //NETPRINTF(0)("getvar %s %d\n", (*it)->getName().c_str(), *sizeIter); | 
|---|
| 294 |       stateTo->sizeList.push_back( (*sizeIter) ); | 
|---|
| 295 |     } | 
|---|
| 296 |  | 
|---|
| 297 |     if ( sizeIter != stateFrom->sizeList.end() ) | 
|---|
| 298 |       sizeIter++; | 
|---|
| 299 |   } | 
|---|
| 300 |  | 
|---|
| 301 |   if ( i != neededSize ) | 
|---|
| 302 |   { | 
|---|
| 303 |     PRINTF(0)("strange error: (%s) %d != %d\n", this->getClassCName(), i, neededSize); | 
|---|
| 304 |     assert(false); | 
|---|
| 305 |   } | 
|---|
| 306 |  | 
|---|
| 307 |   //write diff to data | 
|---|
| 308 |   for ( i = 0; i<neededSize; i++ ) | 
|---|
| 309 |   { | 
|---|
| 310 |     if ( i < stateFrom->dataLength ) | 
|---|
| 311 |       data[i] = stateTo->data[i] - stateFrom->data[i]; | 
|---|
| 312 |     else | 
|---|
| 313 |       data[i] = stateTo->data[i]; | 
|---|
| 314 |   } | 
|---|
| 315 |  | 
|---|
| 316 |   return neededSize; | 
|---|
| 317 | } | 
|---|
| 318 |  | 
|---|
| 319 | /** | 
|---|
| 320 |  * sets a new state out of a diff created on another host (recieving data) | 
|---|
| 321 |  * @param userId hostId of user who send me that diff | 
|---|
| 322 |  * @param data pointer to diff | 
|---|
| 323 |  * @param length length of diff | 
|---|
| 324 |  * @param stateId id of current state | 
|---|
| 325 |  * @param fromStateId id of the base state id | 
|---|
| 326 |  * @return number bytes read | 
|---|
| 327 |  * | 
|---|
| 328 |  * @todo check for permissions | 
|---|
| 329 |  */ | 
|---|
| 330 | int Synchronizeable::setStateDiff( int userId, byte* data, int length, int stateId, int fromStateId ) | 
|---|
| 331 | { | 
|---|
| 332 |   //make sure this user has his history | 
|---|
| 333 |   if ( (int)recvStates.size() <= userId ) | 
|---|
| 334 |     recvStates.resize( userId+1 ); | 
|---|
| 335 |  | 
|---|
| 336 |   //create new state | 
|---|
| 337 |   StateHistoryEntry * stateTo = new StateHistoryEntry(); | 
|---|
| 338 |   stateTo->stateId = stateId; | 
|---|
| 339 |   stateTo->dataLength = length; | 
|---|
| 340 |   stateTo->data = new byte[ length ]; | 
|---|
| 341 |  | 
|---|
| 342 |  | 
|---|
| 343 |   //find state to apply diff to | 
|---|
| 344 |   StateHistoryEntry * stateFrom = NULL; | 
|---|
| 345 |  | 
|---|
| 346 |   // search the state from wich the diff is made of | 
|---|
| 347 |   StateHistory::iterator it = recvStates[userId].begin(); | 
|---|
| 348 |   while ( it != recvStates[userId].end() && (*it)->stateId != fromStateId ) | 
|---|
| 349 |     it++; | 
|---|
| 350 |  | 
|---|
| 351 |   // if this is the first state to receive | 
|---|
| 352 |   if ( it == recvStates[userId].end() ) | 
|---|
| 353 |   { | 
|---|
| 354 |     StateHistoryEntry * initialEntry = new StateHistoryEntry(); | 
|---|
| 355 |  | 
|---|
| 356 |     initialEntry->stateId = fromStateId; | 
|---|
| 357 |     initialEntry->dataLength = 0; | 
|---|
| 358 |     initialEntry->data = NULL; | 
|---|
| 359 |  | 
|---|
| 360 |     stateFrom = initialEntry; | 
|---|
| 361 |  | 
|---|
| 362 |     recvStates[userId].push_back( stateFrom ); | 
|---|
| 363 |   } | 
|---|
| 364 |   else | 
|---|
| 365 |     stateFrom = (*it); | 
|---|
| 366 |  | 
|---|
| 367 |  | 
|---|
| 368 |   // apply diff | 
|---|
| 369 |   for ( int i = 0; i<length; i++ ) | 
|---|
| 370 |   { | 
|---|
| 371 |     if ( i < stateFrom->dataLength ) | 
|---|
| 372 |       stateTo->data[i] = stateFrom->data[i] + data[i]; | 
|---|
| 373 |     else | 
|---|
| 374 |       stateTo->data[i] = data[i]; | 
|---|
| 375 |   } | 
|---|
| 376 |  | 
|---|
| 377 |   //add state to state history | 
|---|
| 378 |   recvStates[userId].push_back( stateTo ); | 
|---|
| 379 |  | 
|---|
| 380 |   int i = 0; | 
|---|
| 381 |   int n = 0; | 
|---|
| 382 |   std::list<int> changes; | 
|---|
| 383 |  | 
|---|
| 384 |   // extract the new state for every client | 
|---|
| 385 |   for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ ) | 
|---|
| 386 |   { | 
|---|
| 387 |     // DATA PERMISSIONS | 
|---|
| 388 |     // check if this synchronizeable has the permissions to write the data | 
|---|
| 389 |  | 
|---|
| 390 |     bool hasPermission = false; | 
|---|
| 391 |     bool b1, b2, b3, b4, b5, b6, b7, b8, b9; | 
|---|
| 392 |     b1 = b2 = b3 = b4 = b5 = b6 = b7 = b8 = b9 = false; | 
|---|
| 393 |  | 
|---|
| 394 |     //////////////////////////////// | 
|---|
| 395 |     // Data RECIEVING Permissions | 
|---|
| 396 |     //////////////////////////////// | 
|---|
| 397 |  | 
|---|
| 398 |     // i should never ever receive a state update from a synchronizeable, that belongs to me! If it does somethings wrong with the send rules | 
|---|
| 399 | //     assert(   !((*it)->checkPermission( PERMISSION_OWNER ) && this->owner == SharedNetworkData::getInstance()->getHostID())); | 
|---|
| 400 |  | 
|---|
| 401 |  | 
|---|
| 402 |     // Permission   OWNER accept if: | 
|---|
| 403 |     // sender == owner | 
|---|
| 404 |     if(      (*it)->checkPermission( PERMISSION_OWNER ) && this->owner == userId) { | 
|---|
| 405 |       hasPermission = true; b1 = true; } | 
|---|
| 406 |     // sender == MASTER_SERVER | 
|---|
| 407 |     else if( (*it)->checkPermission( PERMISSION_OWNER ) && SharedNetworkData::getInstance()->isUserMasterServer( userId) | 
|---|
| 408 |                && this->owner != SharedNetworkData::getInstance()->getHostID()) { | 
|---|
| 409 |       hasPermission = true; b2 = true; } | 
|---|
| 410 |     // sender == PROXY_SERVER | 
|---|
| 411 |       else if( (*it)->checkPermission( PERMISSION_OWNER ) && SharedNetworkData::getInstance()->isUserProxyServerActive( userId) && | 
|---|
| 412 |                  this->owner != SharedNetworkData::getInstance()->getHostID()) { | 
|---|
| 413 |         hasPermission = true; b3 = true; } | 
|---|
| 414 |  | 
|---|
| 415 |  | 
|---|
| 416 |  | 
|---|
| 417 |     // Permission   MASTER_SERVER accept if: | 
|---|
| 418 |     // sender == MASTER_SERVER | 
|---|
| 419 |     else if( (*it)->checkPermission( PERMISSION_MASTER_SERVER) && SharedNetworkData::getInstance()->isUserMasterServer( userId)) { | 
|---|
| 420 |       hasPermission = true; b4 = true; } | 
|---|
| 421 |     // sender == PROXY_SERVER && im not MASTER_SERVER && im not PROXY_SERVER | 
|---|
| 422 |     else if( (*it)->checkPermission( PERMISSION_MASTER_SERVER) && SharedNetworkData::getInstance()->isClient() && | 
|---|
| 423 |                SharedNetworkData::getInstance()->isUserProxyServerActive( userId)) { | 
|---|
| 424 |       hasPermission = true; b5 = true; } | 
|---|
| 425 |  | 
|---|
| 426 |     // Permission   SERVER accept if: | 
|---|
| 427 |     // sender == SERVER | 
|---|
| 428 |     else if( (*it)->checkPermission( PERMISSION_SERVER ) && !SharedNetworkData::getInstance()->isUserClient( userId) /*&& | 
|---|
| 429 |                SharedNetworkData::getInstance()->isClient()*/) { | 
|---|
| 430 |       hasPermission = true; b6 = true; } | 
|---|
| 431 |  | 
|---|
| 432 |  | 
|---|
| 433 |  | 
|---|
| 434 |     // Pemission    ALL accept if: | 
|---|
| 435 |     else if(  (*it)->checkPermission( PERMISSION_ALL )) { | 
|---|
| 436 |       hasPermission = true; b8 = true; } | 
|---|
| 437 |  | 
|---|
| 438 |  | 
|---|
| 439 |    // no rights to over-write local data | 
|---|
| 440 |     else | 
|---|
| 441 |       hasPermission = false; | 
|---|
| 442 |  | 
|---|
| 443 |  | 
|---|
| 444 |  | 
|---|
| 445 |     // if it has the permission to write do it | 
|---|
| 446 |     if( hasPermission) | 
|---|
| 447 |     { | 
|---|
| 448 |       n = (*it)->readFromBuf( stateTo->data + i, stateTo->dataLength - i ); | 
|---|
| 449 |       i += n; | 
|---|
| 450 |       //NETPRINTF(0)("%s::setvar %s %d\n", getClassCName(), (*it)->getName().c_str(), n); | 
|---|
| 451 | //       PRINTF(0)("recieving: %s %d\n",  (*it)->getName().c_str(), n); | 
|---|
| 452 |       //(*it)->debug(); | 
|---|
| 453 |  | 
|---|
| 454 | //       if( this->isA(CL_PLAYABLE)) | 
|---|
| 455 | //       { | 
|---|
| 456 | //         PRINTF(0)("ms: %i, ps: %i, c: %i, sender: %i, reciever: %i, owner: %i, perm: (ow %i, ms %i, s %i, a %i)\n", | 
|---|
| 457 | //         SharedNetworkData::getInstance()->isMasterServer(), SharedNetworkData::getInstance()->isProxyServerActive(), SharedNetworkData::getInstance()->isClient(), | 
|---|
| 458 | //         userId, SharedNetworkData::getInstance()->getHostID(), this->owner, | 
|---|
| 459 | //         (*it)->checkPermission( PERMISSION_OWNER ), (*it)->checkPermission( PERMISSION_MASTER_SERVER ), | 
|---|
| 460 | //         (*it)->checkPermission( PERMISSION_SERVER ), (*it)->checkPermission( PERMISSION_ALL )); | 
|---|
| 461 | //         PRINTF(0)("hasPermission: %i, eval: %i, %i, %i, %i, %i, %i, %i, %i\n", hasPermission, b1, b2, b3, b4, b5, b6, b7, b8); | 
|---|
| 462 | //         PRINTF(0)("rec %s %s %d\n", this->getClassCName(), (*it)->getName().c_str(), n); | 
|---|
| 463 | //       } | 
|---|
| 464 |  | 
|---|
| 465 |  | 
|---|
| 466 |       if ( (*it)->getHasChanged() ) | 
|---|
| 467 |       { | 
|---|
| 468 |         changes.push_back( (*it)->getVarId() ); | 
|---|
| 469 |       } | 
|---|
| 470 |     } | 
|---|
| 471 |     else | 
|---|
| 472 |     { | 
|---|
| 473 | //       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 ); | 
|---|
| 474 |       n = (*it)->getSizeFromBuf( stateTo->data + i, stateTo->dataLength - i ); | 
|---|
| 475 |       //NETPRINTF(0)("%s::setvar %s %d\n", getClassCName(), (*it)->getName().c_str(), n); | 
|---|
| 476 |       //(*it)->debug(); | 
|---|
| 477 |       i += n; | 
|---|
| 478 |     } | 
|---|
| 479 |   } | 
|---|
| 480 |  | 
|---|
| 481 |   this->varChangeHandler( changes ); | 
|---|
| 482 |  | 
|---|
| 483 |   return i; | 
|---|
| 484 | } | 
|---|
| 485 |  | 
|---|
| 486 |  /** | 
|---|
| 487 |  * override this function to be notified on change | 
|---|
| 488 |  * of your registred variables. | 
|---|
| 489 |  * @param id id's which have changed | 
|---|
| 490 |  */ | 
|---|
| 491 | void Synchronizeable::varChangeHandler( std::list<int> & id ) | 
|---|
| 492 | { | 
|---|
| 493 | } | 
|---|
| 494 |  | 
|---|
| 495 | /** | 
|---|
| 496 |  * registers a varable to be synchronized over network | 
|---|
| 497 |  * @param var see src/lib/network/synchronizeable_var/ for available classes | 
|---|
| 498 |  */ | 
|---|
| 499 | void Synchronizeable::registerVar( SynchronizeableVar * var ) | 
|---|
| 500 | { | 
|---|
| 501 |   syncVarList.push_back( var ); | 
|---|
| 502 | } | 
|---|
| 503 |  | 
|---|
| 504 | /** | 
|---|
| 505 |  * registers a varable to be synchronized over network | 
|---|
| 506 |  * return value is passed to varChangeHandler on change | 
|---|
| 507 |  * @param var see src/lib/network/synchronizeable_var/ for available classes | 
|---|
| 508 |  * @return handle passed to varChangeHandler on changes | 
|---|
| 509 |  */ | 
|---|
| 510 | int Synchronizeable::registerVarId( SynchronizeableVar * var ) | 
|---|
| 511 | { | 
|---|
| 512 |   syncVarList.push_back( var ); | 
|---|
| 513 |   var->setWatched( true ); | 
|---|
| 514 |   var->setVarId( syncVarList.size()-1 ); | 
|---|
| 515 |   return syncVarList.size()-1; | 
|---|
| 516 | } | 
|---|
| 517 |  | 
|---|
| 518 | /** | 
|---|
| 519 |  * removed user's states from memory | 
|---|
| 520 |  * @param userId user to clean | 
|---|
| 521 |  */ | 
|---|
| 522 | void Synchronizeable::cleanUpUser( int userId ) | 
|---|
| 523 | { | 
|---|
| 524 |   if ( (int)recvStates.size() > userId ) | 
|---|
| 525 |   { | 
|---|
| 526 |     for ( std::list<StateHistoryEntry*>::iterator it = recvStates[userId].begin(); it != recvStates[userId].end(); it++ ) | 
|---|
| 527 |     { | 
|---|
| 528 |       if ( (*it)->data ) | 
|---|
| 529 |       { | 
|---|
| 530 |         delete [] (*it)->data; | 
|---|
| 531 |         (*it)->data = NULL; | 
|---|
| 532 |       } | 
|---|
| 533 |  | 
|---|
| 534 |       delete *it; | 
|---|
| 535 |     } | 
|---|
| 536 |     recvStates[userId].clear(); | 
|---|
| 537 |   } | 
|---|
| 538 |  | 
|---|
| 539 |   if ( (int)sentStates.size() > userId ) | 
|---|
| 540 |   { | 
|---|
| 541 |  | 
|---|
| 542 |     for ( std::list<StateHistoryEntry*>::iterator it = sentStates[userId].begin(); it != sentStates[userId].end(); it++ ) | 
|---|
| 543 |     { | 
|---|
| 544 |       if ( (*it)->data ) | 
|---|
| 545 |       { | 
|---|
| 546 |         delete [] (*it)->data; | 
|---|
| 547 |         (*it)->data = NULL; | 
|---|
| 548 |       } | 
|---|
| 549 |  | 
|---|
| 550 |       delete *it; | 
|---|
| 551 |     } | 
|---|
| 552 |     sentStates[userId].clear(); | 
|---|
| 553 |   } | 
|---|
| 554 | } | 
|---|
| 555 |  | 
|---|
| 556 | /** | 
|---|
| 557 |  * this function is called after recieving a state. | 
|---|
| 558 |  * @param userId | 
|---|
| 559 |  * @param stateId | 
|---|
| 560 |  * @param fromStateId | 
|---|
| 561 |  */ | 
|---|
| 562 | void Synchronizeable::handleRecvState( int userId, int stateId, int fromStateId ) | 
|---|
| 563 | { | 
|---|
| 564 |    //make sure this user has his history | 
|---|
| 565 |   if ( (int)recvStates.size() <= userId ) | 
|---|
| 566 |     recvStates.resize( userId+1 ); | 
|---|
| 567 |  | 
|---|
| 568 |   //remove old states | 
|---|
| 569 |   StateHistory::iterator it = recvStates[userId].begin(); | 
|---|
| 570 |  | 
|---|
| 571 | #if 0 | 
|---|
| 572 |   while ( it != recvStates[userId].end() && (*it)->stateId < fromStateId ) | 
|---|
| 573 |     it++; | 
|---|
| 574 |  | 
|---|
| 575 |   if ( it != recvStates[userId].begin() ) | 
|---|
| 576 |   { | 
|---|
| 577 |     for ( StateHistory::iterator it2 = recvStates[userId].begin(); it2 != it; it2++ ) | 
|---|
| 578 |     { | 
|---|
| 579 |       if ( (*it2)->data != NULL ) | 
|---|
| 580 |       { | 
|---|
| 581 |         delete [] (*it2)->data; | 
|---|
| 582 |         (*it2)->data = NULL; | 
|---|
| 583 |       } | 
|---|
| 584 |     } | 
|---|
| 585 |     recvStates[userId].erase( recvStates[userId].begin(), it ); | 
|---|
| 586 |   } | 
|---|
| 587 | #endif | 
|---|
| 588 |  | 
|---|
| 589 |   for ( it = recvStates[userId].begin(); it != recvStates[userId].end();  ) | 
|---|
| 590 |   { | 
|---|
| 591 |     if ( (*it)->stateId < fromStateId ) | 
|---|
| 592 |     { | 
|---|
| 593 |       StateHistory::iterator delIt = it; | 
|---|
| 594 |       it ++; | 
|---|
| 595 |  | 
|---|
| 596 |       if ( (*delIt)->data ) | 
|---|
| 597 |       { | 
|---|
| 598 |         delete [] (*delIt)->data; | 
|---|
| 599 |         (*delIt)->data = NULL; | 
|---|
| 600 |       } | 
|---|
| 601 |       delete *delIt; | 
|---|
| 602 |       recvStates[userId].erase( delIt ); | 
|---|
| 603 |  | 
|---|
| 604 |       continue; | 
|---|
| 605 |     } | 
|---|
| 606 |     it++; | 
|---|
| 607 |   } | 
|---|
| 608 |  | 
|---|
| 609 |   StateHistory::iterator fromState = recvStates[userId].end(); | 
|---|
| 610 |   StateHistory::iterator toState = recvStates[userId].end(); | 
|---|
| 611 |  | 
|---|
| 612 |   for ( it = recvStates[userId].begin(); it != recvStates[userId].end(); it++ ) | 
|---|
| 613 |   { | 
|---|
| 614 |     if ( (*it)->stateId == stateId ) | 
|---|
| 615 |       toState = it; | 
|---|
| 616 |     if ( (*it)->stateId == fromStateId ) | 
|---|
| 617 |       fromState = it; | 
|---|
| 618 |  | 
|---|
| 619 |     if ( fromState != recvStates[userId].end() && toState != recvStates[userId].end() ) | 
|---|
| 620 |       break; | 
|---|
| 621 |   } | 
|---|
| 622 |  | 
|---|
| 623 |   // setStateDiff was not called and i know fromStateId | 
|---|
| 624 |   if ( fromState != recvStates[userId].end() && toState == recvStates[userId].end() ) | 
|---|
| 625 |   { | 
|---|
| 626 |     StateHistoryEntry * entry = new StateHistoryEntry; | 
|---|
| 627 |  | 
|---|
| 628 |     entry->dataLength = (*fromState)->dataLength; | 
|---|
| 629 |     if ( entry->dataLength > 0 ) | 
|---|
| 630 |     { | 
|---|
| 631 |       entry->data = new byte[entry->dataLength]; | 
|---|
| 632 |  | 
|---|
| 633 |       assert( (*fromState)->data ); | 
|---|
| 634 |       memcpy( entry->data, (*fromState)->data, entry->dataLength ); | 
|---|
| 635 |     } | 
|---|
| 636 |     else | 
|---|
| 637 |       entry->data = NULL; | 
|---|
| 638 |  | 
|---|
| 639 |     entry->sizeList = (*fromState)->sizeList; | 
|---|
| 640 |     entry->stateId = stateId; | 
|---|
| 641 |  | 
|---|
| 642 |     recvStates[userId].push_back(entry); | 
|---|
| 643 |   } | 
|---|
| 644 | } | 
|---|
| 645 |  | 
|---|
| 646 | /** | 
|---|
| 647 |  * this function is called after sending a state | 
|---|
| 648 |  * @param userId | 
|---|
| 649 |  * @param stateId | 
|---|
| 650 |  * @param fromStateId | 
|---|
| 651 |  */ | 
|---|
| 652 | void Synchronizeable::handleSentState( int userId, int stateId, int fromStateId ) | 
|---|
| 653 | { | 
|---|
| 654 |    //make sure this user has his history | 
|---|
| 655 |   if ( (int)sentStates.size() <= userId ) | 
|---|
| 656 |     sentStates.resize( userId+1 ); | 
|---|
| 657 |  | 
|---|
| 658 |    //remove old states | 
|---|
| 659 |   StateHistory::iterator it = sentStates[userId].begin(); | 
|---|
| 660 |  | 
|---|
| 661 |   for ( it = sentStates[userId].begin(); it != sentStates[userId].end();  ) | 
|---|
| 662 |   { | 
|---|
| 663 |     if ( (*it)->stateId < fromStateId ) | 
|---|
| 664 |     { | 
|---|
| 665 |       StateHistory::iterator delIt = it; | 
|---|
| 666 |       it ++; | 
|---|
| 667 |  | 
|---|
| 668 |       if ( (*delIt)->data ) | 
|---|
| 669 |       { | 
|---|
| 670 |         delete [] (*delIt)->data; | 
|---|
| 671 |         (*delIt)->data = NULL; | 
|---|
| 672 |       } | 
|---|
| 673 |       delete *delIt; | 
|---|
| 674 |       sentStates[userId].erase( delIt ); | 
|---|
| 675 |  | 
|---|
| 676 |       continue; | 
|---|
| 677 |     } | 
|---|
| 678 |     it++; | 
|---|
| 679 |   } | 
|---|
| 680 |  | 
|---|
| 681 |  | 
|---|
| 682 |   StateHistory::iterator fromState = sentStates[userId].end(); | 
|---|
| 683 |   StateHistory::iterator toState = sentStates[userId].end(); | 
|---|
| 684 |  | 
|---|
| 685 |   for ( it = sentStates[userId].begin(); it != sentStates[userId].end(); it++ ) | 
|---|
| 686 |   { | 
|---|
| 687 |     if ( (*it)->stateId == stateId ) | 
|---|
| 688 |       toState = it; | 
|---|
| 689 |     if ( (*it)->stateId == fromStateId ) | 
|---|
| 690 |       fromState = it; | 
|---|
| 691 |  | 
|---|
| 692 |     if ( fromState != sentStates[userId].end() && toState != sentStates[userId].end() ) | 
|---|
| 693 |       break; | 
|---|
| 694 |   } | 
|---|
| 695 |  | 
|---|
| 696 |  | 
|---|
| 697 |   // getStateDiff was not called and i know fromStateId | 
|---|
| 698 |   if ( fromState != sentStates[userId].end() && toState == sentStates[userId].end() ) | 
|---|
| 699 |   { | 
|---|
| 700 |     StateHistoryEntry * entry = new StateHistoryEntry; | 
|---|
| 701 |  | 
|---|
| 702 |     entry->dataLength = (*fromState)->dataLength; | 
|---|
| 703 |     if ( entry->dataLength > 0 ) | 
|---|
| 704 |     { | 
|---|
| 705 |       entry->data = new byte[entry->dataLength]; | 
|---|
| 706 |  | 
|---|
| 707 |       assert( (*fromState)->data ); | 
|---|
| 708 |       memcpy( entry->data, (*fromState)->data, entry->dataLength ); | 
|---|
| 709 |     } | 
|---|
| 710 |     else | 
|---|
| 711 |       entry->data = NULL; | 
|---|
| 712 |  | 
|---|
| 713 |     entry->sizeList = (*fromState)->sizeList; | 
|---|
| 714 |     entry->stateId = stateId; | 
|---|
| 715 |  | 
|---|
| 716 |     sentStates[userId].push_back(entry); | 
|---|
| 717 |   } | 
|---|
| 718 |  | 
|---|
| 719 | } | 
|---|
| 720 |  | 
|---|
| 721 |  | 
|---|
| 722 |  | 
|---|