Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/network/packet/Gamestate.cc @ 2490

Last change on this file since 2490 was 2490, checked in by scheusso, 16 years ago

some cleanup and fix concerning trafficcontrol (cutting gamestates)

  • Property svn:eol-style set to native
File size: 22.0 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:
23 *      Oliver Scheuss, (C) 2008
24 *   Co-authors:
25 *      ...
26 *
27 */
28
[1701]29#include "Gamestate.h"
[2371]30#include "../GamestateHandler.h"
31#include "../synchronisable/Synchronisable.h"
32#include "../TrafficControl.h"
[2485]33#include "core/Core.h"
[1763]34#include "core/CoreIncludes.h"
[1751]35#include "core/Iterator.h"
[1701]36
37#include <zlib.h>
38#include <assert.h>
39
40
41
[2171]42namespace orxonox {
[1701]43
44namespace packet {
45
[1751]46#define GAMESTATE_START(data) (data + sizeof(GamestateHeader))
[1701]47#define GAMESTATE_HEADER(data) ((GamestateHeader *)data)
48#define HEADER GAMESTATE_HEADER(data_)
[1740]49
[2087]50
[1907]51#define PACKET_FLAG_GAMESTATE  ENET_PACKET_FLAG_RELIABLE
[2087]52
[1701]53Gamestate::Gamestate()
54{
[1907]55  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
[1701]56}
57
[1907]58Gamestate::Gamestate(uint8_t *data, unsigned int clientID):
[1711]59    Packet(data, clientID)
[1701]60{
[1907]61  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
[1701]62}
63
[1907]64Gamestate::Gamestate(uint8_t *data)
65{
66  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
67  data_=data;
68}
[1701]69
[1907]70
[1701]71Gamestate::~Gamestate()
72{
73}
74
[2171]75bool Gamestate::collectData(int id, uint8_t mode)
[1701]76{
[2371]77  uint32_t tempsize=0, currentsize=0;
[1751]78  assert(data_==0);
[2371]79  uint32_t size = calcGamestateSize(id, mode);
[1740]80
[1701]81  COUT(4) << "G.ST.Man: producing gamestate with id: " << id << std::endl;
82  if(size==0)
83    return false;
84  data_ = new unsigned char[size + sizeof(GamestateHeader)];
85  if(!data_){
86    COUT(2) << "GameStateManager: could not allocate memory" << std::endl;
87    return false;
88  }
[2087]89
[1701]90  //start collect data synchronisable by synchronisable
[1907]91  uint8_t *mem=data_;
[1701]92  mem+=sizeof(GamestateHeader);
[2171]93  ObjectList<Synchronisable>::iterator it;
94  for(it = ObjectList<Synchronisable>::begin(); it; ++it){
[1907]95    tempsize=it->getSize(id, mode);
[1740]96
[1701]97    if(currentsize+tempsize > size){
[2171]98      assert(0); // if we don't use multithreading this part shouldn't be neccessary
[1701]99      // start allocate additional memory
100      COUT(3) << "G.St.Man: need additional memory" << std::endl;
[2171]101      ObjectList<Synchronisable>::iterator temp = it;
[2490]102      uint32_t addsize=tempsize;
[1701]103      while(++temp)
[1907]104        addsize+=temp->getSize(id, mode);
105      data_ = (uint8_t *)realloc(data_, sizeof(GamestateHeader) + currentsize + addsize);
[1701]106      if(!data_)
107        return false;
108      size = currentsize+addsize;
109    }// stop allocate additional memory
110
[2087]111
[1907]112    //if(it->doSelection(id))
[2371]113    if(tempsize!=0)
114      dataMap_.push_back( obj(it->getObjectID(), it->getCreatorID(), tempsize, mem-data_) );
115//     dataMap_[mem-data_]=(*it);  // save the mem location of the synchronisable data
[1751]116    if(!it->getData(mem, id, mode))
[1701]117      return false; // mem pointer gets automatically increased because of call by reference
118    // increase size counter by size of current synchronisable
119    currentsize+=tempsize;
120  }
[1740]121
122
[1701]123  //start write gamestate header
[1710]124  HEADER->packetType = ENUM::Gamestate;
[1907]125  HEADER->datasize = currentsize;
[1701]126  HEADER->id = id;
127  HEADER->diffed = false;
128  HEADER->complete = true;
[1715]129  HEADER->compressed = false;
[1701]130  //stop write gamestate header
[1740]131
[1701]132  COUT(5) << "G.ST.Man: Gamestate size: " << currentsize << std::endl;
133  COUT(5) << "G.ST.Man: 'estimated' (and corrected) Gamestate size: " << size << std::endl;
134  return true;
135}
136
[2171]137bool Gamestate::spreadData(uint8_t mode)
[1701]138{
[1751]139  assert(data_);
140  assert(!HEADER->compressed);
141  assert(!HEADER->diffed);
[1907]142  uint8_t *mem=data_+sizeof(GamestateHeader);
[1701]143    // get the start of the Synchronisable list
[2171]144  //ObjectList<Synchronisable>::iterator it=ObjectList<Synchronisable>::begin();
[1907]145  Synchronisable *s;
[1740]146
[1907]147  // update the data of the objects we received
148  while(mem < data_+sizeof(GamestateHeader)+HEADER->datasize){
149    synchronisableHeader *objectheader = (synchronisableHeader*)mem;
[1701]150
[1907]151    s = Synchronisable::getSynchronisable( objectheader->objectID );
152    if(!s)
[1701]153    {
[2485]154      if (!Core::isMaster())
155        Synchronisable::fabricate(mem, mode);
156      else
157        mem += objectheader->size;
[2371]158//         COUT(0) << "could not fabricate synchronisable: " << objectheader->objectID << " classid: " << objectheader->classID << " creator: " << objectheader->creatorID << endl;
159//       else
160//         COUT(0) << "fabricated: " << objectheader->objectID << " classid: " << objectheader->classID << " creator: "  << objectheader->creatorID << endl;
[1701]161    }
[1907]162    else
163    {
164      bool b = s->updateData(mem, mode);
165      assert(b);
166    }
[1701]167  }
168
[2464]169   // In debug mode, check first, whether there are no duplicate objectIDs
170#ifndef NDEBUG
171  ObjectList<Synchronisable>::iterator it;
172  for (it = ObjectList<Synchronisable>::begin(); it != ObjectList<Synchronisable>::end(); ++it) {
173    if (it->getObjectID() == OBJECTID_UNKNOWN) {
174      if (it->objectMode_ != 0x0) {
175        COUT(0) << "Found object with OBJECTID_UNKNOWN on the client with objectMode != 0x0!" << std::endl;
[2485]176        COUT(0) << "Possible reason for this error: Client created a synchronized object without the Server's approval." << std::endl;
[2464]177        assert(false);
178      }
179    }
180    else {
181      ObjectList<Synchronisable>::iterator it2;
182      for (it2 = ObjectList<Synchronisable>::begin(); it2 != ObjectList<Synchronisable>::end(); ++it2) {
183        if (it->getObjectID() == it2->getObjectID() && *it != *it2) {
184           COUT(0) << "Found duplicate objectIDs on the client!" << std::endl
185                   << "Are you sure you don't create a Sychnronisable objcect with 'new' \
186                       that doesn't have objectMode = 0x0?" << std::endl;
187           assert(false);
188        }
189      }
190    }
191  }
192#endif
193
[1701]194  return true;
195}
196
[1907]197
198
[1705]199int Gamestate::getID(){
200  return HEADER->id;
201}
202
[2371]203uint32_t Gamestate::getSize() const
[1701]204{
[1711]205  assert(data_);
[1715]206  if(HEADER->compressed)
[1701]207    return HEADER->compsize+sizeof(GamestateHeader);
208  else
209  {
[1907]210    return HEADER->datasize+sizeof(GamestateHeader);
[1701]211  }
212}
213
[1751]214bool Gamestate::operator==(packet::Gamestate gs){
[1907]215  uint8_t *d1 = data_+sizeof(GamestateHeader);
216  uint8_t *d2 = gs.data_+sizeof(GamestateHeader);
[1751]217  assert(!isCompressed());
218  assert(!gs.isCompressed());
[1907]219  while(d1<data_+HEADER->datasize)
[1751]220  {
221    if(*d1!=*d2)
222      return false;
223    d1++;
224    d2++;
225  }
226  return true;
227}
228
[1701]229bool Gamestate::process()
230{
[1705]231  return GamestateHandler::addGamestate(this, getClientID());
[1701]232}
233
[1907]234
235
[1701]236bool Gamestate::compressData()
237{
238  assert(HEADER);
[1751]239  assert(!HEADER->compressed);
[1907]240  uLongf buffer = (uLongf)(((HEADER->datasize + 12)*1.01)+1);
[1701]241  if(buffer==0)
242    return false;
[1740]243
[1907]244  uint8_t *ndata = new uint8_t[buffer+sizeof(GamestateHeader)];
245  uint8_t *dest = GAMESTATE_START(ndata);
[1751]246  //unsigned char *dest = new unsigned char[buffer];
[1907]247  uint8_t *source = GAMESTATE_START(data_);
[1701]248  int retval;
[1907]249  retval = compress( dest, &buffer, source, (uLong)(HEADER->datasize) );
[1701]250  switch ( retval ) {
251    case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break;
[1751]252    case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl; return false;
253    case Z_BUF_ERROR: COUT(2) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << std::endl; return false;
254    case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl; return false;
[1701]255  }
[1751]256#ifndef NDEBUG
257  //decompress and compare the start and the decompressed data
[1907]258  uint8_t *rdata = new uint8_t[HEADER->datasize+sizeof(GamestateHeader)];
259  uint8_t *d2 = GAMESTATE_START(rdata);
260  uLongf length2 = HEADER->datasize;
[1751]261  uncompress(d2, &length2, dest, buffer);
[1907]262  for(unsigned int i=0; i<HEADER->datasize; i++){
[1751]263    assert(*(source+i)==*(d2+i));
264  }
265  delete[] rdata;
266#endif
[1701]267
268  //copy and modify header
[1751]269#ifndef NDEBUG
[1907]270  HEADER->crc32 = calcCRC(data_+sizeof(GamestateHeader), HEADER->datasize);
[1751]271#endif
[1701]272  *GAMESTATE_HEADER(ndata) = *HEADER;
273  //delete old data
274  delete[] data_;
275  //save new data
276  data_ = ndata;
[1751]277  HEADER->compsize = buffer;
278  HEADER->compressed = true;
[1730]279  assert(HEADER->compressed);
[2087]280  COUT(4) << "gamestate compress datasize: " << HEADER->datasize << " compsize: " << HEADER->compsize << std::endl;
[1701]281  return true;
282}
283bool Gamestate::decompressData()
284{
[1751]285  assert(HEADER);
[1715]286  assert(HEADER->compressed);
[2087]287  COUT(4) << "GameStateClient: uncompressing gamestate. id: " << HEADER->id << ", baseid: " << HEADER->base_id << ", datasize: " << HEADER->datasize << ", compsize: " << HEADER->compsize << std::endl;
[2371]288  uint32_t datasize = HEADER->datasize;
289  uint32_t compsize = HEADER->compsize;
290  uint32_t bufsize;
[2087]291//  assert(compsize<=datasize);
[1907]292  bufsize = datasize;
[1751]293  assert(bufsize!=0);
[1907]294  uint8_t *ndata = new uint8_t[bufsize + sizeof(GamestateHeader)];
295  uint8_t *dest = ndata + sizeof(GamestateHeader);
296  uint8_t *source = data_ + sizeof(GamestateHeader);
[1701]297  int retval;
[1751]298  uLongf length=bufsize;
299  retval = uncompress( dest, &length, source, (uLong)compsize );
[1701]300  switch ( retval ) {
301    case Z_OK: COUT(5) << "successfully decompressed" << std::endl; break;
302    case Z_MEM_ERROR: COUT(1) << "not enough memory available" << std::endl; return false;
303    case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer" << std::endl; return false;
304    case Z_DATA_ERROR: COUT(2) << "data corrupted (zlib)" << std::endl; return false;
305  }
[1751]306#ifndef NDEBUG
[1907]307  assert(HEADER->crc32==calcCRC(ndata+sizeof(GamestateHeader), HEADER->datasize));
[1751]308#endif
[1752]309
[1701]310  //copy over the header
311  *GAMESTATE_HEADER(ndata) = *HEADER;
[2087]312
313  if (this->bDataENetAllocated_){
314    // Memory was allocated by ENet. --> We let it be since enet_packet_destroy will
315    // deallocated it anyway. So data and packet stay together.
316    this->bDataENetAllocated_ = false;
317  }
318  else{
319    // We allocated the memory in the first place (unlikely). So we destroy the old data
320    // and overwrite it with the new decompressed data.
321    delete[] this->data_;
322  }
323
[1751]324  //set new pointers
[1701]325  data_ = ndata;
[1751]326  HEADER->compressed = false;
[1907]327  assert(HEADER->datasize==datasize);
[1751]328  assert(HEADER->compsize==compsize);
[1701]329  return true;
330}
331
332Gamestate *Gamestate::diff(Gamestate *base)
333{
[1751]334  assert(HEADER);
335  assert(!HEADER->compressed);
336  assert(!HEADER->diffed);
[1701]337  //unsigned char *basep = base->getGs()/*, *gs = getGs()*/;
[1907]338  uint8_t *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_);
[2371]339  uint32_t of=0; // pointers offset
340  uint32_t dest_length=0;
[1907]341  dest_length=HEADER->datasize;
[1701]342  if(dest_length==0)
343    return NULL;
[1907]344  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+sizeof(GamestateHeader)];
345  uint8_t *dest = ndata + sizeof(GamestateHeader);
346  while(of < GAMESTATE_HEADER(base->data_)->datasize && of < HEADER->datasize){
[1701]347    *(dest+of)=*(basep+of)^*(gs+of); // do the xor
348    ++of;
349  }
[1907]350  if(GAMESTATE_HEADER(base->data_)->datasize!=HEADER->datasize){
351    uint8_t n=0;
352    if(GAMESTATE_HEADER(base->data_)->datasize < HEADER->datasize){
[1701]353      while(of<dest_length){
354        *(dest+of)=n^*(gs+of);
355        of++;
356      }
357    }
358  }
359
[1715]360  *GAMESTATE_HEADER(ndata) = *HEADER;
361  GAMESTATE_HEADER(ndata)->diffed = true;
[1751]362  GAMESTATE_HEADER(ndata)->base_id = base->getID();
363  Gamestate *g = new Gamestate(ndata, getClientID());
364  g->flags_=flags_;
365  g->packetDirection_ = packetDirection_;
[1701]366  return g;
367}
368
[2371]369Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){
[1907]370  assert(data_);
[2371]371  std::list<obj>::iterator it;
[2087]372
[1907]373  // allocate memory for new data
374  uint8_t *gdata = new uint8_t[HEADER->datasize+sizeof(GamestateHeader)];
375  // create a gamestate out of it
376  Gamestate *gs = new Gamestate(gdata);
377  uint8_t *newdata = gdata + sizeof(GamestateHeader);
[2417]378  uint8_t *origdata = GAMESTATE_START(data_);
[2087]379
[1907]380  //copy the GamestateHeader
381  *(GamestateHeader*)gdata = *HEADER;
[2087]382
[1907]383  synchronisableHeader *oldobjectheader, *newobjectheader;
[2371]384  uint32_t objectOffset;
385  unsigned int objectsize, destsize=0;
[2435]386  // TODO: Why is this variable not used?
387  //Synchronisable *object;
[2087]388
[2371]389  //call TrafficControl
[2382]390  TrafficControl::getInstance()->processObjectList( clientID, HEADER->id, &dataMap_ );
[2485]391
[1907]392  //copy in the zeros
[2419]393  for(it=dataMap_.begin(); it!=dataMap_.end();){
[2459]394//    if((*it).objSize==0)
395//      continue;
[2485]396//    if(it->second->getSize(HEADER->id)==0) // merged from objecthierarchy2, doesn't work anymore; TODO: change this
397//      continue;                            // merged from objecthierarchy2, doesn't work anymore; TODO: change this
[2417]398    oldobjectheader = (synchronisableHeader*)origdata;
[1907]399    newobjectheader = (synchronisableHeader*)newdata;
[2476]400    if ( (*it).objSize == 0 )
[2490]401    {
402      ++it;
[2476]403      continue;
[2490]404    }
[2417]405//     object = Synchronisable::getSynchronisable( (*it).objID );
406//     assert(object->objectID == oldobjectheader->objectID);
[2371]407    objectsize = oldobjectheader->size;
[2171]408    objectOffset=sizeof(synchronisableHeader); //skip the size and the availableData variables in the objectheader
[2417]409    if ( (*it).objID == oldobjectheader->objectID ){
410      memcpy(newdata, origdata, objectsize);
[2171]411      assert(newobjectheader->dataAvailable==true);
[2419]412      ++it;
[1907]413    }else{
[2417]414      *newobjectheader = *oldobjectheader;
[1907]415      newobjectheader->dataAvailable=false;
[2171]416      memset(newdata+objectOffset, 0, objectsize-objectOffset);
[1907]417    }
418    newdata += objectsize;
[2417]419    origdata += objectsize;
[2371]420    destsize += objectsize;
421//     origdata += objectsize;
[1907]422  }
[2490]423#ifndef NDEBUG
424  uint32_t origsize = destsize;
425  while ( origsize < HEADER->datasize )
426  {
427    oldobjectheader = (synchronisableHeader*)origdata;
428    objectsize = oldobjectheader->size;
429    origdata += objectsize;
430    origsize += objectsize;
431  }
432  assert(origsize==HEADER->datasize);
433  assert(destsize!=0);
434#endif
[2371]435  ((GamestateHeader*)gdata)->datasize = destsize;
[1907]436  return gs;
437}
438
439
440Gamestate* Gamestate::intelligentDiff(Gamestate *base, unsigned int clientID){
441  // asserts
442  assert(data_);
443  assert(base->data_);
444  assert(!GAMESTATE_HEADER(base->data_)->diffed);
445  assert(!GAMESTATE_HEADER(base->data_)->compressed);
446  assert(!HEADER->compressed);
447  assert(!HEADER->diffed);
[2087]448
[1907]449  //preparations
[2371]450  std::list<obj>::iterator it;
[1907]451  uint8_t *origdata, *basedata, *destdata, *ndata;
[2371]452  uint32_t objectOffset, streamOffset=0;    //data offset
453  uint32_t minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize;
[1907]454  synchronisableHeader *origheader;
455  synchronisableHeader *destheader;
[2371]456  Synchronisable *object;
[2087]457
[1907]458  origdata = GAMESTATE_START(this->data_);
459  basedata = GAMESTATE_START(base->data_);
460  ndata = new uint8_t[HEADER->datasize + sizeof(GamestateHeader)];
461  destdata = ndata + sizeof(GamestateHeader);
[2087]462
[1907]463  // do the diff
464  for(it=dataMap_.begin(); it!=dataMap_.end(); it++){
465    assert(streamOffset<HEADER->datasize);
466    origheader = (synchronisableHeader *)(origdata+streamOffset);
467    destheader = (synchronisableHeader *)(destdata+streamOffset);
[2371]468    object = Synchronisable::getSynchronisable(origheader->objectID);
469    bool sendData = object->doSelection(HEADER->id);
[2087]470
[1907]471    //copy and partially diff the object header
[2371]472    assert(sizeof(synchronisableHeader)==3*sizeof(uint32_t)+sizeof(bool));
[1907]473    *(uint32_t*)destdata = *(uint32_t*)origdata; //size (do not diff)
474    *(bool*)(destdata+sizeof(uint32_t)) = sendData;
475    if(sendData){
476      *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = *(uint32_t*)(basedata+sizeof(uint32_t)+sizeof(bool)) ^ *(uint32_t*)(origdata+sizeof(uint32_t)+sizeof(bool)); //objectid (diff it)
477      *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = *(uint32_t*)(basedata+2*sizeof(uint32_t)+sizeof(bool)) ^ *(uint32_t*)(origdata+2*sizeof(uint32_t)+sizeof(bool)); //classid (diff it)
478    }else{
479      *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = 0;
[2087]480      *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = 0;
[1907]481    }
482    objectOffset=sizeof(synchronisableHeader);
483    streamOffset+=sizeof(synchronisableHeader);
[2087]484
[1907]485    //now handle the object data or fill with zeros
486    while(objectOffset<origheader->size ){
[2087]487
[1907]488      if(sendData && streamOffset<minsize)
489        *(destdata+objectOffset)=*(basedata+objectOffset)^*(origdata+objectOffset); // do the xor
490      else if(sendData)
491        *(destdata+objectOffset)=((uint8_t)0)^*(origdata+objectOffset); // xor with 0 (basestream is too short)
492      else
493        *(destdata+objectOffset)=0; // set to 0 because this object should not be transfered
[2087]494
[1907]495      objectOffset++;
496      streamOffset++;
497    }
498    destdata+=objectOffset;
499    origdata+=objectOffset;
500    basedata+=objectOffset;
501  }
[2087]502
[1907]503  //copy over the gamestate header and set the diffed flag
504  *(GamestateHeader *)ndata = *HEADER; //copy over the header
505  Gamestate *gs = new Gamestate(ndata);
506  GAMESTATE_HEADER(ndata)->diffed=true;
507  return gs;
508}
509
510Gamestate* Gamestate::intelligentUnDiff(Gamestate *base){
511  // asserts
512  assert(data_);
513  assert(base->data_);
514  assert(!GAMESTATE_HEADER(base->data_)->diffed);
515  assert(!GAMESTATE_HEADER(base->data_)->compressed);
516  assert(!HEADER->compressed);
517  assert(HEADER->diffed);
[2087]518
[1907]519  //preparations
[2371]520  std::list<obj>::iterator it;
[1907]521  uint8_t *origdata, *basedata, *destdata, *ndata;
[2371]522  uint32_t objectOffset, streamOffset=0;    //data offset
523  uint32_t minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize;
[1907]524  synchronisableHeader *origheader;
525  synchronisableHeader *destheader;
[2371]526  Synchronisable *object;
[2087]527
[1907]528  origdata = GAMESTATE_START(this->data_);
529  basedata = GAMESTATE_START(base->data_);
530  ndata = new uint8_t[HEADER->datasize + sizeof(GamestateHeader)];
531  destdata = ndata + sizeof(GamestateHeader);
[2087]532
[1907]533  // do the undiff
534  for(it=dataMap_.begin(); it!=dataMap_.end(); it++){
535    assert(streamOffset<HEADER->datasize);
536    origheader = (synchronisableHeader *)(origdata+streamOffset);
537    destheader = (synchronisableHeader *)(destdata+streamOffset);
[2371]538    object = Synchronisable::getSynchronisable( origheader->objectID );
[1907]539    bool sendData;
[2087]540
[1907]541    //copy and partially diff the object header
[2371]542    assert(sizeof(synchronisableHeader)==3*sizeof(uint32_t)+sizeof(bool));
543    *(uint32_t*)destdata = *(uint32_t*)origdata; //size (do not diff)
544    *(bool*)(destdata+sizeof(uint32_t)) = *(bool*)(origdata+sizeof(uint32_t));
545    sendData = *(bool*)(origdata+sizeof(uint32_t));
[1907]546    if(sendData){
[2371]547      *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = *(uint32_t*)(basedata+sizeof(uint32_t)+sizeof(bool)) ^ *(uint32_t*)(origdata+sizeof(uint32_t)+sizeof(bool)); //objectid (diff it)
548      *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = *(uint32_t*)(basedata+2*sizeof(uint32_t)+sizeof(bool)) ^ *(uint32_t*)(origdata+2*sizeof(uint32_t)+sizeof(bool)); //classid (diff it)
[1907]549    }else{
[2371]550      *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = 0;
551      *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = 0;
[1907]552    }
553    objectOffset=sizeof(synchronisableHeader);
554    streamOffset+=sizeof(synchronisableHeader);
[2087]555
[1907]556    //now handle the object data or fill with zeros
557    while(objectOffset<origheader->size ){
[2087]558
[1907]559      if(sendData && streamOffset<minsize)
560        *(destdata+objectOffset)=*(basedata+objectOffset)^*(origdata+objectOffset); // do the xor
561      else if(sendData)
562        *(destdata+objectOffset)=((unsigned char)0)^*(origdata+objectOffset); // xor with 0 (basestream is too short)
563      else
564        *(destdata+objectOffset)=0; // set to 0 because this object should not be transfered
[2087]565
[1907]566      objectOffset++;
567      streamOffset++;
568    }
569    destdata+=objectOffset;
570    origdata+=objectOffset;
571    basedata+=objectOffset;
572  }
[2087]573
[1907]574  //copy over the gamestate header and set the diffed flag
575  *(GamestateHeader *)ndata = *HEADER; //copy over the header
576  Gamestate *gs = new Gamestate(ndata);
577  GAMESTATE_HEADER(ndata)->diffed=false;
578  return gs;
579}
580
[1701]581Gamestate *Gamestate::undiff(Gamestate *base)
582{
[1751]583  assert(this && base);assert(HEADER);
584  assert(HEADER->diffed);
[1715]585  assert(!HEADER->compressed && !GAMESTATE_HEADER(base->data_)->compressed);
[1701]586  //unsigned char *basep = base->getGs()/*, *gs = getGs()*/;
[1907]587  uint8_t *basep = GAMESTATE_START(base->data_);
588  uint8_t *gs = GAMESTATE_START(this->data_);
[2371]589  uint32_t of=0; // pointers offset
590  uint32_t dest_length=0;
[1907]591  dest_length=HEADER->datasize;
[1701]592  if(dest_length==0)
593    return NULL;
[1907]594  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+sizeof(GamestateHeader)];
595  uint8_t *dest = ndata + sizeof(GamestateHeader);
596  while(of < GAMESTATE_HEADER(base->data_)->datasize && of < HEADER->datasize){
[1701]597    *(dest+of)=*(basep+of)^*(gs+of); // do the xor
598    ++of;
599  }
[1907]600  if(GAMESTATE_HEADER(base->data_)->datasize!=HEADER->datasize){
601    uint8_t n=0;
602    if(GAMESTATE_HEADER(base->data_)->datasize < HEADER->datasize){
[1701]603      while(of < dest_length){
604        *(dest+of)=n^*(gs+of);
605        of++;
606      }
607    }
608  }
[1715]609  *GAMESTATE_HEADER(ndata) = *HEADER;
610  GAMESTATE_HEADER(ndata)->diffed = false;
[1751]611  Gamestate *g = new Gamestate(ndata, getClientID());
612  g->flags_=flags_;
613  g->packetDirection_ = packetDirection_;
614  assert(!g->isDiffed());
615  assert(!g->isCompressed());
[1701]616  return g;
617}
618
619
[2371]620uint32_t Gamestate::calcGamestateSize(int32_t id, uint8_t mode)
[1701]621{
[2371]622  uint32_t size=0;
[1701]623    // get the start of the Synchronisable list
[2171]624  ObjectList<Synchronisable>::iterator it;
[1701]625    // get total size of gamestate
[2171]626  for(it = ObjectList<Synchronisable>::begin(); it; ++it)
[1907]627    size+=it->getSize(id, mode); // size of the actual data of the synchronisable
[1701]628//  size+=sizeof(GamestateHeader);
629  return size;
630}
631
632/**
633 * This function removes a Synchronisable out of the universe
634 * @param it iterator of the list pointing to the object
635 * @return iterator pointing to the next object in the list
636 */
[2171]637  void Gamestate::removeObject(ObjectList<Synchronisable>::iterator &it) {
638    ObjectList<Synchronisable>::iterator temp=it;
[1701]639    ++it;
640    delete  *temp;
641  }
642
[1712]643  bool Gamestate::isDiffed(){
644    return HEADER->diffed;
645  }
[1740]646
[1751]647  bool Gamestate::isCompressed(){
648    return HEADER->compressed;
649  }
[1752]650
[1712]651  int Gamestate::getBaseID(){
652    return HEADER->base_id;
653  }
[1701]654}
655
656}
Note: See TracBrowser for help on using the repository browser.