Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network6/src/libraries/network/GamestateManager.cc @ 7878

Last change on this file since 7878 was 7878, checked in by scheusso, 13 years ago

-some cleaning up
-fixing disconnect behaviour
-trying to find a bug

  • Property svn:eol-style set to native
File size: 12.0 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 "packet/Acknowledgement.h"
48#include "packet/Gamestate.h"
[2662]49#include "synchronisable/NetworkCallbackManager.h"
[1705]50
[7801]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"
57
[2171]58namespace orxonox
[1705]59{
[2662]60  GamestateManager::GamestateManager() :
[7801]61  currentGamestate_(0), id_(0)
[2662]62  {
[7801]63//     trafficControl_ = new TrafficControl();
[3304]64//     threadMutex_ = new boost::mutex();
65//     threadPool_ = new ThreadPool();
[1705]66  }
67
[2662]68  GamestateManager::~GamestateManager()
69  {
[7801]70    if( this->currentGamestate_ )
71        delete this->currentGamestate_;std::map<unsigned int, packet::Gamestate*>::iterator it;
[3304]72    for( it = gamestateQueue.begin(); it != gamestateQueue.end(); ++it )
[6417]73      delete it->second;
[7801]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 )
[3304]77    {
[7801]78      for( gamestateIt = peerIt->second.gamestates.begin(); gamestateIt != peerIt->second.gamestates.end(); ++gamestateIt )
79        delete gamestateIt->second;
[3304]80    }
[7801]81//     this->trafficControl_->destroy();
[3304]82//     delete this->threadMutex_;
83//     delete this->threadPool_;
[1705]84  }
85
86  bool GamestateManager::update(){
[1907]87//     cleanup();
[1730]88    return getSnapshot();
[1705]89  }
[2087]90
[7801]91  bool GamestateManager::addGamestate(packet::Gamestate *gs, unsigned int clientID)
92  {
[1705]93    assert(gs);
[2087]94    std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateQueue.find(clientID);
[1705]95    if(it!=gamestateQueue.end()){
96      // delete obsolete gamestate
97      delete it->second;
98    }
99    gamestateQueue[clientID] = gs;
100    return true;
101  }
[2087]102
[7801]103  bool GamestateManager::processGamestates()
104  {
[3304]105    if( this->gamestateQueue.empty() )
106        return true;
[2087]107    std::map<unsigned int, packet::Gamestate*>::iterator it;
[1705]108    // now push only the most recent gamestates we received (ignore obsolete ones)
109    for(it = gamestateQueue.begin(); it!=gamestateQueue.end(); it++){
[2087]110      bool b = processGamestate(it->second);
111      assert(b);
[7878]112//       sendAck( it->second->getID(), it->second->getPeerID() );
[1705]113      delete it->second;
114    }
115    // now clear the queue
116    gamestateQueue.clear();
[2662]117    //and call all queued callbacks
118    NetworkCallbackManager::callCallbacks();
[1705]119    return true;
120  }
[7801]121 
122  bool GamestateManager::sendAck(unsigned int gamestateID, uint32_t peerID)
123  {
[7878]124    assert( gamestateID != ACKID_NACK );
[7801]125    packet::Acknowledgement *ack = new packet::Acknowledgement(gamestateID, peerID);
126    if( !this->sendPacket(ack))
127    {
128      COUT(3) << "could not ack gamestate: " << gamestateID << std::endl;
129      return false;
130    }
131    else
132    {
133      COUT(5) << "acked a gamestate: " << gamestateID << std::endl;
134      return true;
135    }
136  }
[2087]137
138
[1705]139  bool GamestateManager::getSnapshot(){
[7801]140    if ( currentGamestate_ != 0 )
141      delete currentGamestate_;
142    currentGamestate_ = new packet::Gamestate();
143    uint8_t gsMode;
144    if( GameMode::isMaster() )
145      gsMode = packet::GAMESTATE_MODE_SERVER;
146    else
147      gsMode = packet::GAMESTATE_MODE_CLIENT;
148    uint32_t newID;
149    if( GameMode::isMaster() )
150      newID = ++id_;
151    else
[7825]152    {
153      assert(peerMap_.size()!=0);
[7878]154      newID = peerMap_[NETWORK_PEER_ID_SERVER].lastReceivedGamestateID;
[7825]155    }
[7801]156   
157    if(!currentGamestate_->collectData(newID, gsMode)){ //we have no data to send
158      delete currentGamestate_;
159      currentGamestate_=0;
[2087]160    }
[1705]161    return true;
162  }
[6417]163
[7801]164  std::vector<packet::Gamestate*> GamestateManager::getGamestates()
[3304]165  {
[7801]166    if(!currentGamestate_)
167      return std::vector<packet::Gamestate*>();
168    std::vector<packet::Gamestate*> peerGamestates;
169   
170    std::map<uint32_t, peerInfo>::iterator peerIt;
171    for( peerIt=peerMap_.begin(); peerIt!=peerMap_.end(); ++peerIt )
172    {
173      if( !peerIt->second.isSynched )
174      {
[3304]175        COUT(5) << "Server: not sending gamestate" << std::endl;
176        continue;
177      }
[7825]178      COUT(5) << "client id: " << peerIt->first << std::endl;
[3304]179      COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl;
[7801]180      int peerID = peerIt->first; //get client id
[6417]181
[7801]182      unsigned int lastAckedGamestateID = peerIt->second.lastAckedGamestateID;
[6417]183
[7801]184      packet::Gamestate* baseGamestate=0;
185      if(lastAckedGamestateID != GAMESTATEID_INITIAL)
186      {
187        assert(peerMap_.find(peerID)!=peerMap_.end());
188        std::map<uint32_t, packet::Gamestate*>::iterator it = peerMap_[peerID].gamestates.find(lastAckedGamestateID);
189        assert(it!=peerMap_[peerID].gamestates.end());
190        baseGamestate = it->second;
[3304]191      }
[6417]192
[7878]193      peerGamestates.push_back(0);  // insert an empty gamestate* to be changed
[7801]194      finishGamestate( peerID, peerGamestates.back(), baseGamestate, currentGamestate_ );
195      if( peerGamestates.back()==0 )
196        // nothing to send to remove pointer from vector
197        peerGamestates.pop_back();
[6417]198      //FunctorMember<GamestateManager>* functor =
[5929]199//       ExecutorMember<GamestateManager>* executor = createExecutor( createFunctor(&GamestateManager::finishGamestate, this) );
[7801]200//       executor->setDefaultValues( cid, &clientGamestates.back(), client, currentGamestate_ );
[3304]201//       (*static_cast<Executor*>(executor))();
202//       this->threadPool_->passFunction( executor, true );
[7801]203//       (*functor)( cid, &(clientGamestates.back()), client, currentGamestate_ );
[3304]204    }
[6417]205
[3304]206//     threadPool_->synchronise();
[6417]207
[7801]208    return peerGamestates;
[3304]209  }
[2087]210
[1705]211
[7801]212  void GamestateManager::finishGamestate( unsigned int peerID, packet::Gamestate*& destgamestate, packet::Gamestate* base, packet::Gamestate* gamestate ) {
[1705]213    //why are we searching the same client's gamestate id as we searched in
214    //Server::sendGameState?
[1907]215    // save the (undiffed) gamestate in the clients gamestate map
[1705]216    //chose wheather the next gamestate is the first or not
[6417]217
[7163]218//     packet::Gamestate *gs = gamestate->doSelection(clientID, 20000);
219//       packet::Gamestate* gs = new packet::Gamestate(*gamestate);
220//     packet::Gamestate* gs = gamestate;
221    packet::Gamestate *gs = new packet::Gamestate(*gamestate); //TODO: is this neccessary ?
[3304]222//     packet::Gamestate *gs = new packet::Gamestate();
223//     gs->collectData( id_, 0x1 );
224//     this->threadMutex_->lock();
[7801]225    peerMap_[peerID].gamestates[gamestate->getID()]=gs;
[3304]226//     this->threadMutex_->unlock();
[7801]227    Clock clock;
228    clock.capture();
[6417]229
[3304]230    if(base)
231    {
[7163]232      packet::Gamestate *diffed1 = gs->diffVariables(base);
233      if( diffed1->getDataSize() == 0 )
234      {
235        delete diffed1;
236        destgamestate = 0;
237        return;
238      }
239      gs = diffed1;
[1907]240    }
[7163]241    else
242    {
[1907]243      gs = new packet::Gamestate(*gs);
244    }
[6417]245
246
[7801]247//     bool b = gs->compressData();
248//     assert(b);
249    clock.capture();
[7825]250    COUT(5) << "diff and compress time: " << clock.getDeltaTime() << endl;
[7163]251//     COUT(5) << "sending gamestate with id " << gs->getID();
[3304]252//     if(gamestate->isDiffed())
[7163]253//       COUT(5) << " and baseid " << gs->getBaseID() << endl;
[3304]254//     else
[7163]255//       COUT(5) << endl;
[7801]256    gs->setPeerID(peerID);
[7163]257    destgamestate = gs;
[1705]258  }
[2087]259
260
[7801]261  bool GamestateManager::ackGamestate(unsigned int gamestateID, unsigned int peerID)
262  {
263//     ClientInformation *temp = ClientInformation::findClient(peerID);
264//     assert(temp);
265    std::map<uint32_t, peerInfo>::iterator it = this->peerMap_.find(peerID);
266    assert(it!=this->peerMap_.end());
267    unsigned int curid = it->second.lastAckedGamestateID;
[2087]268
[7878]269    assert(gamestateID != ACKID_NACK);
270//     if(gamestateID == ACKID_NACK){
271//       it->second.lastAckedGamestateID = GAMESTATEID_INITIAL;
272// //       temp->setGamestateID(GAMESTATEID_INITIAL);
273//       // now delete all saved gamestates for this client
274//       std::map<uint32_t, packet::Gamestate*>::iterator it2;
275//       for(it2 = it->second.gamestates.begin(); it2!=it->second.gamestates.end(); ++it2 ){
276//         delete it2->second;
277//       }
278//       it->second.gamestates.clear();
279//       return true;
280//     }
[2087]281
[7801]282    assert(curid==GAMESTATEID_INITIAL || curid<=gamestateID);
283    COUT(5) << "acking gamestate " << gamestateID << " for peerID: " << peerID << " curid: " << curid << std::endl;
284    std::map<uint32_t, packet::Gamestate*>::iterator it2;
285    for( it2=it->second.gamestates.begin(); it2!=it->second.gamestates.end(); )
286    {
287      if( it2->second->getID() < gamestateID )
288      {
289        delete it2->second;
290        it->second.gamestates.erase(it2++);
291      }
292      else
293        ++it2;
[1705]294    }
[7801]295   
296//     std::map<unsigned int, packet::Gamestate*>::iterator it;
297//     for(it = gamestateMap_[peerID].begin(); it!=gamestateMap_[peerID].end() && it->first<gamestateID; ){
298//       delete it->second;
299//       gamestateMap_[peerID].erase(it++);
300//     }
301    it->second.lastAckedGamestateID = gamestateID;
302//     temp->setGamestateID(gamestateID);
303//     TrafficControl::processAck(peerID, gamestateID);
[1705]304    return true;
305  }
[7801]306 
[7878]307  uint32_t GamestateManager::getLastReceivedGamestateID(unsigned int peerID)
[7801]308  {
309    assert( this->peerMap_.find(peerID)!=this->peerMap_.end() );
310    if( this->peerMap_.find(peerID) != this->peerMap_.end() )
[7878]311      return this->peerMap_[peerID].lastReceivedGamestateID;
[7801]312    else
313      return GAMESTATEID_INITIAL;
314  }
315 
316 
317  void GamestateManager::addPeer(uint32_t peerID)
318  {
319    assert(peerMap_.find(peerID)==peerMap_.end());
320    peerMap_[peerID].peerID = peerID;
[7878]321    peerMap_[peerID].lastReceivedGamestateID = GAMESTATEID_INITIAL;
[7801]322    peerMap_[peerID].lastAckedGamestateID = GAMESTATEID_INITIAL;
323    if( GameMode::isMaster() )
324      peerMap_[peerID].isSynched = false;
325    else
326      peerMap_[peerID].isSynched = true;
327  }
[1705]328
[7801]329  void GamestateManager::removePeer(uint32_t peerID)
330  {
331    assert(peerMap_.find(peerID)!=peerMap_.end());
332    std::map<uint32_t, packet::Gamestate*>::iterator peerIt;
333    for( peerIt = peerMap_[peerID].gamestates.begin(); peerIt!=peerMap_[peerID].gamestates.end(); ++peerIt )
334    {
335      delete peerIt->second;
336    }
337    peerMap_.erase(peerMap_.find(peerID));
[1705]338  }
[2087]339
[7801]340
341//   void GamestateManager::removeClient(ClientInformation* client){
342//     assert(client);
343//     std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(client->getID());
344//     // first delete all remained gamestates
345//     std::map<unsigned int, packet::Gamestate*>::iterator it;
346//     for(it=clientMap->second.begin(); it!=clientMap->second.end(); it++)
347//       delete it->second;
348//     // now delete the clients gamestatemap
349//     gamestateMap_.erase(clientMap);
350//   }
351
352  bool GamestateManager::processGamestate(packet::Gamestate *gs)
353  {
[1751]354    if(gs->isCompressed())
[1907]355    {
356       bool b = gs->decompressData();
357       assert(b);
358    }
[1712]359    assert(!gs->isDiffed());
[7801]360    uint8_t gsMode;
361    if( GameMode::isMaster() )
362      gsMode = packet::GAMESTATE_MODE_SERVER;
363    else
364      gsMode = packet::GAMESTATE_MODE_CLIENT;
365    if( gs->spreadData(gsMode) )
366    {
[7878]367      this->peerMap_[gs->getPeerID()].lastReceivedGamestateID = gs->getID();
[7801]368      return true;
369    }
370    else
371      return false;
[1712]372  }
[1705]373
374}
Note: See TracBrowser for help on using the repository browser.