- Timestamp:
- Dec 19, 2010, 2:27:06 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/network5/src/libraries/network/GamestateManager.cc
r7773 r7777 43 43 #include <cassert> 44 44 #include <queue> 45 #include "util/Clock.h"46 45 // #include <boost/thread/mutex.hpp> 47 46 48 #include "util/Debug.h"49 #include "core/ThreadPool.h"50 #include "core/command/Executor.h"51 #include "ClientInformation.h"52 47 #include "packet/Acknowledgement.h" 53 48 #include "packet/Gamestate.h" 54 49 #include "synchronisable/NetworkCallbackManager.h" 55 #include "TrafficControl.h" 50 51 #include "core/ThreadPool.h" 52 #include "core/command/Executor.h" 53 #include "core/GameMode.h" 54 #include "util/Debug.h" 55 #include "util/Clock.h" 56 // #include "TrafficControl.h" 56 57 57 58 namespace orxonox 58 59 { 59 60 GamestateManager::GamestateManager() : 60 reference(0), id_(0)61 { 62 trafficControl_ = new TrafficControl();61 currentGamestate_(0), id_(0) 62 { 63 // trafficControl_ = new TrafficControl(); 63 64 // threadMutex_ = new boost::mutex(); 64 65 // threadPool_ = new ThreadPool(); … … 67 68 GamestateManager::~GamestateManager() 68 69 { 69 if( this-> reference)70 delete this-> reference;std::map<unsigned int, packet::Gamestate*>::iterator it;70 if( this->currentGamestate_ ) 71 delete this->currentGamestate_;std::map<unsigned int, packet::Gamestate*>::iterator it; 71 72 for( it = gamestateQueue.begin(); it != gamestateQueue.end(); ++it ) 72 73 delete it->second; 73 std::map<u nsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator it1;74 std::map<u nsigned 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();74 std::map<uint32_t, peerInfo>::iterator peerIt; 75 std::map<uint32_t, packet::Gamestate*>::iterator gamestateIt; 76 for( peerIt = peerMap_.begin(); peerIt != peerMap_.end(); ++peerIt ) 77 { 78 for( gamestateIt = peerIt->second.gamestates.begin(); gamestateIt != peerIt->second.gamestates.end(); ++gamestateIt ) 79 delete gamestateIt->second; 80 } 81 // this->trafficControl_->destroy(); 81 82 // delete this->threadMutex_; 82 83 // delete this->threadPool_; … … 88 89 } 89 90 90 bool GamestateManager::add(packet::Gamestate *gs, unsigned int clientID){ 91 bool GamestateManager::addGamestate(packet::Gamestate *gs, unsigned int clientID) 92 { 91 93 assert(gs); 92 94 std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateQueue.find(clientID); … … 99 101 } 100 102 101 bool GamestateManager::processGamestates(){ 103 bool GamestateManager::processGamestates() 104 { 102 105 if( this->gamestateQueue.empty() ) 103 106 return true; … … 118 121 119 122 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; 123 if ( currentGamestate_ != 0 ) 124 delete currentGamestate_; 125 currentGamestate_ = new packet::Gamestate(); 126 uint8_t gsMode; 127 if( GameMode::isMaster() ) 128 gsMode = packet::GAMESTATE_MODE_SERVER; 129 else 130 gsMode = packet::GAMESTATE_MODE_CLIENT; 131 uint32_t newID; 132 if( GameMode::isMaster() ) 133 newID = ++id_; 134 else 135 newID = peerMap_[NETWORK_PEER_ID_SERVER].lastProcessedGamestateID; 136 137 if(!currentGamestate_->collectData(newID, gsMode)){ //we have no data to send 138 delete currentGamestate_; 139 currentGamestate_=0; 126 140 } 127 141 return true; 128 142 } 129 143 130 void GamestateManager::sendGamestates() 131 { 132 ClientInformation *temp = ClientInformation::getBegin(); 133 std::queue<packet::Gamestate*> clientGamestates; 134 while(temp != NULL){ 135 if( !(temp->getSynched()) ){ 144 std::vector<packet::Gamestate*> GamestateManager::getGamestates() 145 { 146 if(!currentGamestate_) 147 return std::vector<packet::Gamestate*>(); 148 std::vector<packet::Gamestate*> peerGamestates; 149 150 std::map<uint32_t, peerInfo>::iterator peerIt; 151 for( peerIt=peerMap_.begin(); peerIt!=peerMap_.end(); ++peerIt ) 152 { 153 if( !peerIt->second.isSynched ) 154 { 136 155 COUT(5) << "Server: not sending gamestate" << std::endl; 137 temp=temp->next();138 if(!temp)139 break;140 156 continue; 141 157 } 142 COUT(4) << "client id: " << temp->getID() << " RTT: " << temp->getRTT() << " loss: " << temp->getPacketLoss()<< std::endl;158 COUT(4) << "client id: " << peerIt->first << std::endl; 143 159 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 ); 160 int peerID = peerIt->first; //get client id 161 162 unsigned int lastAckedGamestateID = peerIt->second.lastAckedGamestateID; 163 164 packet::Gamestate* baseGamestate=0; 165 if(lastAckedGamestateID != GAMESTATEID_INITIAL) 166 { 167 assert(peerMap_.find(peerID)!=peerMap_.end()); 168 std::map<uint32_t, packet::Gamestate*>::iterator it = peerMap_[peerID].gamestates.find(lastAckedGamestateID); 169 assert(it!=peerMap_[peerID].gamestates.end()); 170 baseGamestate = it->second; 171 } 172 173 peerGamestates.push_back(0); // insert an empty gamestate* to change 174 finishGamestate( peerID, peerGamestates.back(), baseGamestate, currentGamestate_ ); 162 175 //FunctorMember<GamestateManager>* functor = 163 176 // ExecutorMember<GamestateManager>* executor = createExecutor( createFunctor(&GamestateManager::finishGamestate, this) ); 164 // executor->setDefaultValues( cid, &clientGamestates.back(), client, reference);177 // executor->setDefaultValues( cid, &clientGamestates.back(), client, currentGamestate_ ); 165 178 // (*static_cast<Executor*>(executor))(); 166 179 // this->threadPool_->passFunction( executor, true ); 167 // (*functor)( cid, &(clientGamestates.back()), client, reference ); 168 169 temp = temp->next(); 180 // (*functor)( cid, &(clientGamestates.back()), client, currentGamestate_ ); 170 181 } 171 182 172 183 // threadPool_->synchronise(); 173 184 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 ) { 185 return peerGamestates; 186 } 187 188 189 void GamestateManager::finishGamestate( unsigned int peerID, packet::Gamestate*& destgamestate, packet::Gamestate* base, packet::Gamestate* gamestate ) { 184 190 //why are we searching the same client's gamestate id as we searched in 185 191 //Server::sendGameState? … … 194 200 // gs->collectData( id_, 0x1 ); 195 201 // this->threadMutex_->lock(); 196 gamestateMap_[clientID][gamestate->getID()]=gs;202 peerMap_[peerID].gamestates[gamestate->getID()]=gs; 197 203 // this->threadMutex_->unlock(); 198 199 204 Clock clock; 205 clock.capture(); 200 206 201 207 if(base) … … 218 224 // bool b = gs->compressData(); 219 225 // assert(b); 220 221 226 clock.capture(); 227 COUT(4) << "diff and compress time: " << clock.getDeltaTime() << endl; 222 228 // COUT(5) << "sending gamestate with id " << gs->getID(); 223 229 // if(gamestate->isDiffed()) … … 225 231 // else 226 232 // COUT(5) << endl; 227 gs->set ClientID(clientID);233 gs->setPeerID(peerID); 228 234 destgamestate = gs; 229 235 } 230 236 231 237 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(); 238 bool GamestateManager::ackGamestate(unsigned int gamestateID, unsigned int peerID) { 239 // ClientInformation *temp = ClientInformation::findClient(peerID); 240 // assert(temp); 241 std::map<uint32_t, peerInfo>::iterator it = this->peerMap_.find(peerID); 242 assert(it!=this->peerMap_.end()); 243 unsigned int curid = it->second.lastAckedGamestateID; 236 244 237 245 if(gamestateID == ACKID_NACK){ 238 temp->setGamestateID(GAMESTATEID_INITIAL); 246 it->second.lastAckedGamestateID = GAMESTATEID_INITIAL; 247 // temp->setGamestateID(GAMESTATEID_INITIAL); 239 248 // 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 } 249 std::map<uint32_t, packet::Gamestate*>::iterator it2; 250 for(it2 = it->second.gamestates.begin(); it2!=it->second.gamestates.end(); ++it2 ){ 251 delete it2->second; 252 } 253 it->second.gamestates.clear(); 246 254 return true; 247 255 } 248 256 249 257 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 COUT(5) << "acking gamestate " << gamestateID << " for peerID: " << peerID << " curid: " << curid << std::endl; 259 std::map<uint32_t, packet::Gamestate*>::iterator it2; 260 for( it2=it->second.gamestates.begin(); it2!=it->second.gamestates.end(); ) 261 { 262 if( it2->second->getID() < gamestateID ) 263 { 264 delete it2->second; 265 it->second.gamestates.erase(it2++); 266 } 267 else 268 ++it2; 269 } 270 271 // std::map<unsigned int, packet::Gamestate*>::iterator it; 272 // for(it = gamestateMap_[peerID].begin(); it!=gamestateMap_[peerID].end() && it->first<gamestateID; ){ 273 // delete it->second; 274 // gamestateMap_[peerID].erase(it++); 275 // } 276 it->second.lastAckedGamestateID = gamestateID; 277 // temp->setGamestateID(gamestateID); 278 // TrafficControl::processAck(peerID, gamestateID); 258 279 return true; 259 280 } 260 281 261 uint32_t GamestateManager::getLastProcessedGamestateID(unsigned int clientID)262 { 263 assert( this-> lastProcessedGamestateID_.find(clientID) != this->lastProcessedGamestateID_.end() );264 if( this-> lastProcessedGamestateID_.find(clientID) != this->lastProcessedGamestateID_.end() )265 return this-> lastProcessedGamestateID_[clientID];282 uint32_t GamestateManager::getLastProcessedGamestateID(unsigned int peerID) 283 { 284 assert( this->peerMap_.find(peerID)!=this->peerMap_.end() ); 285 if( this->peerMap_.find(peerID) != this->peerMap_.end() ) 286 return this->peerMap_[peerID].lastProcessedGamestateID; 266 287 else 267 288 return GAMESTATEID_INITIAL; 268 289 } 269 270 void GamestateManager::removeClient(ClientInformation* client){ 271 assert(client); 272 std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(client->getID()); 273 // first delete all remained gamestates 274 std::map<unsigned int, packet::Gamestate*>::iterator it; 275 for(it=clientMap->second.begin(); it!=clientMap->second.end(); it++) 276 delete it->second; 277 // now delete the clients gamestatemap 278 gamestateMap_.erase(clientMap); 279 } 280 281 bool GamestateManager::processGamestate(packet::Gamestate *gs){ 290 291 292 void GamestateManager::addPeer(uint32_t peerID) 293 { 294 assert(peerMap_.find(peerID)==peerMap_.end()); 295 peerMap_[peerID].peerID = peerID; 296 peerMap_[peerID].lastProcessedGamestateID = GAMESTATEID_INITIAL; 297 peerMap_[peerID].lastAckedGamestateID = GAMESTATEID_INITIAL; 298 if( GameMode::isMaster() ) 299 peerMap_[peerID].isSynched = false; 300 else 301 peerMap_[peerID].isSynched = true; 302 } 303 304 void GamestateManager::removePeer(uint32_t peerID) 305 { 306 assert(peerMap_.find(peerID)!=peerMap_.end()); 307 std::map<uint32_t, packet::Gamestate*>::iterator peerIt; 308 for( peerIt = peerMap_[peerID].gamestates.begin(); peerIt!=peerMap_[peerID].gamestates.end(); ++peerIt ) 309 { 310 delete peerIt->second; 311 } 312 peerMap_.erase(peerMap_.find(peerID)); 313 } 314 315 316 // void GamestateManager::removeClient(ClientInformation* client){ 317 // assert(client); 318 // std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(client->getID()); 319 // // first delete all remained gamestates 320 // std::map<unsigned int, packet::Gamestate*>::iterator it; 321 // for(it=clientMap->second.begin(); it!=clientMap->second.end(); it++) 322 // delete it->second; 323 // // now delete the clients gamestatemap 324 // gamestateMap_.erase(clientMap); 325 // } 326 327 bool GamestateManager::processGamestate(packet::Gamestate *gs) 328 { 282 329 if(gs->isCompressed()) 283 330 { … … 286 333 } 287 334 assert(!gs->isDiffed()); 288 if( gs->spreadData(0x1) ) 289 { 290 this->lastProcessedGamestateID_[gs->getClientID()] = gs->getID(); 335 uint8_t gsMode; 336 if( GameMode::isMaster() ) 337 gsMode = packet::GAMESTATE_MODE_SERVER; 338 else 339 gsMode = packet::GAMESTATE_MODE_CLIENT; 340 if( gs->spreadData(gsMode) ) 341 { 342 this->peerMap_[gs->getPeerID()].lastProcessedGamestateID = gs->getID(); 291 343 return true; 292 344 }
Note: See TracChangeset
for help on using the changeset viewer.