Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp6/src/network/GamestateManager.cc @ 3227

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

some cleaning up and memory leak fixes

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