Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/network/GamestateManager.cc @ 2371

Last change on this file since 2371 was 2371, checked in by scheusso, 16 years ago

merged network branch to presentation branch

  • Property svn:eol-style set to native
File size: 6.4 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:
23 *      Oliver Scheuss, (C) 2007
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 <utility>
44#include <iostream>
45#include <zlib.h>
[1755]46#include <cassert>
[1705]47
48#include "core/CoreIncludes.h"
49#include "core/BaseObject.h"
50#include "ClientInformation.h"
[2371]51#include "synchronisable/Synchronisable.h"
52#include "synchronisable/NetworkCallbackManager.h"
[1705]53
[2171]54namespace orxonox
[1705]55{
56  GamestateManager::GamestateManager() {
57    id_=0;
58  }
59
60  GamestateManager::~GamestateManager() {
61  }
62
63  bool GamestateManager::update(){
[1907]64//     cleanup();
[1730]65    return getSnapshot();
[1705]66  }
[2087]67
68  bool GamestateManager::add(packet::Gamestate *gs, unsigned int clientID){
[1705]69    assert(gs);
[2087]70    std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateQueue.find(clientID);
[1705]71    if(it!=gamestateQueue.end()){
72      // delete obsolete gamestate
73      delete it->second;
74    }
75    gamestateQueue[clientID] = gs;
76    return true;
77  }
[2087]78
[1705]79  bool GamestateManager::processGamestates(){
[2087]80    std::map<unsigned int, packet::Gamestate*>::iterator it;
[1705]81    // now push only the most recent gamestates we received (ignore obsolete ones)
82    for(it = gamestateQueue.begin(); it!=gamestateQueue.end(); it++){
[2087]83      bool b = processGamestate(it->second);
84      assert(b);
[1705]85      delete it->second;
86    }
87    // now clear the queue
88    gamestateQueue.clear();
[2371]89    //and call all queued callbacks
90    NetworkCallbackManager::callCallbacks();
[1705]91    return true;
92  }
[2087]93
94
[1705]95  bool GamestateManager::getSnapshot(){
96    reference = new packet::Gamestate();
[2087]97    if(!reference->collectData(++id_)){ //we have no data to send
98      delete reference;
99      reference=0;
100    }
[1705]101    return true;
102  }
[2087]103
[1705]104  /**
105   * this function is used to keep the memory usage low
106   * it tries to delete all the unused gamestates
[2087]107   *
108   *
[1705]109   */
[1907]110/*  void GamestateManager::cleanup(){
[1705]111    std::map<int,int>::iterator it = gamestateUsed.begin();
112    while(it!=gamestateUsed.end()){
113      if((id_-(*it).first)<KEEP_GAMESTATES)
114        break;
115      if( (*it).second <= 0 ){
116        COUT(5) << "GameStateManager: deleting gamestate with id: " << (*it).first << ", uses: " << (*it).second << std::endl;
117        std::map<int, packet::Gamestate *>::iterator tempit = gamestateMap.find((*it).first);
118        if( tempit != gamestateMap.end() ){
119          packet::Gamestate *temp = tempit->second;
120          if(temp){
121            delete gamestateMap[(*it).first];
122            gamestateMap.erase((*it).first);
123          }
124        }
125        gamestateUsed.erase(it++);
126        continue;
127      }
128      it++;
129    }
[1907]130  }*/
[1705]131
[2087]132  packet::Gamestate *GamestateManager::popGameState(unsigned int clientID) {
[1705]133    //why are we searching the same client's gamestate id as we searched in
134    //Server::sendGameState?
[1751]135    packet::Gamestate *gs;
[2087]136    unsigned int gID = ClientInformation::findClient(clientID)->getGamestateID();
137    if(!reference)
138      return 0;
[2371]139    gs = reference->doSelection(clientID, 10000);
[1907]140//     gs = new packet::Gamestate(*reference);
141    // save the (undiffed) gamestate in the clients gamestate map
142    gamestateMap_[clientID].insert(std::pair<int, packet::Gamestate*>(gs->getID(), gs));
[1705]143    //chose wheather the next gamestate is the first or not
[1907]144    packet::Gamestate *client=NULL;
[1705]145    if(gID != GAMESTATEID_INITIAL){
[2087]146      std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(clientID);
[1907]147      if(clientMap!=gamestateMap_.end()){
[2087]148        std::map<unsigned int, packet::Gamestate*>::iterator it = clientMap->second.find(gID);
[1907]149        if(it!=clientMap->second.end())
150          client = it->second;
151      }
[1705]152    }
[1907]153    if(client){
154//       COUT(3) << "diffing" << std::endl;
155      gs = gs->diff(client);
156    }
157    else{
158//       COUT(3) << "not diffing" << std::endl;
159      gs = new packet::Gamestate(*gs);
160    }
161    bool b = gs->compressData();
162    assert(b);
[1751]163    return gs;
[1705]164  }
[2087]165
166
167  bool GamestateManager::ack(unsigned int gamestateID, unsigned int clientID) {
[1705]168    ClientInformation *temp = ClientInformation::findClient(clientID);
[1907]169    assert(temp);
[2087]170    unsigned int curid = temp->getGamestateID();
171
[1769]172    if(gamestateID == 0){
[1705]173      temp->setGamestateID(GAMESTATEID_INITIAL);
[1769]174      return true;
[1705]175    }
[2087]176
177    assert(curid==(unsigned int)GAMESTATEID_INITIAL || curid<gamestateID);
[1705]178    COUT(4) << "acking gamestate " << gamestateID << " for clientid: " << clientID << " curid: " << curid << std::endl;
[2087]179    std::map<unsigned int, packet::Gamestate*>::iterator it, tempit;
[1907]180    for(it = gamestateMap_[clientID].begin(); it!=gamestateMap_[clientID].end() && it->first<gamestateID; it++){
181      delete it->second;
182      tempit=it++;
183      gamestateMap_[clientID].erase(tempit);
[1705]184    }
[1907]185    temp->setGamestateID(gamestateID);
[1705]186    return true;
187  }
188
189  void GamestateManager::removeClient(ClientInformation* client){
[1907]190    assert(client);
[2087]191    std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(client->getID());
[1907]192    // first delete all remained gamestates
[2087]193    std::map<unsigned int, packet::Gamestate*>::iterator it;
[1907]194    for(it=clientMap->second.begin(); it!=clientMap->second.end(); it++)
195      delete it->second;
196    // now delete the clients gamestatemap
197    gamestateMap_.erase(clientMap);
[1705]198  }
[2087]199
[1712]200  bool GamestateManager::processGamestate(packet::Gamestate *gs){
[1751]201    if(gs->isCompressed())
[1907]202    {
203       bool b = gs->decompressData();
204       assert(b);
205    }
[1712]206    assert(!gs->isDiffed());
207    return gs->spreadData();
208  }
[1705]209
210}
Note: See TracBrowser for help on using the repository browser.