Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/network/packet/Gamestate.cc @ 7294

Last change on this file since 7294 was 7163, checked in by dafrick, 14 years ago

Merged presentation3 branch into trunk.

  • Property svn:eol-style set to native
File size: 24.5 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
[7163]47inline bool memzero( uint8_t* data, uint32_t datalength)
48{
49  uint64_t* d = (uint64_t*)data;
[2662]50
[7163]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
[7163]72
[1907]73Gamestate::Gamestate(uint8_t *data, unsigned int clientID):
[7163]74  Packet(data, clientID)
[1701]75{
[1907]76  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
[2662]77  header_ = new GamestateHeader(data_);
[1701]78}
79
[7163]80
[1907]81Gamestate::Gamestate(uint8_t *data)
82{
83  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
[7163]84  data_ = data;
[2662]85  header_ = new GamestateHeader(data_);
[1907]86}
[1701]87
[7163]88
[2662]89Gamestate::Gamestate(const Gamestate& g) :
[7163]90    Packet( *(Packet*)&g ), nrOfVariables_(0)
[2662]91{
92  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
93  header_ = new GamestateHeader(data_);
[7163]94  sizes_ = g.sizes_;
[2662]95}
[1907]96
[2662]97
[1701]98Gamestate::~Gamestate()
99{
[3198]100  if( header_ )
101    delete header_;
[1701]102}
103
[7163]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()];
[7163]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
[7163]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;
[7163]130  for(it = ObjectList<Synchronisable>::begin(); it; ++it)
131  {
[6417]132
[3084]133//     tempsize=it->getSize(id, mode);
134
[7163]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
[7163]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
[7163]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
[7163]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      {
[7163]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
[7163]210  if(this->getID()%1000==1)
211  {
[3084]212    std::list<uint32_t> v1;
213    ObjectList<Synchronisable>::iterator it;
[7163]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      }
[7163]226      else
227      {
[3084]228        std::list<uint32_t>::iterator it2;
[7163]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
[7163]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
[7163]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
[7163]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()) );
[7163]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 );
[7163]310  COUT(0) << "gamestate compress datasize: " << header_->getDataSize() << " compsize: " << header_->getCompSize() << std::endl;
[1701]311  return true;
312}
[7163]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 );
[7163]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
[7163]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  }
[7163]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
[7163]365
366Gamestate* Gamestate::diffVariables(Gamestate *base)
[1701]367{
[7163]368  assert(this && base); assert(data_ && base->data_);
369  assert(!header_->isCompressed() && !base->header_->isCompressed());
[2662]370  assert(!header_->isDiffed());
[7163]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      assert( htemp.getDataSize()!=0 );
408      if ( htemp.getObjectID() == objectID )
409      {
410        assert( h.getClassID() == htemp.getClassID() );
411        goto DODIFF;
[1701]412      }
[7163]413//       {
414//         SynchronisableHeader htemp2(temp+htemp.getDataSize()+SynchronisableHeader::getSize());
415//         if( temp+htemp.getDataSize()+SynchronisableHeader::getSize() < baseData+baseLength )
416//         {
417//           assert(htemp2.getClassID()<500);
418//           assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000);
419//           assert(htemp2.isDiffed()==false);
420//         }
421//       }
422      temp += htemp.getDataSize()+SynchronisableHeader::getSize();
423       
[1701]424    }
[7163]425    // If not found start looking at the beginning
426    assert( temp==baseData+baseLength );
427    temp = baseData;
428//     {
429//       SynchronisableHeader htemp2(temp);
430//       if( temp < baseData+baseLength )
431//       {
432//         assert(htemp2.getClassID()<500);
433//         assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000);
434//         assert(htemp2.isDiffed()==false);
435//       }
436//     }
437    while ( temp < baseData+baseOffset )
438    {
439      SynchronisableHeader htemp(temp);
440      if ( htemp.getObjectID() == objectID )
441      {
442        assert( h.getClassID() == htemp.getClassID() );
443        goto DODIFF;
444      }
445//       {
446//         SynchronisableHeader htemp2(temp+htemp.getDataSize()+SynchronisableHeader::getSize());
447//         if( temp+htemp.getDataSize()+SynchronisableHeader::getSize() < baseData+baseLength )
448//         {
449//           assert(htemp2.getClassID()<500);
450//           assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000);
451//           assert(htemp2.isDiffed()==false);
452//         }
453//       }
454      temp += htemp.getDataSize()+SynchronisableHeader::getSize();
455    }
456    // Object is new, thus never transmitted -> just copy over
457    goto DOCOPY;
458
459
460DODIFF:
461    {
462//       COUT(4) << "dodiff" << endl;
463//       if(baseOffset==0)
464//       {
465//         assert(origOffset==0);
466//       }
467      uint32_t objectOffset = SynchronisableHeader::getSize(); // offset inside the object in the origData and baseData
468      // Check whether the whole object stayed the same
469      if( memcmp( origData+origOffset+objectOffset, temp+objectOffset, h.getDataSize()) == 0 )
470      {
471//         COUT(4) << "skip object" << Synchronisable::getSynchronisable(h.getObjectID())->getIdentifier()->getName() << endl;
472        origOffset += objectOffset+ h.getDataSize(); // skip the whole object
473        baseOffset = temp + h.getDataSize()+SynchronisableHeader::getSize() - baseData;
474        sizes += Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables();
475      }
476      else
477      {
478//         if( Synchronisable::getSynchronisable(h.getObjectID())->getIdentifier()->getName() == "Bot" )
479//           COUT(0) << "blub" << endl;
480//         COUT(4) << "object diff: " << Synchronisable::getSynchronisable(h.getObjectID())->getIdentifier()->getName() << endl;
481//         COUT(4) << "diff " << h.getObjectID() << ":";
482        // Now start to diff the Object
483        SynchronisableHeaderLight h2(dest);
484        h2 = h; // copy over the objectheader
485        VariableID variableID = 0;
486        uint32_t newObjectOffset = SynchronisableHeaderLight::getSize();
487        // iterate through all variables
488        while( objectOffset < h.getDataSize()+SynchronisableHeader::getSize() )
489        {
490          // check whether variable changed and write id and copy over variable to the new stream
491          // otherwise skip variable
492          assert(sizes != this->sizes_.end());
493          uint32_t varSize = *sizes;
494          assert( varSize == Synchronisable::getSynchronisable(h.getObjectID())->getVarSize(variableID) );
495          if ( varSize != 0 )
496          {
497            if ( memcmp(origData+origOffset+objectOffset, temp+objectOffset, varSize) != 0 )
498            {
499//               COUT(4) << "copy variable" << endl;
500              *(VariableID*)(dest+newObjectOffset) = variableID; // copy over the variableID
501              newObjectOffset += sizeof(VariableID);
502              memcpy( dest+newObjectOffset, origData+origOffset+objectOffset, varSize );
503              newObjectOffset += varSize;
504              objectOffset += varSize;
505            }
506            else
507            {
508//               COUT(4) << "skip variable" << endl;
509              objectOffset += varSize;
510            }
511          }
512//           else
513//             COUT(4) << "varsize 0" << endl;
514
515          ++variableID;
516          ++sizes;
517        }
518       
519        if( Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables() != variableID )
520          sizes += Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables() - variableID;
521//         COUT(4) << endl;
522        h2.setDiffed(true);
523        h2.setDataSize(newObjectOffset-SynchronisableHeaderLight::getSize());
524        assert(objectOffset == h.getDataSize()+SynchronisableHeader::getSize());
525        origOffset += objectOffset;
526//         baseOffset += temp + h.getDataSize()+SynchronisableHeader::getSize() - baseData;
527        //baseOffset += objectOffset;
528//         SynchronisableHeader htemp(temp);
529//         baseOffset += SynchronisableHeader::getSize() + htemp.getDataSize();
530//         {
531//           SynchronisableHeader htemp2( baseData+(temp-baseData+objectOffset) );
532//           if( baseData+(temp-baseData+objectOffset) < baseData+baseLength )
533//           {
534//             assert(htemp2.getClassID()<500);
535//             assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000);
536//             assert(htemp2.isDiffed()==false);
537//           }
538//         }
539        baseOffset = temp-baseData + objectOffset;
540        dest += newObjectOffset;
541      }
542
543      continue;
544    }
545
546DOCOPY:
547    {
548//       COUT(4) << "docopy" << endl;
549      // Just copy over the whole Object
550      memcpy( dest, origData+origOffset, h.getDataSize()+SynchronisableHeader::getSize() );
551      dest += h.getDataSize()+SynchronisableHeader::getSize();
552      origOffset += h.getDataSize()+SynchronisableHeader::getSize();
553      assert( Synchronisable::getSynchronisable(h.getObjectID()) );
554//       COUT(4) << "copy " << h.getObjectID() << endl;
555//       COUT(4) << "copy " << h.getObjectID() << ":";
556      //sizes += Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables();
557      for( unsigned int i = 0; i < Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables(); ++i )
558      {
559//         COUT(4) << " " << *sizes;
560        ++sizes;
561      }
562//       COUT(4) << endl;
563      assert(sizes != this->sizes_.end() || origOffset>=origLength);
564      continue;
565    }
[1701]566  }
567
[7163]568
569  Gamestate *g = new Gamestate(nData, getClientID());
570  assert(g->header_);
[2662]571  *(g->header_) = *header_;
572  g->header_->setBaseID( base->getID() );
[7163]573  g->header_->setDataSize(dest - nData - GamestateHeader::getSize());
[1751]574  g->flags_=flags_;
575  g->packetDirection_ = packetDirection_;
[7163]576  assert(!g->isCompressed());
[1701]577  return g;
[7163]578}
[3198]579
[7163]580
581Gamestate* Gamestate::diffData(Gamestate *base)
[3198]582{
583  assert(this && base); assert(data_ && base->data_);
584  assert(!header_->isCompressed() && !base->header_->isCompressed());
585  assert(!header_->isDiffed());
[6417]586
[3198]587  uint8_t *basep = GAMESTATE_START(base->data_);
588  uint8_t *gs = GAMESTATE_START(this->data_);
589  uint32_t dest_length = header_->getDataSize();
[6417]590
[3198]591  if(dest_length==0)
592    return NULL;
[6417]593
[3198]594  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];
595  uint8_t *dest = GAMESTATE_START(ndata);
[6417]596
[3198]597  rawDiff( dest, gs, basep, header_->getDataSize(), base->header_->getDataSize() );
598#ifndef NDEBUG
599  uint8_t *dest2 = new uint8_t[dest_length];
600  rawDiff( dest2, dest, basep, header_->getDataSize(), base->header_->getDataSize() );
601  assert( memcmp( dest2, gs, dest_length) == 0 );
[5929]602  delete dest2;
[3198]603#endif
604
605  Gamestate *g = new Gamestate(ndata, getClientID());
606  assert(g->header_);
607  *(g->header_) = *header_;
608  g->header_->setDiffed( true );
609  g->header_->setBaseID( base->getID() );
610  g->flags_=flags_;
611  g->packetDirection_ = packetDirection_;
612  assert(g->isDiffed());
613  assert(!g->isCompressed());
614  return g;
[1701]615}
616
[7163]617
618Gamestate* Gamestate::undiff(Gamestate *base)
[3198]619{
620  assert(this && base); assert(data_ && base->data_);
621  assert(!header_->isCompressed() && !base->header_->isCompressed());
622  assert(header_->isDiffed());
[6417]623
[3198]624  uint8_t *basep = GAMESTATE_START(base->data_);
625  uint8_t *gs = GAMESTATE_START(this->data_);
626  uint32_t dest_length = header_->getDataSize();
[6417]627
[3198]628  if(dest_length==0)
629    return NULL;
[6417]630
[3198]631  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];
632  uint8_t *dest = ndata + GamestateHeader::getSize();
[6417]633
[3198]634  rawDiff( dest, gs, basep, header_->getDataSize(), base->header_->getDataSize() );
[6417]635
[3198]636  Gamestate *g = new Gamestate(ndata, getClientID());
637  assert(g->header_);
638  *(g->header_) = *header_;
639  g->header_->setDiffed( false );
640  g->flags_=flags_;
641  g->packetDirection_ = packetDirection_;
642  assert(!g->isDiffed());
643  assert(!g->isCompressed());
644  return g;
645}
646
647
648void Gamestate::rawDiff( uint8_t* newdata, uint8_t* data, uint8_t* basedata, uint32_t datalength, uint32_t baselength)
649{
650  uint64_t* gd = (uint64_t*)data;
651  uint64_t* bd = (uint64_t*)basedata;
652  uint64_t* nd = (uint64_t*)newdata;
[6417]653
[3198]654  unsigned int i;
655  for( i=0; i<datalength/8; i++ )
656  {
657    if( i<baselength/8 )
658      *(nd+i) = *(gd+i) ^ *(bd+i);  // xor the data
659    else
660      *(nd+i) = *(gd+i); // just copy over the data
661  }
662  unsigned int j;
663  // now process the rest (when datalength isn't a multiple of 4)
664  for( j = 8*(datalength/8); j<datalength; j++ )
665  {
666    if( j<baselength )
667      *(newdata+j) = *(data+j) ^ *(basedata+j); // xor
668    else
669      *(newdata+j) = *(data+j); // just copy
670  }
671  assert(j==datalength);
672}
673
[7163]674
[2662]675Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){
[1907]676  assert(data_);
[2662]677  std::list<obj>::iterator it;
[2087]678
[1907]679  // allocate memory for new data
[2662]680  uint8_t *gdata = new uint8_t[header_->getDataSize()+GamestateHeader::getSize()];
[1907]681  // create a gamestate out of it
682  Gamestate *gs = new Gamestate(gdata);
[2662]683  uint8_t *newdata = gdata + GamestateHeader::getSize();
[1907]684  uint8_t *origdata = GAMESTATE_START(data_);
[2087]685
[1907]686  //copy the GamestateHeader
[2662]687  assert(gs->header_);
688  *(gs->header_) = *header_;
[2087]689
[2662]690  uint32_t objectOffset;
691  unsigned int objectsize, destsize=0;
692  // TODO: Why is this variable not used?
693  //Synchronisable *object;
[2087]694
[2662]695  //call TrafficControl
[3084]696  TrafficControl::getInstance()->processObjectList( clientID, header_->getID(), dataVector_ );
[2662]697
[1907]698  //copy in the zeros
[3084]699//   std::list<obj>::iterator itt;
700//   COUT(0) << "myvector contains:";
701//   for ( itt=dataVector_.begin() ; itt!=dataVector_.end(); itt++ )
[7163]702//     COUT(0) << " " << (*itt).objID;
[3084]703//   COUT(0) << endl;
704  for(it=dataVector_.begin(); it!=dataVector_.end();){
[2662]705    SynchronisableHeader oldobjectheader(origdata);
706    SynchronisableHeader newobjectheader(newdata);
[7163]707    if ( (*it).objSize == 0 )
[2662]708    {
709      ++it;
710      continue;
711    }
[7163]712    objectsize = oldobjectheader.getDataSize()+SynchronisableHeader::getSize();
[2662]713    objectOffset=SynchronisableHeader::getSize(); //skip the size and the availableData variables in the objectheader
[7163]714    if ( (*it).objID == oldobjectheader.getObjectID() ){
[2662]715      memcpy(newdata, origdata, objectsize);
716      ++it;
[1907]717    }else{
[2662]718      newobjectheader = oldobjectheader;
[2171]719      memset(newdata+objectOffset, 0, objectsize-objectOffset);
[1907]720    }
721    newdata += objectsize;
722    origdata += objectsize;
[2662]723    destsize += objectsize;
[1907]724  }
[2662]725#ifndef NDEBUG
726  uint32_t origsize = destsize;
727  while ( origsize < header_->getDataSize() )
728  {
729    SynchronisableHeader oldobjectheader(origdata);
[7163]730    objectsize = oldobjectheader.getDataSize()+SynchronisableHeader::getSize();
[2662]731    origdata += objectsize;
732    origsize += objectsize;
[1907]733  }
[2662]734  assert(origsize==header_->getDataSize());
735  assert(destsize!=0);
736#endif
737  gs->header_->setDataSize( destsize );
[1907]738  return gs;
739}
740
[2087]741
[2662]742uint32_t Gamestate::calcGamestateSize(int32_t id, uint8_t mode)
[1701]743{
[7163]744  uint32_t size = 0;
745  uint32_t nrOfVariables = 0;
[1701]746    // get the start of the Synchronisable list
[2171]747  ObjectList<Synchronisable>::iterator it;
[1701]748    // get total size of gamestate
[7163]749  for(it = ObjectList<Synchronisable>::begin(); it; ++it){
[1907]750    size+=it->getSize(id, mode); // size of the actual data of the synchronisable
[7163]751    nrOfVariables += it->getNrOfVariables();
752  }
753//   COUT(0) << "allocating " << nrOfVariables << " ints" << endl;
754  this->sizes_.reserve(nrOfVariables);
[1701]755  return size;
756}
757
[7163]758
[2662]759} //namespace packet
760} //namespace orxonox
Note: See TracBrowser for help on using the repository browser.