Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/network/GamestateManager.cc @ 6847

Last change on this file since 6847 was 6417, checked in by rgrieder, 15 years ago

Merged presentation2 branch back to trunk.
Major new features:

  • Actual GUI with settings, etc.
  • Improved space ship steering (human interaction)
  • Rocket fire and more particle effects
  • Advanced sound framework
  • 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>
[3304]44#include <queue>
45// #include <boost/thread/mutex.hpp>
[1705]46
[3214]47#include "util/Debug.h"
[3304]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();
[3304]62//     threadMutex_ = new boost::mutex();
63//     threadPool_ = new ThreadPool();
[1705]64  }
65
[2662]66  GamestateManager::~GamestateManager()
67  {
[3198]68    if( this->reference )
[3304]69        delete this->reference;std::map<unsigned int, packet::Gamestate*>::iterator it;
70    for( it = gamestateQueue.begin(); it != gamestateQueue.end(); ++it )
[6417]71      delete it->second;
[3304]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 )
[6417]77        delete it2->second;
[3304]78    }
[5929]79    this->trafficControl_->destroy();
[3304]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(){
[3304]101    if( this->gamestateQueue.empty() )
102        return true;
[2087]103    std::map<unsigned int, packet::Gamestate*>::iterator it;
[1705]104    // now push only the most recent gamestates we received (ignore obsolete ones)
105    for(it = gamestateQueue.begin(); it!=gamestateQueue.end(); it++){
[2087]106      bool b = processGamestate(it->second);
107      assert(b);
[1705]108      delete it->second;
109    }
110    // now clear the queue
111    gamestateQueue.clear();
[2662]112    //and call all queued callbacks
113    NetworkCallbackManager::callCallbacks();
[1705]114    return true;
115  }
[2087]116
117
[1705]118  bool GamestateManager::getSnapshot(){
[2662]119    if ( reference != 0 )
120      delete reference;
[1705]121    reference = new packet::Gamestate();
[3102]122    if(!reference->collectData(++id_, 0x1)){ //we have no data to send
[2087]123      delete reference;
124      reference=0;
125    }
[1705]126    return true;
127  }
[6417]128
[3304]129  void GamestateManager::sendGamestates()
130  {
131    ClientInformation *temp = ClientInformation::getBegin();
132    std::queue<packet::Gamestate*> clientGamestates;
133    while(temp != NULL){
134      if( !(temp->getSynched()) ){
135        COUT(5) << "Server: not sending gamestate" << std::endl;
136        temp=temp->next();
137        if(!temp)
138          break;
139        continue;
140      }
141      COUT(4) << "client id: " << temp->getID() << " RTT: " << temp->getRTT() << " loss: " << temp->getPacketLoss() << std::endl;
142      COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl;
143      int cid = temp->getID(); //get client id
[6417]144
[3304]145      unsigned int gID = temp->getGamestateID();
146      if(!reference)
147        return;
[6417]148
[3304]149      packet::Gamestate *client=0;
150      if(gID != GAMESTATEID_INITIAL){
151        assert(gamestateMap_.find(cid)!=gamestateMap_.end());
152        std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateMap_[cid].find(gID);
153        if(it!=gamestateMap_[cid].end())
154        {
155          client = it->second;
156        }
157      }
[6417]158
[3304]159      clientGamestates.push(0);
160      finishGamestate( cid, &clientGamestates.back(), client, reference );
[6417]161      //FunctorMember<GamestateManager>* functor =
[5929]162//       ExecutorMember<GamestateManager>* executor = createExecutor( createFunctor(&GamestateManager::finishGamestate, this) );
[3304]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 );
[6417]167
[3304]168      temp = temp->next();
169    }
[6417]170
[3304]171//     threadPool_->synchronise();
[6417]172
[3304]173    while( !clientGamestates.empty() )
174    {
175      if(clientGamestates.front())
176        clientGamestates.front()->send();
177      clientGamestates.pop();
178    }
179  }
[2087]180
[1705]181
[3304]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
[6417]187
[3304]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();
[6417]195
[3304]196    if(base)
197    {
[6417]198
[1907]199//       COUT(3) << "diffing" << std::endl;
[3084]200//       packet::Gamestate* gs1  = gs;
[3304]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    }
[6417]212
213
[1907]214    bool b = gs->compressData();
215    assert(b);
[3304]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
[3301]243    assert(curid==GAMESTATEID_INITIAL || curid<gamestateID);
[3304]244    COUT(5) << "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.