Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9844 was 9667, checked in by landauf, 11 years ago

merged core6 back to trunk

  • Property svn:eol-style set to native
File size: 26.9 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
[8858]33#include "util/Output.h"
[8373]34#include "util/OrxAssert.h"
[9667]35#include "core/CoreIncludes.h"
[2896]36#include "core/GameMode.h"
[9667]37#include "core/object/ObjectList.h"
[3214]38#include "network/synchronisable/Synchronisable.h"
39#include "network/GamestateHandler.h"
[7801]40#include "network/Host.h"
[1701]41
[2171]42namespace orxonox {
[1701]43
44namespace packet {
45
[2662]46#define GAMESTATE_START(data) (data + GamestateHeader::getSize())
[1740]47
[7801]48// #define PACKET_FLAG_GAMESTATE  PacketFlag::Reliable
49#define PACKET_FLAG_GAMESTATE  0
[2087]50
[7163]51inline bool memzero( uint8_t* data, uint32_t datalength)
52{
53  uint64_t* d = (uint64_t*)data;
[2662]54
[7163]55  for( unsigned int i=0; i<datalength/8; i++ )
56  {
57    if( *(d+i) != 0 )
58      return false;
59  }
60  // now process the rest (when datalength isn't a multiple of 4)
61  for( unsigned int j = 8*(datalength/8); j<datalength; j++ )
62  {
63    if( *(data+j) != 0 )
64      return false;
65  }
66  return true;
67}
68
69
70Gamestate::Gamestate():
[7801]71  header_()
[1701]72{
[1907]73  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
[1701]74}
75
[7163]76
[1907]77Gamestate::Gamestate(uint8_t *data, unsigned int clientID):
[7801]78  Packet(data, clientID), header_(data)
[1701]79{
[1907]80  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
[1701]81}
82
[7163]83
[7801]84Gamestate::Gamestate(uint8_t *data):
85  header_(data)
[1907]86{
87  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
[7163]88  data_ = data;
[1907]89}
[1701]90
[7163]91
[2662]92Gamestate::Gamestate(const Gamestate& g) :
[7801]93  Packet( *(Packet*)&g ), header_(this->data_), nrOfVariables_(0)
[2662]94{
95  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
[7163]96  sizes_ = g.sizes_;
[2662]97}
[1907]98
[2662]99
[1701]100Gamestate::~Gamestate()
101{
102}
103
[7163]104
[2171]105bool Gamestate::collectData(int id, uint8_t mode)
[1701]106{
[2662]107  uint32_t tempsize=0, currentsize=0;
[1751]108  assert(data_==0);
[2662]109  uint32_t size = calcGamestateSize(id, mode);
[1740]110
[8858]111  orxout(verbose_more, context::packets) << "G.ST.Man: producing gamestate with id: " << id << endl;
[1701]112  if(size==0)
113    return false;
[2662]114  data_ = new uint8_t[size + GamestateHeader::getSize()];
[7163]115  if(!data_)
116  {
[8858]117    orxout(internal_warning, context::packets) << "GameStateManager: could not allocate memory" << endl;
[1701]118    return false;
119  }
[6417]120
[7801]121  // tell the gamestate header where to store the data
122  header_.setData(this->data_);
[2087]123
[1701]124  //start collect data synchronisable by synchronisable
[7163]125  uint8_t *mem = data_; // in this stream store all data of the variables and the headers of the synchronisable
[2662]126  mem += GamestateHeader::getSize();
[2171]127  ObjectList<Synchronisable>::iterator it;
[7163]128  for(it = ObjectList<Synchronisable>::begin(); it; ++it)
129  {
[6417]130
[3084]131//     tempsize=it->getSize(id, mode);
132
[7163]133    tempsize = it->getData(mem, this->sizes_, id, mode);
[3084]134    if ( tempsize != 0 )
[9667]135      dataVector_.push_back( obj(it->getObjectID(), it->getContextID(), tempsize, mem-data_) );
[6417]136
[2662]137#ifndef NDEBUG
[7163]138    if(currentsize+tempsize > size)
139    {
[2171]140      assert(0); // if we don't use multithreading this part shouldn't be neccessary
[1701]141      // start allocate additional memory
[8858]142      orxout(internal_info, context::packets) << "Gamestate: need additional memory" << endl;
[2171]143      ObjectList<Synchronisable>::iterator temp = it;
[2662]144      uint32_t addsize=tempsize;
[1701]145      while(++temp)
[1907]146        addsize+=temp->getSize(id, mode);
[2662]147      data_ = (uint8_t *)realloc(data_, GamestateHeader::getSize() + currentsize + addsize);
[1701]148      if(!data_)
149        return false;
150      size = currentsize+addsize;
151    }// stop allocate additional memory
[2662]152#endif
[3084]153//     if(!it->getData(mem, id, mode))
154//       return false; // mem pointer gets automatically increased because of call by reference
[1701]155    // increase size counter by size of current synchronisable
156    currentsize+=tempsize;
157  }
[1740]158
159
[1701]160  //start write gamestate header
[7801]161  header_.setDataSize( currentsize );
[8327]162  header_.setCompSize( 0 );
[7801]163  header_.setID( id );
164  header_.setBaseID( GAMESTATEID_INITIAL );
165  header_.setDiffed( false );
166  header_.setComplete( true );
167  header_.setCompressed( false );
[1701]168  //stop write gamestate header
[1740]169
[8858]170  orxout(verbose_more, context::packets) << "Gamestate: Gamestate size: " << currentsize << endl;
171  orxout(verbose_more, context::packets) << "Gamestate: 'estimated' (and corrected) Gamestate size: " << size << endl;
[1701]172  return true;
173}
174
[7163]175
[2171]176bool Gamestate::spreadData(uint8_t mode)
[1701]177{
[8858]178  orxout(verbose_more, context::packets) << "processing gamestate with id " << header_.getID() << endl;
[1751]179  assert(data_);
[7801]180  assert(!header_.isCompressed());
[2662]181  uint8_t *mem=data_+GamestateHeader::getSize();
[1907]182  Synchronisable *s;
[7801]183 
[1907]184  // update the data of the objects we received
[7801]185  while(mem < data_+GamestateHeader::getSize()+header_.getDataSize())
[7163]186  {
[2662]187    SynchronisableHeader objectheader(mem);
[1701]188
[2662]189    s = Synchronisable::getSynchronisable( objectheader.getObjectID() );
[1907]190    if(!s)
[1701]191    {
[2896]192      if (!GameMode::isMaster())
[2662]193      {
194        Synchronisable::fabricate(mem, mode);
195      }
196      else
197      {
[8858]198//         orxout(verbose, context::packets) << "not creating object of classid " << objectheader.getClassID() << endl;
[7163]199        mem += objectheader.getDataSize() + ( objectheader.isDiffed() ? SynchronisableHeaderLight::getSize() : SynchronisableHeader::getSize() );
[2662]200      }
[1701]201    }
[1907]202    else
203    {
[8858]204//       orxout(verbose, context::packets) << "updating object of classid " << objectheader.getClassID() << endl;
[8394]205      OrxVerify(s->updateData(mem, mode), "ERROR: could not update Synchronisable with Gamestate data");
[1907]206    }
[1701]207  }
[7844]208  assert((uintptr_t)(mem-data_) == GamestateHeader::getSize()+header_.getDataSize());
[7801]209 
[2662]210   // In debug mode, check first, whether there are no duplicate objectIDs
211#ifndef NDEBUG
[7163]212  if(this->getID()%1000==1)
213  {
[3084]214    std::list<uint32_t> v1;
215    ObjectList<Synchronisable>::iterator it;
[7163]216    for (it = ObjectList<Synchronisable>::begin(); it != ObjectList<Synchronisable>::end(); ++it)
217    {
218      if (it->getObjectID() == OBJECTID_UNKNOWN)
219      {
220        if (it->objectMode_ != 0x0)
221        {
[8858]222          orxout(user_error, context::packets) << "Found object with OBJECTID_UNKNOWN on the client with objectMode != 0x0!" << endl;
223          orxout(user_error, context::packets) << "Possible reason for this error: Client created a synchronized object without the Server's approval." << endl;
224          orxout(user_error, context::packets) << "Objects class: " << it->getIdentifier()->getName() << endl;
[3084]225          assert(false);
226        }
[2662]227      }
[7163]228      else
229      {
[3084]230        std::list<uint32_t>::iterator it2;
[7163]231        for (it2 = v1.begin(); it2 != v1.end(); ++it2)
232        {
233          if (it->getObjectID() == *it2)
234          {
[8858]235            orxout(user_error, context::packets) << "Found duplicate objectIDs on the client!" << endl
236                                                 << "Are you sure you don't create a Sychnronisable objcect with 'new' \
237                                                     that doesn't have objectMode = 0x0?" << endl;
[3084]238            assert(false);
239          }
[2662]240        }
[3084]241        v1.push_back(it->getObjectID());
[2662]242      }
243    }
244  }
245#endif
[1701]246  return true;
247}
248
[7163]249
[2662]250uint32_t Gamestate::getSize() const
[1701]251{
[1711]252  assert(data_);
[7801]253  if(header_.isCompressed())
254    return header_.getCompSize()+GamestateHeader::getSize();
[1701]255  else
256  {
[7801]257    return header_.getDataSize()+GamestateHeader::getSize();
[1701]258  }
259}
260
[7163]261
262bool Gamestate::operator==(packet::Gamestate gs)
263{
[2662]264  uint8_t *d1 = data_+GamestateHeader::getSize();
265  uint8_t *d2 = gs.data_+GamestateHeader::getSize();
[8394]266  GamestateHeader h1(data_);
267  GamestateHeader h2(gs.data_);
268  assert(h1.getDataSize() == h2.getDataSize());
[1751]269  assert(!isCompressed());
270  assert(!gs.isCompressed());
[8394]271  return memcmp(d1, d2, h1.getDataSize())==0;
[1751]272}
273
[7163]274
[7801]275bool Gamestate::process(orxonox::Host* host)
[1701]276{
[7801]277  return host->addGamestate(this, getPeerID());
[1701]278}
279
[1907]280
[1701]281bool Gamestate::compressData()
282{
[2662]283  assert(data_);
[7801]284  assert(!header_.isCompressed());
285  uLongf buffer = (uLongf)(((header_.getDataSize() + 12)*1.01)+1);
[1701]286  if(buffer==0)
287    return false;
[1740]288
[2662]289  uint8_t *ndata = new uint8_t[buffer+GamestateHeader::getSize()];
290  uint8_t *dest = ndata + GamestateHeader::getSize();
291  uint8_t *source = data_ + GamestateHeader::getSize();
[1701]292  int retval;
[7801]293  retval = compress( dest, &buffer, source, (uLong)(header_.getDataSize()) );
[7163]294  switch ( retval )
295  {
[8858]296    case Z_OK: orxout(verbose_more, context::packets) << "G.St.Man: compress: successfully compressed" << endl; break;
297    case Z_MEM_ERROR: orxout(internal_error, context::packets) << "G.St.Man: compress: not enough memory available in gamestate.compress" << endl; return false;
298    case Z_BUF_ERROR: orxout(internal_warning, context::packets) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << endl; return false;
299    case Z_DATA_ERROR: orxout(internal_warning, context::packets) << "G.St.Man: compress: data corrupted in gamestate.compress" << endl; return false;
[1701]300  }
301
302  //copy and modify header
[7801]303  GamestateHeader *temp = new GamestateHeader(data_);
304  header_.setData(ndata);
305  header_ = *temp;
[2662]306  delete temp;
[1701]307  //delete old data
308  delete[] data_;
309  //save new data
310  data_ = ndata;
[7801]311  header_.setCompSize( buffer );
312  header_.setCompressed( true );
[8858]313  orxout(verbose, context::packets) << "gamestate compress datasize: " << header_.getDataSize() << " compsize: " << header_.getCompSize() << endl;
[1701]314  return true;
315}
[7163]316
317
[1701]318bool Gamestate::decompressData()
319{
[2662]320  assert(data_);
[7801]321  assert(header_.isCompressed());
[8858]322  orxout(verbose, context::packets) << "GameStateClient: uncompressing gamestate. id: " << header_.getID() << ", baseid: " << header_.getBaseID() << ", datasize: " << header_.getDataSize() << ", compsize: " << header_.getCompSize() << endl;
[7801]323  uint32_t datasize = header_.getDataSize();
324  uint32_t compsize = header_.getCompSize();
[2662]325  uint32_t bufsize;
[1907]326  bufsize = datasize;
[1751]327  assert(bufsize!=0);
[2662]328  uint8_t *ndata = new uint8_t[bufsize + GamestateHeader::getSize()];
329  uint8_t *dest = ndata + GamestateHeader::getSize();
330  uint8_t *source = data_ + GamestateHeader::getSize();
[1701]331  int retval;
[1751]332  uLongf length=bufsize;
333  retval = uncompress( dest, &length, source, (uLong)compsize );
[7163]334  switch ( retval )
335  {
[8858]336    case Z_OK: orxout(verbose_more, context::packets) << "successfully decompressed" << endl; break;
337    case Z_MEM_ERROR: orxout(internal_error, context::packets) << "not enough memory available" << endl; return false;
338    case Z_BUF_ERROR: orxout(internal_warning, context::packets) << "not enough memory available in the buffer" << endl; return false;
339    case Z_DATA_ERROR: orxout(internal_warning, context::packets) << "data corrupted (zlib)" << endl; return false;
[1701]340  }
[1752]341
[1701]342  //copy over the header
[7801]343  GamestateHeader* temp = new GamestateHeader( data_ );
344  header_.setData(ndata);
345  header_ = *temp;
[2662]346  delete temp;
[2087]347
[7163]348  if (this->bDataENetAllocated_)
349  {
[2087]350    // Memory was allocated by ENet. --> We let it be since enet_packet_destroy will
351    // deallocated it anyway. So data and packet stay together.
352    this->bDataENetAllocated_ = false;
353  }
[7163]354  else
355  {
[2087]356    // We allocated the memory in the first place (unlikely). So we destroy the old data
357    // and overwrite it with the new decompressed data.
358    delete[] this->data_;
359  }
360
[1751]361  //set new pointers
[1701]362  data_ = ndata;
[7801]363  header_.setCompressed( false );
364  assert(header_.getDataSize()==datasize);
365  assert(header_.getCompSize()==compsize);
[1701]366  return true;
367}
368
[7163]369
[7801]370inline void /*Gamestate::*/diffObject( uint8_t*& newDataPtr, uint8_t*& origDataPtr, uint8_t*& baseDataPtr, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes )
371{
372  assert( objectHeader.getDataSize() == SynchronisableHeader(baseDataPtr).getDataSize() );
373 
374  uint32_t objectOffset = SynchronisableHeader::getSize(); // offset inside the object in the origData and baseData
375  // Check whether the whole object stayed the same
376  if( memcmp( origDataPtr+objectOffset, baseDataPtr+objectOffset, objectHeader.getDataSize()) == 0 )
377  {
[8858]378//     orxout(verbose, context::packets) << "skip object " << Synchronisable::getSynchronisable(objectHeader.getObjectID())->getIdentifier()->getName() << endl;
[7801]379    origDataPtr += objectOffset + objectHeader.getDataSize(); // skip the whole object
380    baseDataPtr += objectOffset + objectHeader.getDataSize();
381    sizes += Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables();
382  }
383  else
384  {
385    // Now start to diff the Object
386    SynchronisableHeaderLight newObjectHeader(newDataPtr);
387    newObjectHeader = objectHeader; // copy over the objectheader
388    VariableID variableID = 0;
389    uint32_t diffedObjectOffset = SynchronisableHeaderLight::getSize();
390    // iterate through all variables
391    while( objectOffset < objectHeader.getDataSize()+SynchronisableHeader::getSize() )
392    {
393      // check whether variable changed and write id and copy over variable to the new stream
394      // otherwise skip variable
395      uint32_t varSize = *sizes;
396      assert( varSize == Synchronisable::getSynchronisable(objectHeader.getObjectID())->getVarSize(variableID) );
397      if ( varSize != 0 )
398      {
399        if ( memcmp(origDataPtr+objectOffset, baseDataPtr+objectOffset, varSize) != 0 )
400        {
401          *(VariableID*)(newDataPtr+diffedObjectOffset) = variableID; // copy over the variableID
402          diffedObjectOffset += sizeof(VariableID);
403          memcpy( newDataPtr+diffedObjectOffset, origDataPtr+objectOffset, varSize );
404          diffedObjectOffset += varSize;
405          objectOffset += varSize;
406        }
407        else
408        {
409          objectOffset += varSize;
410        }
411      }
412
413      ++variableID;
414      ++sizes;
415    }
416   
417    // if there are variables from this object with 0 size left in sizes
418    if( Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables() != variableID )
419      sizes += Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables() - variableID;
420   
421    newObjectHeader.setDiffed(true);
422    newObjectHeader.setDataSize(diffedObjectOffset-SynchronisableHeaderLight::getSize());
423    assert(objectOffset == objectHeader.getDataSize()+SynchronisableHeader::getSize());
424    assert(newObjectHeader.getDataSize()>0);
425   
426    origDataPtr += objectOffset;
427    baseDataPtr += objectOffset;
428    newDataPtr += diffedObjectOffset;
429  }
430}
431
432inline void /*Gamestate::*/copyObject( uint8_t*& newData, uint8_t*& origData, uint8_t*& baseData, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes )
433{
[8858]434  //       orxout(verbose, context::packets) << "docopy" << endl;
[7801]435  // Just copy over the whole Object
436  memcpy( newData, origData, objectHeader.getDataSize()+SynchronisableHeader::getSize() );
437  SynchronisableHeader(newData).setDiffed(false);
438 
439  newData += objectHeader.getDataSize()+SynchronisableHeader::getSize();
440  origData += objectHeader.getDataSize()+SynchronisableHeader::getSize();
441//   SynchronisableHeader baseHeader( baseData );
442//   baseData += baseHeader.getDataSize()+SynchronisableHeader::getSize();
[8858]443  //       orxout(verbose, context::packets) << "copy " << h.getObjectID() << endl;
444  //       orxout(verbose, context::packets) << "copy " << h.getObjectID() << ":";
[7801]445  sizes += Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables();
446//   for( unsigned int i = 0; i < Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables(); ++i )
447//   {
[8858]448//     //         orxout(verbose, context::packets) << " " << *sizes;
[7801]449//     ++sizes;
450//   }
[8858]451    //       orxout(verbose, context::packets) << endl;
[7801]452}
453
454inline bool findObject(uint8_t*& dataPtr, uint8_t* endPtr, SynchronisableHeader& objectHeader)
455{
456  // Some assertions to make sure the dataPtr is valid (pointing to a SynchronisableHeader)
457  {
458    SynchronisableHeader htemp2(dataPtr);
459    assert(htemp2.getClassID()<500);
460    assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000);
461    assert(htemp2.isDiffed()==false);
462  }
463  uint32_t objectID = objectHeader.getObjectID();
464  while ( dataPtr < endPtr )
465  {
466    SynchronisableHeader htemp(dataPtr);
467    assert( htemp.getDataSize()!=0 );
468    if ( htemp.getObjectID() == objectID )
469    {
470      assert( objectHeader.getClassID() == htemp.getClassID() );
[9667]471      assert( objectHeader.getContextID() == htemp.getContextID() );
[7801]472      return true;
473    }
474    {
475      if( dataPtr+htemp.getDataSize()+SynchronisableHeader::getSize() < endPtr )
476      {
477        SynchronisableHeader htemp2(dataPtr+htemp.getDataSize()+SynchronisableHeader::getSize());
478        assert(htemp2.getClassID()<500);
479        assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000);
480        assert(htemp2.isDiffed()==false);
481      }
482    }
483    dataPtr += htemp.getDataSize()+SynchronisableHeader::getSize();
484   
485  }
486  assert(dataPtr == endPtr);
487 
488  return false;
489}
490
[7163]491Gamestate* Gamestate::diffVariables(Gamestate *base)
[1701]492{
[7163]493  assert(this && base); assert(data_ && base->data_);
[7801]494  assert(!header_.isCompressed() && !base->header_.isCompressed());
495  assert(!header_.isDiffed());
496  assert( header_.getDataSize() && base->header_.getDataSize() );
[7163]497
498
499  // *** first do a raw diff of the two gamestates
500
[7801]501  uint8_t *baseDataPtr = GAMESTATE_START(base->data_);
502  uint8_t *origDataPtr = GAMESTATE_START(this->data_);
503  uint8_t *origDataEnd = origDataPtr + header_.getDataSize();
504  uint8_t *baseDataEnd = baseDataPtr + base->header_.getDataSize();
505//   uint32_t origLength = header_.getDataSize();
506//   uint32_t baseLength = base->header_.getDataSize();
[7163]507
[7801]508  // Allocate new space for diffed gamestate
509  uint32_t newDataSize = header_.getDataSize() + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_;
510  uint8_t *newData = new uint8_t[newDataSize]; // this is the maximum size needed in the worst case
511  uint8_t *destDataPtr = GAMESTATE_START(newData);
[7163]512
[7801]513  std::vector<uint32_t>::iterator sizesIt = this->sizes_.begin();
[7163]514
[7801]515  while( origDataPtr < origDataEnd )
[7163]516  {
517    //iterate through all objects
518
[7801]519    SynchronisableHeader origHeader(origDataPtr);
[7163]520
521    // Find (if possible) the current object in the datastream of the old gamestate
522    // Start at the current offset position
[7801]523    if(baseDataPtr == baseDataEnd)
524      baseDataPtr = GAMESTATE_START(base->data_);
525    uint8_t* oldBaseDataPtr = baseDataPtr;
526   
527    assert(baseDataPtr < baseDataEnd);
528    assert(destDataPtr < newData + newDataSize);
529    assert(sizesIt != this->sizes_.end());
530   
531    assert(Synchronisable::getSynchronisable(origHeader.getObjectID()));
532    assert(ClassByID(origHeader.getClassID()));
533    assert(origHeader.getDataSize() < 500);
534   
535    if( findObject(baseDataPtr, baseDataEnd, origHeader) )
[7163]536    {
[7801]537      SynchronisableHeader baseHeader(baseDataPtr);
538      assert(Synchronisable::getSynchronisable(baseHeader.getObjectID()));
539      assert(ClassByID(baseHeader.getClassID()));
540      assert(baseHeader.getDataSize() < 500);
541      if( SynchronisableHeader(baseDataPtr).getDataSize()==origHeader.getDataSize() )
[7163]542      {
[8858]543//         orxout(verbose, context::packets) << "diffing object in order: " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl;
[7801]544        diffObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt);
[1701]545      }
[7801]546      else
[7163]547      {
[8858]548//         orxout(verbose, context::packets) << "copy object because of different data sizes (1): " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl;
[7801]549        copyObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt);
550        assert(sizesIt != this->sizes_.end() || origDataPtr==origDataEnd);
[7163]551      }
[7801]552       
[7163]553    }
[7801]554    else
[7163]555    {
[7801]556      assert( baseDataPtr == baseDataEnd );
557      baseDataPtr = GAMESTATE_START(base->data_);
558      if( findObject(baseDataPtr, oldBaseDataPtr, origHeader) )
[7163]559      {
[7801]560        SynchronisableHeader baseHeader(baseDataPtr);
561        assert(Synchronisable::getSynchronisable(baseHeader.getObjectID()));
562        assert(ClassByID(baseHeader.getClassID()));
563        assert(baseHeader.getDataSize() < 500);
564        if( SynchronisableHeader(baseDataPtr).getDataSize()==origHeader.getDataSize() )
[7163]565        {
[8858]566//           orxout(verbose, context::packets) << "diffing object out of order: " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl;
[7801]567          diffObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt);
[7163]568        }
[7801]569        else
570        {
[8858]571//           orxout(verbose, context::packets) << "copy object because of different data sizes (2): " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl;
[7801]572          copyObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt);
573          assert(sizesIt != this->sizes_.end() || origDataPtr==origDataEnd);
574        }
[7163]575      }
[7801]576      else
[7163]577      {
[8858]578//         orxout(verbose, context::packets) << "copy object: " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl;
[7801]579        assert(baseDataPtr == oldBaseDataPtr);
580        copyObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt);
581        assert(sizesIt != this->sizes_.end() || origDataPtr==origDataEnd);
[7163]582      }
583    }
[1701]584  }
[7801]585  assert(sizesIt==this->sizes_.end());
[1701]586
[7163]587
[7801]588  Gamestate *g = new Gamestate(newData, getPeerID());
589  (g->header_) = header_;
590  g->header_.setBaseID( base->getID() );
591  g->header_.setDataSize(destDataPtr - newData - GamestateHeader::getSize());
[1751]592  g->flags_=flags_;
593  g->packetDirection_ = packetDirection_;
[7163]594  assert(!g->isCompressed());
[1701]595  return g;
[7163]596}
[3198]597
[7163]598
[7801]599/*Gamestate* Gamestate::diffData(Gamestate *base)
[3198]600{
601  assert(this && base); assert(data_ && base->data_);
[7801]602  assert(!header_.isCompressed() && !base->header_.isCompressed());
603  assert(!header_.isDiffed());
[6417]604
[3198]605  uint8_t *basep = GAMESTATE_START(base->data_);
606  uint8_t *gs = GAMESTATE_START(this->data_);
[7801]607  uint32_t dest_length = header_.getDataSize();
[6417]608
[3198]609  if(dest_length==0)
610    return NULL;
[6417]611
[3198]612  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];
613  uint8_t *dest = GAMESTATE_START(ndata);
[6417]614
[7801]615  rawDiff( dest, gs, basep, header_.getDataSize(), base->header_.getDataSize() );
[3198]616#ifndef NDEBUG
617  uint8_t *dest2 = new uint8_t[dest_length];
[7801]618  rawDiff( dest2, dest, basep, header_.getDataSize(), base->header_.getDataSize() );
[3198]619  assert( memcmp( dest2, gs, dest_length) == 0 );
[5929]620  delete dest2;
[3198]621#endif
622
623  Gamestate *g = new Gamestate(ndata, getClientID());
624  assert(g->header_);
625  *(g->header_) = *header_;
[7801]626  g->header_.setDiffed( true );
627  g->header_.setBaseID( base->getID() );
[3198]628  g->flags_=flags_;
629  g->packetDirection_ = packetDirection_;
630  assert(g->isDiffed());
631  assert(!g->isCompressed());
632  return g;
[1701]633}
634
[7163]635
636Gamestate* Gamestate::undiff(Gamestate *base)
[3198]637{
638  assert(this && base); assert(data_ && base->data_);
[7801]639  assert(!header_.isCompressed() && !base->header_.isCompressed());
640  assert(header_.isDiffed());
[6417]641
[3198]642  uint8_t *basep = GAMESTATE_START(base->data_);
643  uint8_t *gs = GAMESTATE_START(this->data_);
[7801]644  uint32_t dest_length = header_.getDataSize();
[6417]645
[3198]646  if(dest_length==0)
647    return NULL;
[6417]648
[3198]649  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];
650  uint8_t *dest = ndata + GamestateHeader::getSize();
[6417]651
[7801]652  rawDiff( dest, gs, basep, header_.getDataSize(), base->header_.getDataSize() );
[6417]653
[3198]654  Gamestate *g = new Gamestate(ndata, getClientID());
655  assert(g->header_);
656  *(g->header_) = *header_;
[7801]657  g->header_.setDiffed( false );
[3198]658  g->flags_=flags_;
659  g->packetDirection_ = packetDirection_;
660  assert(!g->isDiffed());
661  assert(!g->isCompressed());
662  return g;
663}
664
665
666void Gamestate::rawDiff( uint8_t* newdata, uint8_t* data, uint8_t* basedata, uint32_t datalength, uint32_t baselength)
667{
668  uint64_t* gd = (uint64_t*)data;
669  uint64_t* bd = (uint64_t*)basedata;
670  uint64_t* nd = (uint64_t*)newdata;
[6417]671
[3198]672  unsigned int i;
673  for( i=0; i<datalength/8; i++ )
674  {
675    if( i<baselength/8 )
676      *(nd+i) = *(gd+i) ^ *(bd+i);  // xor the data
677    else
678      *(nd+i) = *(gd+i); // just copy over the data
679  }
680  unsigned int j;
681  // now process the rest (when datalength isn't a multiple of 4)
682  for( j = 8*(datalength/8); j<datalength; j++ )
683  {
684    if( j<baselength )
685      *(newdata+j) = *(data+j) ^ *(basedata+j); // xor
686    else
687      *(newdata+j) = *(data+j); // just copy
688  }
689  assert(j==datalength);
[7801]690}*/
[3198]691
[7163]692
[7801]693/*Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){
[1907]694  assert(data_);
[2662]695  std::list<obj>::iterator it;
[2087]696
[1907]697  // allocate memory for new data
[7801]698  uint8_t *gdata = new uint8_t[header_.getDataSize()+GamestateHeader::getSize()];
[1907]699  // create a gamestate out of it
700  Gamestate *gs = new Gamestate(gdata);
[2662]701  uint8_t *newdata = gdata + GamestateHeader::getSize();
[1907]702  uint8_t *origdata = GAMESTATE_START(data_);
[2087]703
[1907]704  //copy the GamestateHeader
[2662]705  assert(gs->header_);
706  *(gs->header_) = *header_;
[2087]707
[2662]708  uint32_t objectOffset;
709  unsigned int objectsize, destsize=0;
710  // TODO: Why is this variable not used?
711  //Synchronisable *object;
[2087]712
[2662]713  //call TrafficControl
[7801]714  TrafficControl::getInstance()->processObjectList( clientID, header_.getID(), dataVector_ );
[2662]715
[1907]716  //copy in the zeros
[3084]717//   std::list<obj>::iterator itt;
[8858]718//   orxout() << "myvector contains:";
[3084]719//   for ( itt=dataVector_.begin() ; itt!=dataVector_.end(); itt++ )
[8858]720//     orxout() << " " << (*itt).objID;
721//   orxout() << endl;
[3084]722  for(it=dataVector_.begin(); it!=dataVector_.end();){
[2662]723    SynchronisableHeader oldobjectheader(origdata);
724    SynchronisableHeader newobjectheader(newdata);
[7163]725    if ( (*it).objSize == 0 )
[2662]726    {
727      ++it;
728      continue;
729    }
[7163]730    objectsize = oldobjectheader.getDataSize()+SynchronisableHeader::getSize();
[2662]731    objectOffset=SynchronisableHeader::getSize(); //skip the size and the availableData variables in the objectheader
[7163]732    if ( (*it).objID == oldobjectheader.getObjectID() ){
[2662]733      memcpy(newdata, origdata, objectsize);
734      ++it;
[1907]735    }else{
[2662]736      newobjectheader = oldobjectheader;
[2171]737      memset(newdata+objectOffset, 0, objectsize-objectOffset);
[1907]738    }
739    newdata += objectsize;
740    origdata += objectsize;
[2662]741    destsize += objectsize;
[1907]742  }
[2662]743#ifndef NDEBUG
744  uint32_t origsize = destsize;
[7801]745  while ( origsize < header_.getDataSize() )
[2662]746  {
747    SynchronisableHeader oldobjectheader(origdata);
[7163]748    objectsize = oldobjectheader.getDataSize()+SynchronisableHeader::getSize();
[2662]749    origdata += objectsize;
750    origsize += objectsize;
[1907]751  }
[7801]752  assert(origsize==header_.getDataSize());
[2662]753  assert(destsize!=0);
754#endif
[7801]755  gs->header_.setDataSize( destsize );
[1907]756  return gs;
[7801]757}*/
[1907]758
[2087]759
[7801]760uint32_t Gamestate::calcGamestateSize(uint32_t id, uint8_t mode)
[1701]761{
[7163]762  uint32_t size = 0;
763  uint32_t nrOfVariables = 0;
[1701]764    // get the start of the Synchronisable list
[2171]765  ObjectList<Synchronisable>::iterator it;
[1701]766    // get total size of gamestate
[7163]767  for(it = ObjectList<Synchronisable>::begin(); it; ++it){
[1907]768    size+=it->getSize(id, mode); // size of the actual data of the synchronisable
[7163]769    nrOfVariables += it->getNrOfVariables();
770  }
[8858]771//   orxout() << "allocating " << nrOfVariables << " ints" << endl;
[7163]772  this->sizes_.reserve(nrOfVariables);
[1701]773  return size;
774}
775
[7163]776
[2662]777} //namespace packet
778} //namespace orxonox
Note: See TracBrowser for help on using the repository browser.