Changeset 1199 for code/branches/network3/src/network/GameStateClient.cc
- Timestamp:
- Apr 28, 2008, 11:09:34 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/network3/src/network/GameStateClient.cc
r1179 r1199 34 34 #include "core/BaseObject.h" 35 35 #include "Synchronisable.h" 36 37 #define GAMESTATEID_INITIAL -1 36 38 37 39 namespace network … … 44 46 GameStateClient::GameStateClient() { 45 47 COUT(5) << "this: " << this << std::endl; 48 last_diff_=0; 46 49 } 47 50 … … 50 53 51 54 bool GameStateClient::pushGameState(GameStateCompressed *compstate) { 52 GameState *gs;53 if(compstate->diffed){54 while(compstate->base_id > gameStateList.front()->id){55 // clean up old gamestates56 free(gameStateList.front()->data);57 // TODO: critical section58 delete gameStateList.front();59 gameStateList.pop();60 }61 if( compstate->base_id!=gameStateList.front()->id){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){ 62 65 COUT(4) << "pushGameState: no reference found to diff" << std::endl; 63 66 return false; 64 67 } 65 gs = decode( gameStateList.front(), compstate);68 gs = decode(reference, compstate); 66 69 } 67 70 else 68 71 gs = decode(compstate); 69 if(gs) 70 return loadSnapshot(gs); 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 } 71 85 COUT(4) << "could not use gamestate sent by server" << std::endl; 72 86 return false; … … 117 131 ///sigsegv may happen here again for some reason 118 132 ///sigsegv is receved after the COUT(4) above 119 Synchronisable *no = dynamic_cast<Synchronisable *>(ID((unsigned int) sync.classID)->fabricate()); 133 orxonox::Identifier* id = ID((unsigned int)sync.classID); 134 if(!id){ 135 COUT(4) << "We could not identify a new object" << std::endl; 136 continue; 137 } 138 Synchronisable *no = dynamic_cast<Synchronisable *>(id->fabricate()); 120 139 COUT(4) << "loadsnapshort: classid: " << sync.classID << " objectID: " << sync.objectID << " length: " << sync.length << std::endl; 121 140 no->objectID=sync.objectID; … … 140 159 } 141 160 142 GameState *GameStateClient::undiff(GameState * a, GameState *b) {143 unsigned char *ap = a->data, *bp = b->data;161 GameState *GameStateClient::undiff(GameState *old, GameState *diff) { 162 unsigned char *ap = old->data, *bp = diff->data; 144 163 int of=0; // pointers offset 145 164 int dest_length=0; 146 if( a->size>=b->size)147 dest_length= a->size;165 if(old->size>=diff->size) 166 dest_length=old->size; 148 167 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){ 168 dest_length=diff->size; 169 // unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char)); 170 unsigned char *dp = new unsigned char[dest_length*sizeof(unsigned char)]; 171 while(of<old->size && of<diff->size){ 152 172 *(dp+of)=*(ap+of)^*(bp+of); // do the xor 153 173 ++of; 154 174 } 155 if( a->size!=b->size){ // do we have to fill up ?175 if(old->size!=diff->size){ // do we have to fill up ? 156 176 unsigned char n=0; 157 if( a->size<b->size){177 if(old->size<diff->size){ 158 178 while(of<dest_length){ 159 179 *(dp+of)=n^*(bp+of); … … 170 190 // FIXME: is it true or false now? (struct has changed, producing warnings) 171 191 GameState *r = new GameState; 172 r->id = b->id;192 r->id = diff->id; 173 193 r->size = dest_length; 194 r->base_id = diff->base_id; 174 195 r->diffed = false; 175 196 r->data = dp; … … 196 217 else 197 218 bufsize = normsize; 198 unsigned char* dest = (unsigned char*)malloc( bufsize ); 219 // unsigned char* dest = (unsigned char*)malloc( bufsize ); 220 unsigned char *dest = new unsigned char[bufsize]; 199 221 int retval; 200 222 uLongf length=normsize; … … 216 238 gamestate->diffed = a->diffed; 217 239 218 delete a->data; //delete compressed data240 delete[] a->data; //delete compressed data 219 241 delete a; //we do not need the old (struct) gamestate anymore 220 242 … … 222 244 } 223 245 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;246 GameState *GameStateClient::decode(GameState *old, GameStateCompressed *diff) { 247 COUT(4) << "using diffed gamestate" << std::endl; 248 GameState *t = decode(diff); 249 return undiff(old, t); 250 // return t; 229 251 } 230 252 … … 237 259 t->data = x->data; 238 260 t->size = x->normsize; 239 gameStateList.push(t);240 261 return t; 241 262 } 242 263 264 void GameStateClient::cleanup(){ 265 std::map<int, GameState*>::iterator temp, it = gameStateMap.begin(); 266 while(it!=gameStateMap.end()){ 267 if(it->first>=last_diff_) 268 break; 269 // otherwise delete that stuff 270 delete[] (*it).second->data; 271 delete (*it).second; 272 temp=it++; 273 gameStateMap.erase(temp); 274 } 275 } 276 277 void GameStateClient::printGameStateMap(){ 278 std::map<int, GameState*>::iterator it; 279 COUT(4) << "gamestates: "; 280 for(it=gameStateMap.begin(); it!=gameStateMap.end(); it++){ 281 COUT(4) << it->first << ":" << it->second << "|"; 282 } 283 COUT(4) << std::endl; 284 285 } 286 243 287 }
Note: See TracChangeset
for help on using the changeset viewer.