Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/particleEffectsHS15/src/libraries/network/packet/Gamestate.cc @ 11089

Last change on this file since 11089 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
Line 
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:
23 *      Oliver Scheuss
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "Gamestate.h"
30
31#include <zlib.h>
32
33#include "util/Output.h"
34#include "util/OrxAssert.h"
35#include "core/CoreIncludes.h"
36#include "core/GameMode.h"
37#include "core/object/ObjectList.h"
38#include "network/synchronisable/Synchronisable.h"
39#include "network/GamestateHandler.h"
40#include "network/Host.h"
41
42namespace orxonox {
43
44namespace packet {
45
46#define GAMESTATE_START(data) (data + GamestateHeader::getSize())
47
48// #define PACKET_FLAG_GAMESTATE  PacketFlag::Reliable
49#define PACKET_FLAG_GAMESTATE  0
50
51inline bool memzero( uint8_t* data, uint32_t datalength)
52{
53  uint64_t* d = (uint64_t*)data;
54
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():
71  header_()
72{
73  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
74}
75
76
77Gamestate::Gamestate(uint8_t *data, unsigned int clientID):
78  Packet(data, clientID), header_(data)
79{
80  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
81}
82
83
84Gamestate::Gamestate(uint8_t *data):
85  header_(data)
86{
87  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
88  data_ = data;
89}
90
91
92Gamestate::Gamestate(const Gamestate& g) :
93  Packet( *(Packet*)&g ), header_(this->data_), nrOfVariables_(0)
94{
95  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
96  sizes_ = g.sizes_;
97}
98
99
100Gamestate::~Gamestate()
101{
102}
103
104
105bool Gamestate::collectData(int id, uint8_t mode)
106{
107  uint32_t tempsize=0, currentsize=0;
108  assert(data_==0);
109  uint32_t size = calcGamestateSize(id, mode);
110
111  orxout(verbose_more, context::packets) << "G.ST.Man: producing gamestate with id: " << id << endl;
112  if(size==0)
113    return false;
114  data_ = new uint8_t[size + GamestateHeader::getSize()];
115  if(!data_)
116  {
117    orxout(internal_warning, context::packets) << "GameStateManager: could not allocate memory" << endl;
118    return false;
119  }
120
121  // tell the gamestate header where to store the data
122  header_.setData(this->data_);
123
124  //start collect data synchronisable by synchronisable
125  uint8_t *mem = data_; // in this stream store all data of the variables and the headers of the synchronisable
126  mem += GamestateHeader::getSize();
127  ObjectList<Synchronisable>::iterator it;
128  for(it = ObjectList<Synchronisable>::begin(); it; ++it)
129  {
130
131//     tempsize=it->getSize(id, mode);
132
133    tempsize = it->getData(mem, this->sizes_, id, mode);
134    if ( tempsize != 0 )
135      dataVector_.push_back( obj(it->getObjectID(), it->getContextID(), tempsize, mem-data_) );
136
137#ifndef NDEBUG
138    if(currentsize+tempsize > size)
139    {
140      assert(0); // if we don't use multithreading this part shouldn't be neccessary
141      // start allocate additional memory
142      orxout(internal_info, context::packets) << "Gamestate: need additional memory" << endl;
143      ObjectList<Synchronisable>::iterator temp = it;
144      uint32_t addsize=tempsize;
145      while(++temp)
146        addsize+=temp->getSize(id, mode);
147      data_ = (uint8_t *)realloc(data_, GamestateHeader::getSize() + currentsize + addsize);
148      if(!data_)
149        return false;
150      size = currentsize+addsize;
151    }// stop allocate additional memory
152#endif
153//     if(!it->getData(mem, id, mode))
154//       return false; // mem pointer gets automatically increased because of call by reference
155    // increase size counter by size of current synchronisable
156    currentsize+=tempsize;
157  }
158
159
160  //start write gamestate header
161  header_.setDataSize( currentsize );
162  header_.setCompSize( 0 );
163  header_.setID( id );
164  header_.setBaseID( GAMESTATEID_INITIAL );
165  header_.setDiffed( false );
166  header_.setComplete( true );
167  header_.setCompressed( false );
168  //stop write gamestate header
169
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;
172  return true;
173}
174
175
176bool Gamestate::spreadData(uint8_t mode)
177{
178  orxout(verbose_more, context::packets) << "processing gamestate with id " << header_.getID() << endl;
179  assert(data_);
180  assert(!header_.isCompressed());
181  uint8_t *mem=data_+GamestateHeader::getSize();
182  Synchronisable *s;
183 
184  // update the data of the objects we received
185  while(mem < data_+GamestateHeader::getSize()+header_.getDataSize())
186  {
187    SynchronisableHeader objectheader(mem);
188
189    s = Synchronisable::getSynchronisable( objectheader.getObjectID() );
190    if(!s)
191    {
192      if (!GameMode::isMaster())
193      {
194        Synchronisable::fabricate(mem, mode);
195      }
196      else
197      {
198//         orxout(verbose, context::packets) << "not creating object of classid " << objectheader.getClassID() << endl;
199        mem += objectheader.getDataSize() + ( objectheader.isDiffed() ? SynchronisableHeaderLight::getSize() : SynchronisableHeader::getSize() );
200      }
201    }
202    else
203    {
204//       orxout(verbose, context::packets) << "updating object of classid " << objectheader.getClassID() << endl;
205      OrxVerify(s->updateData(mem, mode), "ERROR: could not update Synchronisable with Gamestate data");
206    }
207  }
208  assert((uintptr_t)(mem-data_) == GamestateHeader::getSize()+header_.getDataSize());
209 
210   // In debug mode, check first, whether there are no duplicate objectIDs
211#ifndef NDEBUG
212  if(this->getID()%1000==1)
213  {
214    std::list<uint32_t> v1;
215    ObjectList<Synchronisable>::iterator it;
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        {
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;
225          assert(false);
226        }
227      }
228      else
229      {
230        std::list<uint32_t>::iterator it2;
231        for (it2 = v1.begin(); it2 != v1.end(); ++it2)
232        {
233          if (it->getObjectID() == *it2)
234          {
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;
238            assert(false);
239          }
240        }
241        v1.push_back(it->getObjectID());
242      }
243    }
244  }
245#endif
246  return true;
247}
248
249
250uint32_t Gamestate::getSize() const
251{
252  assert(data_);
253  if(header_.isCompressed())
254    return header_.getCompSize()+GamestateHeader::getSize();
255  else
256  {
257    return header_.getDataSize()+GamestateHeader::getSize();
258  }
259}
260
261
262bool Gamestate::operator==(packet::Gamestate gs)
263{
264  uint8_t *d1 = data_+GamestateHeader::getSize();
265  uint8_t *d2 = gs.data_+GamestateHeader::getSize();
266  GamestateHeader h1(data_);
267  GamestateHeader h2(gs.data_);
268  assert(h1.getDataSize() == h2.getDataSize());
269  assert(!isCompressed());
270  assert(!gs.isCompressed());
271  return memcmp(d1, d2, h1.getDataSize())==0;
272}
273
274
275bool Gamestate::process(orxonox::Host* host)
276{
277  return host->addGamestate(this, getPeerID());
278}
279
280
281bool Gamestate::compressData()
282{
283  assert(data_);
284  assert(!header_.isCompressed());
285  uLongf buffer = (uLongf)(((header_.getDataSize() + 12)*1.01)+1);
286  if(buffer==0)
287    return false;
288
289  uint8_t *ndata = new uint8_t[buffer+GamestateHeader::getSize()];
290  uint8_t *dest = ndata + GamestateHeader::getSize();
291  uint8_t *source = data_ + GamestateHeader::getSize();
292  int retval;
293  retval = compress( dest, &buffer, source, (uLong)(header_.getDataSize()) );
294  switch ( retval )
295  {
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;
300  }
301
302  //copy and modify header
303  GamestateHeader *temp = new GamestateHeader(data_);
304  header_.setData(ndata);
305  header_ = *temp;
306  delete temp;
307  //delete old data
308  delete[] data_;
309  //save new data
310  data_ = ndata;
311  header_.setCompSize( buffer );
312  header_.setCompressed( true );
313  orxout(verbose, context::packets) << "gamestate compress datasize: " << header_.getDataSize() << " compsize: " << header_.getCompSize() << endl;
314  return true;
315}
316
317
318bool Gamestate::decompressData()
319{
320  assert(data_);
321  assert(header_.isCompressed());
322  orxout(verbose, context::packets) << "GameStateClient: uncompressing gamestate. id: " << header_.getID() << ", baseid: " << header_.getBaseID() << ", datasize: " << header_.getDataSize() << ", compsize: " << header_.getCompSize() << endl;
323  uint32_t datasize = header_.getDataSize();
324  uint32_t compsize = header_.getCompSize();
325  uint32_t bufsize;
326  bufsize = datasize;
327  assert(bufsize!=0);
328  uint8_t *ndata = new uint8_t[bufsize + GamestateHeader::getSize()];
329  uint8_t *dest = ndata + GamestateHeader::getSize();
330  uint8_t *source = data_ + GamestateHeader::getSize();
331  int retval;
332  uLongf length=bufsize;
333  retval = uncompress( dest, &length, source, (uLong)compsize );
334  switch ( retval )
335  {
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;
340  }
341
342  //copy over the header
343  GamestateHeader* temp = new GamestateHeader( data_ );
344  header_.setData(ndata);
345  header_ = *temp;
346  delete temp;
347
348  if (this->bDataENetAllocated_)
349  {
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  }
354  else
355  {
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
361  //set new pointers
362  data_ = ndata;
363  header_.setCompressed( false );
364  assert(header_.getDataSize()==datasize);
365  assert(header_.getCompSize()==compsize);
366  return true;
367}
368
369
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  {
378//     orxout(verbose, context::packets) << "skip object " << Synchronisable::getSynchronisable(objectHeader.getObjectID())->getIdentifier()->getName() << endl;
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{
434  //       orxout(verbose, context::packets) << "docopy" << endl;
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();
443  //       orxout(verbose, context::packets) << "copy " << h.getObjectID() << endl;
444  //       orxout(verbose, context::packets) << "copy " << h.getObjectID() << ":";
445  sizes += Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables();
446//   for( unsigned int i = 0; i < Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables(); ++i )
447//   {
448//     //         orxout(verbose, context::packets) << " " << *sizes;
449//     ++sizes;
450//   }
451    //       orxout(verbose, context::packets) << endl;
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() );
471      assert( objectHeader.getContextID() == htemp.getContextID() );
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
491Gamestate* Gamestate::diffVariables(Gamestate *base)
492{
493  assert(this && base); assert(data_ && base->data_);
494  assert(!header_.isCompressed() && !base->header_.isCompressed());
495  assert(!header_.isDiffed());
496  assert( header_.getDataSize() && base->header_.getDataSize() );
497
498
499  // *** first do a raw diff of the two gamestates
500
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();
507
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);
512
513  std::vector<uint32_t>::iterator sizesIt = this->sizes_.begin();
514
515  while( origDataPtr < origDataEnd )
516  {
517    //iterate through all objects
518
519    SynchronisableHeader origHeader(origDataPtr);
520
521    // Find (if possible) the current object in the datastream of the old gamestate
522    // Start at the current offset position
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) )
536    {
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() )
542      {
543//         orxout(verbose, context::packets) << "diffing object in order: " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl;
544        diffObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt);
545      }
546      else
547      {
548//         orxout(verbose, context::packets) << "copy object because of different data sizes (1): " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl;
549        copyObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt);
550        assert(sizesIt != this->sizes_.end() || origDataPtr==origDataEnd);
551      }
552       
553    }
554    else
555    {
556      assert( baseDataPtr == baseDataEnd );
557      baseDataPtr = GAMESTATE_START(base->data_);
558      if( findObject(baseDataPtr, oldBaseDataPtr, origHeader) )
559      {
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() )
565        {
566//           orxout(verbose, context::packets) << "diffing object out of order: " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl;
567          diffObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt);
568        }
569        else
570        {
571//           orxout(verbose, context::packets) << "copy object because of different data sizes (2): " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl;
572          copyObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt);
573          assert(sizesIt != this->sizes_.end() || origDataPtr==origDataEnd);
574        }
575      }
576      else
577      {
578//         orxout(verbose, context::packets) << "copy object: " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl;
579        assert(baseDataPtr == oldBaseDataPtr);
580        copyObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt);
581        assert(sizesIt != this->sizes_.end() || origDataPtr==origDataEnd);
582      }
583    }
584  }
585  assert(sizesIt==this->sizes_.end());
586
587
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());
592  g->flags_=flags_;
593  g->packetDirection_ = packetDirection_;
594  assert(!g->isCompressed());
595  return g;
596}
597
598
599/*Gamestate* Gamestate::diffData(Gamestate *base)
600{
601  assert(this && base); assert(data_ && base->data_);
602  assert(!header_.isCompressed() && !base->header_.isCompressed());
603  assert(!header_.isDiffed());
604
605  uint8_t *basep = GAMESTATE_START(base->data_);
606  uint8_t *gs = GAMESTATE_START(this->data_);
607  uint32_t dest_length = header_.getDataSize();
608
609  if(dest_length==0)
610    return NULL;
611
612  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];
613  uint8_t *dest = GAMESTATE_START(ndata);
614
615  rawDiff( dest, gs, basep, header_.getDataSize(), base->header_.getDataSize() );
616#ifndef NDEBUG
617  uint8_t *dest2 = new uint8_t[dest_length];
618  rawDiff( dest2, dest, basep, header_.getDataSize(), base->header_.getDataSize() );
619  assert( memcmp( dest2, gs, dest_length) == 0 );
620  delete dest2;
621#endif
622
623  Gamestate *g = new Gamestate(ndata, getClientID());
624  assert(g->header_);
625  *(g->header_) = *header_;
626  g->header_.setDiffed( true );
627  g->header_.setBaseID( base->getID() );
628  g->flags_=flags_;
629  g->packetDirection_ = packetDirection_;
630  assert(g->isDiffed());
631  assert(!g->isCompressed());
632  return g;
633}
634
635
636Gamestate* Gamestate::undiff(Gamestate *base)
637{
638  assert(this && base); assert(data_ && base->data_);
639  assert(!header_.isCompressed() && !base->header_.isCompressed());
640  assert(header_.isDiffed());
641
642  uint8_t *basep = GAMESTATE_START(base->data_);
643  uint8_t *gs = GAMESTATE_START(this->data_);
644  uint32_t dest_length = header_.getDataSize();
645
646  if(dest_length==0)
647    return NULL;
648
649  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];
650  uint8_t *dest = ndata + GamestateHeader::getSize();
651
652  rawDiff( dest, gs, basep, header_.getDataSize(), base->header_.getDataSize() );
653
654  Gamestate *g = new Gamestate(ndata, getClientID());
655  assert(g->header_);
656  *(g->header_) = *header_;
657  g->header_.setDiffed( false );
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;
671
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);
690}*/
691
692
693/*Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){
694  assert(data_);
695  std::list<obj>::iterator it;
696
697  // allocate memory for new data
698  uint8_t *gdata = new uint8_t[header_.getDataSize()+GamestateHeader::getSize()];
699  // create a gamestate out of it
700  Gamestate *gs = new Gamestate(gdata);
701  uint8_t *newdata = gdata + GamestateHeader::getSize();
702  uint8_t *origdata = GAMESTATE_START(data_);
703
704  //copy the GamestateHeader
705  assert(gs->header_);
706  *(gs->header_) = *header_;
707
708  uint32_t objectOffset;
709  unsigned int objectsize, destsize=0;
710  // TODO: Why is this variable not used?
711  //Synchronisable *object;
712
713  //call TrafficControl
714  TrafficControl::getInstance()->processObjectList( clientID, header_.getID(), dataVector_ );
715
716  //copy in the zeros
717//   std::list<obj>::iterator itt;
718//   orxout() << "myvector contains:";
719//   for ( itt=dataVector_.begin() ; itt!=dataVector_.end(); itt++ )
720//     orxout() << " " << (*itt).objID;
721//   orxout() << endl;
722  for(it=dataVector_.begin(); it!=dataVector_.end();){
723    SynchronisableHeader oldobjectheader(origdata);
724    SynchronisableHeader newobjectheader(newdata);
725    if ( (*it).objSize == 0 )
726    {
727      ++it;
728      continue;
729    }
730    objectsize = oldobjectheader.getDataSize()+SynchronisableHeader::getSize();
731    objectOffset=SynchronisableHeader::getSize(); //skip the size and the availableData variables in the objectheader
732    if ( (*it).objID == oldobjectheader.getObjectID() ){
733      memcpy(newdata, origdata, objectsize);
734      ++it;
735    }else{
736      newobjectheader = oldobjectheader;
737      memset(newdata+objectOffset, 0, objectsize-objectOffset);
738    }
739    newdata += objectsize;
740    origdata += objectsize;
741    destsize += objectsize;
742  }
743#ifndef NDEBUG
744  uint32_t origsize = destsize;
745  while ( origsize < header_.getDataSize() )
746  {
747    SynchronisableHeader oldobjectheader(origdata);
748    objectsize = oldobjectheader.getDataSize()+SynchronisableHeader::getSize();
749    origdata += objectsize;
750    origsize += objectsize;
751  }
752  assert(origsize==header_.getDataSize());
753  assert(destsize!=0);
754#endif
755  gs->header_.setDataSize( destsize );
756  return gs;
757}*/
758
759
760uint32_t Gamestate::calcGamestateSize(uint32_t id, uint8_t mode)
761{
762  uint32_t size = 0;
763  uint32_t nrOfVariables = 0;
764    // get the start of the Synchronisable list
765  ObjectList<Synchronisable>::iterator it;
766    // get total size of gamestate
767  for(it = ObjectList<Synchronisable>::begin(); it; ++it){
768    size+=it->getSize(id, mode); // size of the actual data of the synchronisable
769    nrOfVariables += it->getNrOfVariables();
770  }
771//   orxout() << "allocating " << nrOfVariables << " ints" << endl;
772  this->sizes_.reserve(nrOfVariables);
773  return size;
774}
775
776
777} //namespace packet
778} //namespace orxonox
Note: See TracBrowser for help on using the repository browser.