Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/network/GameStateClient.cc @ 1088

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

catched some return values

File size: 7.6 KB
Line 
1/*
2*   ORXONOX - the hottest 3D action shooter ever to exist
3*
4*
5*   License notice:
6*
7*   This program is free software; you can redistribute it and/or
8*   modify it under the terms of the GNU General Public License
9*   as published by the Free Software Foundation; either version 2
10*   of the License, or (at your option) any later version.
11*
12*   This program is distributed in the hope that it will be useful,
13*   but WITHOUT ANY WARRANTY; without even the implied warranty of
14*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*   GNU General Public License for more details.
16*
17*   You should have received a copy of the GNU General Public License
18*   along with this program; if not, write to the Free Software
19*   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20*
21*   Author:
22*      ...
23*   Co-authors:
24*      ...
25*
26*/
27#include <zlib.h>
28
29#include "core/CoreIncludes.h"
30#include "core/BaseObject.h"
31#include "Synchronisable.h"
32#include "GameStateClient.h"
33
34namespace network
35{
36  struct GameStateItem{
37    GameState *state;
38    int id;
39  };
40 
41  GameStateClient::GameStateClient() {
42    COUT(5) << "this: " << this << std::endl;
43  }
44
45  GameStateClient::~GameStateClient() {
46  }
47
48  bool GameStateClient::pushGameState(GameStateCompressed *compstate) {
49    GameState *gs;
50    if(compstate->diffed){
51      while(compstate->base_id > gameStateList.front()->id){
52        // clean up old gamestates
53        free(gameStateList.front()->data);
54        // TODO: critical section
55        delete gameStateList.front();
56        gameStateList.pop();
57      }
58      if(compstate->base_id!=gameStateList.front()->id){
59        COUT(4) << "pushGameState: no reference found to diff" << std::endl;
60        return false;
61      }
62      gs = decode(gameStateList.front(), compstate);
63    }
64    else
65      gs = decode(compstate);
66    if(gs)
67      return loadSnapshot(gs);
68    COUT(4) << "could not use gamestate sent by server" << std::endl;
69    return false;
70  }
71
72  /**
73  * This function removes a Synchronisable out of the universe
74  * @param it iterator of the list pointing to the object
75  * @return iterator pointing to the next object in the list
76  */
77  void GameStateClient::removeObject(orxonox::Iterator<Synchronisable> &it) {
78    orxonox::Iterator<Synchronisable> temp=it;
79    ++it;
80    delete  *temp;
81  }
82
83  /**
84  * This function loads a Snapshort of the gamestate into the universe
85  * @param state a GameState struct containing the size of the gamestate and a pointer linking to a flat list (returned by getSnapshot)
86  */
87  bool GameStateClient::loadSnapshot(GameState *state) {
88    unsigned char *data=state->data;
89    COUT(4) << "loadSnapshot: loading gs: " << state->id << std::endl;
90    // get the start of the Synchronisable list
91    orxonox::Iterator<Synchronisable> it=orxonox::ObjectList<Synchronisable>::start();
92    syncData sync;
93    // loop as long as we have some data ;)
94    while(data < state->data+state->size){
95      // prepare the syncData struct
96      sync.length = (int)*data;
97      data+=sizeof(int);
98      sync.objectID = (int)*data;
99      data+=sizeof(int);
100      sync.classID = (int)*data;
101      data+=sizeof(int);
102      sync.data = data;
103      data+=sync.length;
104
105      if(it->objectID!=sync.objectID){
106        // bad luck ;)
107        // delete the synchronisable (obviously seems to be deleted on the server)
108        while(it && it->objectID!=sync.objectID)
109          removeObject(it);
110       
111       
112        if(!it){
113          COUT(4) << "loadSnapshot:\tclassid: " << sync.classID << ", name: " << ID((unsigned int) sync.classID)->getName() << std::endl;
114          Synchronisable *no = dynamic_cast<Synchronisable *>(ID((unsigned int) sync.classID)->fabricate());
115          no->objectID=sync.objectID;
116          no->classID=sync.classID;
117          it=orxonox::ObjectList<Synchronisable>::end();
118          // update data and create object/entity...
119          if( !no->updateData(sync) )
120            COUT(1) << "We couldn't update the object: " << sync.objectID << std::endl;
121          if( !no->create() )
122            COUT(1) << "We couldn't manifest (create() ) the object: " << sync.objectID << std::endl;
123        }
124      } else {
125        // we have our object
126        if(! it->updateData(sync))
127          COUT(1) << "We couldn't update objectID: " \
128          << sync.objectID << "; classID: " << sync.classID << std::endl;
129      }
130      ++it;
131    }
132
133    return true;
134  }
135
136  GameState *GameStateClient::undiff(GameState *a, GameState *b) {
137    unsigned char *ap = a->data, *bp = b->data;
138    int of=0; // pointers offset
139    int dest_length=0;
140    if(a->size>=b->size)
141      dest_length=a->size;
142    else
143      dest_length=b->size;
144    unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char));
145    while(of<a->size && of<b->size){
146      *(dp+of)=*(ap+of)^*(bp+of); // do the xor
147      ++of;
148    }
149    if(a->size!=b->size){ // do we have to fill up ?
150      unsigned char n=0;
151      if(a->size<b->size){
152        while(of<dest_length){
153          *(dp+of)=n^*(bp+of);
154          of++;
155        }
156      } else{
157        while(of<dest_length){
158          *(dp+of)=*(ap+of)^n;
159          of++;
160        }
161      }
162    }
163    // should be finished now
164    // FIXME: is it true or false now? (struct has changed, producing warnings)
165    GameState *r = new GameState;
166    r->id = b->id;
167    r->size = dest_length;
168    r->diffed = false;
169    r->data = dp;
170    return r;
171  }
172
173  //##### ADDED FOR TESTING PURPOSE #####
174  GameState* GameStateClient::testDecompress( GameStateCompressed* gc ) {
175    return decompress( gc );
176  }
177 
178  GameState* GameStateClient::testUndiff( GameState* g_old, GameState* g_diffed ) {
179    return undiff( g_old, g_diffed );
180  }
181  //##### ADDED FOR TESTING PURPOSE #####
182
183  GameState *GameStateClient::decompress(GameStateCompressed *a) {
184    //COUT(4) << "GameStateClient: uncompressing gamestate. id: " << a->id << ", baseid: " << a->base_id << ", normsize: " << a->normsize << ", compsize: " << a->compsize << std::endl;
185    int normsize = a->normsize;
186    int compsize = a->compsize;
187    int bufsize;
188    if(normsize < compsize)
189      bufsize = compsize;
190    else
191      bufsize = normsize;
192    unsigned char* dest = (unsigned char*)malloc( bufsize );
193    int retval;
194    uLongf length=normsize;
195    //std::cout << "gamestateclient" << std::endl;
196    //std::cout << "normsize " << a.normsize << " compsize " << a.compsize << " " << bufsize << std::endl;
197    retval = uncompress( dest, &length, a->data, (uLong)compsize );
198    //std::cout << "length " << length << std::endl;
199    switch ( retval ) {
200      case Z_OK: COUT(4) << "successfully decompressed" << std::endl; break;
201      case Z_MEM_ERROR: COUT(1) << "not enough memory available" << std::endl; return NULL;
202      case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer" << std::endl; return NULL;
203      case Z_DATA_ERROR: COUT(2) << "data corrupted (zlib)" << std::endl; return NULL;
204    }
205
206    GameState *gamestate = new GameState;
207    gamestate->id = a->id;
208    gamestate->size = normsize;
209    gamestate->data = dest;
210    gamestate->diffed = a->diffed;
211   
212    delete a->data; //delete compressed data
213    delete a; //we do not need the old (struct) gamestate anymore
214
215    return gamestate;
216  }
217
218  GameState *GameStateClient::decode(GameState *a, GameStateCompressed *x) {
219    GameState *t = decode(x);
220    gameStateList.push(t);
221    //return undiff(a, t);
222    return t;
223  }
224
225  GameState *GameStateClient::decode(GameStateCompressed *x) {
226    //GameState *t = decompress(x);
227    GameState *t = new GameState;
228    t->base_id = x->base_id;
229    t->id = x->id;
230    t->diffed = x->diffed;
231    t->data = x->data;
232    t->size = x->normsize;
233    gameStateList.push(t);
234    return t;
235  }
236
237}
Note: See TracBrowser for help on using the repository browser.