Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1747 was 1747, checked in by landauf, 17 years ago

merged core3 back to trunk

File size: 10.6 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/Iterator.h"
33
34#include <zlib.h>
35#include <assert.h>
36
37
38
39namespace network {
40
41namespace packet {
42
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 /*&& bs_==0*/);
66  int size = calcGamestateSize(mode);
67
68  COUT(4) << "G.ST.Man: producing gamestate with id: " << id << std::endl;
69    //retval->data = (unsigned char*)malloc(size);
70  if(size==0)
71    return false;
72  data_ = new unsigned char[size + sizeof(GamestateHeader)];
73  //bs_ = new Bytestream(data_+sizeof(GamestateHeader), size);
74  if(!data_){
75    COUT(2) << "GameStateManager: could not allocate memory" << std::endl;
76    return false;
77  }
78
79  //start collect data synchronisable by synchronisable
80  unsigned char *mem=data_;
81  mem+=sizeof(GamestateHeader);
82  orxonox::ObjectList<Synchronisable>::iterator it;
83  for(it = orxonox::ObjectList<Synchronisable>::begin(); it; ++it){
84    tempsize=it->getSize2(mode);
85
86    if(currentsize+tempsize > size){
87      // start allocate additional memory
88      COUT(3) << "G.St.Man: need additional memory" << std::endl;
89      orxonox::ObjectList<Synchronisable>::iterator temp = it;
90      int addsize=tempsize;
91      while(++temp)
92        addsize+=temp->getSize2(mode);
93      data_ = (unsigned char *)realloc(data_, sizeof(GamestateHeader) + currentsize + addsize);
94      if(!data_)
95        return false;
96      size = currentsize+addsize;
97    }// stop allocate additional memory
98
99    if(!it->getData2(mem, mode))
100      return false; // mem pointer gets automatically increased because of call by reference
101    // increase size counter by size of current synchronisable
102    currentsize+=tempsize;
103  }
104
105
106  //start write gamestate header
107  HEADER->packetType = ENUM::Gamestate;
108  assert( *(ENUM::Type *)(data_) == ENUM::Gamestate);
109  HEADER->normsize = currentsize;
110  HEADER->id = id;
111  HEADER->diffed = false;
112  HEADER->complete = true;
113  HEADER->compressed = false;
114  //stop write gamestate header
115
116  COUT(5) << "G.ST.Man: Gamestate size: " << currentsize << std::endl;
117  COUT(5) << "G.ST.Man: 'estimated' (and corrected) Gamestate size: " << size << std::endl;
118  return true;
119}
120
121bool Gamestate::spreadData(int mode)
122{
123  assert(data_ && !HEADER->compressed && !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::process()
178{
179  return GamestateHandler::addGamestate(this, getClientID());
180}
181
182bool Gamestate::compressData()
183{
184  assert(HEADER);
185  uLongf buffer = (uLongf)((HEADER->normsize + 12)*1.01)+1;
186  if(buffer==0)
187    return false;
188
189  unsigned char *ndata = new unsigned char[buffer+sizeof(GamestateHeader)];
190  unsigned char *dest = GAMESTATE_START(ndata);
191  int retval;
192  retval = compress( dest, &buffer, GAMESTATE_START(data_), (uLong)(HEADER->normsize) );
193  switch ( retval ) {
194    case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break;
195    case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl;
196    return false;
197    case Z_BUF_ERROR: COUT(2) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << std::endl;
198    return false;
199    case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl;
200    return false;
201  }
202
203  //copy and modify header
204  HEADER->compsize = buffer;
205  HEADER->compressed = true;
206  *GAMESTATE_HEADER(ndata) = *HEADER;
207  //delete old data
208  delete[] data_;
209  //save new data
210  data_ = ndata;
211  assert(HEADER->compressed);
212  COUT(3) << "gamestate compress normsize: " << HEADER->normsize << " compsize: " << HEADER->compsize << std::endl;
213  return true;
214}
215bool Gamestate::decompressData()
216{
217  assert(HEADER->compressed);
218  //COUT(4) << "GameStateClient: uncompressing gamestate. id: " << a->id << ", baseid: " << a->base_id << ", normsize: " << a->normsize << ", compsize: " << a->compsize << std::endl;
219  int normsize = HEADER->normsize;
220  int compsize = HEADER->compsize;
221  int bufsize;
222  if(normsize < compsize)
223    bufsize = compsize;
224  else
225    bufsize = normsize;
226  if(bufsize==0)
227    return false;
228  unsigned char *ndata = new unsigned char[bufsize + sizeof(GamestateHeader)];
229  unsigned char *dest = ndata + sizeof(GamestateHeader);
230  int retval;
231  uLongf length=normsize;
232  retval = uncompress( dest, &length, data_+sizeof(GamestateHeader), (uLong)compsize );
233  switch ( retval ) {
234    case Z_OK: COUT(5) << "successfully decompressed" << std::endl; break;
235    case Z_MEM_ERROR: COUT(1) << "not enough memory available" << std::endl; return false;
236    case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer" << std::endl; return false;
237    case Z_DATA_ERROR: COUT(2) << "data corrupted (zlib)" << std::endl; return false;
238  }
239
240  HEADER->compressed = false;
241  //copy over the header
242  *GAMESTATE_HEADER(ndata) = *HEADER;
243  //delete old (compressed data)
244  delete[] data_;
245  //set new pointers and create bytestream
246  data_ = ndata;
247  //bs_ = new Bytestream(getGs(), GAMESTATE_HEADER->normsize);
248
249  return true;
250}
251
252Gamestate *Gamestate::diff(Gamestate *base)
253{
254  //unsigned char *basep = base->getGs()/*, *gs = getGs()*/;
255  unsigned char *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_);
256  unsigned int of=0; // pointers offset
257  unsigned int dest_length=0;
258  dest_length=HEADER->normsize;
259  if(dest_length==0)
260    return NULL;
261  unsigned char *ndata = new unsigned char[dest_length*sizeof(unsigned char)+sizeof(GamestateHeader)];
262  unsigned char *dest = ndata + sizeof(GamestateHeader);
263  while(of < GAMESTATE_HEADER(base->data_)->normsize && of < HEADER->normsize){
264    *(dest+of)=*(basep+of)^*(gs+of); // do the xor
265    ++of;
266  }
267  if(GAMESTATE_HEADER(base->data_)->normsize!=HEADER->normsize){
268    unsigned char n=0;
269    if(GAMESTATE_HEADER(base->data_)->normsize < HEADER->normsize){
270      while(of<dest_length){
271        *(dest+of)=n^*(gs+of);
272        of++;
273      }
274    }
275  }
276
277  *GAMESTATE_HEADER(ndata) = *HEADER;
278  GAMESTATE_HEADER(ndata)->diffed = true;
279  Gamestate *g = new Gamestate(ndata, 0);
280  return g;
281}
282
283Gamestate *Gamestate::undiff(Gamestate *base)
284{
285  assert(this && base);
286  assert(!HEADER->compressed && !GAMESTATE_HEADER(base->data_)->compressed);
287  //unsigned char *basep = base->getGs()/*, *gs = getGs()*/;
288  unsigned char *basep = GAMESTATE_START(base->data_);
289  unsigned char *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  *GAMESTATE_HEADER(ndata) = *HEADER;
311  GAMESTATE_HEADER(ndata)->diffed = false;
312  Gamestate *g = new Gamestate(ndata, 0);
313  return g;
314}
315
316
317unsigned int Gamestate::calcGamestateSize(int mode)
318{
319  int size=0;
320    // get the start of the Synchronisable list
321  orxonox::ObjectList<Synchronisable>::iterator it;
322    // get total size of gamestate
323  for(it = orxonox::ObjectList<Synchronisable>::begin(); it; ++it)
324    size+=it->getSize2(mode); // size of the actual data of the synchronisable
325//  size+=sizeof(GamestateHeader);
326  return size;
327}
328
329/**
330 * This function removes a Synchronisable out of the universe
331 * @param it iterator of the list pointing to the object
332 * @return iterator pointing to the next object in the list
333 */
334  void Gamestate::removeObject(orxonox::ObjectList<Synchronisable>::iterator &it) {
335    orxonox::ObjectList<Synchronisable>::iterator temp=it;
336    ++it;
337    delete  *temp;
338  }
339
340  bool Gamestate::isDiffed(){
341    return HEADER->diffed;
342  }
343
344  int Gamestate::getBaseID(){
345    return HEADER->base_id;
346  }
347}
348
349}
Note: See TracBrowser for help on using the repository browser.