Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1739 was 1739, checked in by scheusso, 16 years ago

further fixes (diff/undiff not working yet)

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