Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

further bugfixes concerning packet handling

File size: 10.3 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_ && !HEADER->compressed && !HEADER->diffed);
123  unsigned int size, objectID, classID;
124  unsigned char *mem=data_+sizeof(GamestateHeader);
125    // get the start of the Synchronisable list
126  orxonox::Iterator<Synchronisable> it=orxonox::ObjectList<Synchronisable>::start();
127 
128  while(mem < data_+sizeof(GamestateHeader)+HEADER->normsize){
129      // extract synchronisable header
130    size = *(int *)mem;
131    objectID = *(int*)(mem+sizeof(int));
132    classID = *(int*)(mem+2*sizeof(int));
133
134    if(!it || it->objectID!=objectID){
135        // bad luck ;)
136        // delete the synchronisable (obviously seems to be deleted on the server)
137      while(it && it->objectID!=objectID)
138        removeObject(it);
139
140      if(!it){
141        //fabricate the new synchronisable
142        if(!Synchronisable::fabricate(mem, mode))
143          return false;
144        it=orxonox::ObjectList<Synchronisable>::end();
145      }
146    } else 
147    {
148        // we have our object
149      if(! it->updateData(mem, mode))
150      {
151        COUT(1) << "We couldn't update objectID: " \
152            << objectID << "; classID: " << classID << std::endl;
153      }
154    }
155    ++it;
156  }
157
158  return true;
159}
160
161int Gamestate::getID(){
162  return HEADER->id;
163}
164
165unsigned int Gamestate::getSize() const
166{
167  assert(data_);
168  if(HEADER->compressed)
169    return HEADER->compsize+sizeof(GamestateHeader);
170  else
171  {
172    return HEADER->normsize+sizeof(GamestateHeader);
173  }
174}
175
176bool Gamestate::process()
177{
178  return GamestateHandler::addGamestate(this, getClientID());
179}
180
181bool Gamestate::compressData()
182{
183  assert(HEADER);
184  uLongf buffer = (uLongf)((HEADER->normsize + 12)*1.01)+1;
185  if(buffer==0)
186    return false;
187 
188  unsigned char *ndata = new unsigned char[buffer+sizeof(GamestateHeader)];
189  unsigned char *dest = GAMESTATE_START(ndata);
190  int retval;
191  retval = compress( dest, &buffer, GAMESTATE_START(data_), (uLong)(HEADER->normsize) );
192  switch ( retval ) {
193    case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break;
194    case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl; 
195    return false;
196    case Z_BUF_ERROR: COUT(2) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << std::endl;
197    return false;
198    case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl;
199    return false;
200  }
201
202  //copy and modify header
203  HEADER->compsize = buffer;
204  HEADER->compressed = true;
205  *GAMESTATE_HEADER(ndata) = *HEADER;
206  //delete old data
207  delete[] data_;
208  //save new data
209  data_ = ndata;
210  return true;
211}
212bool Gamestate::decompressData()
213{
214  assert(HEADER->compressed);
215  //COUT(4) << "GameStateClient: uncompressing gamestate. id: " << a->id << ", baseid: " << a->base_id << ", normsize: " << a->normsize << ", compsize: " << a->compsize << std::endl;
216  int normsize = HEADER->normsize;
217  int compsize = HEADER->compsize;
218  int bufsize;
219  if(normsize < compsize)
220    bufsize = compsize;
221  else
222    bufsize = normsize;
223  if(bufsize==0)
224    return NULL;
225  unsigned char *ndata = new unsigned char[bufsize + sizeof(GamestateHeader)];
226  unsigned char *dest = ndata + sizeof(GamestateHeader);
227  int retval;
228  uLongf length=normsize;
229  retval = uncompress( dest, &length, data_+sizeof(GamestateHeader), (uLong)compsize );
230  switch ( retval ) {
231    case Z_OK: COUT(5) << "successfully decompressed" << std::endl; break;
232    case Z_MEM_ERROR: COUT(1) << "not enough memory available" << std::endl; return false;
233    case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer" << std::endl; return false;
234    case Z_DATA_ERROR: COUT(2) << "data corrupted (zlib)" << std::endl; return false;
235  }
236 
237  HEADER->compressed = false;
238  //copy over the header
239  *GAMESTATE_HEADER(ndata) = *HEADER;
240  //delete old (compressed data)
241  delete[] data_;
242  //set new pointers and create bytestream
243  data_ = ndata;
244  //bs_ = new Bytestream(getGs(), GAMESTATE_HEADER->normsize);
245 
246  return true;
247}
248
249Gamestate *Gamestate::diff(Gamestate *base)
250{
251  //unsigned char *basep = base->getGs()/*, *gs = getGs()*/;
252  unsigned char *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_);
253  unsigned int of=0; // pointers offset
254  unsigned int dest_length=0;
255  dest_length=HEADER->normsize;
256  if(dest_length==0)
257    return NULL;
258  unsigned char *ndata = new unsigned char[dest_length*sizeof(unsigned char)+sizeof(GamestateHeader)];
259  unsigned char *dest = ndata + sizeof(GamestateHeader);
260  while(of < GAMESTATE_HEADER(base->data_)->normsize && of < HEADER->normsize){
261    *(dest+of)=*(basep+of)^*(gs+of); // do the xor
262    ++of;
263  }
264  if(GAMESTATE_HEADER(base->data_)->normsize!=HEADER->normsize){
265    unsigned char n=0;
266    if(GAMESTATE_HEADER(base->data_)->normsize < HEADER->normsize){
267      while(of<dest_length){
268        *(dest+of)=n^*(gs+of);
269        of++;
270      }
271    }
272  }
273
274  *GAMESTATE_HEADER(ndata) = *HEADER;
275  GAMESTATE_HEADER(ndata)->diffed = true;
276  Gamestate *g = new Gamestate(ndata, 0);
277  return g;
278}
279
280Gamestate *Gamestate::undiff(Gamestate *base)
281{
282  assert(this && base);
283  assert(!HEADER->compressed && !GAMESTATE_HEADER(base->data_)->compressed);
284  //unsigned char *basep = base->getGs()/*, *gs = getGs()*/;
285  unsigned char *basep = GAMESTATE_START(base->data_);
286  unsigned char *gs = GAMESTATE_START(this->data_);
287  unsigned int of=0; // pointers offset
288  unsigned int dest_length=0;
289  dest_length=HEADER->normsize;
290  if(dest_length==0)
291    return NULL;
292  unsigned char *ndata = new unsigned char[dest_length*sizeof(unsigned char)+sizeof(GamestateHeader)];
293  unsigned char *dest = ndata + sizeof(GamestateHeader);
294  while(of < GAMESTATE_HEADER(base->data_)->normsize && of < HEADER->normsize){
295    *(dest+of)=*(basep+of)^*(gs+of); // do the xor
296    ++of;
297  }
298  if(GAMESTATE_HEADER(base->data_)->normsize!=HEADER->normsize){
299    unsigned char n=0;
300    if(GAMESTATE_HEADER(base->data_)->normsize < HEADER->normsize){
301      while(of < dest_length){
302        *(dest+of)=n^*(gs+of);
303        of++;
304      }
305    }
306  }
307  *GAMESTATE_HEADER(ndata) = *HEADER;
308  GAMESTATE_HEADER(ndata)->diffed = false;
309  Gamestate *g = new Gamestate(ndata, 0);
310  return g;
311}
312
313
314unsigned int Gamestate::calcGamestateSize(int mode)
315{
316  int size=0;
317    // get the start of the Synchronisable list
318  orxonox::Iterator<Synchronisable> it;
319    // get total size of gamestate
320  for(it = orxonox::ObjectList<Synchronisable>::start(); it; ++it)
321    size+=it->getSize2(mode); // size of the actual data of the synchronisable
322//  size+=sizeof(GamestateHeader);
323  return size;
324}
325
326/**
327 * This function removes a Synchronisable out of the universe
328 * @param it iterator of the list pointing to the object
329 * @return iterator pointing to the next object in the list
330 */
331  void Gamestate::removeObject(orxonox::Iterator<Synchronisable> &it) {
332    orxonox::Iterator<Synchronisable> temp=it;
333    ++it;
334    delete  *temp;
335  }
336
337  bool Gamestate::isDiffed(){
338    return HEADER->diffed;
339  }
340 
341  int Gamestate::getBaseID(){
342    return HEADER->base_id;
343  }
344}
345
346}
Note: See TracBrowser for help on using the repository browser.