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