Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation3/src/libraries/network/packet/Gamestate.cc @ 7155

Last change on this file since 7155 was 7153, checked in by scheusso, 14 years ago
  • merged network2 branch into presentation3 branch (lots of network traffic and cpu load improvements)
  • fixed network related bugs in BigExplosion
  • Property svn:eol-style set to native
File size: 22.1 KB
RevLine 
[1711]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
[3084]23 *      Oliver Scheuss
[1711]24 *   Co-authors:
25 *      ...
26 *
27 */
28
[1701]29#include "Gamestate.h"
[3214]30
[2773]31#include <zlib.h>
[3214]32
33#include "util/Debug.h"
[2896]34#include "core/GameMode.h"
[3214]35#include "core/ObjectList.h"
36#include "network/synchronisable/Synchronisable.h"
37#include "network/GamestateHandler.h"
[1701]38
[2171]39namespace orxonox {
[1701]40
41namespace packet {
42
[2662]43#define GAMESTATE_START(data) (data + GamestateHeader::getSize())
[1740]44
[3214]45#define PACKET_FLAG_GAMESTATE  PacketFlag::Reliable
[2087]46
[7153]47inline bool memzero( uint8_t* data, uint32_t datalength)
48{
49  uint64_t* d = (uint64_t*)data;
[2662]50
[7153]51  for( unsigned int i=0; i<datalength/8; i++ )
52  {
53    if( *(d+i) != 0 )
54      return false;
55  }
56  // now process the rest (when datalength isn't a multiple of 4)
57  for( unsigned int j = 8*(datalength/8); j<datalength; j++ )
58  {
59    if( *(data+j) != 0 )
60      return false;
61  }
62  return true;
63}
64
65
66Gamestate::Gamestate():
67  header_(0)
[1701]68{
[1907]69  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
[1701]70}
71
[7153]72
[1907]73Gamestate::Gamestate(uint8_t *data, unsigned int clientID):
[7153]74  Packet(data, clientID)
[1701]75{
[1907]76  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
[2662]77  header_ = new GamestateHeader(data_);
[1701]78}
79
[7153]80
[1907]81Gamestate::Gamestate(uint8_t *data)
82{
83  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
[7153]84  data_ = data;
[2662]85  header_ = new GamestateHeader(data_);
[1907]86}
[1701]87
[7153]88
[2662]89Gamestate::Gamestate(const Gamestate& g) :
[7153]90    Packet( *(Packet*)&g ), nrOfVariables_(0)
[2662]91{
92  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
93  header_ = new GamestateHeader(data_);
[7153]94  sizes_ = g.sizes_;
[2662]95}
[1907]96
[2662]97
[1701]98Gamestate::~Gamestate()
99{
[3198]100  if( header_ )
101    delete header_;
[1701]102}
103
[7153]104
[2171]105bool Gamestate::collectData(int id, uint8_t mode)
[1701]106{
[2662]107  assert(this->header_==0); // make sure the header didn't exist before
108  uint32_t tempsize=0, currentsize=0;
[1751]109  assert(data_==0);
[2662]110  uint32_t size = calcGamestateSize(id, mode);
[1740]111
[1701]112  COUT(4) << "G.ST.Man: producing gamestate with id: " << id << std::endl;
113  if(size==0)
114    return false;
[2662]115  data_ = new uint8_t[size + GamestateHeader::getSize()];
[7153]116  if(!data_)
117  {
[1701]118    COUT(2) << "GameStateManager: could not allocate memory" << std::endl;
119    return false;
120  }
[6417]121
[2662]122  // create the header object
[3198]123  assert( header_ == 0 );
[2662]124  header_ = new GamestateHeader(data_);
[2087]125
[1701]126  //start collect data synchronisable by synchronisable
[7153]127  uint8_t *mem = data_; // in this stream store all data of the variables and the headers of the synchronisable
[2662]128  mem += GamestateHeader::getSize();
[2171]129  ObjectList<Synchronisable>::iterator it;
[7153]130  for(it = ObjectList<Synchronisable>::begin(); it; ++it)
131  {
[6417]132
[3084]133//     tempsize=it->getSize(id, mode);
134
[7153]135    tempsize = it->getData(mem, this->sizes_, id, mode);
[3084]136    if ( tempsize != 0 )
137      dataVector_.push_back( obj(it->getObjectID(), it->getCreatorID(), tempsize, mem-data_) );
[6417]138
[2662]139#ifndef NDEBUG
[7153]140    if(currentsize+tempsize > size)
141    {
[2171]142      assert(0); // if we don't use multithreading this part shouldn't be neccessary
[1701]143      // start allocate additional memory
144      COUT(3) << "G.St.Man: need additional memory" << std::endl;
[2171]145      ObjectList<Synchronisable>::iterator temp = it;
[2662]146      uint32_t addsize=tempsize;
[1701]147      while(++temp)
[1907]148        addsize+=temp->getSize(id, mode);
[2662]149      data_ = (uint8_t *)realloc(data_, GamestateHeader::getSize() + currentsize + addsize);
[1701]150      if(!data_)
151        return false;
152      size = currentsize+addsize;
153    }// stop allocate additional memory
[2662]154#endif
[3084]155//     if(!it->getData(mem, id, mode))
156//       return false; // mem pointer gets automatically increased because of call by reference
[1701]157    // increase size counter by size of current synchronisable
158    currentsize+=tempsize;
159  }
[1740]160
161
[1701]162  //start write gamestate header
[2662]163  header_->setDataSize( currentsize );
164  header_->setID( id );
[3198]165  header_->setBaseID( GAMESTATEID_INITIAL );
[2662]166  header_->setDiffed( false );
167  header_->setComplete( true );
168  header_->setCompressed( false );
[1701]169  //stop write gamestate header
[1740]170
[1701]171  COUT(5) << "G.ST.Man: Gamestate size: " << currentsize << std::endl;
172  COUT(5) << "G.ST.Man: 'estimated' (and corrected) Gamestate size: " << size << std::endl;
173  return true;
174}
175
[7153]176
[2171]177bool Gamestate::spreadData(uint8_t mode)
[1701]178{
[2662]179  COUT(4) << "processing gamestate with id " << header_->getID() << endl;
[1751]180  assert(data_);
[2662]181  assert(!header_->isCompressed());
182  uint8_t *mem=data_+GamestateHeader::getSize();
[1907]183  Synchronisable *s;
[1740]184
[1907]185  // update the data of the objects we received
[7153]186  while(mem < data_+GamestateHeader::getSize()+header_->getDataSize())
187  {
[2662]188    SynchronisableHeader objectheader(mem);
[1701]189
[2662]190    s = Synchronisable::getSynchronisable( objectheader.getObjectID() );
[1907]191    if(!s)
[1701]192    {
[2896]193      if (!GameMode::isMaster())
[2662]194      {
195        Synchronisable::fabricate(mem, mode);
196      }
197      else
198      {
[7153]199        mem += objectheader.getDataSize() + ( objectheader.isDiffed() ? SynchronisableHeaderLight::getSize() : SynchronisableHeader::getSize() );
[2662]200      }
[1701]201    }
[1907]202    else
203    {
204      bool b = s->updateData(mem, mode);
205      assert(b);
206    }
[1701]207  }
[2662]208   // In debug mode, check first, whether there are no duplicate objectIDs
209#ifndef NDEBUG
[7153]210  if(this->getID()%1000==1)
211  {
[3084]212    std::list<uint32_t> v1;
213    ObjectList<Synchronisable>::iterator it;
[7153]214    for (it = ObjectList<Synchronisable>::begin(); it != ObjectList<Synchronisable>::end(); ++it)
215    {
216      if (it->getObjectID() == OBJECTID_UNKNOWN)
217      {
218        if (it->objectMode_ != 0x0)
219        {
[3084]220          COUT(0) << "Found object with OBJECTID_UNKNOWN on the client with objectMode != 0x0!" << std::endl;
221          COUT(0) << "Possible reason for this error: Client created a synchronized object without the Server's approval." << std::endl;
222          COUT(0) << "Objects class: " << it->getIdentifier()->getName() << std::endl;
223          assert(false);
224        }
[2662]225      }
[7153]226      else
227      {
[3084]228        std::list<uint32_t>::iterator it2;
[7153]229        for (it2 = v1.begin(); it2 != v1.end(); ++it2)
230        {
231          if (it->getObjectID() == *it2)
232          {
[3084]233            COUT(0) << "Found duplicate objectIDs on the client!" << std::endl
234                    << "Are you sure you don't create a Sychnronisable objcect with 'new' \
235                        that doesn't have objectMode = 0x0?" << std::endl;
236            assert(false);
237          }
[2662]238        }
[3084]239        v1.push_back(it->getObjectID());
[2662]240      }
241    }
242  }
243#endif
[1701]244  return true;
245}
246
[7153]247
[2662]248uint32_t Gamestate::getSize() const
[1701]249{
[1711]250  assert(data_);
[2662]251  if(header_->isCompressed())
252    return header_->getCompSize()+GamestateHeader::getSize();
[1701]253  else
254  {
[2662]255    return header_->getDataSize()+GamestateHeader::getSize();
[1701]256  }
257}
258
[7153]259
260bool Gamestate::operator==(packet::Gamestate gs)
261{
[2662]262  uint8_t *d1 = data_+GamestateHeader::getSize();
263  uint8_t *d2 = gs.data_+GamestateHeader::getSize();
[3084]264  GamestateHeader* h1 = new GamestateHeader(data_);
265  GamestateHeader* h2 = new GamestateHeader(gs.data_);
266  assert(h1->getDataSize() == h2->getDataSize());
[1751]267  assert(!isCompressed());
268  assert(!gs.isCompressed());
[3084]269  return memcmp(d1, d2, h1->getDataSize())==0;
[1751]270}
271
[7153]272
[1701]273bool Gamestate::process()
274{
[1705]275  return GamestateHandler::addGamestate(this, getClientID());
[1701]276}
277
[1907]278
[1701]279bool Gamestate::compressData()
280{
[2662]281  assert(data_);
282  assert(!header_->isCompressed());
283  uLongf buffer = (uLongf)(((header_->getDataSize() + 12)*1.01)+1);
[1701]284  if(buffer==0)
285    return false;
[1740]286
[2662]287  uint8_t *ndata = new uint8_t[buffer+GamestateHeader::getSize()];
288  uint8_t *dest = ndata + GamestateHeader::getSize();
289  uint8_t *source = data_ + GamestateHeader::getSize();
[1701]290  int retval;
[2662]291  retval = compress( dest, &buffer, source, (uLong)(header_->getDataSize()) );
[7153]292  switch ( retval )
293  {
[1701]294    case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break;
[1751]295    case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl; return false;
296    case Z_BUF_ERROR: COUT(2) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << std::endl; return false;
297    case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl; return false;
[1701]298  }
299
300  //copy and modify header
[2662]301  GamestateHeader *temp = header_;
302  header_ = new GamestateHeader(ndata, temp);
303  delete temp;
[1701]304  //delete old data
305  delete[] data_;
306  //save new data
307  data_ = ndata;
[2662]308  header_->setCompSize( buffer );
309  header_->setCompressed( true );
[7153]310  COUT(0) << "gamestate compress datasize: " << header_->getDataSize() << " compsize: " << header_->getCompSize() << std::endl;
[1701]311  return true;
312}
[7153]313
314
[1701]315bool Gamestate::decompressData()
316{
[2662]317  assert(data_);
318  assert(header_->isCompressed());
319  COUT(4) << "GameStateClient: uncompressing gamestate. id: " << header_->getID() << ", baseid: " << header_->getBaseID() << ", datasize: " << header_->getDataSize() << ", compsize: " << header_->getCompSize() << std::endl;
320  uint32_t datasize = header_->getDataSize();
321  uint32_t compsize = header_->getCompSize();
322  uint32_t bufsize;
[1907]323  bufsize = datasize;
[1751]324  assert(bufsize!=0);
[2662]325  uint8_t *ndata = new uint8_t[bufsize + GamestateHeader::getSize()];
326  uint8_t *dest = ndata + GamestateHeader::getSize();
327  uint8_t *source = data_ + GamestateHeader::getSize();
[1701]328  int retval;
[1751]329  uLongf length=bufsize;
330  retval = uncompress( dest, &length, source, (uLong)compsize );
[7153]331  switch ( retval )
332  {
[1701]333    case Z_OK: COUT(5) << "successfully decompressed" << std::endl; break;
334    case Z_MEM_ERROR: COUT(1) << "not enough memory available" << std::endl; return false;
335    case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer" << std::endl; return false;
336    case Z_DATA_ERROR: COUT(2) << "data corrupted (zlib)" << std::endl; return false;
337  }
[1752]338
[1701]339  //copy over the header
[2662]340  GamestateHeader *temp = header_;
341  header_ = new GamestateHeader( data_, header_ );
342  delete temp;
[2087]343
[7153]344  if (this->bDataENetAllocated_)
345  {
[2087]346    // Memory was allocated by ENet. --> We let it be since enet_packet_destroy will
347    // deallocated it anyway. So data and packet stay together.
348    this->bDataENetAllocated_ = false;
349  }
[7153]350  else
351  {
[2087]352    // We allocated the memory in the first place (unlikely). So we destroy the old data
353    // and overwrite it with the new decompressed data.
354    delete[] this->data_;
355  }
356
[1751]357  //set new pointers
[1701]358  data_ = ndata;
[2662]359  header_->setCompressed( false );
360  assert(header_->getDataSize()==datasize);
361  assert(header_->getCompSize()==compsize);
[1701]362  return true;
363}
364
[7153]365
366Gamestate* Gamestate::diffVariables(Gamestate *base)
[1701]367{
[7153]368  assert(this && base); assert(data_ && base->data_);
369  assert(!header_->isCompressed() && !base->header_->isCompressed());
[2662]370  assert(!header_->isDiffed());
[7153]371
372
373  // *** first do a raw diff of the two gamestates
374
375  uint8_t *baseData = GAMESTATE_START(base->data_);
376  uint8_t *origData = GAMESTATE_START(this->data_);
377  uint32_t origLength = header_->getDataSize();
378  uint32_t baseLength = base->header_->getDataSize();
379
380  assert( origLength && baseLength );
381
382  uint8_t *nData = new uint8_t[origLength + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_]; // this is the maximum size needed in the worst case
383  uint8_t *dest = GAMESTATE_START(nData);
384
385  uint32_t baseOffset = 0; //offset in the diffed stream
386  uint32_t origOffset = 0; //offset in the new stream with removed 0's
387  std::vector<uint32_t>::iterator sizes = this->sizes_.begin();
388
389  while( origOffset < origLength )
390  {
391    //iterate through all objects
392
393    SynchronisableHeader h(origData+origOffset);
394
395    // Find (if possible) the current object in the datastream of the old gamestate
396    // Start at the current offset position
397    if(baseOffset >= baseLength)
398      baseOffset = 0;
399    uint8_t* temp = baseData + baseOffset;
400    uint32_t objectID = h.getObjectID();
401    assert(temp < baseData+baseLength);
402    assert(dest < nData + origLength + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_);
403    assert(sizes != this->sizes_.end());
404    while ( temp < baseData+baseLength )
405    {
406      SynchronisableHeader htemp(temp);
407      if ( htemp.getObjectID() == objectID )
408      {
409        assert( h.getClassID() == htemp.getClassID() );
410        goto DODIFF;
[1701]411      }
[7153]412      temp += htemp.getDataSize()+SynchronisableHeader::getSize();
[1701]413    }
[7153]414    // If not found start looking at the beginning
415    temp = baseData;
416    while ( temp < baseData+baseOffset )
417    {
418      SynchronisableHeader htemp(temp);
419      if ( htemp.getObjectID() == objectID )
420      {
421        assert( h.getClassID() == htemp.getClassID() );
422        goto DODIFF;
423      }
424      temp += htemp.getDataSize()+SynchronisableHeader::getSize();
425    }
426    // Object is new, thus never transmitted -> just copy over
427    goto DOCOPY;
428
429
430DODIFF:
431    {
432//       if(baseOffset==0)
433//       {
434//         assert(origOffset==0);
435//       }
436      uint32_t objectOffset = SynchronisableHeader::getSize(); // offset inside the object in the origData and baseData
437      // Check whether the whole object stayed the same
438      if( memcmp( origData+origOffset+objectOffset, temp+objectOffset, h.getDataSize()) == 0 )
439      {
440        origOffset += objectOffset+ h.getDataSize(); // skip the whole object
441        baseOffset = temp + h.getDataSize()+SynchronisableHeader::getSize() - baseData;
442        sizes += Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables();
443      }
444      else
445      {
446//         COUT(4) << "diff " << h.getObjectID() << ":";
447        // Now start to diff the Object
448        SynchronisableHeaderLight h2(dest);
449        h2 = h; // copy over the objectheader
450        VariableID variableID = 0;
451        uint32_t newObjectOffset = SynchronisableHeaderLight::getSize();
452        // iterate through all variables
453        while( objectOffset < h.getDataSize()+SynchronisableHeader::getSize() )
454        {
455          // check whether variable changed and write id and copy over variable to the new stream
456          // otherwise skip variable
457          assert(sizes != this->sizes_.end());
458          uint32_t varSize = *sizes;
459          assert( varSize == Synchronisable::getSynchronisable(h.getObjectID())->getVarSize(variableID) );
460          if ( varSize != 0 )
461          {
462            if ( memcmp(origData+origOffset+objectOffset, temp+objectOffset, varSize) != 0 )
463            {
464//               COUT(4) << " c" << varSize;
465              *(VariableID*)(dest+newObjectOffset) = variableID; // copy over the variableID
466              newObjectOffset += sizeof(VariableID);
467              memcpy( dest+newObjectOffset, origData+origOffset+objectOffset, varSize );
468              newObjectOffset += varSize;
469              objectOffset += varSize;
470            }
471            else
472            {
473//               COUT(4) << " s" << varSize;
474              objectOffset += varSize;
475            }
476          }
477
478          ++variableID;
479          ++sizes;
480        }
481        if( Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables() != variableID )
482          sizes += Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables() - variableID;
483//         COUT(4) << endl;
484        h2.setDiffed(true);
485        h2.setDataSize(newObjectOffset-SynchronisableHeaderLight::getSize());
486        assert(objectOffset == h.getDataSize()+SynchronisableHeader::getSize());
487        origOffset += objectOffset;
488        baseOffset += temp + h.getDataSize()+SynchronisableHeader::getSize() - baseData;
489        dest += newObjectOffset;
490      }
491
492      continue;
493    }
494
495DOCOPY:
496    {
497      // Just copy over the whole Object
498      memcpy( dest, origData+origOffset, h.getDataSize()+SynchronisableHeader::getSize() );
499      dest += h.getDataSize()+SynchronisableHeader::getSize();
500      origOffset += h.getDataSize()+SynchronisableHeader::getSize();
501      assert( Synchronisable::getSynchronisable(h.getObjectID()) );
502//       COUT(4) << "copy " << h.getObjectID() << endl;
503//       COUT(4) << "copy " << h.getObjectID() << ":";
504      //sizes += Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables();
505      for( unsigned int i = 0; i < Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables(); ++i )
506      {
507//         COUT(4) << " " << *sizes;
508        ++sizes;
509      }
510//       COUT(4) << endl;
511      assert(sizes != this->sizes_.end() || origOffset>=origLength);
512      continue;
513    }
[1701]514  }
515
[7153]516
517  Gamestate *g = new Gamestate(nData, getClientID());
518  assert(g->header_);
[2662]519  *(g->header_) = *header_;
520  g->header_->setBaseID( base->getID() );
[7153]521  g->header_->setDataSize(dest - nData - GamestateHeader::getSize());
[1751]522  g->flags_=flags_;
523  g->packetDirection_ = packetDirection_;
[7153]524  assert(!g->isCompressed());
[1701]525  return g;
[7153]526}
[3198]527
[7153]528
529Gamestate* Gamestate::diffData(Gamestate *base)
[3198]530{
531  assert(this && base); assert(data_ && base->data_);
532  assert(!header_->isCompressed() && !base->header_->isCompressed());
533  assert(!header_->isDiffed());
[6417]534
[3198]535  uint8_t *basep = GAMESTATE_START(base->data_);
536  uint8_t *gs = GAMESTATE_START(this->data_);
537  uint32_t dest_length = header_->getDataSize();
[6417]538
[3198]539  if(dest_length==0)
540    return NULL;
[6417]541
[3198]542  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];
543  uint8_t *dest = GAMESTATE_START(ndata);
[6417]544
[3198]545  rawDiff( dest, gs, basep, header_->getDataSize(), base->header_->getDataSize() );
546#ifndef NDEBUG
547  uint8_t *dest2 = new uint8_t[dest_length];
548  rawDiff( dest2, dest, basep, header_->getDataSize(), base->header_->getDataSize() );
549  assert( memcmp( dest2, gs, dest_length) == 0 );
[5929]550  delete dest2;
[3198]551#endif
552
553  Gamestate *g = new Gamestate(ndata, getClientID());
554  assert(g->header_);
555  *(g->header_) = *header_;
556  g->header_->setDiffed( true );
557  g->header_->setBaseID( base->getID() );
558  g->flags_=flags_;
559  g->packetDirection_ = packetDirection_;
560  assert(g->isDiffed());
561  assert(!g->isCompressed());
562  return g;
[1701]563}
564
[7153]565
566Gamestate* Gamestate::undiff(Gamestate *base)
[3198]567{
568  assert(this && base); assert(data_ && base->data_);
569  assert(!header_->isCompressed() && !base->header_->isCompressed());
570  assert(header_->isDiffed());
[6417]571
[3198]572  uint8_t *basep = GAMESTATE_START(base->data_);
573  uint8_t *gs = GAMESTATE_START(this->data_);
574  uint32_t dest_length = header_->getDataSize();
[6417]575
[3198]576  if(dest_length==0)
577    return NULL;
[6417]578
[3198]579  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];
580  uint8_t *dest = ndata + GamestateHeader::getSize();
[6417]581
[3198]582  rawDiff( dest, gs, basep, header_->getDataSize(), base->header_->getDataSize() );
[6417]583
[3198]584  Gamestate *g = new Gamestate(ndata, getClientID());
585  assert(g->header_);
586  *(g->header_) = *header_;
587  g->header_->setDiffed( false );
588  g->flags_=flags_;
589  g->packetDirection_ = packetDirection_;
590  assert(!g->isDiffed());
591  assert(!g->isCompressed());
592  return g;
593}
594
595
596void Gamestate::rawDiff( uint8_t* newdata, uint8_t* data, uint8_t* basedata, uint32_t datalength, uint32_t baselength)
597{
598  uint64_t* gd = (uint64_t*)data;
599  uint64_t* bd = (uint64_t*)basedata;
600  uint64_t* nd = (uint64_t*)newdata;
[6417]601
[3198]602  unsigned int i;
603  for( i=0; i<datalength/8; i++ )
604  {
605    if( i<baselength/8 )
606      *(nd+i) = *(gd+i) ^ *(bd+i);  // xor the data
607    else
608      *(nd+i) = *(gd+i); // just copy over the data
609  }
610  unsigned int j;
611  // now process the rest (when datalength isn't a multiple of 4)
612  for( j = 8*(datalength/8); j<datalength; j++ )
613  {
614    if( j<baselength )
615      *(newdata+j) = *(data+j) ^ *(basedata+j); // xor
616    else
617      *(newdata+j) = *(data+j); // just copy
618  }
619  assert(j==datalength);
620}
621
[7153]622
[2662]623Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){
[1907]624  assert(data_);
[2662]625  std::list<obj>::iterator it;
[2087]626
[1907]627  // allocate memory for new data
[2662]628  uint8_t *gdata = new uint8_t[header_->getDataSize()+GamestateHeader::getSize()];
[1907]629  // create a gamestate out of it
630  Gamestate *gs = new Gamestate(gdata);
[2662]631  uint8_t *newdata = gdata + GamestateHeader::getSize();
[1907]632  uint8_t *origdata = GAMESTATE_START(data_);
[2087]633
[1907]634  //copy the GamestateHeader
[2662]635  assert(gs->header_);
636  *(gs->header_) = *header_;
[2087]637
[2662]638  uint32_t objectOffset;
639  unsigned int objectsize, destsize=0;
640  // TODO: Why is this variable not used?
641  //Synchronisable *object;
[2087]642
[2662]643  //call TrafficControl
[3084]644  TrafficControl::getInstance()->processObjectList( clientID, header_->getID(), dataVector_ );
[2662]645
[1907]646  //copy in the zeros
[3084]647//   std::list<obj>::iterator itt;
648//   COUT(0) << "myvector contains:";
649//   for ( itt=dataVector_.begin() ; itt!=dataVector_.end(); itt++ )
[7153]650//     COUT(0) << " " << (*itt).objID;
[3084]651//   COUT(0) << endl;
652  for(it=dataVector_.begin(); it!=dataVector_.end();){
[2662]653    SynchronisableHeader oldobjectheader(origdata);
654    SynchronisableHeader newobjectheader(newdata);
[7153]655    if ( (*it).objSize == 0 )
[2662]656    {
657      ++it;
658      continue;
659    }
[7153]660    objectsize = oldobjectheader.getDataSize()+SynchronisableHeader::getSize();
[2662]661    objectOffset=SynchronisableHeader::getSize(); //skip the size and the availableData variables in the objectheader
[7153]662    if ( (*it).objID == oldobjectheader.getObjectID() ){
[2662]663      memcpy(newdata, origdata, objectsize);
664      ++it;
[1907]665    }else{
[2662]666      newobjectheader = oldobjectheader;
[2171]667      memset(newdata+objectOffset, 0, objectsize-objectOffset);
[1907]668    }
669    newdata += objectsize;
670    origdata += objectsize;
[2662]671    destsize += objectsize;
[1907]672  }
[2662]673#ifndef NDEBUG
674  uint32_t origsize = destsize;
675  while ( origsize < header_->getDataSize() )
676  {
677    SynchronisableHeader oldobjectheader(origdata);
[7153]678    objectsize = oldobjectheader.getDataSize()+SynchronisableHeader::getSize();
[2662]679    origdata += objectsize;
680    origsize += objectsize;
[1907]681  }
[2662]682  assert(origsize==header_->getDataSize());
683  assert(destsize!=0);
684#endif
685  gs->header_->setDataSize( destsize );
[1907]686  return gs;
687}
688
[2087]689
[2662]690uint32_t Gamestate::calcGamestateSize(int32_t id, uint8_t mode)
[1701]691{
[7153]692  uint32_t size = 0;
693  uint32_t nrOfVariables = 0;
[1701]694    // get the start of the Synchronisable list
[2171]695  ObjectList<Synchronisable>::iterator it;
[1701]696    // get total size of gamestate
[7153]697  for(it = ObjectList<Synchronisable>::begin(); it; ++it){
[1907]698    size+=it->getSize(id, mode); // size of the actual data of the synchronisable
[7153]699    nrOfVariables += it->getNrOfVariables();
700  }
701//   COUT(0) << "allocating " << nrOfVariables << " ints" << endl;
702  this->sizes_.reserve(nrOfVariables);
[1701]703  return size;
704}
705
[7153]706
[2662]707} //namespace packet
708} //namespace orxonox
Note: See TracBrowser for help on using the repository browser.