Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/buildsystem3/src/network/packet/Gamestate.cc @ 2663

Last change on this file since 2663 was 2662, checked in by rgrieder, 16 years ago

Merged presentation branch back to trunk.

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