Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/data_cleanup/src/libraries/network/GamestateManager.cc @ 7537

Last change on this file since 7537 was 7284, checked in by landauf, 14 years ago

merged consolecommands3 branch back to trunk.

note: the console command interface has changed completely, but the documentation is not yet up to date. just copy an existing command and change it to your needs, it's pretty self-explanatory. also the include files related to console commands are now located in core/command/. in the game it should work exactly like before, except for some changes in the auto-completion.

  • Property svn:eol-style set to native
File size: 8.8 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 "util/Clock.h"
46// #include <boost/thread/mutex.hpp>
47
48#include "util/Debug.h"
49#include "core/ThreadPool.h"
50#include "core/command/Executor.h"
51#include "ClientInformation.h"
52#include "packet/Acknowledgement.h"
53#include "packet/Gamestate.h"
54#include "synchronisable/NetworkCallbackManager.h"
55#include "TrafficControl.h"
56
57namespace orxonox
58{
59  GamestateManager::GamestateManager() :
60  reference(0), id_(0)
61  {
62    trafficControl_ = new TrafficControl();
63//     threadMutex_ = new boost::mutex();
64//     threadPool_ = new ThreadPool();
65  }
66
67  GamestateManager::~GamestateManager()
68  {
69    if( this->reference )
70        delete this->reference;std::map<unsigned int, packet::Gamestate*>::iterator it;
71    for( it = gamestateQueue.begin(); it != gamestateQueue.end(); ++it )
72      delete it->second;
73    std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator it1;
74    std::map<unsigned int, packet::Gamestate*>::iterator it2;
75    for( it1 = gamestateMap_.begin(); it1 != gamestateMap_.end(); ++it1 )
76    {
77      for( it2 = it1->second.begin(); it2 != it1->second.end(); ++it2 )
78        delete it2->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::add(packet::Gamestate *gs, unsigned int clientID){
91    assert(gs);
92    std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateQueue.find(clientID);
93    if(it!=gamestateQueue.end()){
94      // delete obsolete gamestate
95      delete it->second;
96    }
97    gamestateQueue[clientID] = gs;
98    return true;
99  }
100
101  bool GamestateManager::processGamestates(){
102    if( this->gamestateQueue.empty() )
103        return true;
104    std::map<unsigned int, packet::Gamestate*>::iterator it;
105    // now push only the most recent gamestates we received (ignore obsolete ones)
106    for(it = gamestateQueue.begin(); it!=gamestateQueue.end(); it++){
107      bool b = processGamestate(it->second);
108      assert(b);
109      delete it->second;
110    }
111    // now clear the queue
112    gamestateQueue.clear();
113    //and call all queued callbacks
114    NetworkCallbackManager::callCallbacks();
115    return true;
116  }
117
118
119  bool GamestateManager::getSnapshot(){
120    if ( reference != 0 )
121      delete reference;
122    reference = new packet::Gamestate();
123    if(!reference->collectData(++id_, 0x1)){ //we have no data to send
124      delete reference;
125      reference=0;
126    }
127    return true;
128  }
129
130  void GamestateManager::sendGamestates()
131  {
132    ClientInformation *temp = ClientInformation::getBegin();
133    std::queue<packet::Gamestate*> clientGamestates;
134    while(temp != NULL){
135      if( !(temp->getSynched()) ){
136        COUT(5) << "Server: not sending gamestate" << std::endl;
137        temp=temp->next();
138        if(!temp)
139          break;
140        continue;
141      }
142      COUT(4) << "client id: " << temp->getID() << " RTT: " << temp->getRTT() << " loss: " << temp->getPacketLoss() << std::endl;
143      COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl;
144      int cid = temp->getID(); //get client id
145
146      unsigned int gID = temp->getGamestateID();
147      if(!reference)
148        return;
149
150      packet::Gamestate *client=0;
151      if(gID != GAMESTATEID_INITIAL){
152        assert(gamestateMap_.find(cid)!=gamestateMap_.end());
153        std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateMap_[cid].find(gID);
154        if(it!=gamestateMap_[cid].end())
155        {
156          client = it->second;
157        }
158      }
159
160      clientGamestates.push(0);
161      finishGamestate( cid, clientGamestates.back(), client, reference );
162      //FunctorMember<GamestateManager>* functor =
163//       ExecutorMember<GamestateManager>* executor = createExecutor( createFunctor(&GamestateManager::finishGamestate, this) );
164//       executor->setDefaultValues( cid, &clientGamestates.back(), client, reference );
165//       (*static_cast<Executor*>(executor))();
166//       this->threadPool_->passFunction( executor, true );
167//       (*functor)( cid, &(clientGamestates.back()), client, reference );
168
169      temp = temp->next();
170    }
171
172//     threadPool_->synchronise();
173
174    while( !clientGamestates.empty() )
175    {
176      if(clientGamestates.front())
177        clientGamestates.front()->send();
178      clientGamestates.pop();
179    }
180  }
181
182
183  void GamestateManager::finishGamestate( unsigned int clientID, packet::Gamestate*& destgamestate, packet::Gamestate* base, packet::Gamestate* gamestate ) {
184    //why are we searching the same client's gamestate id as we searched in
185    //Server::sendGameState?
186    // save the (undiffed) gamestate in the clients gamestate map
187    //chose wheather the next gamestate is the first or not
188
189//     packet::Gamestate *gs = gamestate->doSelection(clientID, 20000);
190//       packet::Gamestate* gs = new packet::Gamestate(*gamestate);
191//     packet::Gamestate* gs = gamestate;
192    packet::Gamestate *gs = new packet::Gamestate(*gamestate); //TODO: is this neccessary ?
193//     packet::Gamestate *gs = new packet::Gamestate();
194//     gs->collectData( id_, 0x1 );
195//     this->threadMutex_->lock();
196    gamestateMap_[clientID][gamestate->getID()]=gs;
197//     this->threadMutex_->unlock();
198      Clock clock;
199      clock.capture();
200
201    if(base)
202    {
203      packet::Gamestate *diffed1 = gs->diffVariables(base);
204      if( diffed1->getDataSize() == 0 )
205      {
206        delete diffed1;
207        destgamestate = 0;
208        return;
209      }
210      gs = diffed1;
211    }
212    else
213    {
214      gs = new packet::Gamestate(*gs);
215    }
216
217
218    bool b = gs->compressData();
219    assert(b);
220      clock.capture();
221      COUT(0) << "diff time: " << clock.getDeltaTime() << endl;
222//     COUT(5) << "sending gamestate with id " << gs->getID();
223//     if(gamestate->isDiffed())
224//       COUT(5) << " and baseid " << gs->getBaseID() << endl;
225//     else
226//       COUT(5) << endl;
227    gs->setClientID(clientID);
228    destgamestate = gs;
229  }
230
231
232  bool GamestateManager::ack(unsigned int gamestateID, unsigned int clientID) {
233    ClientInformation *temp = ClientInformation::findClient(clientID);
234    assert(temp);
235    unsigned int curid = temp->getGamestateID();
236
237    if(gamestateID == ACKID_NACK){
238      temp->setGamestateID(GAMESTATEID_INITIAL);
239      // now delete all saved gamestates for this client
240      std::map<unsigned int, packet::Gamestate*>::iterator it;
241      for(it = gamestateMap_[clientID].begin(); it!=gamestateMap_[clientID].end(); ){
242        delete it->second;
243
244        gamestateMap_[clientID].erase(it++);
245      }
246      return true;
247    }
248
249    assert(curid==GAMESTATEID_INITIAL || curid<gamestateID);
250    COUT(5) << "acking gamestate " << gamestateID << " for clientid: " << clientID << " curid: " << curid << std::endl;
251    std::map<unsigned int, packet::Gamestate*>::iterator it;
252    for(it = gamestateMap_[clientID].begin(); it!=gamestateMap_[clientID].end() && it->first<gamestateID; ){
253      delete it->second;
254      gamestateMap_[clientID].erase(it++);
255    }
256    temp->setGamestateID(gamestateID);
257    TrafficControl::processAck(clientID, gamestateID);
258    return true;
259  }
260
261  void GamestateManager::removeClient(ClientInformation* client){
262    assert(client);
263    std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(client->getID());
264    // first delete all remained gamestates
265    std::map<unsigned int, packet::Gamestate*>::iterator it;
266    for(it=clientMap->second.begin(); it!=clientMap->second.end(); it++)
267      delete it->second;
268    // now delete the clients gamestatemap
269    gamestateMap_.erase(clientMap);
270  }
271
272  bool GamestateManager::processGamestate(packet::Gamestate *gs){
273    if(gs->isCompressed())
274    {
275       bool b = gs->decompressData();
276       assert(b);
277    }
278    assert(!gs->isDiffed());
279    return gs->spreadData(0x1);
280  }
281
282}
Note: See TracBrowser for help on using the repository browser.