Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 19, 2010, 2:27:06 PM (13 years ago)
Author:
scheusso
Message:

some () structural changes
some functional changes (GamestateClient replaced through GamestateManager on client)
reliable packets get buffered until a recent gamestate arrived and got processed

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/network5/src/libraries/network/GamestateManager.cc

    r7773 r7777  
    4343#include <cassert>
    4444#include <queue>
    45 #include "util/Clock.h"
    4645// #include <boost/thread/mutex.hpp>
    4746
    48 #include "util/Debug.h"
    49 #include "core/ThreadPool.h"
    50 #include "core/command/Executor.h"
    51 #include "ClientInformation.h"
    5247#include "packet/Acknowledgement.h"
    5348#include "packet/Gamestate.h"
    5449#include "synchronisable/NetworkCallbackManager.h"
    55 #include "TrafficControl.h"
     50
     51#include "core/ThreadPool.h"
     52#include "core/command/Executor.h"
     53#include "core/GameMode.h"
     54#include "util/Debug.h"
     55#include "util/Clock.h"
     56// #include "TrafficControl.h"
    5657
    5758namespace orxonox
    5859{
    5960  GamestateManager::GamestateManager() :
    60   reference(0), id_(0)
    61   {
    62     trafficControl_ = new TrafficControl();
     61  currentGamestate_(0), id_(0)
     62  {
     63//     trafficControl_ = new TrafficControl();
    6364//     threadMutex_ = new boost::mutex();
    6465//     threadPool_ = new ThreadPool();
     
    6768  GamestateManager::~GamestateManager()
    6869  {
    69     if( this->reference )
    70         delete this->reference;std::map<unsigned int, packet::Gamestate*>::iterator it;
     70    if( this->currentGamestate_ )
     71        delete this->currentGamestate_;std::map<unsigned int, packet::Gamestate*>::iterator it;
    7172    for( it = gamestateQueue.begin(); it != gamestateQueue.end(); ++it )
    7273      delete it->second;
    73     std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator it1;
    74     std::map<unsigned int, packet::Gamestate*>::iterator it2;
    75     for( it1 = gamestateMap_.begin(); it1 != gamestateMap_.end(); ++it1 )
    76     {
    77       for( it2 = it1->second.begin(); it2 != it1->second.end(); ++it2 )
    78         delete it2->second;
    79     }
    80     this->trafficControl_->destroy();
     74    std::map<uint32_t, peerInfo>::iterator peerIt;
     75    std::map<uint32_t, packet::Gamestate*>::iterator gamestateIt;
     76    for( peerIt = peerMap_.begin(); peerIt != peerMap_.end(); ++peerIt )
     77    {
     78      for( gamestateIt = peerIt->second.gamestates.begin(); gamestateIt != peerIt->second.gamestates.end(); ++gamestateIt )
     79        delete gamestateIt->second;
     80    }
     81//     this->trafficControl_->destroy();
    8182//     delete this->threadMutex_;
    8283//     delete this->threadPool_;
     
    8889  }
    8990
    90   bool GamestateManager::add(packet::Gamestate *gs, unsigned int clientID){
     91  bool GamestateManager::addGamestate(packet::Gamestate *gs, unsigned int clientID)
     92  {
    9193    assert(gs);
    9294    std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateQueue.find(clientID);
     
    99101  }
    100102
    101   bool GamestateManager::processGamestates(){
     103  bool GamestateManager::processGamestates()
     104  {
    102105    if( this->gamestateQueue.empty() )
    103106        return true;
     
    118121
    119122  bool GamestateManager::getSnapshot(){
    120     if ( reference != 0 )
    121       delete reference;
    122     reference = new packet::Gamestate();
    123     if(!reference->collectData(++id_, 0x1)){ //we have no data to send
    124       delete reference;
    125       reference=0;
     123    if ( currentGamestate_ != 0 )
     124      delete currentGamestate_;
     125    currentGamestate_ = new packet::Gamestate();
     126    uint8_t gsMode;
     127    if( GameMode::isMaster() )
     128      gsMode = packet::GAMESTATE_MODE_SERVER;
     129    else
     130      gsMode = packet::GAMESTATE_MODE_CLIENT;
     131    uint32_t newID;
     132    if( GameMode::isMaster() )
     133      newID = ++id_;
     134    else
     135      newID = peerMap_[NETWORK_PEER_ID_SERVER].lastProcessedGamestateID;
     136   
     137    if(!currentGamestate_->collectData(newID, gsMode)){ //we have no data to send
     138      delete currentGamestate_;
     139      currentGamestate_=0;
    126140    }
    127141    return true;
    128142  }
    129143
    130   void GamestateManager::sendGamestates()
    131   {
    132     ClientInformation *temp = ClientInformation::getBegin();
    133     std::queue<packet::Gamestate*> clientGamestates;
    134     while(temp != NULL){
    135       if( !(temp->getSynched()) ){
     144  std::vector<packet::Gamestate*> GamestateManager::getGamestates()
     145  {
     146    if(!currentGamestate_)
     147      return std::vector<packet::Gamestate*>();
     148    std::vector<packet::Gamestate*> peerGamestates;
     149   
     150    std::map<uint32_t, peerInfo>::iterator peerIt;
     151    for( peerIt=peerMap_.begin(); peerIt!=peerMap_.end(); ++peerIt )
     152    {
     153      if( !peerIt->second.isSynched )
     154      {
    136155        COUT(5) << "Server: not sending gamestate" << std::endl;
    137         temp=temp->next();
    138         if(!temp)
    139           break;
    140156        continue;
    141157      }
    142       COUT(4) << "client id: " << temp->getID() << " RTT: " << temp->getRTT() << " loss: " << temp->getPacketLoss() << std::endl;
     158      COUT(4) << "client id: " << peerIt->first << std::endl;
    143159      COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl;
    144       int cid = temp->getID(); //get client id
    145 
    146       unsigned int gID = temp->getGamestateID();
    147       if(!reference)
    148         return;
    149 
    150       packet::Gamestate *client=0;
    151       if(gID != GAMESTATEID_INITIAL){
    152         assert(gamestateMap_.find(cid)!=gamestateMap_.end());
    153         std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateMap_[cid].find(gID);
    154         if(it!=gamestateMap_[cid].end())
    155         {
    156           client = it->second;
    157         }
    158       }
    159 
    160       clientGamestates.push(0);
    161       finishGamestate( cid, clientGamestates.back(), client, reference );
     160      int peerID = peerIt->first; //get client id
     161
     162      unsigned int lastAckedGamestateID = peerIt->second.lastAckedGamestateID;
     163
     164      packet::Gamestate* baseGamestate=0;
     165      if(lastAckedGamestateID != GAMESTATEID_INITIAL)
     166      {
     167        assert(peerMap_.find(peerID)!=peerMap_.end());
     168        std::map<uint32_t, packet::Gamestate*>::iterator it = peerMap_[peerID].gamestates.find(lastAckedGamestateID);
     169        assert(it!=peerMap_[peerID].gamestates.end());
     170        baseGamestate = it->second;
     171      }
     172
     173      peerGamestates.push_back(0);  // insert an empty gamestate* to change
     174      finishGamestate( peerID, peerGamestates.back(), baseGamestate, currentGamestate_ );
    162175      //FunctorMember<GamestateManager>* functor =
    163176//       ExecutorMember<GamestateManager>* executor = createExecutor( createFunctor(&GamestateManager::finishGamestate, this) );
    164 //       executor->setDefaultValues( cid, &clientGamestates.back(), client, reference );
     177//       executor->setDefaultValues( cid, &clientGamestates.back(), client, currentGamestate_ );
    165178//       (*static_cast<Executor*>(executor))();
    166179//       this->threadPool_->passFunction( executor, true );
    167 //       (*functor)( cid, &(clientGamestates.back()), client, reference );
    168 
    169       temp = temp->next();
     180//       (*functor)( cid, &(clientGamestates.back()), client, currentGamestate_ );
    170181    }
    171182
    172183//     threadPool_->synchronise();
    173184
    174     while( !clientGamestates.empty() )
    175     {
    176       if(clientGamestates.front())
    177         clientGamestates.front()->send();
    178       clientGamestates.pop();
    179     }
    180   }
    181 
    182 
    183   void GamestateManager::finishGamestate( unsigned int clientID, packet::Gamestate*& destgamestate, packet::Gamestate* base, packet::Gamestate* gamestate ) {
     185    return peerGamestates;
     186  }
     187
     188
     189  void GamestateManager::finishGamestate( unsigned int peerID, packet::Gamestate*& destgamestate, packet::Gamestate* base, packet::Gamestate* gamestate ) {
    184190    //why are we searching the same client's gamestate id as we searched in
    185191    //Server::sendGameState?
     
    194200//     gs->collectData( id_, 0x1 );
    195201//     this->threadMutex_->lock();
    196     gamestateMap_[clientID][gamestate->getID()]=gs;
     202    peerMap_[peerID].gamestates[gamestate->getID()]=gs;
    197203//     this->threadMutex_->unlock();
    198       Clock clock;
    199       clock.capture();
     204    Clock clock;
     205    clock.capture();
    200206
    201207    if(base)
     
    218224//     bool b = gs->compressData();
    219225//     assert(b);
    220       clock.capture();
    221       COUT(4) << "diff and compress time: " << clock.getDeltaTime() << endl;
     226    clock.capture();
     227    COUT(4) << "diff and compress time: " << clock.getDeltaTime() << endl;
    222228//     COUT(5) << "sending gamestate with id " << gs->getID();
    223229//     if(gamestate->isDiffed())
     
    225231//     else
    226232//       COUT(5) << endl;
    227     gs->setClientID(clientID);
     233    gs->setPeerID(peerID);
    228234    destgamestate = gs;
    229235  }
    230236
    231237
    232   bool GamestateManager::ack(unsigned int gamestateID, unsigned int clientID) {
    233     ClientInformation *temp = ClientInformation::findClient(clientID);
    234     assert(temp);
    235     unsigned int curid = temp->getGamestateID();
     238  bool GamestateManager::ackGamestate(unsigned int gamestateID, unsigned int peerID) {
     239//     ClientInformation *temp = ClientInformation::findClient(peerID);
     240//     assert(temp);
     241    std::map<uint32_t, peerInfo>::iterator it = this->peerMap_.find(peerID);
     242    assert(it!=this->peerMap_.end());
     243    unsigned int curid = it->second.lastAckedGamestateID;
    236244
    237245    if(gamestateID == ACKID_NACK){
    238       temp->setGamestateID(GAMESTATEID_INITIAL);
     246      it->second.lastAckedGamestateID = GAMESTATEID_INITIAL;
     247//       temp->setGamestateID(GAMESTATEID_INITIAL);
    239248      // now delete all saved gamestates for this client
    240       std::map<unsigned int, packet::Gamestate*>::iterator it;
    241       for(it = gamestateMap_[clientID].begin(); it!=gamestateMap_[clientID].end(); ){
    242         delete it->second;
    243 
    244         gamestateMap_[clientID].erase(it++);
    245       }
     249      std::map<uint32_t, packet::Gamestate*>::iterator it2;
     250      for(it2 = it->second.gamestates.begin(); it2!=it->second.gamestates.end(); ++it2 ){
     251        delete it2->second;
     252      }
     253      it->second.gamestates.clear();
    246254      return true;
    247255    }
    248256
    249257    assert(curid==GAMESTATEID_INITIAL || curid<gamestateID);
    250     COUT(5) << "acking gamestate " << gamestateID << " for clientid: " << clientID << " curid: " << curid << std::endl;
    251     std::map<unsigned int, packet::Gamestate*>::iterator it;
    252     for(it = gamestateMap_[clientID].begin(); it!=gamestateMap_[clientID].end() && it->first<gamestateID; ){
    253       delete it->second;
    254       gamestateMap_[clientID].erase(it++);
    255     }
    256     temp->setGamestateID(gamestateID);
    257     TrafficControl::processAck(clientID, gamestateID);
     258    COUT(5) << "acking gamestate " << gamestateID << " for peerID: " << peerID << " curid: " << curid << std::endl;
     259    std::map<uint32_t, packet::Gamestate*>::iterator it2;
     260    for( it2=it->second.gamestates.begin(); it2!=it->second.gamestates.end(); )
     261    {
     262      if( it2->second->getID() < gamestateID )
     263      {
     264        delete it2->second;
     265        it->second.gamestates.erase(it2++);
     266      }
     267      else
     268        ++it2;
     269    }
     270   
     271//     std::map<unsigned int, packet::Gamestate*>::iterator it;
     272//     for(it = gamestateMap_[peerID].begin(); it!=gamestateMap_[peerID].end() && it->first<gamestateID; ){
     273//       delete it->second;
     274//       gamestateMap_[peerID].erase(it++);
     275//     }
     276    it->second.lastAckedGamestateID = gamestateID;
     277//     temp->setGamestateID(gamestateID);
     278//     TrafficControl::processAck(peerID, gamestateID);
    258279    return true;
    259280  }
    260281 
    261   uint32_t GamestateManager::getLastProcessedGamestateID(unsigned int clientID)
    262   {
    263     assert( this->lastProcessedGamestateID_.find(clientID) != this->lastProcessedGamestateID_.end() );
    264     if( this->lastProcessedGamestateID_.find(clientID) != this->lastProcessedGamestateID_.end() )
    265       return this->lastProcessedGamestateID_[clientID];
     282  uint32_t GamestateManager::getLastProcessedGamestateID(unsigned int peerID)
     283  {
     284    assert( this->peerMap_.find(peerID)!=this->peerMap_.end() );
     285    if( this->peerMap_.find(peerID) != this->peerMap_.end() )
     286      return this->peerMap_[peerID].lastProcessedGamestateID;
    266287    else
    267288      return GAMESTATEID_INITIAL;
    268289  }
    269 
    270   void GamestateManager::removeClient(ClientInformation* client){
    271     assert(client);
    272     std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(client->getID());
    273     // first delete all remained gamestates
    274     std::map<unsigned int, packet::Gamestate*>::iterator it;
    275     for(it=clientMap->second.begin(); it!=clientMap->second.end(); it++)
    276       delete it->second;
    277     // now delete the clients gamestatemap
    278     gamestateMap_.erase(clientMap);
    279   }
    280 
    281   bool GamestateManager::processGamestate(packet::Gamestate *gs){
     290 
     291 
     292  void GamestateManager::addPeer(uint32_t peerID)
     293  {
     294    assert(peerMap_.find(peerID)==peerMap_.end());
     295    peerMap_[peerID].peerID = peerID;
     296    peerMap_[peerID].lastProcessedGamestateID = GAMESTATEID_INITIAL;
     297    peerMap_[peerID].lastAckedGamestateID = GAMESTATEID_INITIAL;
     298    if( GameMode::isMaster() )
     299      peerMap_[peerID].isSynched = false;
     300    else
     301      peerMap_[peerID].isSynched = true;
     302  }
     303
     304  void GamestateManager::removePeer(uint32_t peerID)
     305  {
     306    assert(peerMap_.find(peerID)!=peerMap_.end());
     307    std::map<uint32_t, packet::Gamestate*>::iterator peerIt;
     308    for( peerIt = peerMap_[peerID].gamestates.begin(); peerIt!=peerMap_[peerID].gamestates.end(); ++peerIt )
     309    {
     310      delete peerIt->second;
     311    }
     312    peerMap_.erase(peerMap_.find(peerID));
     313  }
     314
     315
     316//   void GamestateManager::removeClient(ClientInformation* client){
     317//     assert(client);
     318//     std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(client->getID());
     319//     // first delete all remained gamestates
     320//     std::map<unsigned int, packet::Gamestate*>::iterator it;
     321//     for(it=clientMap->second.begin(); it!=clientMap->second.end(); it++)
     322//       delete it->second;
     323//     // now delete the clients gamestatemap
     324//     gamestateMap_.erase(clientMap);
     325//   }
     326
     327  bool GamestateManager::processGamestate(packet::Gamestate *gs)
     328  {
    282329    if(gs->isCompressed())
    283330    {
     
    286333    }
    287334    assert(!gs->isDiffed());
    288     if( gs->spreadData(0x1) )
    289     {
    290       this->lastProcessedGamestateID_[gs->getClientID()] = gs->getID();
     335    uint8_t gsMode;
     336    if( GameMode::isMaster() )
     337      gsMode = packet::GAMESTATE_MODE_SERVER;
     338    else
     339      gsMode = packet::GAMESTATE_MODE_CLIENT;
     340    if( gs->spreadData(gsMode) )
     341    {
     342      this->peerMap_[gs->getPeerID()].lastProcessedGamestateID = gs->getID();
    291343      return true;
    292344    }
Note: See TracChangeset for help on using the changeset viewer.