Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2552 was 2552, checked in by scheusso, 15 years ago

found a memory leak

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