Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged netp4 back to trunk

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