Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/tutorialFS09/src/network/packet/Gamestate.cc @ 9215

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

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

  • Property svn:eol-style set to native
File size: 14.5 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, (C) 2008
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "Gamestate.h"
30#include "../GamestateHandler.h"
31#include "../synchronisable/Synchronisable.h"
32#include "../TrafficControl.h"
33#include "core/Core.h"
34#include "core/CoreIncludes.h"
35#include "core/Iterator.h"
36
37#include <zlib.h>
38#include <cassert>
39
40
41
42namespace orxonox {
43
44namespace packet {
45
46#define GAMESTATE_START(data) (data + GamestateHeader::getSize())
47
48#define PACKET_FLAG_GAMESTATE  ENET_PACKET_FLAG_RELIABLE
49
50
51Gamestate::Gamestate()
52{
53  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
54  header_ = 0;
55}
56
57Gamestate::Gamestate(uint8_t *data, unsigned int clientID):
58    Packet(data, clientID)
59{
60  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
61  header_ = new GamestateHeader(data_);
62}
63
64Gamestate::Gamestate(uint8_t *data)
65{
66  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
67  data_=data;
68  header_ = new GamestateHeader(data_);
69}
70
71Gamestate::Gamestate(const Gamestate& g) :
72    Packet( *(Packet*)&g )
73{
74  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
75  header_ = new GamestateHeader(data_);
76}
77
78
79Gamestate::~Gamestate()
80{
81}
82
83bool Gamestate::collectData(int id, uint8_t mode)
84{
85  assert(this->header_==0); // make sure the header didn't exist before
86  uint32_t tempsize=0, currentsize=0;
87  assert(data_==0);
88  uint32_t size = calcGamestateSize(id, mode);
89
90  COUT(4) << "G.ST.Man: producing gamestate with id: " << id << std::endl;
91  if(size==0)
92    return false;
93  data_ = new uint8_t[size + GamestateHeader::getSize()];
94  if(!data_){
95    COUT(2) << "GameStateManager: could not allocate memory" << std::endl;
96    return false;
97  }
98 
99  // create the header object
100  header_ = new GamestateHeader(data_);
101
102  //start collect data synchronisable by synchronisable
103  uint8_t *mem=data_;
104  mem += GamestateHeader::getSize();
105  ObjectList<Synchronisable>::iterator it;
106  for(it = ObjectList<Synchronisable>::begin(); it; ++it){
107   
108    tempsize=it->getSize(id, mode);
109#ifndef NDEBUG
110    if(currentsize+tempsize > size){
111      assert(0); // if we don't use multithreading this part shouldn't be neccessary
112      // start allocate additional memory
113      COUT(3) << "G.St.Man: need additional memory" << std::endl;
114      ObjectList<Synchronisable>::iterator temp = it;
115      uint32_t addsize=tempsize;
116      while(++temp)
117        addsize+=temp->getSize(id, mode);
118      data_ = (uint8_t *)realloc(data_, GamestateHeader::getSize() + currentsize + addsize);
119      if(!data_)
120        return false;
121      size = currentsize+addsize;
122    }// stop allocate additional memory
123#endif
124
125    if ( it->doSync( id, mode ) )
126      dataMap_.push_back( obj(it->getObjectID(), it->getCreatorID(), tempsize, mem-data_) );
127    if(!it->getData(mem, id, mode))
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  }
132
133
134  //start write gamestate header
135  header_->setDataSize( currentsize );
136  header_->setID( id );
137  header_->setDiffed( false );
138  header_->setComplete( true );
139  header_->setCompressed( false );
140  //stop write gamestate header
141
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
147bool Gamestate::spreadData(uint8_t mode)
148{
149  COUT(4) << "processing gamestate with id " << header_->getID() << endl;
150  assert(data_);
151  assert(!header_->isCompressed());
152  assert(!header_->isDiffed());
153  uint8_t *mem=data_+GamestateHeader::getSize();
154  Synchronisable *s;
155
156  // update the data of the objects we received
157  while(mem < data_+GamestateHeader::getSize()+header_->getDataSize()){
158    SynchronisableHeader objectheader(mem);
159
160    s = Synchronisable::getSynchronisable( objectheader.getObjectID() );
161    if(!s)
162    {
163      if (!Core::isMaster())
164      {
165        Synchronisable::fabricate(mem, mode);
166      }
167      else
168      {
169        mem += objectheader.getDataSize();
170      }
171    }
172    else
173    {
174      bool b = s->updateData(mem, mode);
175      assert(b);
176    }
177  }
178
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
205  return true;
206}
207
208uint32_t Gamestate::getSize() const
209{
210  assert(data_);
211  if(header_->isCompressed())
212    return header_->getCompSize()+GamestateHeader::getSize();
213  else
214  {
215    return header_->getDataSize()+GamestateHeader::getSize();
216  }
217}
218
219bool Gamestate::operator==(packet::Gamestate gs){
220  uint8_t *d1 = data_+GamestateHeader::getSize();
221  uint8_t *d2 = gs.data_+GamestateHeader::getSize();
222  assert(!isCompressed());
223  assert(!gs.isCompressed());
224  while(d1<data_+header_->getDataSize())
225  {
226    if(*d1!=*d2)
227      return false;
228    d1++;
229    d2++;
230  }
231  return true;
232}
233
234bool Gamestate::process()
235{
236  return GamestateHandler::addGamestate(this, getClientID());
237}
238
239
240
241bool Gamestate::compressData()
242{
243  assert(data_);
244  assert(!header_->isCompressed());
245  uLongf buffer = (uLongf)(((header_->getDataSize() + 12)*1.01)+1);
246  if(buffer==0)
247    return false;
248
249  uint8_t *ndata = new uint8_t[buffer+GamestateHeader::getSize()];
250  uint8_t *dest = ndata + GamestateHeader::getSize();
251  uint8_t *source = data_ + GamestateHeader::getSize();
252  int retval;
253  retval = compress( dest, &buffer, source, (uLong)(header_->getDataSize()) );
254  switch ( retval ) {
255    case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break;
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;
259  }
260
261  //copy and modify header
262  GamestateHeader *temp = header_;
263  header_ = new GamestateHeader(ndata, temp);
264  delete temp;
265  //delete old data
266  delete[] data_;
267  //save new data
268  data_ = ndata;
269  header_->setCompSize( buffer );
270  header_->setCompressed( true );
271  COUT(5) << "gamestate compress datasize: " << header_->getDataSize() << " compsize: " << header_->getCompSize() << std::endl;
272  return true;
273}
274bool Gamestate::decompressData()
275{
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;
282  bufsize = datasize;
283  assert(bufsize!=0);
284  uint8_t *ndata = new uint8_t[bufsize + GamestateHeader::getSize()];
285  uint8_t *dest = ndata + GamestateHeader::getSize();
286  uint8_t *source = data_ + GamestateHeader::getSize();
287  int retval;
288  uLongf length=bufsize;
289  retval = uncompress( dest, &length, source, (uLong)compsize );
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  }
296
297  //copy over the header
298  GamestateHeader *temp = header_;
299  header_ = new GamestateHeader( data_, header_ );
300  delete temp;
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
313  //set new pointers
314  data_ = ndata;
315  header_->setCompressed( false );
316  assert(header_->getDataSize()==datasize);
317  assert(header_->getCompSize()==compsize);
318  return true;
319}
320
321Gamestate *Gamestate::diff(Gamestate *base)
322{
323  assert(data_);
324  assert(!header_->isCompressed());
325  assert(!header_->isDiffed());
326  GamestateHeader diffHeader(base->data_);
327  uint8_t *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_);
328  uint32_t of=0; // pointers offset
329  uint32_t dest_length=0;
330  dest_length=header_->getDataSize();
331  if(dest_length==0)
332    return NULL;
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()){
336    *(dest+of)=*(basep+of)^*(gs+of); // do the xor
337    ++of;
338  }
339  if(diffHeader.getDataSize()!=header_->getDataSize()){
340    uint8_t n=0;
341    if(diffHeader.getDataSize() < header_->getDataSize()){
342      while(of<dest_length){
343        *(dest+of)=n^*(gs+of);
344        of++;
345      }
346    }
347  }
348
349  Gamestate *g = new Gamestate(ndata, getClientID());
350  *(g->header_) = *header_;
351  g->header_->setDiffed( true );
352  g->header_->setBaseID( base->getID() );
353  g->flags_=flags_;
354  g->packetDirection_ = packetDirection_;
355  return g;
356}
357
358Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){
359  assert(data_);
360  std::list<obj>::iterator it;
361
362  // allocate memory for new data
363  uint8_t *gdata = new uint8_t[header_->getDataSize()+GamestateHeader::getSize()];
364  // create a gamestate out of it
365  Gamestate *gs = new Gamestate(gdata);
366  uint8_t *newdata = gdata + GamestateHeader::getSize();
367  uint8_t *origdata = GAMESTATE_START(data_);
368
369  //copy the GamestateHeader
370  assert(gs->header_);
371  *(gs->header_) = *header_;
372
373  uint32_t objectOffset;
374  unsigned int objectsize, destsize=0;
375  // TODO: Why is this variable not used?
376  //Synchronisable *object;
377
378  //call TrafficControl
379  TrafficControl::getInstance()->processObjectList( clientID, header_->getID(), &dataMap_ );
380
381  //copy in the zeros
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;
396    }else{
397      newobjectheader = oldobjectheader;
398      newobjectheader.setDataAvailable(false);
399      memset(newdata+objectOffset, 0, objectsize-objectOffset);
400    }
401    newdata += objectsize;
402    origdata += objectsize;
403    destsize += objectsize;
404  }
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;
413  }
414  assert(origsize==header_->getDataSize());
415  assert(destsize!=0);
416#endif
417  gs->header_->setDataSize( destsize );
418  return gs;
419}
420
421
422Gamestate *Gamestate::undiff(Gamestate *base)
423{
424  assert(this && base);assert(data_);
425  assert(header_->isDiffed());
426  assert(!header_->isCompressed() && !base->header_->isCompressed());
427  uint8_t *basep = GAMESTATE_START(base->data_);
428  uint8_t *gs = GAMESTATE_START(this->data_);
429  uint32_t of=0; // pointers offset
430  uint32_t dest_length=0;
431  dest_length=header_->getDataSize();
432  if(dest_length==0)
433    return NULL;
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()){
437    *(dest+of)=*(basep+of)^*(gs+of); // do the xor
438    ++of;
439  }
440  if(base->header_->getDataSize()!=header_->getDataSize()){
441    uint8_t n=0;
442    if(base->header_->getDataSize() < header_->getDataSize()){
443      while(of < dest_length){
444        *(dest+of)=n^*(gs+of);
445        of++;
446      }
447    }
448  }
449  Gamestate *g = new Gamestate(ndata, getClientID());
450  assert(g->header_);
451  *(g->header_) = *header_;
452  g->header_->setDiffed( false );
453  g->flags_=flags_;
454  g->packetDirection_ = packetDirection_;
455  assert(!g->isDiffed());
456  assert(!g->isCompressed());
457  return g;
458}
459
460
461uint32_t Gamestate::calcGamestateSize(int32_t id, uint8_t mode)
462{
463  uint32_t size=0;
464    // get the start of the Synchronisable list
465  ObjectList<Synchronisable>::iterator it;
466    // get total size of gamestate
467  for(it = ObjectList<Synchronisable>::begin(); it; ++it)
468    size+=it->getSize(id, mode); // size of the actual data of the synchronisable
469  return size;
470}
471
472} //namespace packet
473} //namespace orxonox
Note: See TracBrowser for help on using the repository browser.