Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added svn:eol-style native to all the files that were missing it

  • Property svn:eol-style set to native
File size: 12.1 KB
RevLine 
[1711]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
[1701]29#include "Gamestate.h"
30#include "network/ClientInformation.h"
[1705]31#include "network/GamestateHandler.h"
[1763]32#include "core/CoreIncludes.h"
[1751]33#include "core/Iterator.h"
[1701]34
35#include <zlib.h>
36#include <assert.h>
37
38
39
40namespace network {
41
42namespace packet {
43
[1751]44#define GAMESTATE_START(data) (data + sizeof(GamestateHeader))
[1701]45#define GAMESTATE_HEADER(data) ((GamestateHeader *)data)
46#define HEADER GAMESTATE_HEADER(data_)
[1740]47
[1701]48Gamestate::Gamestate()
49{
50}
51
[1715]52Gamestate::Gamestate(unsigned char *data, int clientID):
[1711]53    Packet(data, clientID)
[1701]54{
55}
56
57
58Gamestate::~Gamestate()
59{
60}
61
62bool Gamestate::collectData(int id, int mode)
63{
64  int tempsize=0, currentsize=0;
[1751]65  assert(data_==0);
66  int size = calcGamestateSize(id, mode);
[1740]67
[1701]68  COUT(4) << "G.ST.Man: producing gamestate with id: " << id << std::endl;
69  if(size==0)
70    return false;
71  data_ = new unsigned char[size + sizeof(GamestateHeader)];
72  if(!data_){
73    COUT(2) << "GameStateManager: could not allocate memory" << std::endl;
74    return false;
75  }
76
77  //start collect data synchronisable by synchronisable
78  unsigned char *mem=data_;
79  mem+=sizeof(GamestateHeader);
[1747]80  orxonox::ObjectList<Synchronisable>::iterator it;
81  for(it = orxonox::ObjectList<Synchronisable>::begin(); it; ++it){
[1751]82    tempsize=it->getSize2(id, mode);
[1740]83
[1701]84    if(currentsize+tempsize > size){
85      // start allocate additional memory
86      COUT(3) << "G.St.Man: need additional memory" << std::endl;
[1747]87      orxonox::ObjectList<Synchronisable>::iterator temp = it;
[1701]88      int addsize=tempsize;
89      while(++temp)
[1751]90        addsize+=temp->getSize2(id, mode);
[1701]91      data_ = (unsigned char *)realloc(data_, sizeof(GamestateHeader) + currentsize + addsize);
92      if(!data_)
93        return false;
94      size = currentsize+addsize;
95    }// stop allocate additional memory
96
[1751]97    if(!it->getData(mem, id, mode))
[1701]98      return false; // mem pointer gets automatically increased because of call by reference
99    // increase size counter by size of current synchronisable
100    currentsize+=tempsize;
101  }
[1740]102
103
[1701]104  //start write gamestate header
[1710]105  HEADER->packetType = ENUM::Gamestate;
[1740]106  assert( *(ENUM::Type *)(data_) == ENUM::Gamestate);
[1701]107  HEADER->normsize = currentsize;
108  HEADER->id = id;
109  HEADER->diffed = false;
110  HEADER->complete = true;
[1715]111  HEADER->compressed = false;
[1701]112  //stop write gamestate header
[1740]113
[1701]114  COUT(5) << "G.ST.Man: Gamestate size: " << currentsize << std::endl;
115  COUT(5) << "G.ST.Man: 'estimated' (and corrected) Gamestate size: " << size << std::endl;
116  return true;
117}
118
119bool Gamestate::spreadData(int mode)
120{
[1751]121  assert(data_);
122  assert(!HEADER->compressed);
123  assert(!HEADER->diffed);
[1701]124  unsigned int size, objectID, classID;
125  unsigned char *mem=data_+sizeof(GamestateHeader);
126    // get the start of the Synchronisable list
[1747]127  orxonox::ObjectList<Synchronisable>::iterator it=orxonox::ObjectList<Synchronisable>::begin();
[1740]128
[1701]129  while(mem < data_+sizeof(GamestateHeader)+HEADER->normsize){
130      // extract synchronisable header
[1730]131    size = *(unsigned int *)mem;
132    objectID = *(unsigned int*)(mem+sizeof(unsigned int));
133    classID = *(unsigned int*)(mem+2*sizeof(unsigned int));
[1701]134
[1730]135    if(!it || it->objectID!=objectID || it->classID!=classID){
[1701]136        // bad luck ;)
137        // delete the synchronisable (obviously seems to be deleted on the server)
138      while(it && it->objectID!=objectID)
139        removeObject(it);
140
141      if(!it){
142        //fabricate the new synchronisable
143        if(!Synchronisable::fabricate(mem, mode))
[1751]144          return false;
[1701]145        it=orxonox::ObjectList<Synchronisable>::end();
146      }
[1740]147    } else
[1701]148    {
149        // we have our object
150      if(! it->updateData(mem, mode))
151      {
152        COUT(1) << "We couldn't update objectID: " \
153            << objectID << "; classID: " << classID << std::endl;
154      }
155    }
156    ++it;
157  }
158
159  return true;
160}
161
[1705]162int Gamestate::getID(){
163  return HEADER->id;
164}
165
[1701]166unsigned int Gamestate::getSize() const
167{
[1711]168  assert(data_);
[1715]169  if(HEADER->compressed)
[1701]170    return HEADER->compsize+sizeof(GamestateHeader);
171  else
172  {
173    return HEADER->normsize+sizeof(GamestateHeader);
174  }
175}
176
[1751]177bool Gamestate::operator==(packet::Gamestate gs){
178  unsigned char *d1 = data_+sizeof(GamestateHeader);
179  unsigned char *d2 = gs.data_+sizeof(GamestateHeader);
180  assert(!isCompressed());
181  assert(!gs.isCompressed());
182  while(d1<data_+HEADER->normsize)
183  {
184    if(*d1!=*d2)
185      return false;
186    d1++;
187    d2++;
188  }
189  return true;
190}
191
[1701]192bool Gamestate::process()
193{
[1705]194  return GamestateHandler::addGamestate(this, getClientID());
[1701]195}
196
197bool Gamestate::compressData()
198{
199  assert(HEADER);
[1751]200  assert(!HEADER->compressed);
201  uLongf buffer = (uLongf)(((HEADER->normsize + 12)*1.01)+1);
[1701]202  if(buffer==0)
203    return false;
[1740]204
[1701]205  unsigned char *ndata = new unsigned char[buffer+sizeof(GamestateHeader)];
206  unsigned char *dest = GAMESTATE_START(ndata);
[1751]207  //unsigned char *dest = new unsigned char[buffer];
208  unsigned char *source = GAMESTATE_START(data_);
[1701]209  int retval;
[1751]210  retval = compress( dest, &buffer, source, (uLong)(HEADER->normsize) );
[1701]211  switch ( retval ) {
212    case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break;
[1751]213    case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl; return false;
214    case Z_BUF_ERROR: COUT(2) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << std::endl; return false;
215    case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl; return false;
[1701]216  }
[1751]217#ifndef NDEBUG
218  //decompress and compare the start and the decompressed data
219  unsigned char *rdata = new unsigned char[HEADER->normsize+sizeof(GamestateHeader)];
220  unsigned char *d2 = GAMESTATE_START(rdata);
221  uLongf length2 = HEADER->normsize;
222  uncompress(d2, &length2, dest, buffer);
223  for(unsigned int i=0; i<HEADER->normsize; i++){
224    assert(*(source+i)==*(d2+i));
225  }
226  delete[] rdata;
227#endif
[1701]228
229  //copy and modify header
[1751]230#ifndef NDEBUG
231  HEADER->crc32 = calcCRC(data_+sizeof(GamestateHeader), HEADER->normsize);
232#endif
[1701]233  *GAMESTATE_HEADER(ndata) = *HEADER;
234  //delete old data
235  delete[] data_;
236  //save new data
237  data_ = ndata;
[1751]238  HEADER->compsize = buffer;
239  HEADER->compressed = true;
[1730]240  assert(HEADER->compressed);
241  COUT(3) << "gamestate compress normsize: " << HEADER->normsize << " compsize: " << HEADER->compsize << std::endl;
[1701]242  return true;
243}
244bool Gamestate::decompressData()
245{
[1751]246  assert(HEADER);
[1715]247  assert(HEADER->compressed);
[1751]248  COUT(3) << "GameStateClient: uncompressing gamestate. id: " << HEADER->id << ", baseid: " << HEADER->base_id << ", normsize: " << HEADER->normsize << ", compsize: " << HEADER->compsize << std::endl;
249  unsigned int normsize = HEADER->normsize;
250  unsigned int compsize = HEADER->compsize;
251  unsigned int bufsize;
252  assert(compsize<=normsize);
253  bufsize = normsize;
254  assert(bufsize!=0);
[1701]255  unsigned char *ndata = new unsigned char[bufsize + sizeof(GamestateHeader)];
256  unsigned char *dest = ndata + sizeof(GamestateHeader);
[1751]257  unsigned char *source = data_ + sizeof(GamestateHeader);
[1701]258  int retval;
[1751]259  uLongf length=bufsize;
260  retval = uncompress( dest, &length, source, (uLong)compsize );
[1701]261  switch ( retval ) {
262    case Z_OK: COUT(5) << "successfully decompressed" << std::endl; break;
263    case Z_MEM_ERROR: COUT(1) << "not enough memory available" << std::endl; return false;
264    case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer" << std::endl; return false;
265    case Z_DATA_ERROR: COUT(2) << "data corrupted (zlib)" << std::endl; return false;
266  }
[1751]267#ifndef NDEBUG
268  assert(HEADER->crc32==calcCRC(ndata+sizeof(GamestateHeader), HEADER->normsize));
269#endif
[1752]270
[1701]271  //copy over the header
272  *GAMESTATE_HEADER(ndata) = *HEADER;
273  //delete old (compressed data)
[1712]274  delete[] data_;
[1751]275  //set new pointers
[1701]276  data_ = ndata;
[1751]277  HEADER->compressed = false;
278  assert(HEADER->normsize==normsize);
279  assert(HEADER->compsize==compsize);
[1701]280  return true;
281}
282
283Gamestate *Gamestate::diff(Gamestate *base)
284{
[1751]285  assert(HEADER);
286  assert(!HEADER->compressed);
287  assert(!HEADER->diffed);
[1701]288  //unsigned char *basep = base->getGs()/*, *gs = getGs()*/;
289  unsigned char *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_);
290  unsigned int of=0; // pointers offset
291  unsigned int dest_length=0;
292  dest_length=HEADER->normsize;
293  if(dest_length==0)
294    return NULL;
295  unsigned char *ndata = new unsigned char[dest_length*sizeof(unsigned char)+sizeof(GamestateHeader)];
296  unsigned char *dest = ndata + sizeof(GamestateHeader);
297  while(of < GAMESTATE_HEADER(base->data_)->normsize && of < HEADER->normsize){
298    *(dest+of)=*(basep+of)^*(gs+of); // do the xor
299    ++of;
300  }
301  if(GAMESTATE_HEADER(base->data_)->normsize!=HEADER->normsize){
302    unsigned char n=0;
303    if(GAMESTATE_HEADER(base->data_)->normsize < HEADER->normsize){
304      while(of<dest_length){
305        *(dest+of)=n^*(gs+of);
306        of++;
307      }
308    }
309  }
310
[1715]311  *GAMESTATE_HEADER(ndata) = *HEADER;
312  GAMESTATE_HEADER(ndata)->diffed = true;
[1751]313  GAMESTATE_HEADER(ndata)->base_id = base->getID();
314  Gamestate *g = new Gamestate(ndata, getClientID());
315  g->flags_=flags_;
316  g->packetDirection_ = packetDirection_;
[1701]317  return g;
318}
319
320Gamestate *Gamestate::undiff(Gamestate *base)
321{
[1751]322  assert(this && base);assert(HEADER);
323  assert(HEADER->diffed);
[1715]324  assert(!HEADER->compressed && !GAMESTATE_HEADER(base->data_)->compressed);
[1701]325  //unsigned char *basep = base->getGs()/*, *gs = getGs()*/;
326  unsigned char *basep = GAMESTATE_START(base->data_);
327  unsigned char *gs = GAMESTATE_START(this->data_);
328  unsigned int of=0; // pointers offset
329  unsigned int dest_length=0;
330  dest_length=HEADER->normsize;
331  if(dest_length==0)
332    return NULL;
333  unsigned char *ndata = new unsigned char[dest_length*sizeof(unsigned char)+sizeof(GamestateHeader)];
334  unsigned char *dest = ndata + sizeof(GamestateHeader);
335  while(of < GAMESTATE_HEADER(base->data_)->normsize && of < HEADER->normsize){
336    *(dest+of)=*(basep+of)^*(gs+of); // do the xor
337    ++of;
338  }
339  if(GAMESTATE_HEADER(base->data_)->normsize!=HEADER->normsize){
340    unsigned char n=0;
341    if(GAMESTATE_HEADER(base->data_)->normsize < HEADER->normsize){
342      while(of < dest_length){
343        *(dest+of)=n^*(gs+of);
344        of++;
345      }
346    }
347  }
[1715]348  *GAMESTATE_HEADER(ndata) = *HEADER;
349  GAMESTATE_HEADER(ndata)->diffed = false;
[1751]350  Gamestate *g = new Gamestate(ndata, getClientID());
351  g->flags_=flags_;
352  g->packetDirection_ = packetDirection_;
353  assert(!g->isDiffed());
354  assert(!g->isCompressed());
[1701]355  return g;
356}
357
358
[1751]359unsigned int Gamestate::calcGamestateSize(unsigned int id, int mode)
[1701]360{
361  int size=0;
362    // get the start of the Synchronisable list
[1747]363  orxonox::ObjectList<Synchronisable>::iterator it;
[1701]364    // get total size of gamestate
[1747]365  for(it = orxonox::ObjectList<Synchronisable>::begin(); it; ++it)
[1751]366    size+=it->getSize2(id, mode); // size of the actual data of the synchronisable
[1701]367//  size+=sizeof(GamestateHeader);
368  return size;
369}
370
371/**
372 * This function removes a Synchronisable out of the universe
373 * @param it iterator of the list pointing to the object
374 * @return iterator pointing to the next object in the list
375 */
[1747]376  void Gamestate::removeObject(orxonox::ObjectList<Synchronisable>::iterator &it) {
377    orxonox::ObjectList<Synchronisable>::iterator temp=it;
[1701]378    ++it;
379    delete  *temp;
380  }
381
[1712]382  bool Gamestate::isDiffed(){
383    return HEADER->diffed;
384  }
[1740]385
[1751]386  bool Gamestate::isCompressed(){
387    return HEADER->compressed;
388  }
[1752]389
[1712]390  int Gamestate::getBaseID(){
391    return HEADER->base_id;
392  }
[1701]393}
394
395}
Note: See TracBrowser for help on using the repository browser.