Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 28, 2009, 3:04:30 PM (15 years ago)
Author:
scheusso
Message:

a lot of cleanup
some bugfixes (Thread, ThreadPool)
the biggest part of the network (~80% cpu time) is now multithreaded (1 thread for each client)

File:
1 edited

Legend:

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

    r3227 r3240  
    4242
    4343#include <cassert>
     44#include <queue>
     45#include <boost/thread/mutex.hpp>
    4446
    4547#include "util/Debug.h"
     48#include "core/Executor.h"
     49#include "core/ThreadPool.h"
    4650#include "ClientInformation.h"
    4751#include "packet/Acknowledgement.h"
     
    5660  {
    5761    trafficControl_ = new TrafficControl();
     62    threadMutex_ = new boost::mutex();
     63    threadPool_ = new ThreadPool();
    5864  }
    5965
     
    7177        delete (*it2).second;
    7278    }
    73     delete trafficControl_;
     79    delete this->trafficControl_;
     80    delete this->threadMutex_;
     81    delete this->threadPool_;
    7482  }
    7583
     
    116124    return true;
    117125  }
    118 
    119 
    120   packet::Gamestate *GamestateManager::popGameState(unsigned int clientID) {
     126 
     127  void GamestateManager::sendGamestates()
     128  {
     129    ClientInformation *temp = ClientInformation::getBegin();
     130    std::queue<packet::Gamestate*> clientGamestates;
     131    while(temp != NULL){
     132      if( !(temp->getSynched()) ){
     133        COUT(5) << "Server: not sending gamestate" << std::endl;
     134        temp=temp->next();
     135        if(!temp)
     136          break;
     137        continue;
     138      }
     139      COUT(4) << "client id: " << temp->getID() << " RTT: " << temp->getRTT() << " loss: " << temp->getPacketLoss() << std::endl;
     140      COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl;
     141      int cid = temp->getID(); //get client id
     142     
     143      packet::Gamestate *gs;
     144      unsigned int gID = temp->getGamestateID();
     145      if(!reference)
     146        return;
     147     
     148      packet::Gamestate *client=0;
     149      if(gID != GAMESTATEID_INITIAL){
     150        assert(gamestateMap_.find(cid)!=gamestateMap_.end());
     151        std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateMap_[cid].find(gID);
     152        if(it!=gamestateMap_[cid].end())
     153        {
     154          client = it->second;
     155        }
     156      }
     157     
     158      clientGamestates.push(0);
     159//       finishGamestate( cid, clientGamestates.back(), client, reference );
     160      //FunctorMember<GamestateManager>* functor =
     161      ExecutorMember<GamestateManager>* executor = createExecutor( createFunctor(&GamestateManager::finishGamestate) );
     162      executor->setObject(this);
     163      executor->setDefaultValues( cid, &clientGamestates.back(), client, reference );
     164//       (*static_cast<Executor*>(executor))();
     165      this->threadPool_->passFunction( executor, true );
     166//       (*functor)( cid, &(clientGamestates.back()), client, reference );
     167     
     168      temp = temp->next();
     169    }
     170   
     171    threadPool_->synchronise();
     172   
     173    while( !clientGamestates.empty() )
     174    {
     175      if(clientGamestates.front())
     176        clientGamestates.front()->send();
     177      clientGamestates.pop();
     178    }
     179  }
     180
     181
     182  void GamestateManager::finishGamestate( unsigned int clientID, packet::Gamestate** destgamestate, packet::Gamestate* base, packet::Gamestate* gamestate ) {
    121183    //why are we searching the same client's gamestate id as we searched in
    122184    //Server::sendGameState?
    123     packet::Gamestate *gs;
    124     unsigned int gID = ClientInformation::findClient(clientID)->getGamestateID();
    125     if(!reference)
    126       return 0;
    127     gs = reference->doSelection(clientID, 10000);
    128185    // save the (undiffed) gamestate in the clients gamestate map
    129     gamestateMap_[clientID][gs->getID()]=gs;
    130186    //chose wheather the next gamestate is the first or not
    131     packet::Gamestate *client=0;
    132     if(gID != GAMESTATEID_INITIAL){
    133       assert(gamestateMap_.find(clientID)!=gamestateMap_.end());
    134       std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateMap_[clientID].find(gID);
    135       if(it!=gamestateMap_[clientID].end())
    136       {
    137         client = it->second;
    138       }
    139     }
    140     if(client){
     187   
     188    packet::Gamestate *gs = gamestate->doSelection(clientID, 20000);
     189//     packet::Gamestate *gs = new packet::Gamestate(*gamestate);
     190//     packet::Gamestate *gs = new packet::Gamestate();
     191//     gs->collectData( id_, 0x1 );
     192    this->threadMutex_->lock();
     193    gamestateMap_[clientID][gamestate->getID()]=gs;
     194    this->threadMutex_->unlock();
     195   
     196    if(base)
     197    {
     198       
    141199//       COUT(3) << "diffing" << std::endl;
    142200//       packet::Gamestate* gs1  = gs;
    143       packet::Gamestate *diffed = gs->diff(client);
     201      packet::Gamestate *diffed = gs->diff(base);
    144202      //packet::Gamestate *gs2 = diffed->undiff(gs);
    145203//       assert(*gs == *gs2);
     
    150208    }
    151209    else{
    152 //       COUT(3) << "not diffing" << std::endl;
    153210      gs = new packet::Gamestate(*gs);
    154211    }
     212   
     213   
    155214    bool b = gs->compressData();
    156215    assert(b);
    157     COUT(4) << "sending gamestate with id " << gs->getID();
    158     if(gs->isDiffed())
    159     COUT(4) << " and baseid " << gs->getBaseID() << endl;
    160     else
    161     COUT(4) << endl;
    162     return gs;
     216//     COUT(4) << "sending gamestate with id " << gs->getID();
     217//     if(gamestate->isDiffed())
     218//     COUT(4) << " and baseid " << gs->getBaseID() << endl;
     219//     else
     220//     COUT(4) << endl;
     221    gs->setClientID(clientID);
     222    *destgamestate = gs;
    163223  }
    164224
Note: See TracChangeset for help on using the changeset viewer.