Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network3/src/network/GameStateClient.cc @ 1251

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

fixed some memory leaks (not all though)

File size: 14.0 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 *      ...
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "GameStateClient.h"
30
31#include <zlib.h>
32
33#include "core/CoreIncludes.h"
34#include "core/BaseObject.h"
35#include "Synchronisable.h"
36
37#define GAMESTATEID_INITIAL -1
38
39namespace network
40{
41  struct GameStateItem{
42    GameState *state;
43    int id;
44  };
45 
46  GameStateClient::GameStateClient() {
47    COUT(5) << "this: " << this << std::endl;
48    last_diff_=0;
49  }
50
51  GameStateClient::~GameStateClient() {
52  }
53
54  bool GameStateClient::pushGameState(GameStateCompressed *compstate) {
55    cleanup();
56    printGameStateMap();
57    GameState *gs, *reference;
58    if(compstate->diffed && compstate->base_id!=GAMESTATEID_INITIAL){
59      std::map<int, GameState*>::iterator it = gameStateMap.find(compstate->base_id);
60      if(it!=gameStateMap.end())
61        reference = (it)->second;
62      else
63        reference = NULL;
64      if(!reference){
65        COUT(4) << "pushGameState: no reference found to diff" << std::endl;
66        return false;
67      }
68      gs = decode(reference, compstate);
69    }
70    else
71      gs = decode(compstate);
72    if(gs){
73      if (loadSnapshot(gs)){
74        gameStateMap.insert(std::pair<int, GameState*>(gs->id, gs));
75        COUT(4) << "adding decoded gs with id: " << gs->id << " diffed from: " << gs->base_id << std::endl;
76        last_diff_=gs->base_id;
77        return true;
78      }else{
79        COUT(4) << "could not decode gs with id: " << gs->id << " diffed from: " << gs->base_id << std::endl;
80        delete[] gs->data;
81        delete gs;
82        return false;
83      }
84    }
85    COUT(4) << "could not use gamestate sent by server" << std::endl;
86    return false;
87  }
88 
89  GameStateCompressed *GameStateClient::popPartialGameState(){
90    GameState *gs = getPartialSnapshot();
91    return compress_(gs);
92  }
93 
94
95  /**
96  * This function removes a Synchronisable out of the universe
97  * @param it iterator of the list pointing to the object
98  * @return iterator pointing to the next object in the list
99  */
100  void GameStateClient::removeObject(orxonox::Iterator<Synchronisable> &it) {
101    orxonox::Iterator<Synchronisable> temp=it;
102    ++it;
103    delete  *temp;
104  }
105
106  /**
107  * This function loads a Snapshort of the gamestate into the universe
108  * @param state a GameState struct containing the size of the gamestate and a pointer linking to a flat list (returned by getSnapshot)
109  */
110  bool GameStateClient::loadSnapshot(GameState *state) {
111    unsigned char *data=state->data;
112    COUT(4) << "loadSnapshot: loading gs: " << state->id << std::endl;
113    // get the start of the Synchronisable list
114    orxonox::Iterator<Synchronisable> it=orxonox::ObjectList<Synchronisable>::start();
115    syncData sync;
116    // loop as long as we have some data ;)
117    while(data < state->data+state->size){
118      // prepare the syncData struct
119      sync.length = *(int *)data;
120      data+=sizeof(int);
121      sync.objectID = *(int*)data;
122      data+=sizeof(int);
123      sync.classID = *(int*)data;
124      data+=sizeof(int);
125      sync.data = data;
126      data+=sync.length;
127
128      if(!it || it->objectID!=sync.objectID){
129        // bad luck ;)
130        // delete the synchronisable (obviously seems to be deleted on the server)
131        while(it && it->objectID!=sync.objectID)
132          removeObject(it);
133
134
135        if(!it){
136          //COUT(4) << "loadSnapshot:\tclassid: " << sync.classID << ", name: " << ID((unsigned int) sync.classID)->getName() << std::endl;
137          ///sigsegv may happen here again for some reason
138          ///sigsegv is receved after the COUT(4) above
139          orxonox::Identifier* id = ID((unsigned int)sync.classID);
140          if(!id){
141            COUT(4) << "We could not identify a new object; classid: " << sync.classID << std::endl;
142            continue;
143          }
144          Synchronisable *no = dynamic_cast<Synchronisable *>(id->fabricate());
145          COUT(4) << "loadsnapshot: classid: " << sync.classID << " objectID: " << sync.objectID << " length: " << sync.length << std::endl;
146          no->objectID=sync.objectID;
147          no->classID=sync.classID;
148          // update data and create object/entity...
149          if( !no->updateData(sync) )
150            COUT(1) << "We couldn't update the object: " << sync.objectID << std::endl;
151          if( !no->create() )
152            COUT(1) << "We couldn't manifest (create() ) the object: " << sync.objectID << std::endl;
153          it=orxonox::ObjectList<Synchronisable>::end();
154        }
155      } else {
156        // we have our object
157        if(! it->updateData(sync))
158          COUT(1) << "We couldn't update objectID: " \
159          << sync.objectID << "; classID: " << sync.classID << std::endl;
160      }
161      ++it;
162    }
163
164    return true;
165  }
166
167  GameState *GameStateClient::getPartialSnapshot(){
168   
169    GameState *reference;
170//     std::map<int, GameState*>::iterator it = --gameStateMap.end();
171//     reference=(--gameStateMap.end())->second;
172   
173    //std::cout << "begin getSnapshot" << std::endl;
174    //the size of the gamestate
175    int totalsize=0;
176    int memsize=0;
177    //the size of one specific synchronisable
178    int tempsize=0;
179    // get the start of the Synchronisable list
180    orxonox::Iterator<Synchronisable> it;
181    // struct for return value of Synchronisable::getData()
182    syncData sync;
183
184    GameState *retval=new GameState; //return value
185//     retval->id=reference->id;
186    if(gameStateMap.size()!=0)
187      retval->id=(--gameStateMap.end())->second->id;
188    retval->diffed=false;
189    retval->complete=false;
190    COUT(4) << "G.ST.Client: producing partial gamestate with id: " << retval->id << std::endl;
191    // offset of memory functions
192    int offset=0, size=0;
193    // get total size of gamestate
194    for(it = orxonox::ObjectList<Synchronisable>::start(); it; ++it){
195      if(!it->getBacksync())
196        continue;
197      size+=it->getSize(); // size of the actual data of the synchronisable
198      size+=3*sizeof(int); // size of datasize, classID and objectID
199      COUT(4) << "getpartialsnapshot: size: " << size << std::endl;
200    }
201    //retval->data = (unsigned char*)malloc(size);
202    retval->data = new unsigned char[size];
203    if(!retval->data){
204      COUT(2) << "GameStateClient: could not allocate memory" << std::endl;
205      return NULL;
206    }
207    memsize=size;
208    // go through all Synchronisables
209    for(it = orxonox::ObjectList<Synchronisable>::start(); it; ++it){
210      if(!it->getBacksync())
211        continue;
212      //get size of the synchronisable
213      tempsize=it->getSize();
214      // add place for data and 3 ints (length,classid,objectid)
215      totalsize+=tempsize+3*sizeof(int);
216      // allocate+tempsize additional space
217      if(totalsize > size){
218        COUT(3) << "G.St.Cl: need additional memory" << std::endl;
219      }
220
221      // run Synchronisable::getData with offset and additional place for 3 ints in between (for ids and length)
222      sync=it->getData((retval->data)+offset+3*sizeof(int));
223      memcpy(retval->data+offset, (void *)&(sync.length), sizeof(int));
224      memcpy(retval->data+offset+sizeof(int), (void *)&(sync.objectID), sizeof(int));
225      memcpy(retval->data+offset+2*sizeof(int), (void *)&(sync.classID), sizeof(int));
226      // increase data pointer
227      offset+=tempsize+3*sizeof(int);
228    }
229    retval->size=totalsize;
230    COUT(5) << "G.ST.Cl: Gamestate size: " << totalsize << std::endl;
231    COUT(5) << "G.ST.Cl: 'estimated' Gamestate size: " << size << std::endl;
232    return retval;
233  }
234 
235 
236  GameState *GameStateClient::undiff(GameState *old, GameState *diff) {
237    if(!old || !diff)
238      return NULL;
239    unsigned char *ap = old->data, *bp = diff->data;
240    int of=0; // pointers offset
241    int dest_length=0;
242    if(old->size>=diff->size)
243      dest_length=old->size;
244    else
245      dest_length=diff->size;
246//     unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char));
247    unsigned char *dp = new unsigned char[dest_length*sizeof(unsigned char)];
248    while(of<old->size && of<diff->size){
249      *(dp+of)=*(ap+of)^*(bp+of); // do the xor
250      ++of;
251    }
252    if(old->size!=diff->size){ // do we have to fill up ?
253      unsigned char n=0;
254      if(old->size<diff->size){
255        while(of<dest_length){
256          *(dp+of)=n^*(bp+of);
257          of++;
258        }
259      } else{
260        while(of<dest_length){
261          *(dp+of)=*(ap+of)^n;
262          of++;
263        }
264      }
265    }
266    // should be finished now
267    // FIXME: is it true or false now? (struct has changed, producing warnings)
268    GameState *r = new GameState;
269    r->id = diff->id;
270    r->size = dest_length;
271    r->base_id = old->id;
272    r->diffed = false;
273    r->data = dp;
274    r->complete = true;
275    return r;
276  }
277
278
279
280  GameStateCompressed *GameStateClient::compress_(GameState *a) {
281    if(!a)
282      return NULL;
283    int size = a->size;
284
285    uLongf buffer = (uLongf)((a->size + 12)*1.01)+1;
286    unsigned char *dest = new unsigned char[buffer];
287    int retval;
288    retval = compress( dest, &buffer, a->data, (uLong)size );
289
290    switch ( retval ) {
291      case Z_OK: COUT(5) << "G.St.Cl: compress: successfully compressed" << std::endl; break;
292      case Z_MEM_ERROR: COUT(1) << "G.St.Cl: compress: not enough memory available in gamestate.compress" << std::endl; 
293      return NULL;
294      case Z_BUF_ERROR: COUT(2) << "G.St.Cl: compress: not enough memory available in the buffer in gamestate.compress" << std::endl;
295      return NULL;
296      case Z_DATA_ERROR: COUT(2) << "G.St.Cl: compress: data corrupted in gamestate.compress" << std::endl;
297      return NULL;
298    }
299
300    GameStateCompressed *compressedGamestate = new GameStateCompressed;
301    compressedGamestate->compsize = buffer;
302    compressedGamestate->normsize = size;
303    compressedGamestate->id = a->id;
304    compressedGamestate->data = dest;
305    compressedGamestate->diffed = a->diffed;
306    compressedGamestate->complete = a->complete;
307    compressedGamestate->base_id = a->base_id;
308    delete[] a->data;
309    delete a;
310    return compressedGamestate;
311  }
312 
313 
314  GameState *GameStateClient::decompress(GameStateCompressed *a) {
315    //COUT(4) << "GameStateClient: uncompressing gamestate. id: " << a->id << ", baseid: " << a->base_id << ", normsize: " << a->normsize << ", compsize: " << a->compsize << std::endl;
316    int normsize = a->normsize;
317    int compsize = a->compsize;
318    int bufsize;
319    if(normsize < compsize)
320      bufsize = compsize;
321    else
322      bufsize = normsize;
323//     unsigned char* dest = (unsigned char*)malloc( bufsize );
324    unsigned char *dest = new unsigned char[bufsize];
325    int retval;
326    uLongf length=normsize;
327    //std::cout << "gamestateclient" << std::endl;
328    //std::cout << "normsize " << a.normsize << " compsize " << a.compsize << " " << bufsize << std::endl;
329    retval = uncompress( dest, &length, a->data, (uLong)compsize );
330    //std::cout << "length " << length << std::endl;
331    switch ( retval ) {
332      case Z_OK: COUT(4) << "successfully decompressed" << std::endl; break;
333      case Z_MEM_ERROR: COUT(1) << "not enough memory available" << std::endl; return NULL;
334      case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer" << std::endl; return NULL;
335      case Z_DATA_ERROR: COUT(2) << "data corrupted (zlib)" << std::endl; return NULL;
336    }
337
338    GameState *gamestate = new GameState;
339    gamestate->id = a->id;
340    gamestate->size = normsize;
341    gamestate->data = dest;
342    gamestate->base_id = a->base_id;
343    gamestate->diffed = a->diffed;
344    gamestate->complete = a->complete;
345
346    delete[] a->data; //delete compressed data
347    delete a; //we do not need the old (struct) gamestate anymore
348
349    return gamestate;
350  }
351
352  GameState *GameStateClient::decode(GameState *old, GameStateCompressed *diff) {
353    COUT(4) << "using diffed gamestate" << std::endl;
354    GameState *t = decode(diff);
355    return undiff(old, t);
356//     return t;
357  }
358
359  GameState *GameStateClient::decode(GameStateCompressed *x) {
360    GameState *t = decompress(x);
361    /*GameState *t = new GameState;
362    t->base_id = x->base_id;
363    t->id = x->id;
364    t->diffed = x->diffed;
365    t->data = x->data;
366    t->size = x->normsize;*/
367    return t;
368  }
369 
370  void GameStateClient::cleanup(){
371    std::map<int, GameState*>::iterator temp, it = gameStateMap.begin();
372    while(it!=gameStateMap.end()){
373      if(it->first>=last_diff_)
374        break;
375      // otherwise delete that stuff
376      delete[] (*it).second->data;
377      delete (*it).second;
378      temp=it++;
379      gameStateMap.erase(temp);
380    }
381  }
382
383  void GameStateClient::printGameStateMap(){
384    std::map<int, GameState*>::iterator it;
385    COUT(4) << "gamestates: ";
386    for(it=gameStateMap.begin(); it!=gameStateMap.end(); it++){
387      COUT(4) << it->first << ":" << it->second << "|";
388    }
389    COUT(4) << std::endl;
390   
391  }
392 
393 
394    //##### ADDED FOR TESTING PURPOSE #####
395  GameState* GameStateClient::testDecompress( GameStateCompressed* gc ) {
396    return decompress( gc );
397  }
398 
399  GameState* GameStateClient::testUndiff( GameState* g_old, GameState* g_diffed ) {
400    return undiff( g_old, g_diffed );
401  }
402  //##### ADDED FOR TESTING PURPOSE #####
403 
404 
405}
406
Note: See TracBrowser for help on using the repository browser.