Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2677 was 2662, checked in by rgrieder, 16 years ago

Merged presentation branch back to trunk.

  • Property svn:eol-style set to native
File size: 15.7 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"
[2662]30#include "../GamestateHandler.h"
31#include "../synchronisable/Synchronisable.h"
32#include "../TrafficControl.h"
33#include "core/Core.h"
[1763]34#include "core/CoreIncludes.h"
[1751]35#include "core/Iterator.h"
[1701]36
37#include <zlib.h>
[2662]38#include <cassert>
[1701]39
40
41
[2171]42namespace orxonox {
[1701]43
44namespace packet {
45
[2662]46#define GAMESTATE_START(data) (data + GamestateHeader::getSize())
[1740]47
[1907]48#define PACKET_FLAG_GAMESTATE  ENET_PACKET_FLAG_RELIABLE
[2087]49
[2662]50// Gamestate::Gamestate()
51// {
52//   flags_ = flags_ | PACKET_FLAG_GAMESTATE;
53// }
54
[1701]55Gamestate::Gamestate()
56{
[1907]57  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
[2662]58  header_ = 0;
[1701]59}
60
[1907]61Gamestate::Gamestate(uint8_t *data, unsigned int clientID):
[1711]62    Packet(data, clientID)
[1701]63{
[1907]64  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
[2662]65  header_ = new GamestateHeader(data_);
[1701]66}
67
[1907]68Gamestate::Gamestate(uint8_t *data)
69{
70  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
71  data_=data;
[2662]72  header_ = new GamestateHeader(data_);
[1907]73}
[1701]74
[2662]75Gamestate::Gamestate(const Gamestate& g) :
76    Packet( *(Packet*)&g )
77{
78  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
79  header_ = new GamestateHeader(data_);
80}
[1907]81
[2662]82
[1701]83Gamestate::~Gamestate()
84{
85}
86
[2171]87bool Gamestate::collectData(int id, uint8_t mode)
[1701]88{
[2662]89  assert(this->header_==0); // make sure the header didn't exist before
90  uint32_t tempsize=0, currentsize=0;
[1751]91  assert(data_==0);
[2662]92  uint32_t size = calcGamestateSize(id, mode);
[1740]93
[1701]94  COUT(4) << "G.ST.Man: producing gamestate with id: " << id << std::endl;
95  if(size==0)
96    return false;
[2662]97  data_ = new uint8_t[size + GamestateHeader::getSize()];
[1701]98  if(!data_){
99    COUT(2) << "GameStateManager: could not allocate memory" << std::endl;
100    return false;
101  }
[2662]102 
103  // create the header object
104  header_ = new GamestateHeader(data_);
[2087]105
[1701]106  //start collect data synchronisable by synchronisable
[1907]107  uint8_t *mem=data_;
[2662]108  mem += GamestateHeader::getSize();
[2171]109  ObjectList<Synchronisable>::iterator it;
110  for(it = ObjectList<Synchronisable>::begin(); it; ++it){
[2662]111   
112#ifndef NDEBUG
[1907]113    tempsize=it->getSize(id, mode);
[1701]114    if(currentsize+tempsize > size){
[2171]115      assert(0); // if we don't use multithreading this part shouldn't be neccessary
[1701]116      // start allocate additional memory
117      COUT(3) << "G.St.Man: need additional memory" << std::endl;
[2171]118      ObjectList<Synchronisable>::iterator temp = it;
[2662]119      uint32_t addsize=tempsize;
[1701]120      while(++temp)
[1907]121        addsize+=temp->getSize(id, mode);
[2662]122      data_ = (uint8_t *)realloc(data_, GamestateHeader::getSize() + currentsize + addsize);
[1701]123      if(!data_)
124        return false;
125      size = currentsize+addsize;
126    }// stop allocate additional memory
[2662]127#endif
[1701]128
[1907]129    //if(it->doSelection(id))
[2662]130    if ( it->doSync( id, mode ) )
131      dataMap_.push_back( obj(it->getObjectID(), it->getCreatorID(), tempsize, mem-data_) );
132//     dataMap_[mem-data_]=(*it);  // save the mem location of the synchronisable data
[1751]133    if(!it->getData(mem, id, mode))
[1701]134      return false; // mem pointer gets automatically increased because of call by reference
135    // increase size counter by size of current synchronisable
136    currentsize+=tempsize;
137  }
[1740]138
139
[1701]140  //start write gamestate header
[2662]141  header_->setDataSize( currentsize );
142  header_->setID( id );
143  header_->setDiffed( false );
144  header_->setComplete( true );
145  header_->setCompressed( false );
[1701]146  //stop write gamestate header
[1740]147
[1701]148  COUT(5) << "G.ST.Man: Gamestate size: " << currentsize << std::endl;
149  COUT(5) << "G.ST.Man: 'estimated' (and corrected) Gamestate size: " << size << std::endl;
150  return true;
151}
152
[2171]153bool Gamestate::spreadData(uint8_t mode)
[1701]154{
[2662]155  COUT(4) << "processing gamestate with id " << header_->getID() << endl;
[1751]156  assert(data_);
[2662]157  assert(!header_->isCompressed());
158  assert(!header_->isDiffed());
159  uint8_t *mem=data_+GamestateHeader::getSize();
[1701]160    // get the start of the Synchronisable list
[2171]161  //ObjectList<Synchronisable>::iterator it=ObjectList<Synchronisable>::begin();
[1907]162  Synchronisable *s;
[1740]163
[1907]164  // update the data of the objects we received
[2662]165  while(mem < data_+GamestateHeader::getSize()+header_->getDataSize()){
166    SynchronisableHeader objectheader(mem);
[1701]167
[2662]168    s = Synchronisable::getSynchronisable( objectheader.getObjectID() );
[1907]169    if(!s)
[1701]170    {
[2662]171      if (!Core::isMaster())
172      {
173        Synchronisable::fabricate(mem, mode);
174      }
175      else
176      {
177        mem += objectheader.getDataSize();
178      }
179//         COUT(0) << "could not fabricate synchronisable: " << objectheader->objectID << " classid: " << objectheader->classID << " creator: " << objectheader->creatorID << endl;
180//       else
181//         COUT(0) << "fabricated: " << objectheader->objectID << " classid: " << objectheader->classID << " creator: "  << objectheader->creatorID << endl;
[1701]182    }
[1907]183    else
184    {
185      bool b = s->updateData(mem, mode);
186      assert(b);
187    }
[1701]188  }
189
[2662]190   // In debug mode, check first, whether there are no duplicate objectIDs
191#ifndef NDEBUG
192  ObjectList<Synchronisable>::iterator it;
193  for (it = ObjectList<Synchronisable>::begin(); it != ObjectList<Synchronisable>::end(); ++it) {
194    if (it->getObjectID() == OBJECTID_UNKNOWN) {
195      if (it->objectMode_ != 0x0) {
196        COUT(0) << "Found object with OBJECTID_UNKNOWN on the client with objectMode != 0x0!" << std::endl;
197        COUT(0) << "Possible reason for this error: Client created a synchronized object without the Server's approval." << std::endl;
198        COUT(0) << "Objects class: " << it->getIdentifier()->getName() << std::endl;
199        assert(false);
200      }
201    }
202    else {
203      ObjectList<Synchronisable>::iterator it2;
204      for (it2 = ObjectList<Synchronisable>::begin(); it2 != ObjectList<Synchronisable>::end(); ++it2) {
205        if (it->getObjectID() == it2->getObjectID() && *it != *it2) {
206           COUT(0) << "Found duplicate objectIDs on the client!" << std::endl
207                   << "Are you sure you don't create a Sychnronisable objcect with 'new' \
208                       that doesn't have objectMode = 0x0?" << std::endl;
209           assert(false);
210        }
211      }
212    }
213  }
214#endif
215
[1701]216  return true;
217}
218
[2662]219uint32_t Gamestate::getSize() const
[1701]220{
[1711]221  assert(data_);
[2662]222  if(header_->isCompressed())
223    return header_->getCompSize()+GamestateHeader::getSize();
[1701]224  else
225  {
[2662]226    return header_->getDataSize()+GamestateHeader::getSize();
[1701]227  }
228}
229
[1751]230bool Gamestate::operator==(packet::Gamestate gs){
[2662]231  uint8_t *d1 = data_+GamestateHeader::getSize();
232  uint8_t *d2 = gs.data_+GamestateHeader::getSize();
[1751]233  assert(!isCompressed());
234  assert(!gs.isCompressed());
[2662]235  while(d1<data_+header_->getDataSize())
[1751]236  {
237    if(*d1!=*d2)
238      return false;
239    d1++;
240    d2++;
241  }
242  return true;
243}
244
[1701]245bool Gamestate::process()
246{
[1705]247  return GamestateHandler::addGamestate(this, getClientID());
[1701]248}
249
[1907]250
251
[1701]252bool Gamestate::compressData()
253{
[2662]254  assert(data_);
255  assert(!header_->isCompressed());
256  uLongf buffer = (uLongf)(((header_->getDataSize() + 12)*1.01)+1);
[1701]257  if(buffer==0)
258    return false;
[1740]259
[2662]260  uint8_t *ndata = new uint8_t[buffer+GamestateHeader::getSize()];
261  uint8_t *dest = ndata + GamestateHeader::getSize();
[1751]262  //unsigned char *dest = new unsigned char[buffer];
[2662]263  uint8_t *source = data_ + GamestateHeader::getSize();
[1701]264  int retval;
[2662]265  retval = compress( dest, &buffer, source, (uLong)(header_->getDataSize()) );
[1701]266  switch ( retval ) {
267    case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break;
[1751]268    case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl; return false;
269    case Z_BUF_ERROR: COUT(2) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << std::endl; return false;
270    case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl; return false;
[1701]271  }
272
273  //copy and modify header
[2662]274  GamestateHeader *temp = header_;
275  header_ = new GamestateHeader(ndata, temp);
276  delete temp;
[1701]277  //delete old data
278  delete[] data_;
279  //save new data
280  data_ = ndata;
[2662]281  header_->setCompSize( buffer );
282  header_->setCompressed( true );
283  COUT(5) << "gamestate compress datasize: " << header_->getDataSize() << " compsize: " << header_->getCompSize() << std::endl;
[1701]284  return true;
285}
286bool Gamestate::decompressData()
287{
[2662]288  assert(data_);
289  assert(header_->isCompressed());
290  COUT(4) << "GameStateClient: uncompressing gamestate. id: " << header_->getID() << ", baseid: " << header_->getBaseID() << ", datasize: " << header_->getDataSize() << ", compsize: " << header_->getCompSize() << std::endl;
291  uint32_t datasize = header_->getDataSize();
292  uint32_t compsize = header_->getCompSize();
293  uint32_t bufsize;
[2087]294//  assert(compsize<=datasize);
[1907]295  bufsize = datasize;
[1751]296  assert(bufsize!=0);
[2662]297  uint8_t *ndata = new uint8_t[bufsize + GamestateHeader::getSize()];
298  uint8_t *dest = ndata + GamestateHeader::getSize();
299  uint8_t *source = data_ + GamestateHeader::getSize();
[1701]300  int retval;
[1751]301  uLongf length=bufsize;
302  retval = uncompress( dest, &length, source, (uLong)compsize );
[1701]303  switch ( retval ) {
304    case Z_OK: COUT(5) << "successfully decompressed" << std::endl; break;
305    case Z_MEM_ERROR: COUT(1) << "not enough memory available" << std::endl; return false;
306    case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer" << std::endl; return false;
307    case Z_DATA_ERROR: COUT(2) << "data corrupted (zlib)" << std::endl; return false;
308  }
[1752]309
[1701]310  //copy over the header
[2662]311  GamestateHeader *temp = header_;
312  header_ = new GamestateHeader( data_, header_ );
313  delete temp;
[2087]314
315  if (this->bDataENetAllocated_){
316    // Memory was allocated by ENet. --> We let it be since enet_packet_destroy will
317    // deallocated it anyway. So data and packet stay together.
318    this->bDataENetAllocated_ = false;
319  }
320  else{
321    // We allocated the memory in the first place (unlikely). So we destroy the old data
322    // and overwrite it with the new decompressed data.
323    delete[] this->data_;
324  }
325
[1751]326  //set new pointers
[1701]327  data_ = ndata;
[2662]328  header_->setCompressed( false );
329  assert(header_->getDataSize()==datasize);
330  assert(header_->getCompSize()==compsize);
[1701]331  return true;
332}
333
334Gamestate *Gamestate::diff(Gamestate *base)
335{
[2662]336  assert(data_);
337  assert(!header_->isCompressed());
338  assert(!header_->isDiffed());
339  GamestateHeader diffHeader(base->data_);
[1701]340  //unsigned char *basep = base->getGs()/*, *gs = getGs()*/;
[1907]341  uint8_t *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_);
[2662]342  uint32_t of=0; // pointers offset
343  uint32_t dest_length=0;
344  dest_length=header_->getDataSize();
[1701]345  if(dest_length==0)
346    return NULL;
[2662]347  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];
348  uint8_t *dest = ndata + GamestateHeader::getSize();
349  while(of < diffHeader.getDataSize() && of < header_->getDataSize()){
[1701]350    *(dest+of)=*(basep+of)^*(gs+of); // do the xor
351    ++of;
352  }
[2662]353  if(diffHeader.getDataSize()!=header_->getDataSize()){
[1907]354    uint8_t n=0;
[2662]355    if(diffHeader.getDataSize() < header_->getDataSize()){
[1701]356      while(of<dest_length){
357        *(dest+of)=n^*(gs+of);
358        of++;
359      }
360    }
361  }
362
[1751]363  Gamestate *g = new Gamestate(ndata, getClientID());
[2662]364  *(g->header_) = *header_;
365  g->header_->setDiffed( true );
366  g->header_->setBaseID( base->getID() );
[1751]367  g->flags_=flags_;
368  g->packetDirection_ = packetDirection_;
[1701]369  return g;
370}
371
[2662]372Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){
[1907]373  assert(data_);
[2662]374  std::list<obj>::iterator it;
[2087]375
[1907]376  // allocate memory for new data
[2662]377  uint8_t *gdata = new uint8_t[header_->getDataSize()+GamestateHeader::getSize()];
[1907]378  // create a gamestate out of it
379  Gamestate *gs = new Gamestate(gdata);
[2662]380  uint8_t *newdata = gdata + GamestateHeader::getSize();
[1907]381  uint8_t *origdata = GAMESTATE_START(data_);
[2087]382
[1907]383  //copy the GamestateHeader
[2662]384  assert(gs->header_);
385  *(gs->header_) = *header_;
[2087]386
[2662]387  uint32_t objectOffset;
388  unsigned int objectsize, destsize=0;
389  // TODO: Why is this variable not used?
390  //Synchronisable *object;
[2087]391
[2662]392  //call TrafficControl
393  TrafficControl::getInstance()->processObjectList( clientID, header_->getID(), &dataMap_ );
394
[1907]395  //copy in the zeros
[2662]396  for(it=dataMap_.begin(); it!=dataMap_.end();){
397//    if((*it).objSize==0)
398//      continue;
399//    if(it->second->getSize(HEADER->id)==0) // merged from objecthierarchy2, doesn't work anymore; TODO: change this
400//      continue;                            // merged from objecthierarchy2, doesn't work anymore; TODO: change this
401    SynchronisableHeader oldobjectheader(origdata);
402    SynchronisableHeader newobjectheader(newdata);
403    if ( (*it).objSize == 0 )
404    {
405      ++it;
406      continue;
407    }
408//     object = Synchronisable::getSynchronisable( (*it).objID );
409//     assert(object->objectID == oldobjectheader->objectID);
410    objectsize = oldobjectheader.getDataSize();
411    objectOffset=SynchronisableHeader::getSize(); //skip the size and the availableData variables in the objectheader
412    if ( (*it).objID == oldobjectheader.getObjectID() ){
413      memcpy(newdata, origdata, objectsize);
414      assert(newobjectheader.isDataAvailable()==true);
415      ++it;
[1907]416    }else{
[2662]417      newobjectheader = oldobjectheader;
418      newobjectheader.setDataAvailable(false);
[2171]419      memset(newdata+objectOffset, 0, objectsize-objectOffset);
[1907]420    }
421    newdata += objectsize;
422    origdata += objectsize;
[2662]423    destsize += objectsize;
[1907]424  }
[2662]425#ifndef NDEBUG
426  uint32_t origsize = destsize;
427  while ( origsize < header_->getDataSize() )
428  {
429    SynchronisableHeader oldobjectheader(origdata);
430    objectsize = oldobjectheader.getDataSize();
431    origdata += objectsize;
432    origsize += objectsize;
[1907]433  }
[2662]434  assert(origsize==header_->getDataSize());
435  assert(destsize!=0);
436#endif
437  gs->header_->setDataSize( destsize );
[1907]438  return gs;
439}
440
[2087]441
[1701]442Gamestate *Gamestate::undiff(Gamestate *base)
443{
[2662]444  assert(this && base);assert(data_);
445  assert(header_->isDiffed());
446  assert(!header_->isCompressed() && !base->header_->isCompressed());
[1701]447  //unsigned char *basep = base->getGs()/*, *gs = getGs()*/;
[1907]448  uint8_t *basep = GAMESTATE_START(base->data_);
449  uint8_t *gs = GAMESTATE_START(this->data_);
[2662]450  uint32_t of=0; // pointers offset
451  uint32_t dest_length=0;
452  dest_length=header_->getDataSize();
[1701]453  if(dest_length==0)
454    return NULL;
[2662]455  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];
456  uint8_t *dest = ndata + GamestateHeader::getSize();
457  while(of < base->header_->getDataSize() && of < header_->getDataSize()){
[1701]458    *(dest+of)=*(basep+of)^*(gs+of); // do the xor
459    ++of;
460  }
[2662]461  if(base->header_->getDataSize()!=header_->getDataSize()){
[1907]462    uint8_t n=0;
[2662]463    if(base->header_->getDataSize() < header_->getDataSize()){
[1701]464      while(of < dest_length){
465        *(dest+of)=n^*(gs+of);
466        of++;
467      }
468    }
469  }
[1751]470  Gamestate *g = new Gamestate(ndata, getClientID());
[2662]471  assert(g->header_);
472  *(g->header_) = *header_;
473  g->header_->setDiffed( false );
[1751]474  g->flags_=flags_;
475  g->packetDirection_ = packetDirection_;
476  assert(!g->isDiffed());
477  assert(!g->isCompressed());
[1701]478  return g;
479}
480
481
[2662]482uint32_t Gamestate::calcGamestateSize(int32_t id, uint8_t mode)
[1701]483{
[2662]484  uint32_t size=0;
[1701]485    // get the start of the Synchronisable list
[2171]486  ObjectList<Synchronisable>::iterator it;
[1701]487    // get total size of gamestate
[2171]488  for(it = ObjectList<Synchronisable>::begin(); it; ++it)
[1907]489    size+=it->getSize(id, mode); // size of the actual data of the synchronisable
[1701]490  return size;
491}
492
[2662]493} //namespace packet
494} //namespace orxonox
Note: See TracBrowser for help on using the repository browser.