Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/hudimprovements/src/libraries/network/packet/Gamestate.cc @ 7919

Last change on this file since 7919 was 7844, checked in by landauf, 14 years ago

fixed warning. hope this works on all supported systems, because (u)intptr_t is actually C99 standard, not C++

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