Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

diffing work now: wohoo

Location:
code/branches/network3/src/network
Files:
8 edited

Legend:

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

    r1184 r1199  
    230230    int id = data->id;
    231231    COUT(5) << "received gamestate id: " << data->id << std::endl;
    232     gamestate.pushGameState(data);
    233     client_connection.addPacket(pck_gen.acknowledgement(id));
    234     client_connection.sendPackets();
    235     return;
     232    if(gamestate.pushGameState(data)){
     233      client_connection.addPacket(pck_gen.acknowledgement(id));
     234      if(!client_connection.sendPackets())
     235        COUT(2) << "Could not send acknowledgment" << std::endl;
     236    }
    236237  }
    237238
  • 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}
  • code/branches/network3/src/network/GameStateClient.h

    r1168 r1199  
    4141#define _GameStateClient_H__
    4242
    43 #include <queue>
     43#include <map>
    4444
    4545#include "NetworkPrereqs.h"
     
    6161  private:
    6262    bool loadSnapshot(GameState *state);
    63     GameState *undiff(GameState *a, GameState *b);
     63    void cleanup();
     64    GameState *undiff(GameState *old, GameState *diff);
    6465    GameState *decompress(GameStateCompressed *a);
    65     GameState *decode(GameState *a, GameStateCompressed *x);
     66    GameState *decode(GameState *old, GameStateCompressed *diff);
    6667    GameState *decode(GameStateCompressed *x);
    6768    void removeObject(orxonox::Iterator<Synchronisable> &it);
     69    void printGameStateMap();
    6870
    6971    GameState     *reference;
    70     std::queue<GameState *> gameStateList;
     72    int           last_diff_;
     73    std::map<int, GameState *> gameStateMap;
    7174  };
    7275
  • code/branches/network3/src/network/GameStateManager.cc

    r1173 r1199  
    5252{
    5353  GameStateManager::GameStateManager(ClientInformation *head) {
    54     id=0;
     54    id_=0;
    5555    head_=head;
    5656  }
     
    6161  void GameStateManager::update(){
    6262    cleanup();
    63     reference = getSnapshot(id);
    64     gameStateMap.insert(std::pair<int, GameState*>(id, reference));
    65     gameStateUsed[id]=0;
    66     ++id;
     63    reference = getSnapshot();
     64    COUT(4) << "inserting gamestate: " << reference << std::endl;
     65    gameStateMap.insert(std::pair<int, GameState*>(id_, reference));
     66    gameStateUsed[id_]=0;
     67    printGameStates();
    6768    return;
    6869  }
     
    7980    while(it!=gameStateUsed.end()){
    8081      if( (*it).second <= 0 ){
    81         free(gameStateMap[(*it).first]->data);
     82        COUT(4) << "GameStateManager: deleting gamestate with id: " << (*it).first << ", uses: " << (*it).second << std::endl;
     83        delete[] gameStateMap[(*it).first]->data;
    8284        delete gameStateMap[(*it).first];
    8385        gameStateMap.erase((*it).first);
    8486        gameStateUsed.erase(it++);
    85       }else  //as soon as we got a used gamestate break here because we could use newer gamestates in future
     87        continue;
     88      }else if(id_-it->first<=KEEP_GAMESTATES){  //as soon as we got a used gamestate break here because we could use newer gamestates in future but only if we do not exceed KEEP_GAMESTATES # of gamestates in cache
     89        COUT(4) << "breaking " << std::endl;
    8690        break;
     91      }
     92      it++;
    8793    }
    8894  }
     
    9298    //Server::sendGameState?
    9399    int gID = head_->findClient(clientID)->getGamestateID();
    94     COUT(4) << "G.St.Man: popgamestate: sending gstate_id: " << id << " diffed from: " << gID << " (not diffed yet)" << std::endl;
    95    
     100    COUT(4) << "G.St.Man: popgamestate: sending gstate_id: " << id_ << " diffed from: " << gID << " (not diffed yet)" << std::endl;
     101//     COUT(3) << "gamestatemap: " << &gameStateMap << std::endl;
    96102    //chose wheather the next gamestate is the first or not
    97103    if(gID != GAMESTATEID_INITIAL){
    98       GameState *client = gameStateMap[gID];
     104      // TODO something with the gamestatemap is wrong
     105      GameState *client = gameStateMap.find(gID)->second;
    99106      GameState *server = reference;
    100107      //head_->findClient(clientID)->setGamestateID(id);
    101       return encode(client, server);
     108      COUT(3) << "client: " << client << " server: " << server << " gamestatemap: " << &gameStateMap << std::endl;
     109      if(client)
     110        return encode(client, server);
     111      else
     112        return encode(server);
    102113    } else {
     114      COUT(4) << "we got a GAMESTATEID_INITIAL for clientID: " << clientID << std::endl;
    103115      GameState *server = reference;
     116      ackGameState(clientID, reference->id);
    104117      //head_->findClient(clientID)->setGamestateID(id);
    105118      return encode(server);
     
    113126  * @return struct of type gamestate containing the size of the whole gamestate and a pointer linking to the flat list
    114127  */
    115   GameState *GameStateManager::getSnapshot(int id)
     128  GameState *GameStateManager::getSnapshot()
    116129  {
    117130    //std::cout << "begin getSnapshot" << std::endl;
    118131    //the size of the gamestate
    119132    int totalsize=0;
    120     int memsize=1000;
     133    int memsize=0;
    121134    //the size of one specific synchronisable
    122135    int tempsize=0;
     
    127140
    128141    GameState *retval=new GameState; //return value
    129     retval->id=id++;
     142    retval->id=++id_;
    130143    COUT(4) << "G.ST.Man: producing gamestate with id: " << retval->id << std::endl;
    131144    // offset of memory functions
     
    133146    // get total size of gamestate
    134147    for(it = orxonox::ObjectList<Synchronisable>::start(); it; ++it){
    135       size+=it->getSize();
    136       size+=3*sizeof(int);
    137     }
    138     retval->data = (unsigned char*)malloc(size);
     148      size+=it->getSize(); // size of the actual data of the synchronisable
     149      size+=3*sizeof(int); // size of datasize, classID and objectID
     150    }
     151    //retval->data = (unsigned char*)malloc(size);
     152    retval->data = new unsigned char[size];
    139153    if(!retval->data){
    140154      COUT(2) << "GameStateManager: could not allocate memory" << std::endl;
     
    148162      // add place for data and 3 ints (length,classid,objectid)
    149163      totalsize+=tempsize+3*sizeof(int);
    150       // allocate additional space
    151       if((totalsize+tempsize) > memsize){
    152         COUT(5) << "G.St.Man: need additional memory" << std::endl;
     164      // allocate+tempsize additional space
     165      if(totalsize > size){
     166        COUT(3) << "G.St.Man: need additional memory" << std::endl;
    153167//         if(tempsize < 1000){
    154168//           retval->data = (unsigned char *)realloc((void *)retval->data, totalsize+1000);
     
    190204  GameStateCompressed *GameStateManager::encode(GameState *a, GameState *b) {
    191205    COUT(5) << "G.St.Man: this will be a DIFFED gamestate" << std::endl;
    192     //GameState r = diff(a,b);
     206    GameState *r = diff(a,b);
    193207    //r.diffed = true;
    194     GameState *r = b;
    195     r->diffed = false;
     208//     GameState *r = b;
     209//     r->diffed = false;
    196210    //return compress_(r);
    197211    GameStateCompressed *g = new GameStateCompressed;
    198     g->base_id = b->base_id;
    199     g->id = b->id;
    200     g->diffed = b->diffed;
    201     g->data = b->data;
    202     g->normsize = b->size;
    203     g->compsize = b->size;
     212    g->base_id = r->base_id;
     213    g->id = r->id;
     214    g->diffed = r->diffed;
     215    g->data = r->data;
     216    g->normsize = r->size;
     217    g->compsize = r->size;
    204218    return g;
    205219  }
     
    226240    else
    227241      dest_length=b->size;
    228     unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char));
     242    //unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char));
     243    unsigned char *dp = new unsigned char[dest_length*sizeof(unsigned char)];
    229244    while(of<a->size && of<b->size){
    230245      *(dp+of)=*(ap+of)^*(bp+of); // do the xor
     
    263278    uLongf buffer = (uLongf)((a->size + 12)*1.01)+1;
    264279    //COUT(4) << "size: " << size << ", buffer: " << buffer << std::endl;
    265     unsigned char* dest = (unsigned char*)malloc( buffer );
     280    //unsigned char* dest = (unsigned char*)malloc( buffer );
     281    unsigned char *dest = new unsigned char[buffer];
    266282    //COUT(4) << "dest: " << dest << std::endl;
    267283    int retval;
     
    296312  void GameStateManager::ackGameState(int clientID, int gamestateID) {
    297313    ClientInformation *temp = head_->findClient(clientID);
    298     int curid = temp->getID();
     314    int curid = temp->getGamestateID();
     315    COUT(4) << "acking gamestate " << gamestateID << " for clientid: " << clientID << " curid: " << curid << std::endl;
    299316    // decrease usage of gamestate and save it
    300     deleteUnusedGameState(curid);
     317//     deleteUnusedGameState(curid);
    301318    //increase gamestateused
    302     ++gameStateUsed.find(gamestateID)->second;
     319    --(gameStateUsed.find(curid)->second);
     320    ++(gameStateUsed.find(gamestateID)->second);
    303321    temp->setGamestateID(gamestateID);
    304322    /*
     
    308326  }
    309327
    310   bool GameStateManager::deleteUnusedGameState(int gamestateID) {
    311     int used = --(gameStateUsed.find(gamestateID)->second);
    312     if(id-gamestateID>KEEP_GAMESTATES && used==0){
    313       // delete gamestate
    314       delete gameStateMap.find(gamestateID)->second;
    315       gameStateMap.erase(gamestateID);
    316       return true;
    317     }
    318     return false;
     328  bool GameStateManager::printGameStates() {
     329    std::map<int, GameState*>::iterator it;
     330    COUT(4) << "gamestates: ";
     331    for(it = gameStateMap.begin(); it!=gameStateMap.end(); it++){
     332      COUT(4) << (*it).first << ":" << (*it).second << " | ";
     333    }
     334    COUT(4) << std::endl;
    319335  }
    320336
  • code/branches/network3/src/network/GameStateManager.h

    r1168 r1199  
    5050{
    5151
    52 #define KEEP_GAMESTATES 20
     52#define KEEP_GAMESTATES 100
    5353
    5454  /**
     
    7878    GameStateCompressed *popGameState(int clientID);
    7979    void ackGameState(int clientID, int gamestateID);
    80     int id;
    8180  private:
    8281    void cleanup(); // "garbage handler"
    83     GameState *getSnapshot(int id);
     82    GameState *getSnapshot();
    8483    GameStateCompressed *encode(GameState *a, GameState *b);
    8584    GameStateCompressed *encode(GameState *a);
    8685    GameState *diff(GameState *a, GameState *b);
    8786    GameStateCompressed *compress_(GameState *a);
    88     bool deleteUnusedGameState(int gamestateID);
     87    bool printGameStates();
    8988
    9089    std::map<int, GameState*> gameStateMap; //map gsID to gamestate*
     
    9291    GameState *reference;
    9392    ClientInformation *head_;
     93    int id_;
    9494  };
    9595
  • code/branches/network3/src/network/PacketDecoder.cc

    r1177 r1199  
    9797  {
    9898    ack* a = new ack;
    99     *a = *(ack*)packet->data; //press pattern of ack on new data
    100 
    101 
    102     COUT(5) << "PacketDecoder: got ack id: " << a->id << std::endl;
     99    *a = *(ack*)(packet->data); //press pattern of ack on new data
     100
     101
     102    COUT(4) << "PacketDecoder: got ack id: " << a->a << std::endl;
    103103    processAck( a, clientId ); //debug info
    104104
  • code/branches/network3/src/network/Server.cc

    r1184 r1199  
    143143    updateGamestate();
    144144
    145     //sleep(1); // TODO remove
     145    usleep(200000); // TODO remove
    146146    return;
    147147  }
     
    220220
    221221  void Server::processAck( ack *data, int clientID) {
    222     COUT(5) << "Server: processing ack from client: " << clientID << "; ack-id: " << data->id << std::endl;
    223     clients->findClient(clientID)->setGamestateID(data->a);
     222    COUT(4) << "Server: processing ack from client: " << clientID << "; ack-id: " << data->a << std::endl;
     223    gamestates->ackGameState(clientID, data->a);
    224224  }
    225225
  • code/branches/network3/src/network/Synchronisable.cc

    r1180 r1199  
    147147    retVal.classID=this->classID;
    148148    retVal.length=getSize();
    149     COUT(4) << "Synchronisable getting data from objectID: " << retVal.objectID << " classID: " << retVal.classID << " length: " << retVal.length << std::endl;
     149    COUT(5) << "Synchronisable getting data from objectID: " << retVal.objectID << " classID: " << retVal.classID << " length: " << retVal.length << std::endl;
    150150    retVal.data=mem;
    151151    // copy to location
     
    163163        const char *data = ( ( *(std::string *) (*i)->var).c_str());
    164164        std::memcpy( retVal.data+n, (void*)data, (*i)->size);
    165         COUT(4) << "synchronisable: char: " << (const char *)(retVal.data+n) << " data: " << data << " string: " << *(std::string *)((*i)->var) << std::endl;
     165        COUT(5) << "synchronisable: char: " << (const char *)(retVal.data+n) << " data: " << data << " string: " << *(std::string *)((*i)->var) << std::endl;
    166166        n+=(*i)->size;
    167167        break;
     
    193193      case STRING:
    194194        (*i)->size = *(int *)data;
    195         COUT(4) << "string size: " << (*i)->size << std::endl;
     195        COUT(5) << "string size: " << (*i)->size << std::endl;
    196196        data+=sizeof(int);
    197197        *((std::string *)((*i)->var)) = std::string((const char*)data);
    198         COUT(4) << "synchronisable: char: " << (const char*)data << " string: " << std::string((const char*)data) << std::endl;
     198        COUT(5) << "synchronisable: char: " << (const char*)data << " string: " << std::string((const char*)data) << std::endl;
    199199        data += (*i)->size;
    200200        break;
     
    219219        tsize+=sizeof(int);
    220220        (*i)->size=((std::string *)(*i)->var)->length()+1;
    221         COUT(4) << "String size: " << (*i)->size << std::endl;
     221        COUT(5) << "String size: " << (*i)->size << std::endl;
    222222        tsize+=(*i)->size;
    223223        break;
Note: See TracChangeset for help on using the changeset viewer.