Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1176 was 1176, checked in by dumenim, 16 years ago

some output bugfix with objectID and classID - spagetti

File size: 8.1 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          ///sigsegv may happen here again for some reason
118          ///sigsegv is receved after the COUT(4) above
119          Synchronisable *no = dynamic_cast<Synchronisable *>(ID((unsigned int) sync.classID)->fabricate());
120          COUT(4) << "loadsnapshort: classid: " << sync.classID << " objectID: " << sync.classID << " length: " << sync.length << std::endl;
121          no->objectID=sync.objectID;
122          no->classID=sync.classID;
123          it=orxonox::ObjectList<Synchronisable>::end();
124          // update data and create object/entity...
125          if( !no->updateData(sync) )
126            COUT(1) << "We couldn't update the object: " << sync.objectID << std::endl;
127          if( !no->create() )
128            COUT(1) << "We couldn't manifest (create() ) the object: " << sync.objectID << std::endl;
129        }
130      } else {
131        // we have our object
132        if(! it->updateData(sync))
133          COUT(1) << "We couldn't update objectID: " \
134          << sync.objectID << "; classID: " << sync.classID << std::endl;
135      }
136      ++it;
137    }
138
139    return true;
140  }
141
142  GameState *GameStateClient::undiff(GameState *a, GameState *b) {
143    unsigned char *ap = a->data, *bp = b->data;
144    int of=0; // pointers offset
145    int dest_length=0;
146    if(a->size>=b->size)
147      dest_length=a->size;
148    else
149      dest_length=b->size;
150    unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char));
151    while(of<a->size && of<b->size){
152      *(dp+of)=*(ap+of)^*(bp+of); // do the xor
153      ++of;
154    }
155    if(a->size!=b->size){ // do we have to fill up ?
156      unsigned char n=0;
157      if(a->size<b->size){
158        while(of<dest_length){
159          *(dp+of)=n^*(bp+of);
160          of++;
161        }
162      } else{
163        while(of<dest_length){
164          *(dp+of)=*(ap+of)^n;
165          of++;
166        }
167      }
168    }
169    // should be finished now
170    // FIXME: is it true or false now? (struct has changed, producing warnings)
171    GameState *r = new GameState;
172    r->id = b->id;
173    r->size = dest_length;
174    r->diffed = false;
175    r->data = dp;
176    return r;
177  }
178
179  //##### ADDED FOR TESTING PURPOSE #####
180  GameState* GameStateClient::testDecompress( GameStateCompressed* gc ) {
181    return decompress( gc );
182  }
183 
184  GameState* GameStateClient::testUndiff( GameState* g_old, GameState* g_diffed ) {
185    return undiff( g_old, g_diffed );
186  }
187  //##### ADDED FOR TESTING PURPOSE #####
188
189  GameState *GameStateClient::decompress(GameStateCompressed *a) {
190    //COUT(4) << "GameStateClient: uncompressing gamestate. id: " << a->id << ", baseid: " << a->base_id << ", normsize: " << a->normsize << ", compsize: " << a->compsize << std::endl;
191    int normsize = a->normsize;
192    int compsize = a->compsize;
193    int bufsize;
194    if(normsize < compsize)
195      bufsize = compsize;
196    else
197      bufsize = normsize;
198    unsigned char* dest = (unsigned char*)malloc( bufsize );
199    int retval;
200    uLongf length=normsize;
201    //std::cout << "gamestateclient" << std::endl;
202    //std::cout << "normsize " << a.normsize << " compsize " << a.compsize << " " << bufsize << std::endl;
203    retval = uncompress( dest, &length, a->data, (uLong)compsize );
204    //std::cout << "length " << length << std::endl;
205    switch ( retval ) {
206      case Z_OK: COUT(4) << "successfully decompressed" << std::endl; break;
207      case Z_MEM_ERROR: COUT(1) << "not enough memory available" << std::endl; return NULL;
208      case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer" << std::endl; return NULL;
209      case Z_DATA_ERROR: COUT(2) << "data corrupted (zlib)" << std::endl; return NULL;
210    }
211
212    GameState *gamestate = new GameState;
213    gamestate->id = a->id;
214    gamestate->size = normsize;
215    gamestate->data = dest;
216    gamestate->diffed = a->diffed;
217
218    delete a->data; //delete compressed data
219    delete a; //we do not need the old (struct) gamestate anymore
220
221    return gamestate;
222  }
223
224  GameState *GameStateClient::decode(GameState *a, GameStateCompressed *x) {
225    GameState *t = decode(x);
226    gameStateList.push(t);
227    //return undiff(a, t);
228    return t;
229  }
230
231  GameState *GameStateClient::decode(GameStateCompressed *x) {
232    //GameState *t = decompress(x);
233    GameState *t = new GameState;
234    t->base_id = x->base_id;
235    t->id = x->id;
236    t->diffed = x->diffed;
237    t->data = x->data;
238    t->size = x->normsize;
239    gameStateList.push(t);
240    return t;
241  }
242
243}
Note: See TracBrowser for help on using the repository browser.