Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/network/GamestateManager.cc @ 1741

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

compressing doesnt work yet (maybe rather decompressing)

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