Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/network/packet/Gamestate.cc @ 3187

Last change on this file since 3187 was 3187, checked in by rgrieder, 15 years ago

Officially included ObjectList.h in CoreIncludes.h (it gets included by the Identifier anyway).

  • Property svn:eol-style set to native
File size: 16.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
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}
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
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_->setDiffed( false );
140  header_->setComplete( true );
141  header_->setCompressed( false );
142  //stop write gamestate header
143
144  COUT(5) << "G.ST.Man: Gamestate size: " << currentsize << std::endl;
145  COUT(5) << "G.ST.Man: 'estimated' (and corrected) Gamestate size: " << size << std::endl;
146  return true;
147}
148
149bool Gamestate::spreadData(uint8_t mode)
150{
151  COUT(4) << "processing gamestate with id " << header_->getID() << endl;
152  assert(data_);
153  assert(!header_->isCompressed());
154  assert(!header_->isDiffed());
155  uint8_t *mem=data_+GamestateHeader::getSize();
156  Synchronisable *s;
157
158  // update the data of the objects we received
159  while(mem < data_+GamestateHeader::getSize()+header_->getDataSize()){
160    SynchronisableHeader objectheader(mem);
161
162    s = Synchronisable::getSynchronisable( objectheader.getObjectID() );
163    if(!s)
164    {
165      if (!GameMode::isMaster())
166      {
167        Synchronisable::fabricate(mem, mode);
168      }
169      else
170      {
171        mem += objectheader.getDataSize();
172      }
173    }
174    else
175    {
176      bool b = s->updateData(mem, mode);
177      assert(b);
178    }
179  }
180   // In debug mode, check first, whether there are no duplicate objectIDs
181#ifndef NDEBUG
182  if(this->getID()%1000==0){
183    std::list<uint32_t> v1;
184    ObjectList<Synchronisable>::iterator it;
185    for (it = ObjectList<Synchronisable>::begin(); it != ObjectList<Synchronisable>::end(); ++it) {
186      if (it->getObjectID() == OBJECTID_UNKNOWN) {
187        if (it->objectMode_ != 0x0) {
188          COUT(0) << "Found object with OBJECTID_UNKNOWN on the client with objectMode != 0x0!" << std::endl;
189          COUT(0) << "Possible reason for this error: Client created a synchronized object without the Server's approval." << std::endl;
190          COUT(0) << "Objects class: " << it->getIdentifier()->getName() << std::endl;
191          assert(false);
192        }
193      }
194      else {
195        std::list<uint32_t>::iterator it2;
196        for (it2 = v1.begin(); it2 != v1.end(); ++it2) {
197          if (it->getObjectID() == *it2) {
198            COUT(0) << "Found duplicate objectIDs on the client!" << std::endl
199                    << "Are you sure you don't create a Sychnronisable objcect with 'new' \
200                        that doesn't have objectMode = 0x0?" << std::endl;
201            assert(false);
202          }
203        }
204        v1.push_back(it->getObjectID());
205      }
206    }
207  }
208#endif
209
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
322Gamestate *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
359// Gamestate *Gamestate::diff(Gamestate *base)
360// {
361//   assert(data_);
362//   assert(!header_->isCompressed());
363//   assert(!header_->isDiffed());
364//   GamestateHeader diffHeader(base->data_);
365//   uint8_t *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_);
366//   uint32_t of=0; // pointers offset
367//   uint32_t dest_length=0;
368//   dest_length=header_->getDataSize();
369//   if(dest_length==0)
370//     return NULL;
371//   uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];
372//   uint8_t *dest = ndata + GamestateHeader::getSize();
373//   
374//   
375//   // LOOP-UNROLLED DIFFING
376//   uint32_t *dest32 = (uint32_t*)dest, *base32 = (uint32_t*)basep, *gs32 = (uint32_t*)gs;
377//   // diff in 4-byte steps
378//   while( of < (uint32_t)(header_->getDataSize())/4 ){
379//     if( of < (uint32_t)(diffHeader.getDataSize())/4 )
380//     {
381//       *(dest32+of)=*(base32+of) ^ *(gs32+of); // do the xor
382//       ++of;
383//     }
384//     else
385//     {
386//       *(dest32+of)=*(gs32+of); // same as 0 ^ *(gs32+of)
387//       ++of;
388//     }
389//   }
390//   for( unsigned int of2 = 0; of2 < header_->getDataSize()%4; ++of2 )
391//   {
392//     if( of*4+of2 < diffHeader.getDataSize() )
393//     {
394//       *(dest+4*of+of2)=*(basep+4*of+of2) ^ *(gs+4*of+of2); // do the xor
395//     }
396//     else
397//     {
398//       *(dest+4*of+of2)=*(gs+4*of+of2); // same as 0 ^ *(gs32+of)
399//     }
400//   }
401//
402//   Gamestate *g = new Gamestate(ndata, getClientID());
403//   *(g->header_) = *header_;
404//   g->header_->setDiffed( true );
405//   g->header_->setBaseID( base->getID() );
406//   g->flags_=flags_;
407//   g->packetDirection_ = packetDirection_;
408//   return g;
409// }
410
411Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){
412  assert(data_);
413  std::list<obj>::iterator it;
414
415  // allocate memory for new data
416  uint8_t *gdata = new uint8_t[header_->getDataSize()+GamestateHeader::getSize()];
417  // create a gamestate out of it
418  Gamestate *gs = new Gamestate(gdata);
419  uint8_t *newdata = gdata + GamestateHeader::getSize();
420  uint8_t *origdata = GAMESTATE_START(data_);
421
422  //copy the GamestateHeader
423  assert(gs->header_);
424  *(gs->header_) = *header_;
425
426  uint32_t objectOffset;
427  unsigned int objectsize, destsize=0;
428  // TODO: Why is this variable not used?
429  //Synchronisable *object;
430
431  //call TrafficControl
432  TrafficControl::getInstance()->processObjectList( clientID, header_->getID(), dataVector_ );
433
434  //copy in the zeros
435//   std::list<obj>::iterator itt;
436//   COUT(0) << "myvector contains:";
437//   for ( itt=dataVector_.begin() ; itt!=dataVector_.end(); itt++ )
438//     COUT(0) << " " << (*itt).objID;
439//   COUT(0) << endl;
440  for(it=dataVector_.begin(); it!=dataVector_.end();){
441    SynchronisableHeader oldobjectheader(origdata);
442    SynchronisableHeader newobjectheader(newdata);
443    if ( (*it).objSize == 0 )
444    {
445      ++it;
446      continue;
447    }
448    objectsize = oldobjectheader.getDataSize();
449    objectOffset=SynchronisableHeader::getSize(); //skip the size and the availableData variables in the objectheader
450    if ( (*it).objID == oldobjectheader.getObjectID() ){
451      memcpy(newdata, origdata, objectsize);
452      assert(newobjectheader.isDataAvailable()==true);
453      ++it;
454    }else{
455      newobjectheader = oldobjectheader;
456      newobjectheader.setDataAvailable(false);
457      memset(newdata+objectOffset, 0, objectsize-objectOffset);
458    }
459    newdata += objectsize;
460    origdata += objectsize;
461    destsize += objectsize;
462  }
463#ifndef NDEBUG
464  uint32_t origsize = destsize;
465  while ( origsize < header_->getDataSize() )
466  {
467    SynchronisableHeader oldobjectheader(origdata);
468    objectsize = oldobjectheader.getDataSize();
469    origdata += objectsize;
470    origsize += objectsize;
471  }
472  assert(origsize==header_->getDataSize());
473  assert(destsize!=0);
474#endif
475  gs->header_->setDataSize( destsize );
476  return gs;
477}
478
479
480Gamestate *Gamestate::undiff(Gamestate *base)
481{
482  assert(this && base);assert(data_);
483  assert(header_->isDiffed());
484  assert(!header_->isCompressed() && !base->header_->isCompressed());
485  uint8_t *basep = GAMESTATE_START(base->data_);
486  uint8_t *gs = GAMESTATE_START(this->data_);
487  uint32_t of=0; // pointers offset
488  uint32_t dest_length=0;
489  dest_length=header_->getDataSize();
490  if(dest_length==0)
491    return NULL;
492  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];
493  uint8_t *dest = ndata + GamestateHeader::getSize();
494  while(of < base->header_->getDataSize() && of < header_->getDataSize()){
495    *(dest+of)=*(basep+of)^*(gs+of); // do the xor
496    ++of;
497  }
498  if(base->header_->getDataSize()!=header_->getDataSize()){
499    uint8_t n=0;
500    if(base->header_->getDataSize() < header_->getDataSize()){
501      while(of < dest_length){
502        *(dest+of)=n^*(gs+of);
503        of++;
504      }
505    }
506  }
507  Gamestate *g = new Gamestate(ndata, getClientID());
508  assert(g->header_);
509  *(g->header_) = *header_;
510  g->header_->setDiffed( false );
511  g->flags_=flags_;
512  g->packetDirection_ = packetDirection_;
513  assert(!g->isDiffed());
514  assert(!g->isCompressed());
515  return g;
516}
517
518
519uint32_t Gamestate::calcGamestateSize(int32_t id, uint8_t mode)
520{
521  uint32_t size=0;
522    // get the start of the Synchronisable list
523  ObjectList<Synchronisable>::iterator it;
524    // get total size of gamestate
525  for(it = ObjectList<Synchronisable>::begin(); it; ++it)
526    size+=it->getSize(id, mode); // size of the actual data of the synchronisable
527  return size;
528}
529
530} //namespace packet
531} //namespace orxonox
Note: See TracBrowser for help on using the repository browser.