Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/proxy/src/lib/network/synchronizeable.cc @ 9649

Last change on this file since 9649 was 9636, checked in by patrick, 18 years ago

connection redirection now works also as it should

File size: 21.7 KB
RevLine 
[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]38Synchronizeable::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 );
[9625]57  mLeafClassId = this->registerVarId( new SynchronizeableInt( (int*)&this->getLeafClassID(), (int*)&this->getLeafClassID(), "leafClassId", PERMISSION_MASTER_SERVER) );
[9406]58
[9625]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 ) );
[5997]61}
62
[5523]63
[5996]64
[5547]65/**
[5807]66 *  default destructor deletes all unneded stuff
[5547]67 */
68Synchronizeable::~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]126int Synchronizeable::getStateDiff( int userId, byte* data, int maxLength, int stateId, int fromStateId, int priorityTH )
127{
128  //make sure this user has his history
[9625]129  if ( (int)sentStates.size() <= userId )
[7954]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
[8623]203  bool sizeChanged = false;
[7954]204
205  // now do the actual synchronization: kick all variables to write into a common buffer
206  for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ )
207  {
[9625]208
209    ////////////////////////////////
210    // Data SENDING Permissions
211    ////////////////////////////////
[9627]212    bool hasPermission = false;
213    bool b1, b2, b3, b4, b5, b6, b7, b8, b9;
214    b1 = b2 = b3 = b4 = b5 = b6 = b7 = b8 = b9 = false;
[9406]215
[9627]216
[9625]217    // Permission   OWNER accept if:
218    // I am the owner
[9627]219    if(       (*it)->checkPermission( PERMISSION_OWNER ) && this->owner == SharedNetworkData::getInstance()->getHostID()) {
220      hasPermission = true; b1 = true; }
[9625]221    // reciever != owner && owner is local
222    else if(  (*it)->checkPermission( PERMISSION_OWNER ) && userId != this->owner &&
[9627]223                (SharedNetworkData::getInstance()->isUserLocal(this->owner) || this->owner == SharedNetworkData::getInstance()->getHostID())) {
224      hasPermission = true; b2 = true; }
[9625]225
226
227    // Permission   MASTER_SERVER accept if:
228    // im MASTER_SERVER
[9627]229    else if( (*it)->checkPermission( PERMISSION_MASTER_SERVER ) && SharedNetworkData::getInstance()->isMasterServer()) {
230      hasPermission = true; b3 = true; }
[9625]231    // im PROXY_SERVER && reciever == CLIENT
232    else if( (*it)->checkPermission( PERMISSION_MASTER_SERVER ) && SharedNetworkData::getInstance()->isProxyServerActive() &&
[9627]233               SharedNetworkData::getInstance()->isUserClient( userId)) {
234      hasPermission = true;  b4 = true; }
[9625]235
236
237    // Pemission    SERVER accept if:
238    // i am server && reciever == CLIENT
239    else if( (*it)->checkPermission( PERMISSION_SERVER ) && !SharedNetworkData::getInstance()->isClient() &&
[9627]240               SharedNetworkData::getInstance()->isUserClient( userId)) {
241      hasPermission = true; b5 = true; }
[9625]242    // i am SERVER && reciever == SERVER && reciever != owner && ( owner is local || i am owner)
243    else if( (*it)->checkPermission( PERMISSION_SERVER ) && !SharedNetworkData::getInstance()->isClient() &&
244               userId != this->owner &&
[9633]245               ( SharedNetworkData::getInstance()->isUserLocal( this->owner) || this->owner ==  SharedNetworkData::getInstance()->getHostID())) {
[9627]246      hasPermission = true; b6 = true; }
[9625]247
248
249    // Permission   ALL accept if:
[9627]250    else if( (*it)->checkPermission( PERMISSION_ALL )) {
251      hasPermission = true; b7 = true; }
[9625]252    // or else refuse sending data
253    else
254      hasPermission = false;
255
256
[9626]257
[9406]258    if ( sizeIter == stateFrom->sizeList.end() || *sizeIter != (*it)->getSize() )
[8623]259      sizeChanged = true;
[9406]260
[8623]261    if ( ( hasPermission && (*it)->getPriority() >= priorityTH ) || sizeChanged )
[7954]262    {
263      n = (*it)->writeToBuf( stateTo->data+i, stateTo->dataLength - i );
[9579]264      //NETPRINTF(0)("getvar %s %d\n", (*it)->getName().c_str(), n);
[9626]265//       PRINTF(0)("sending %s %d\n", (*it)->getName().c_str(), n);
[9627]266
267//       if( this->isA(CL_PLAYABLE))
268//       {
269//         PRINTF(0)("ms: %i, ps: %i, c: %i, sender: %i, reciever: %i, owner: %i, perm: (ow %i, ms %i, s %i, a %i)\n",
270//         SharedNetworkData::getInstance()->isMasterServer(), SharedNetworkData::getInstance()->isProxyServerActive(), SharedNetworkData::getInstance()->isClient(),
271//         SharedNetworkData::getInstance()->getHostID(), userId, this->owner,
272//         (*it)->checkPermission( PERMISSION_OWNER ), (*it)->checkPermission( PERMISSION_MASTER_SERVER ),
273//         (*it)->checkPermission( PERMISSION_SERVER ), (*it)->checkPermission( PERMISSION_ALL ));
274//         PRINTF(0)("hasPermission: %i, sizeChanged: %i, eval: %i, %i, %i, %i, %i, %i, %i\n", hasPermission, sizeChanged, b1, b2, b3, b4, b5, b6, b7);
275//         PRINTF(0)("sending %s %s %d\n", this->getClassCName(), (*it)->getName().c_str(), n);
276//       }
277
278
[7954]279      stateTo->sizeList.push_back( n );
[9406]280      // this is only for very hardcore debug sessions
281      // (*it)->debug();
[7954]282      i += n;
283    }
284    else
285    {
286      for ( int j = 0; j<(*sizeIter); j++ )
287      {
288        assert( i < stateFrom->dataLength );
289        stateTo->data[i] = stateFrom->data[i];
290        i++;
291      }
292      //NETPRINTF(0)("getvar %s %d\n", (*it)->getName().c_str(), *sizeIter);
293      stateTo->sizeList.push_back( (*sizeIter) );
294    }
295
296    if ( sizeIter != stateFrom->sizeList.end() )
297      sizeIter++;
298  }
299
[8147]300  if ( i != neededSize )
301  {
[9406]302    PRINTF(0)("strange error: (%s) %d != %d\n", this->getClassCName(), i, neededSize);
[8147]303    assert(false);
304  }
[7954]305
306  //write diff to data
307  for ( i = 0; i<neededSize; i++ )
308  {
309    if ( i < stateFrom->dataLength )
310      data[i] = stateTo->data[i] - stateFrom->data[i];
311    else
312      data[i] = stateTo->data[i];
313  }
314
315  return neededSize;
316}
317
[5997]318/**
[9626]319 * sets a new state out of a diff created on another host (recieving data)
[7954]320 * @param userId hostId of user who send me that diff
321 * @param data pointer to diff
322 * @param length length of diff
323 * @param stateId id of current state
324 * @param fromStateId id of the base state id
325 * @return number bytes read
[9406]326 *
[7954]327 * @todo check for permissions
[5997]328 */
[7954]329int Synchronizeable::setStateDiff( int userId, byte* data, int length, int stateId, int fromStateId )
[5997]330{
[7954]331  //make sure this user has his history
[9625]332  if ( (int)recvStates.size() <= userId )
[7954]333    recvStates.resize( userId+1 );
334
335  //create new state
336  StateHistoryEntry * stateTo = new StateHistoryEntry();
337  stateTo->stateId = stateId;
338  stateTo->dataLength = length;
339  stateTo->data = new byte[ length ];
340
341
342  //find state to apply diff to
343  StateHistoryEntry * stateFrom = NULL;
344
[9406]345  // search the state from wich the diff is made of
[7954]346  StateHistory::iterator it = recvStates[userId].begin();
347  while ( it != recvStates[userId].end() && (*it)->stateId != fromStateId )
348    it++;
349
[9406]350  // if this is the first state to receive
[7954]351  if ( it == recvStates[userId].end() )
352  {
353    StateHistoryEntry * initialEntry = new StateHistoryEntry();
354
355    initialEntry->stateId = fromStateId;
356    initialEntry->dataLength = 0;
357    initialEntry->data = NULL;
358
359    stateFrom = initialEntry;
[9406]360
[8623]361    recvStates[userId].push_back( stateFrom );
[7954]362  }
[5997]363  else
[7954]364    stateFrom = (*it);
[9406]365
366
367  // apply diff
[7954]368  for ( int i = 0; i<length; i++ )
369  {
370    if ( i < stateFrom->dataLength )
371      stateTo->data[i] = stateFrom->data[i] + data[i];
372    else
373      stateTo->data[i] = data[i];
374  }
[9406]375
[7954]376  //add state to state history
377  recvStates[userId].push_back( stateTo );
[9406]378
[7954]379  int i = 0;
380  int n = 0;
381  std::list<int> changes;
[9406]382
383  // extract the new state for every client
[7954]384  for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ )
385  {
[9406]386    // DATA PERMISSIONS
387    // check if this synchronizeable has the permissions to write the data
388
[9627]389    bool hasPermission = false;
390    bool b1, b2, b3, b4, b5, b6, b7, b8, b9;
391    b1 = b2 = b3 = b4 = b5 = b6 = b7 = b8 = b9 = false;
[9592]392
[9625]393    ////////////////////////////////
394    // Data RECIEVING Permissions
395    ////////////////////////////////
396
397    // i should never ever receive a state update from a synchronizeable, that belongs to me! If it does somethings wrong with the send rules
398//     assert(   !((*it)->checkPermission( PERMISSION_OWNER ) && this->owner == SharedNetworkData::getInstance()->getHostID()));
399
400
401    // Permission   OWNER accept if:
402    // sender == owner
[9627]403    if(      (*it)->checkPermission( PERMISSION_OWNER ) && this->owner == userId) {
404      hasPermission = true; b1 = true; }
[9625]405    // sender == MASTER_SERVER
[9627]406    else if( (*it)->checkPermission( PERMISSION_OWNER ) && SharedNetworkData::getInstance()->isUserMasterServer( userId)
407               && this->owner != SharedNetworkData::getInstance()->getHostID()) {
408      hasPermission = true; b2 = true; }
[9625]409    // sender == PROXY_SERVER
[9628]410      else if( (*it)->checkPermission( PERMISSION_OWNER ) && SharedNetworkData::getInstance()->isUserProxyServerActive( userId) &&
411                 this->owner != SharedNetworkData::getInstance()->getHostID()) {
[9627]412        hasPermission = true; b3 = true; }
[9592]413
[9636]414
415
[9625]416    // Permission   MASTER_SERVER accept if:
417    // sender == MASTER_SERVER
[9627]418    else if( (*it)->checkPermission( PERMISSION_MASTER_SERVER) && SharedNetworkData::getInstance()->isUserMasterServer( userId)) {
419      hasPermission = true; b4 = true; }
[9625]420    // sender == PROXY_SERVER && im not MASTER_SERVER && im not PROXY_SERVER
421    else if( (*it)->checkPermission( PERMISSION_MASTER_SERVER) && SharedNetworkData::getInstance()->isClient() &&
[9627]422               SharedNetworkData::getInstance()->isUserProxyServerActive( userId)) {
423      hasPermission = true; b5 = true; }
[9592]424
[9625]425    // Permission   SERVER accept if:
426    // sender == SERVER
[9635]427    else if( (*it)->checkPermission( PERMISSION_SERVER ) && !SharedNetworkData::getInstance()->isUserClient( userId) /*&&
428               SharedNetworkData::getInstance()->isClient()*/) {
[9627]429      hasPermission = true; b6 = true; }
[9592]430
[9629]431
[9636]432
[9625]433    // Pemission    ALL accept if:
[9627]434    else if(  (*it)->checkPermission( PERMISSION_ALL )) {
[9629]435      hasPermission = true; b8 = true; }
[9592]436
[9636]437
[9625]438   // no rights to over-write local data
[9406]439    else
440      hasPermission = false;
441
442
443
444    // if it has the permission to write do it
445    if( hasPermission)
[7954]446    {
447      n = (*it)->readFromBuf( stateTo->data + i, stateTo->dataLength - i );
448      i += n;
[9406]449      //NETPRINTF(0)("%s::setvar %s %d\n", getClassCName(), (*it)->getName().c_str(), n);
[9626]450//       PRINTF(0)("recieving: %s %d\n",  (*it)->getName().c_str(), n);
[7954]451      //(*it)->debug();
[9627]452
453//       if( this->isA(CL_PLAYABLE))
454//       {
455//         PRINTF(0)("ms: %i, ps: %i, c: %i, sender: %i, reciever: %i, owner: %i, perm: (ow %i, ms %i, s %i, a %i)\n",
456//         SharedNetworkData::getInstance()->isMasterServer(), SharedNetworkData::getInstance()->isProxyServerActive(), SharedNetworkData::getInstance()->isClient(),
457//         userId, SharedNetworkData::getInstance()->getHostID(), this->owner,
458//         (*it)->checkPermission( PERMISSION_OWNER ), (*it)->checkPermission( PERMISSION_MASTER_SERVER ),
459//         (*it)->checkPermission( PERMISSION_SERVER ), (*it)->checkPermission( PERMISSION_ALL ));
[9629]460//         PRINTF(0)("hasPermission: %i, eval: %i, %i, %i, %i, %i, %i, %i, %i\n", hasPermission, b1, b2, b3, b4, b5, b6, b7, b8);
[9627]461//         PRINTF(0)("rec %s %s %d\n", this->getClassCName(), (*it)->getName().c_str(), n);
462//       }
463
464
[7954]465      if ( (*it)->getHasChanged() )
466      {
467        changes.push_back( (*it)->getVarId() );
468      }
469    }
470    else
471    {
[9494]472//       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]473      n = (*it)->getSizeFromBuf( stateTo->data + i, stateTo->dataLength - i );
[9406]474      //NETPRINTF(0)("%s::setvar %s %d\n", getClassCName(), (*it)->getName().c_str(), n);
[7954]475      //(*it)->debug();
476      i += n;
477    }
478  }
479
480  this->varChangeHandler( changes );
[9406]481
[7954]482  return i;
[5997]483}
484
[7954]485 /**
486 * override this function to be notified on change
487 * of your registred variables.
488 * @param id id's which have changed
489 */
490void Synchronizeable::varChangeHandler( std::list<int> & id )
491{
492}
[6695]493
[5997]494/**
[7954]495 * registers a varable to be synchronized over network
496 * @param var see src/lib/network/synchronizeable_var/ for available classes
[5997]497 */
[7954]498void Synchronizeable::registerVar( SynchronizeableVar * var )
[5997]499{
[7954]500  syncVarList.push_back( var );
[5997]501}
502
503/**
[7954]504 * registers a varable to be synchronized over network
505 * return value is passed to varChangeHandler on change
506 * @param var see src/lib/network/synchronizeable_var/ for available classes
507 * @return handle passed to varChangeHandler on changes
[5997]508 */
[7954]509int Synchronizeable::registerVarId( SynchronizeableVar * var )
[5997]510{
[7954]511  syncVarList.push_back( var );
512  var->setWatched( true );
513  var->setVarId( syncVarList.size()-1 );
514  return syncVarList.size()-1;
[5997]515}
516
517/**
[7954]518 * removed user's states from memory
519 * @param userId user to clean
[5997]520 */
[7954]521void Synchronizeable::cleanUpUser( int userId )
[5997]522{
[9625]523  if ( (int)recvStates.size() > userId )
[7954]524  {
[8228]525    for ( std::list<StateHistoryEntry*>::iterator it = recvStates[userId].begin(); it != recvStates[userId].end(); it++ )
[7954]526    {
[8228]527      if ( (*it)->data )
[8623]528      {
[8228]529        delete [] (*it)->data;
[8623]530        (*it)->data = NULL;
531      }
[9406]532
[8228]533      delete *it;
[7954]534    }
[8228]535    recvStates[userId].clear();
[7954]536  }
[9406]537
[9625]538  if ( (int)sentStates.size() > userId )
[7954]539  {
[9406]540
[8228]541    for ( std::list<StateHistoryEntry*>::iterator it = sentStates[userId].begin(); it != sentStates[userId].end(); it++ )
[7954]542    {
[8228]543      if ( (*it)->data )
[8623]544      {
[8228]545        delete [] (*it)->data;
[8623]546        (*it)->data = NULL;
547      }
[9406]548
[8228]549      delete *it;
[7954]550    }
[8228]551    sentStates[userId].clear();
[7954]552  }
[5997]553}
[6139]554
[6341]555/**
[7954]556 * this function is called after recieving a state.
[9406]557 * @param userId
558 * @param stateId
559 * @param fromStateId
[6341]560 */
[7954]561void Synchronizeable::handleRecvState( int userId, int stateId, int fromStateId )
[6341]562{
[7954]563   //make sure this user has his history
[9625]564  if ( (int)recvStates.size() <= userId )
[7954]565    recvStates.resize( userId+1 );
[9406]566
[7954]567  //remove old states
568  StateHistory::iterator it = recvStates[userId].begin();
569
570#if 0
571  while ( it != recvStates[userId].end() && (*it)->stateId < fromStateId )
572    it++;
573
574  if ( it != recvStates[userId].begin() )
575  {
576    for ( StateHistory::iterator it2 = recvStates[userId].begin(); it2 != it; it2++ )
577    {
578      if ( (*it2)->data != NULL )
579      {
580        delete [] (*it2)->data;
581        (*it2)->data = NULL;
582      }
583    }
584    recvStates[userId].erase( recvStates[userId].begin(), it );
585  }
586#endif
587
588  for ( it = recvStates[userId].begin(); it != recvStates[userId].end();  )
589  {
590    if ( (*it)->stateId < fromStateId )
591    {
592      StateHistory::iterator delIt = it;
593      it ++;
[9406]594
[7954]595      if ( (*delIt)->data )
[8623]596      {
[7954]597        delete [] (*delIt)->data;
[8623]598        (*delIt)->data = NULL;
599      }
600      delete *delIt;
[7954]601      recvStates[userId].erase( delIt );
[9406]602
[7954]603      continue;
604    }
605    it++;
606  }
[9406]607
[7954]608  StateHistory::iterator fromState = recvStates[userId].end();
609  StateHistory::iterator toState = recvStates[userId].end();
[9406]610
[7954]611  for ( it = recvStates[userId].begin(); it != recvStates[userId].end(); it++ )
612  {
613    if ( (*it)->stateId == stateId )
614      toState = it;
615    if ( (*it)->stateId == fromStateId )
616      fromState = it;
[9406]617
[7954]618    if ( fromState != recvStates[userId].end() && toState != recvStates[userId].end() )
619      break;
620  }
[9406]621
[7954]622  // setStateDiff was not called and i know fromStateId
623  if ( fromState != recvStates[userId].end() && toState == recvStates[userId].end() )
624  {
625    StateHistoryEntry * entry = new StateHistoryEntry;
[9406]626
[7954]627    entry->dataLength = (*fromState)->dataLength;
628    if ( entry->dataLength > 0 )
629    {
630      entry->data = new byte[entry->dataLength];
[9406]631
[7954]632      assert( (*fromState)->data );
633      memcpy( entry->data, (*fromState)->data, entry->dataLength );
634    }
635    else
636      entry->data = NULL;
[9406]637
[7954]638    entry->sizeList = (*fromState)->sizeList;
639    entry->stateId = stateId;
[9406]640
[7954]641    recvStates[userId].push_back(entry);
642  }
[6341]643}
[6139]644
[6341]645/**
[7954]646 * this function is called after sending a state
[9406]647 * @param userId
648 * @param stateId
649 * @param fromStateId
[6341]650 */
[7954]651void Synchronizeable::handleSentState( int userId, int stateId, int fromStateId )
[6341]652{
[7954]653   //make sure this user has his history
[9625]654  if ( (int)sentStates.size() <= userId )
[7954]655    sentStates.resize( userId+1 );
656
657   //remove old states
658  StateHistory::iterator it = sentStates[userId].begin();
659
660  for ( it = sentStates[userId].begin(); it != sentStates[userId].end();  )
661  {
662    if ( (*it)->stateId < fromStateId )
663    {
664      StateHistory::iterator delIt = it;
665      it ++;
[9406]666
[7954]667      if ( (*delIt)->data )
[8623]668      {
[7954]669        delete [] (*delIt)->data;
[8623]670        (*delIt)->data = NULL;
671      }
672      delete *delIt;
[7954]673      sentStates[userId].erase( delIt );
[9406]674
[7954]675      continue;
676    }
677    it++;
678  }
679
[9406]680
[7954]681  StateHistory::iterator fromState = sentStates[userId].end();
682  StateHistory::iterator toState = sentStates[userId].end();
[9406]683
[7954]684  for ( it = sentStates[userId].begin(); it != sentStates[userId].end(); it++ )
685  {
686    if ( (*it)->stateId == stateId )
687      toState = it;
688    if ( (*it)->stateId == fromStateId )
689      fromState = it;
[9406]690
[7954]691    if ( fromState != sentStates[userId].end() && toState != sentStates[userId].end() )
692      break;
693  }
694
[9406]695
[7954]696  // getStateDiff was not called and i know fromStateId
697  if ( fromState != sentStates[userId].end() && toState == sentStates[userId].end() )
698  {
699    StateHistoryEntry * entry = new StateHistoryEntry;
[9406]700
[7954]701    entry->dataLength = (*fromState)->dataLength;
702    if ( entry->dataLength > 0 )
703    {
704      entry->data = new byte[entry->dataLength];
[9406]705
[7954]706      assert( (*fromState)->data );
707      memcpy( entry->data, (*fromState)->data, entry->dataLength );
708    }
709    else
710      entry->data = NULL;
[9406]711
[7954]712    entry->sizeList = (*fromState)->sizeList;
713    entry->stateId = stateId;
[9406]714
[7954]715    sentStates[userId].push_back(entry);
716  }
[9406]717
[6341]718}
[6139]719
[6341]720
721
Note: See TracBrowser for help on using the repository browser.