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
Line 
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:
23 *      Oliver Scheuss
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
43#include <cassert>
44#include <queue>
45// #include <boost/thread/mutex.hpp>
46
47#include "packet/Acknowledgement.h"
48#include "packet/Gamestate.h"
49#include "synchronisable/NetworkCallbackManager.h"
50
51#include "core/ThreadPool.h"
52#include "core/command/Executor.h"
53#include "core/GameMode.h"
54#include "util/Output.h"
55#include "util/Clock.h"
56#include "util/OrxAssert.h"
57// #include "TrafficControl.h"
58
59namespace orxonox
60{ 
61  GamestateManager::GamestateManager() :
62  currentGamestate_(nullptr), id_(0)
63  {
64//     trafficControl_ = new TrafficControl();
65//     threadMutex_ = new boost::mutex();
66//     threadPool_ = new ThreadPool();
67  }
68
69  GamestateManager::~GamestateManager()
70  {
71    if( this->currentGamestate_ )
72        delete this->currentGamestate_;
73    for( const auto& mapEntry : gamestateQueue )
74      delete mapEntry.second;
75    for( const auto& mapEntryPeer : peerMap_ )
76    {
77      for( const auto& mapEntryGamestate : mapEntryPeer.second.gamestates )
78        delete mapEntryGamestate.second;
79    }
80//     this->trafficControl_->destroy();
81//     delete this->threadMutex_;
82//     delete this->threadPool_;
83  }
84
85  bool GamestateManager::update(){
86//     cleanup();
87    return getSnapshot();
88  }
89
90  bool GamestateManager::addGamestate(packet::Gamestate *gs, unsigned int clientID)
91  {
92    assert(gs);
93    std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateQueue.find(clientID);
94    if(it!=gamestateQueue.end())
95    {
96      // delete obsolete gamestate
97      delete it->second;
98    }
99    gamestateQueue[clientID] = gs;
100    return true;
101  }
102
103  bool GamestateManager::processGamestates()
104  {
105    if( this->gamestateQueue.empty() )
106        return true;
107    // now push only the most recent gamestates we received (ignore obsolete ones)
108    for(const auto& mapEntry : gamestateQueue)
109    {
110      OrxVerify(processGamestate(mapEntry.second), "ERROR: could not process Gamestate");
111      sendAck( mapEntry.second->getID(), mapEntry.second->getPeerID() );
112      delete mapEntry.second;
113    }
114    // now clear the queue
115    gamestateQueue.clear();
116    //and call all queued callbacks
117    NetworkCallbackManager::callCallbacks();
118    return true;
119  }
120 
121  bool GamestateManager::sendAck(unsigned int gamestateID, uint32_t peerID)
122  {
123    assert( gamestateID != ACKID_NACK );
124    packet::Acknowledgement *ack = new packet::Acknowledgement(gamestateID, peerID);
125    if( !this->sendPacket(ack))
126    {
127      orxout(internal_warning, context::network) << "could not ack gamestate: " << gamestateID << endl;
128      return false;
129    }
130    else
131    {
132      orxout(verbose_more, context::network) << "acked a gamestate: " << gamestateID << endl;
133      return true;
134    }
135  }
136
137
138  bool GamestateManager::getSnapshot(){
139    if ( currentGamestate_ != nullptr )
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
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    }
158   
159    currentGamestate_ = new packet::Gamestate();
160   
161    if(!currentGamestate_->collectData(newID, gsMode))
162    { //we have no data to send
163      delete currentGamestate_;
164      currentGamestate_=nullptr;
165      return false;
166    }
167    return true;
168  }
169
170  std::vector<packet::Gamestate*> GamestateManager::getGamestates()
171  {
172    if(!currentGamestate_)
173      return std::vector<packet::Gamestate*>();
174    std::vector<packet::Gamestate*> peerGamestates;
175   
176    for( const auto& mapEntry : peerMap_ )
177    {
178      if( !mapEntry.second.isSynched )
179      {
180        orxout(verbose_more, context::network) << "Server: not sending gamestate" << endl;
181        continue;
182      }
183      orxout(verbose_more, context::network) << "client id: " << mapEntry.first << endl;
184      orxout(verbose_more, context::network) << "Server: doing gamestate gamestate preparation" << endl;
185      int peerID = mapEntry.first; //get client id
186
187      unsigned int lastAckedGamestateID = mapEntry.second.lastAckedGamestateID;
188
189      packet::Gamestate* baseGamestate=nullptr;
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;
196      }
197
198      peerGamestates.push_back(nullptr);  // insert an empty gamestate* to be changed
199      finishGamestate( peerID, peerGamestates.back(), baseGamestate, currentGamestate_ );
200      if( peerGamestates.back()==nullptr )
201        // nothing to send to remove pointer from vector
202        peerGamestates.pop_back();
203      //FunctorMember<GamestateManager>* functor =
204//       ExecutorMember<GamestateManager>* executor = createExecutor( createFunctor(&GamestateManager::finishGamestate, this) );
205//       executor->setDefaultValues( cid, &clientGamestates.back(), client, currentGamestate_ );
206//       (*static_cast<Executor*>(executor))();
207//       this->threadPool_->passFunction( executor, true );
208//       (*functor)( cid, &(clientGamestates.back()), client, currentGamestate_ );
209    }
210
211//     threadPool_->synchronise();
212
213    return peerGamestates;
214  }
215
216
217  void GamestateManager::finishGamestate( unsigned int peerID, packet::Gamestate*& destgamestate, packet::Gamestate* base, packet::Gamestate* gamestate ) {
218    //why are we searching the same client's gamestate id as we searched in
219    //Server::sendGameState?
220    // save the (undiffed) gamestate in the clients gamestate map
221    //chose wheather the next gamestate is the first or not
222
223//     packet::Gamestate *gs = gamestate->doSelection(clientID, 20000);
224//       packet::Gamestate* gs = new packet::Gamestate(*gamestate);
225//     packet::Gamestate* gs = gamestate;
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
227//     packet::Gamestate *gs = new packet::Gamestate();
228//     gs->collectData( id_, 0x1 );
229//     this->threadMutex_->lock();
230    peerMap_[peerID].gamestates[gamestate->getID()]=gs;
231//     this->threadMutex_->unlock();
232    Clock clock;
233    clock.capture();
234
235    if(base)
236    {
237      packet::Gamestate *diffed1 = gs->diffVariables(base);
238      if( diffed1->getDataSize() == 0 )
239      {
240        delete diffed1;
241        destgamestate = nullptr;
242        return;
243      }
244      gs = diffed1;
245    }
246    else
247    {
248      gs = new packet::Gamestate(*gs);
249    }
250
251
252//     OrxVerify(gs->compressData(), "");
253    clock.capture();
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();
256//     if(gamestate->isDiffed())
257//       orxout(verbose_more, context::network) << " and baseid " << gs->getBaseID() << endl;
258//     else
259//       orxout(verbose_more, context::network) << endl;
260    gs->setPeerID(peerID);
261    destgamestate = gs;
262  }
263
264
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;
272
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//     }
285
286//    assert(curid==GAMESTATEID_INITIAL || curid<=gamestateID); // this line is commented out because acknowledgements are unreliable and may arrive in distorted order
287    if( gamestateID <= curid && curid != GAMESTATEID_INITIAL )
288        return true;
289orxout(verbose, context::network) << "acking gamestate " << gamestateID << " for peerID: " << peerID << " curid: " << curid << endl;
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;
300    }
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);
310    return true;
311  }
312 
313  uint32_t GamestateManager::getLastReceivedGamestateID(unsigned int peerID)
314  {
315    assert( this->peerMap_.find(peerID)!=this->peerMap_.end() );
316    if( this->peerMap_.find(peerID) != this->peerMap_.end() )
317      return this->peerMap_[peerID].lastReceivedGamestateID;
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;
327    peerMap_[peerID].lastReceivedGamestateID = GAMESTATEID_INITIAL;
328    peerMap_[peerID].lastAckedGamestateID = GAMESTATEID_INITIAL;
329    if( GameMode::isMaster() )
330      peerMap_[peerID].isSynched = false;
331    else
332      peerMap_[peerID].isSynched = true;
333  }
334
335  void GamestateManager::removePeer(uint32_t peerID)
336  {
337    assert(peerMap_.find(peerID)!=peerMap_.end());
338    for( const auto& mapEntry : peerMap_[peerID].gamestates )
339    {
340      delete mapEntry.second;
341    }
342    peerMap_.erase(peerMap_.find(peerID));
343  }
344
345
346//   void GamestateManager::removeClient(ClientInformation* client){
347//     assert(client);
348//     std::map<unsigned int, std::map<unsigned int, packet::Gamestate*>>::iterator clientMap = gamestateMap_.find(client->getID());
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  {
359    if(gs->isCompressed())
360    {
361       OrxVerify(gs->decompressData(), "ERROR: could not decompress Gamestate");
362    }
363    assert(!gs->isDiffed());
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    {
371      this->peerMap_[gs->getPeerID()].lastReceivedGamestateID = gs->getID();
372      return true;
373    }
374    else
375      return false;
376  }
377
378}
Note: See TracBrowser for help on using the repository browser.