Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp6/src/network/GamestateManager.cc @ 3240

Last change on this file since 3240 was 3240, checked in by scheusso, 15 years ago

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)

  • Property svn:eol-style set to native
File size: 8.7 KB
RevLine 
[1705]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
[3084]23 *      Oliver Scheuss
[1705]24 *   Co-authors:
25 *      ...
26 *
27 */
28
29//
30// C++ Implementation: GameStateManager
31//
32// Description:
33//
34//
35// Author:  Oliver Scheuss, (C) 2007
36//
37// Copyright: See COPYING file that comes with this distribution
38//
39//
40
41#include "GamestateManager.h"
42
[1755]43#include <cassert>
[3240]44#include <queue>
45#include <boost/thread/mutex.hpp>
[1705]46
[3214]47#include "util/Debug.h"
[3240]48#include "core/Executor.h"
49#include "core/ThreadPool.h"
[1705]50#include "ClientInformation.h"
[3214]51#include "packet/Acknowledgement.h"
52#include "packet/Gamestate.h"
[2662]53#include "synchronisable/NetworkCallbackManager.h"
[3214]54#include "TrafficControl.h"
[1705]55
[2171]56namespace orxonox
[1705]57{
[2662]58  GamestateManager::GamestateManager() :
59  reference(0), id_(0)
60  {
61    trafficControl_ = new TrafficControl();
[3240]62    threadMutex_ = new boost::mutex();
63    threadPool_ = new ThreadPool();
[1705]64  }
65
[2662]66  GamestateManager::~GamestateManager()
67  {
[3198]68    if( this->reference )
[3227]69        delete this->reference;std::map<unsigned int, packet::Gamestate*>::iterator it;
70    for( it = gamestateQueue.begin(); it != gamestateQueue.end(); ++it )
[3198]71      delete (*it).second;
[3227]72    std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator it1;
73    std::map<unsigned int, packet::Gamestate*>::iterator it2;
74    for( it1 = gamestateMap_.begin(); it1 != gamestateMap_.end(); ++it1 )
75    {
76      for( it2 = it1->second.begin(); it2 != it1->second.end(); ++it2 )
77        delete (*it2).second;
78    }
[3240]79    delete this->trafficControl_;
80    delete this->threadMutex_;
81    delete this->threadPool_;
[1705]82  }
83
84  bool GamestateManager::update(){
[1907]85//     cleanup();
[1730]86    return getSnapshot();
[1705]87  }
[2087]88
89  bool GamestateManager::add(packet::Gamestate *gs, unsigned int clientID){
[1705]90    assert(gs);
[2087]91    std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateQueue.find(clientID);
[1705]92    if(it!=gamestateQueue.end()){
93      // delete obsolete gamestate
94      delete it->second;
95    }
96    gamestateQueue[clientID] = gs;
97    return true;
98  }
[2087]99
[1705]100  bool GamestateManager::processGamestates(){
[2087]101    std::map<unsigned int, packet::Gamestate*>::iterator it;
[1705]102    // now push only the most recent gamestates we received (ignore obsolete ones)
103    for(it = gamestateQueue.begin(); it!=gamestateQueue.end(); it++){
[2087]104      bool b = processGamestate(it->second);
105      assert(b);
[1705]106      delete it->second;
107    }
108    // now clear the queue
109    gamestateQueue.clear();
[2662]110    //and call all queued callbacks
111    NetworkCallbackManager::callCallbacks();
[1705]112    return true;
113  }
[2087]114
115
[1705]116  bool GamestateManager::getSnapshot(){
[2662]117    if ( reference != 0 )
118      delete reference;
[1705]119    reference = new packet::Gamestate();
[3102]120    if(!reference->collectData(++id_, 0x1)){ //we have no data to send
[2087]121      delete reference;
122      reference=0;
123    }
[1705]124    return true;
125  }
[3240]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  }
[2087]180
[1705]181
[3240]182  void GamestateManager::finishGamestate( unsigned int clientID, packet::Gamestate** destgamestate, packet::Gamestate* base, packet::Gamestate* gamestate ) {
[1705]183    //why are we searching the same client's gamestate id as we searched in
184    //Server::sendGameState?
[1907]185    // save the (undiffed) gamestate in the clients gamestate map
[1705]186    //chose wheather the next gamestate is the first or not
[3240]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       
[1907]199//       COUT(3) << "diffing" << std::endl;
[3084]200//       packet::Gamestate* gs1  = gs;
[3240]201      packet::Gamestate *diffed = gs->diff(base);
[3198]202      //packet::Gamestate *gs2 = diffed->undiff(gs);
203//       assert(*gs == *gs2);
204      gs = diffed;
[3084]205//       packet::Gamestate* gs2 = gs->undiff(client);
[2662]206//       gs = new packet::Gamestate(*gs);
[3084]207//       assert(*gs1==*gs2);
[1907]208    }
209    else{
210      gs = new packet::Gamestate(*gs);
211    }
[3240]212   
213   
[1907]214    bool b = gs->compressData();
215    assert(b);
[3240]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;
[1705]223  }
[2087]224
225
226  bool GamestateManager::ack(unsigned int gamestateID, unsigned int clientID) {
[1705]227    ClientInformation *temp = ClientInformation::findClient(clientID);
[1907]228    assert(temp);
[2087]229    unsigned int curid = temp->getGamestateID();
230
[2662]231    if(gamestateID == ACKID_NACK){
[1705]232      temp->setGamestateID(GAMESTATEID_INITIAL);
[2662]233      // now delete all saved gamestates for this client
234      std::map<unsigned int, packet::Gamestate*>::iterator it;
235      for(it = gamestateMap_[clientID].begin(); it!=gamestateMap_[clientID].end(); ){
236        delete it->second;
[3084]237
[2662]238        gamestateMap_[clientID].erase(it++);
239      }
[1769]240      return true;
[1705]241    }
[2087]242
243    assert(curid==(unsigned int)GAMESTATEID_INITIAL || curid<gamestateID);
[1705]244    COUT(4) << "acking gamestate " << gamestateID << " for clientid: " << clientID << " curid: " << curid << std::endl;
[2662]245    std::map<unsigned int, packet::Gamestate*>::iterator it;
246    for(it = gamestateMap_[clientID].begin(); it!=gamestateMap_[clientID].end() && it->first<gamestateID; ){
[1907]247      delete it->second;
[2662]248      gamestateMap_[clientID].erase(it++);
[1705]249    }
[1907]250    temp->setGamestateID(gamestateID);
[2662]251    TrafficControl::processAck(clientID, gamestateID);
[1705]252    return true;
253  }
254
255  void GamestateManager::removeClient(ClientInformation* client){
[1907]256    assert(client);
[2087]257    std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(client->getID());
[1907]258    // first delete all remained gamestates
[2087]259    std::map<unsigned int, packet::Gamestate*>::iterator it;
[1907]260    for(it=clientMap->second.begin(); it!=clientMap->second.end(); it++)
261      delete it->second;
262    // now delete the clients gamestatemap
263    gamestateMap_.erase(clientMap);
[1705]264  }
[2087]265
[1712]266  bool GamestateManager::processGamestate(packet::Gamestate *gs){
[1751]267    if(gs->isCompressed())
[1907]268    {
269       bool b = gs->decompressData();
270       assert(b);
271    }
[1712]272    assert(!gs->isDiffed());
[3102]273    return gs->spreadData(0x1);
[1712]274  }
[1705]275
276}
Note: See TracBrowser for help on using the repository browser.