Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 28, 2008, 11:09:34 PM (16 years ago)
Author:
scheusso
Message:

diffing work now: wohoo

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/network3/src/network/GameStateClient.cc

    r1179 r1199  
    3434#include "core/BaseObject.h"
    3535#include "Synchronisable.h"
     36
     37#define GAMESTATEID_INITIAL -1
    3638
    3739namespace network
     
    4446  GameStateClient::GameStateClient() {
    4547    COUT(5) << "this: " << this << std::endl;
     48    last_diff_=0;
    4649  }
    4750
     
    5053
    5154  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){
     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){
    6265        COUT(4) << "pushGameState: no reference found to diff" << std::endl;
    6366        return false;
    6467      }
    65       gs = decode(gameStateList.front(), compstate);
     68      gs = decode(reference, compstate);
    6669    }
    6770    else
    6871      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    }
    7185    COUT(4) << "could not use gamestate sent by server" << std::endl;
    7286    return false;
     
    117131          ///sigsegv may happen here again for some reason
    118132          ///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());
    120139          COUT(4) << "loadsnapshort: classid: " << sync.classID << " objectID: " << sync.objectID << " length: " << sync.length << std::endl;
    121140          no->objectID=sync.objectID;
     
    140159  }
    141160
    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;
    144163    int of=0; // pointers offset
    145164    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;
    148167    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){
    152172      *(dp+of)=*(ap+of)^*(bp+of); // do the xor
    153173      ++of;
    154174    }
    155     if(a->size!=b->size){ // do we have to fill up ?
     175    if(old->size!=diff->size){ // do we have to fill up ?
    156176      unsigned char n=0;
    157       if(a->size<b->size){
     177      if(old->size<diff->size){
    158178        while(of<dest_length){
    159179          *(dp+of)=n^*(bp+of);
     
    170190    // FIXME: is it true or false now? (struct has changed, producing warnings)
    171191    GameState *r = new GameState;
    172     r->id = b->id;
     192    r->id = diff->id;
    173193    r->size = dest_length;
     194    r->base_id = diff->base_id;
    174195    r->diffed = false;
    175196    r->data = dp;
     
    196217    else
    197218      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];
    199221    int retval;
    200222    uLongf length=normsize;
     
    216238    gamestate->diffed = a->diffed;
    217239
    218     delete a->data; //delete compressed data
     240    delete[] a->data; //delete compressed data
    219241    delete a; //we do not need the old (struct) gamestate anymore
    220242
     
    222244  }
    223245
    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;
    229251  }
    230252
     
    237259    t->data = x->data;
    238260    t->size = x->normsize;
    239     gameStateList.push(t);
    240261    return t;
    241262  }
    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 
    243287}
Note: See TracChangeset for help on using the changeset viewer.