Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network2/src/network/GameStateClient.cc @ 1132

Last change on this file since 1132 was 1106, checked in by scheusso, 17 years ago

one segfault less

File size: 7.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 *      ...
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
37namespace network
38{
39  struct GameStateItem{
40    GameState *state;
41    int id;
42  };
43 
44  GameStateClient::GameStateClient() {
45    COUT(5) << "this: " << this << std::endl;
46  }
47
48  GameStateClient::~GameStateClient() {
49  }
50
51  bool GameStateClient::pushGameState(GameStateCompressed *compstate) {
52    GameState *gs;
53    if(compstate->diffed){
54      while(compstate->base_id > gameStateList.front()->id){
55        // clean up old gamestates
56        free(gameStateList.front()->data);
57        // TODO: critical section
58        delete gameStateList.front();
59        gameStateList.pop();
60      }
61      if(compstate->base_id!=gameStateList.front()->id){
62        COUT(4) << "pushGameState: no reference found to diff" << std::endl;
63        return false;
64      }
65      gs = decode(gameStateList.front(), compstate);
66    }
67    else
68      gs = decode(compstate);
69    if(gs)
70      return loadSnapshot(gs);
71    COUT(4) << "could not use gamestate sent by server" << std::endl;
72    return false;
73  }
74
75  /**
76  * This function removes a Synchronisable out of the universe
77  * @param it iterator of the list pointing to the object
78  * @return iterator pointing to the next object in the list
79  */
80  void GameStateClient::removeObject(orxonox::Iterator<Synchronisable> &it) {
81    orxonox::Iterator<Synchronisable> temp=it;
82    ++it;
83    delete  *temp;
84  }
85
86  /**
87  * This function loads a Snapshort of the gamestate into the universe
88  * @param state a GameState struct containing the size of the gamestate and a pointer linking to a flat list (returned by getSnapshot)
89  */
90  bool GameStateClient::loadSnapshot(GameState *state) {
91    unsigned char *data=state->data;
92    COUT(4) << "loadSnapshot: loading gs: " << state->id << std::endl;
93    // get the start of the Synchronisable list
94    orxonox::Iterator<Synchronisable> it=orxonox::ObjectList<Synchronisable>::start();
95    syncData sync;
96    // loop as long as we have some data ;)
97    while(data < state->data+state->size){
98      // prepare the syncData struct
99      sync.length = (int)*data;
100      data+=sizeof(int);
101      sync.objectID = (int)*data;
102      data+=sizeof(int);
103      sync.classID = (int)*data;
104      data+=sizeof(int);
105      sync.data = data;
106      data+=sync.length;
107
108      if(!it || it->objectID!=sync.objectID){
109        // bad luck ;)
110        // delete the synchronisable (obviously seems to be deleted on the server)
111        while(it && it->objectID!=sync.objectID)
112          removeObject(it);
113
114
115        if(!it){
116          COUT(4) << "loadSnapshot:\tclassid: " << sync.classID << ", name: " << ID((unsigned int) sync.classID)->getName() << std::endl;
117          Synchronisable *no = dynamic_cast<Synchronisable *>(ID((unsigned int) sync.classID)->fabricate());
118          no->objectID=sync.objectID;
119          no->classID=sync.classID;
120          it=orxonox::ObjectList<Synchronisable>::end();
121          // update data and create object/entity...
122          if( !no->updateData(sync) )
123            COUT(1) << "We couldn't update the object: " << sync.objectID << std::endl;
124          if( !no->create() )
125            COUT(1) << "We couldn't manifest (create() ) the object: " << sync.objectID << std::endl;
126        }
127      } else {
128        // we have our object
129        if(! it->updateData(sync))
130          COUT(1) << "We couldn't update objectID: " \
131          << sync.objectID << "; classID: " << sync.classID << std::endl;
132      }
133      ++it;
134    }
135
136    return true;
137  }
138
139  GameState *GameStateClient::undiff(GameState *a, GameState *b) {
140    unsigned char *ap = a->data, *bp = b->data;
141    int of=0; // pointers offset
142    int dest_length=0;
143    if(a->size>=b->size)
144      dest_length=a->size;
145    else
146      dest_length=b->size;
147    unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char));
148    while(of<a->size && of<b->size){
149      *(dp+of)=*(ap+of)^*(bp+of); // do the xor
150      ++of;
151    }
152    if(a->size!=b->size){ // do we have to fill up ?
153      unsigned char n=0;
154      if(a->size<b->size){
155        while(of<dest_length){
156          *(dp+of)=n^*(bp+of);
157          of++;
158        }
159      } else{
160        while(of<dest_length){
161          *(dp+of)=*(ap+of)^n;
162          of++;
163        }
164      }
165    }
166    // should be finished now
167    // FIXME: is it true or false now? (struct has changed, producing warnings)
168    GameState *r = new GameState;
169    r->id = b->id;
170    r->size = dest_length;
171    r->diffed = false;
172    r->data = dp;
173    return r;
174  }
175
176  //##### ADDED FOR TESTING PURPOSE #####
177  GameState* GameStateClient::testDecompress( GameStateCompressed* gc ) {
178    return decompress( gc );
179  }
180 
181  GameState* GameStateClient::testUndiff( GameState* g_old, GameState* g_diffed ) {
182    return undiff( g_old, g_diffed );
183  }
184  //##### ADDED FOR TESTING PURPOSE #####
185
186  GameState *GameStateClient::decompress(GameStateCompressed *a) {
187    //COUT(4) << "GameStateClient: uncompressing gamestate. id: " << a->id << ", baseid: " << a->base_id << ", normsize: " << a->normsize << ", compsize: " << a->compsize << std::endl;
188    int normsize = a->normsize;
189    int compsize = a->compsize;
190    int bufsize;
191    if(normsize < compsize)
192      bufsize = compsize;
193    else
194      bufsize = normsize;
195    unsigned char* dest = (unsigned char*)malloc( bufsize );
196    int retval;
197    uLongf length=normsize;
198    //std::cout << "gamestateclient" << std::endl;
199    //std::cout << "normsize " << a.normsize << " compsize " << a.compsize << " " << bufsize << std::endl;
200    retval = uncompress( dest, &length, a->data, (uLong)compsize );
201    //std::cout << "length " << length << std::endl;
202    switch ( retval ) {
203      case Z_OK: COUT(4) << "successfully decompressed" << std::endl; break;
204      case Z_MEM_ERROR: COUT(1) << "not enough memory available" << std::endl; return NULL;
205      case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer" << std::endl; return NULL;
206      case Z_DATA_ERROR: COUT(2) << "data corrupted (zlib)" << std::endl; return NULL;
207    }
208
209    GameState *gamestate = new GameState;
210    gamestate->id = a->id;
211    gamestate->size = normsize;
212    gamestate->data = dest;
213    gamestate->diffed = a->diffed;
214
215    delete a->data; //delete compressed data
216    delete a; //we do not need the old (struct) gamestate anymore
217
218    return gamestate;
219  }
220
221  GameState *GameStateClient::decode(GameState *a, GameStateCompressed *x) {
222    GameState *t = decode(x);
223    gameStateList.push(t);
224    //return undiff(a, t);
225    return t;
226  }
227
228  GameState *GameStateClient::decode(GameStateCompressed *x) {
229    //GameState *t = decompress(x);
230    GameState *t = new GameState;
231    t->base_id = x->base_id;
232    t->id = x->id;
233    t->diffed = x->diffed;
234    t->data = x->data;
235    t->size = x->normsize;
236    gameStateList.push(t);
237    return t;
238  }
239
240}
Note: See TracBrowser for help on using the repository browser.