Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain/src/lib/network/synchronizeable.cc @ 10508

Last change on this file since 10508 was 10114, checked in by patrick, 18 years ago

merged network back to trunk

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