Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/resource2/src/network/packet/Gamestate.cc @ 5295

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

merged netp5 back to trunk

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