Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/ODE/src/lib/network/synchronizeable.cc @ 9904

Last change on this file since 9904 was 9869, checked in by bensch, 18 years ago

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

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