Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2759 was 2759, checked in by scheusso, 15 years ago

merged network branch (windows,multiplayer fixes) back to trunk

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