Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11242 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

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