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
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 "network/ClientInformation.h"
31#include "network/GamestateHandler.h"
32#include "core/CoreIncludes.h"
33#include "core/Iterator.h"
34
35#include <zlib.h>
36#include <assert.h>
37
38
39
40namespace network {
41
42namespace packet {
43
44#define GAMESTATE_START(data) (data + sizeof(GamestateHeader))
45#define GAMESTATE_HEADER(data) ((GamestateHeader *)data)
46#define HEADER GAMESTATE_HEADER(data_)
47
48Gamestate::Gamestate()
49{
50}
51
52Gamestate::Gamestate(unsigned char *data, int clientID):
53    Packet(data, clientID)
54{
55}
56
57
58Gamestate::~Gamestate()
59{
60}
61
62bool Gamestate::collectData(int id, int mode)
63{
64  int tempsize=0, currentsize=0;
65  assert(data_==0);
66  int size = calcGamestateSize(id, mode);
67
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);
80  orxonox::ObjectList<Synchronisable>::iterator it;
81  for(it = orxonox::ObjectList<Synchronisable>::begin(); it; ++it){
82    tempsize=it->getSize2(id, mode);
83
84    if(currentsize+tempsize > size){
85      // start allocate additional memory
86      COUT(3) << "G.St.Man: need additional memory" << std::endl;
87      orxonox::ObjectList<Synchronisable>::iterator temp = it;
88      int addsize=tempsize;
89      while(++temp)
90        addsize+=temp->getSize2(id, mode);
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
97    if(!it->getData(mem, id, mode))
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  }
102
103
104  //start write gamestate header
105  HEADER->packetType = ENUM::Gamestate;
106  assert( *(ENUM::Type *)(data_) == ENUM::Gamestate);
107  HEADER->normsize = currentsize;
108  HEADER->id = id;
109  HEADER->diffed = false;
110  HEADER->complete = true;
111  HEADER->compressed = false;
112  //stop write gamestate header
113
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{
121  assert(data_);
122  assert(!HEADER->compressed);
123  assert(!HEADER->diffed);
124  unsigned int size, objectID, classID;
125  unsigned char *mem=data_+sizeof(GamestateHeader);
126    // get the start of the Synchronisable list
127  orxonox::ObjectList<Synchronisable>::iterator it=orxonox::ObjectList<Synchronisable>::begin();
128
129  while(mem < data_+sizeof(GamestateHeader)+HEADER->normsize){
130      // extract synchronisable header
131    size = *(unsigned int *)mem;
132    objectID = *(unsigned int*)(mem+sizeof(unsigned int));
133    classID = *(unsigned int*)(mem+2*sizeof(unsigned int));
134
135    if(!it || it->objectID!=objectID || it->classID!=classID){
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))
144          return false;
145        it=orxonox::ObjectList<Synchronisable>::end();
146      }
147    } else
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
162int Gamestate::getID(){
163  return HEADER->id;
164}
165
166unsigned int Gamestate::getSize() const
167{
168  assert(data_);
169  if(HEADER->compressed)
170    return HEADER->compsize+sizeof(GamestateHeader);
171  else
172  {
173    return HEADER->normsize+sizeof(GamestateHeader);
174  }
175}
176
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
192bool Gamestate::process()
193{
194  return GamestateHandler::addGamestate(this, getClientID());
195}
196
197bool Gamestate::compressData()
198{
199  assert(HEADER);
200  assert(!HEADER->compressed);
201  uLongf buffer = (uLongf)(((HEADER->normsize + 12)*1.01)+1);
202  if(buffer==0)
203    return false;
204
205  unsigned char *ndata = new unsigned char[buffer+sizeof(GamestateHeader)];
206  unsigned char *dest = GAMESTATE_START(ndata);
207  //unsigned char *dest = new unsigned char[buffer];
208  unsigned char *source = GAMESTATE_START(data_);
209  int retval;
210  retval = compress( dest, &buffer, source, (uLong)(HEADER->normsize) );
211  switch ( retval ) {
212    case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break;
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;
216  }
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
228
229  //copy and modify header
230#ifndef NDEBUG
231  HEADER->crc32 = calcCRC(data_+sizeof(GamestateHeader), HEADER->normsize);
232#endif
233  *GAMESTATE_HEADER(ndata) = *HEADER;
234  //delete old data
235  delete[] data_;
236  //save new data
237  data_ = ndata;
238  HEADER->compsize = buffer;
239  HEADER->compressed = true;
240  assert(HEADER->compressed);
241  COUT(3) << "gamestate compress normsize: " << HEADER->normsize << " compsize: " << HEADER->compsize << std::endl;
242  return true;
243}
244bool Gamestate::decompressData()
245{
246  assert(HEADER);
247  assert(HEADER->compressed);
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);
255  unsigned char *ndata = new unsigned char[bufsize + sizeof(GamestateHeader)];
256  unsigned char *dest = ndata + sizeof(GamestateHeader);
257  unsigned char *source = data_ + sizeof(GamestateHeader);
258  int retval;
259  uLongf length=bufsize;
260  retval = uncompress( dest, &length, source, (uLong)compsize );
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  }
267#ifndef NDEBUG
268  assert(HEADER->crc32==calcCRC(ndata+sizeof(GamestateHeader), HEADER->normsize));
269#endif
270
271  //copy over the header
272  *GAMESTATE_HEADER(ndata) = *HEADER;
273  //delete old (compressed data)
274  delete[] data_;
275  //set new pointers
276  data_ = ndata;
277  HEADER->compressed = false;
278  assert(HEADER->normsize==normsize);
279  assert(HEADER->compsize==compsize);
280  return true;
281}
282
283Gamestate *Gamestate::diff(Gamestate *base)
284{
285  assert(HEADER);
286  assert(!HEADER->compressed);
287  assert(!HEADER->diffed);
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
311  *GAMESTATE_HEADER(ndata) = *HEADER;
312  GAMESTATE_HEADER(ndata)->diffed = true;
313  GAMESTATE_HEADER(ndata)->base_id = base->getID();
314  Gamestate *g = new Gamestate(ndata, getClientID());
315  g->flags_=flags_;
316  g->packetDirection_ = packetDirection_;
317  return g;
318}
319
320Gamestate *Gamestate::undiff(Gamestate *base)
321{
322  assert(this && base);assert(HEADER);
323  assert(HEADER->diffed);
324  assert(!HEADER->compressed && !GAMESTATE_HEADER(base->data_)->compressed);
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  }
348  *GAMESTATE_HEADER(ndata) = *HEADER;
349  GAMESTATE_HEADER(ndata)->diffed = false;
350  Gamestate *g = new Gamestate(ndata, getClientID());
351  g->flags_=flags_;
352  g->packetDirection_ = packetDirection_;
353  assert(!g->isDiffed());
354  assert(!g->isCompressed());
355  return g;
356}
357
358
359unsigned int Gamestate::calcGamestateSize(unsigned int id, int mode)
360{
361  int size=0;
362    // get the start of the Synchronisable list
363  orxonox::ObjectList<Synchronisable>::iterator it;
364    // get total size of gamestate
365  for(it = orxonox::ObjectList<Synchronisable>::begin(); it; ++it)
366    size+=it->getSize2(id, mode); // size of the actual data of the synchronisable
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 */
376  void Gamestate::removeObject(orxonox::ObjectList<Synchronisable>::iterator &it) {
377    orxonox::ObjectList<Synchronisable>::iterator temp=it;
378    ++it;
379    delete  *temp;
380  }
381
382  bool Gamestate::isDiffed(){
383    return HEADER->diffed;
384  }
385
386  bool Gamestate::isCompressed(){
387    return HEADER->compressed;
388  }
389
390  int Gamestate::getBaseID(){
391    return HEADER->base_id;
392  }
393}
394
395}
Note: See TracBrowser for help on using the repository browser.