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/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
Note: See TracChangeset for help on using the changeset viewer.