[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: |
---|
[3084] | 23 | * Oliver Scheuss |
---|
[1705] | 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 | |
---|
[1755] | 43 | #include <cassert> |
---|
[3304] | 44 | #include <queue> |
---|
| 45 | // #include <boost/thread/mutex.hpp> |
---|
[1705] | 46 | |
---|
[3214] | 47 | #include "packet/Acknowledgement.h" |
---|
| 48 | #include "packet/Gamestate.h" |
---|
[2662] | 49 | #include "synchronisable/NetworkCallbackManager.h" |
---|
[1705] | 50 | |
---|
[7788] | 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" |
---|
| 57 | |
---|
[2171] | 58 | namespace orxonox |
---|
[1705] | 59 | { |
---|
[2662] | 60 | GamestateManager::GamestateManager() : |
---|
[7788] | 61 | currentGamestate_(0), id_(0) |
---|
[2662] | 62 | { |
---|
[7788] | 63 | // trafficControl_ = new TrafficControl(); |
---|
[3304] | 64 | // threadMutex_ = new boost::mutex(); |
---|
| 65 | // threadPool_ = new ThreadPool(); |
---|
[1705] | 66 | } |
---|
| 67 | |
---|
[2662] | 68 | GamestateManager::~GamestateManager() |
---|
| 69 | { |
---|
[7788] | 70 | if( this->currentGamestate_ ) |
---|
| 71 | delete this->currentGamestate_;std::map<unsigned int, packet::Gamestate*>::iterator it; |
---|
[3304] | 72 | for( it = gamestateQueue.begin(); it != gamestateQueue.end(); ++it ) |
---|
[6417] | 73 | delete it->second; |
---|
[7788] | 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 ) |
---|
[3304] | 77 | { |
---|
[7788] | 78 | for( gamestateIt = peerIt->second.gamestates.begin(); gamestateIt != peerIt->second.gamestates.end(); ++gamestateIt ) |
---|
| 79 | delete gamestateIt->second; |
---|
[3304] | 80 | } |
---|
[7788] | 81 | // this->trafficControl_->destroy(); |
---|
[3304] | 82 | // delete this->threadMutex_; |
---|
| 83 | // delete this->threadPool_; |
---|
[1705] | 84 | } |
---|
| 85 | |
---|
| 86 | bool GamestateManager::update(){ |
---|
[1907] | 87 | // cleanup(); |
---|
[1730] | 88 | return getSnapshot(); |
---|
[1705] | 89 | } |
---|
[2087] | 90 | |
---|
[7788] | 91 | bool GamestateManager::addGamestate(packet::Gamestate *gs, unsigned int clientID) |
---|
| 92 | { |
---|
[1705] | 93 | assert(gs); |
---|
[2087] | 94 | std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateQueue.find(clientID); |
---|
[1705] | 95 | if(it!=gamestateQueue.end()){ |
---|
| 96 | // delete obsolete gamestate |
---|
| 97 | delete it->second; |
---|
| 98 | } |
---|
| 99 | gamestateQueue[clientID] = gs; |
---|
| 100 | return true; |
---|
| 101 | } |
---|
[2087] | 102 | |
---|
[7788] | 103 | bool GamestateManager::processGamestates() |
---|
| 104 | { |
---|
[3304] | 105 | if( this->gamestateQueue.empty() ) |
---|
| 106 | return true; |
---|
[2087] | 107 | std::map<unsigned int, packet::Gamestate*>::iterator it; |
---|
[1705] | 108 | // now push only the most recent gamestates we received (ignore obsolete ones) |
---|
| 109 | for(it = gamestateQueue.begin(); it!=gamestateQueue.end(); it++){ |
---|
[2087] | 110 | bool b = processGamestate(it->second); |
---|
| 111 | assert(b); |
---|
[7788] | 112 | sendAck( it->second->getID(), it->second->getPeerID() ); |
---|
[1705] | 113 | delete it->second; |
---|
| 114 | } |
---|
| 115 | // now clear the queue |
---|
| 116 | gamestateQueue.clear(); |
---|
[2662] | 117 | //and call all queued callbacks |
---|
| 118 | NetworkCallbackManager::callCallbacks(); |
---|
[1705] | 119 | return true; |
---|
| 120 | } |
---|
[7788] | 121 | |
---|
| 122 | bool GamestateManager::sendAck(unsigned int gamestateID, uint32_t peerID) |
---|
| 123 | { |
---|
| 124 | packet::Acknowledgement *ack = new packet::Acknowledgement(gamestateID, peerID); |
---|
| 125 | if( !this->sendPacket(ack)) |
---|
| 126 | { |
---|
| 127 | COUT(3) << "could not ack gamestate: " << gamestateID << std::endl; |
---|
| 128 | return false; |
---|
| 129 | } |
---|
| 130 | else |
---|
| 131 | { |
---|
| 132 | COUT(5) << "acked a gamestate: " << gamestateID << std::endl; |
---|
| 133 | return true; |
---|
| 134 | } |
---|
| 135 | } |
---|
[2087] | 136 | |
---|
| 137 | |
---|
[1705] | 138 | bool GamestateManager::getSnapshot(){ |
---|
[7788] | 139 | if ( currentGamestate_ != 0 ) |
---|
| 140 | delete currentGamestate_; |
---|
| 141 | currentGamestate_ = new packet::Gamestate(); |
---|
| 142 | uint8_t gsMode; |
---|
| 143 | if( GameMode::isMaster() ) |
---|
| 144 | gsMode = packet::GAMESTATE_MODE_SERVER; |
---|
| 145 | else |
---|
| 146 | gsMode = packet::GAMESTATE_MODE_CLIENT; |
---|
| 147 | uint32_t newID; |
---|
| 148 | if( GameMode::isMaster() ) |
---|
| 149 | newID = ++id_; |
---|
| 150 | else |
---|
| 151 | newID = peerMap_[NETWORK_PEER_ID_SERVER].lastProcessedGamestateID; |
---|
| 152 | |
---|
| 153 | if(!currentGamestate_->collectData(newID, gsMode)){ //we have no data to send |
---|
| 154 | delete currentGamestate_; |
---|
| 155 | currentGamestate_=0; |
---|
[2087] | 156 | } |
---|
[1705] | 157 | return true; |
---|
| 158 | } |
---|
[6417] | 159 | |
---|
[7788] | 160 | std::vector<packet::Gamestate*> GamestateManager::getGamestates() |
---|
[3304] | 161 | { |
---|
[7788] | 162 | if(!currentGamestate_) |
---|
| 163 | return std::vector<packet::Gamestate*>(); |
---|
| 164 | std::vector<packet::Gamestate*> peerGamestates; |
---|
| 165 | |
---|
| 166 | std::map<uint32_t, peerInfo>::iterator peerIt; |
---|
| 167 | for( peerIt=peerMap_.begin(); peerIt!=peerMap_.end(); ++peerIt ) |
---|
| 168 | { |
---|
| 169 | if( !peerIt->second.isSynched ) |
---|
| 170 | { |
---|
[3304] | 171 | COUT(5) << "Server: not sending gamestate" << std::endl; |
---|
| 172 | continue; |
---|
| 173 | } |
---|
[7788] | 174 | COUT(4) << "client id: " << peerIt->first << std::endl; |
---|
[3304] | 175 | COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl; |
---|
[7788] | 176 | int peerID = peerIt->first; //get client id |
---|
[6417] | 177 | |
---|
[7788] | 178 | unsigned int lastAckedGamestateID = peerIt->second.lastAckedGamestateID; |
---|
[6417] | 179 | |
---|
[7788] | 180 | packet::Gamestate* baseGamestate=0; |
---|
| 181 | if(lastAckedGamestateID != GAMESTATEID_INITIAL) |
---|
| 182 | { |
---|
| 183 | assert(peerMap_.find(peerID)!=peerMap_.end()); |
---|
| 184 | std::map<uint32_t, packet::Gamestate*>::iterator it = peerMap_[peerID].gamestates.find(lastAckedGamestateID); |
---|
| 185 | assert(it!=peerMap_[peerID].gamestates.end()); |
---|
| 186 | baseGamestate = it->second; |
---|
[3304] | 187 | } |
---|
[6417] | 188 | |
---|
[7788] | 189 | peerGamestates.push_back(0); // insert an empty gamestate* to change |
---|
| 190 | finishGamestate( peerID, peerGamestates.back(), baseGamestate, currentGamestate_ ); |
---|
| 191 | if( peerGamestates.back()==0 ) |
---|
| 192 | // nothing to send to remove pointer from vector |
---|
| 193 | peerGamestates.pop_back(); |
---|
[6417] | 194 | //FunctorMember<GamestateManager>* functor = |
---|
[5929] | 195 | // ExecutorMember<GamestateManager>* executor = createExecutor( createFunctor(&GamestateManager::finishGamestate, this) ); |
---|
[7788] | 196 | // executor->setDefaultValues( cid, &clientGamestates.back(), client, currentGamestate_ ); |
---|
[3304] | 197 | // (*static_cast<Executor*>(executor))(); |
---|
| 198 | // this->threadPool_->passFunction( executor, true ); |
---|
[7788] | 199 | // (*functor)( cid, &(clientGamestates.back()), client, currentGamestate_ ); |
---|
[3304] | 200 | } |
---|
[6417] | 201 | |
---|
[3304] | 202 | // threadPool_->synchronise(); |
---|
[6417] | 203 | |
---|
[7788] | 204 | return peerGamestates; |
---|
[3304] | 205 | } |
---|
[2087] | 206 | |
---|
[1705] | 207 | |
---|
[7788] | 208 | void GamestateManager::finishGamestate( unsigned int peerID, packet::Gamestate*& destgamestate, packet::Gamestate* base, packet::Gamestate* gamestate ) { |
---|
[1705] | 209 | //why are we searching the same client's gamestate id as we searched in |
---|
| 210 | //Server::sendGameState? |
---|
[1907] | 211 | // save the (undiffed) gamestate in the clients gamestate map |
---|
[1705] | 212 | //chose wheather the next gamestate is the first or not |
---|
[6417] | 213 | |
---|
[7163] | 214 | // packet::Gamestate *gs = gamestate->doSelection(clientID, 20000); |
---|
| 215 | // packet::Gamestate* gs = new packet::Gamestate(*gamestate); |
---|
| 216 | // packet::Gamestate* gs = gamestate; |
---|
| 217 | packet::Gamestate *gs = new packet::Gamestate(*gamestate); //TODO: is this neccessary ? |
---|
[3304] | 218 | // packet::Gamestate *gs = new packet::Gamestate(); |
---|
| 219 | // gs->collectData( id_, 0x1 ); |
---|
| 220 | // this->threadMutex_->lock(); |
---|
[7788] | 221 | peerMap_[peerID].gamestates[gamestate->getID()]=gs; |
---|
[3304] | 222 | // this->threadMutex_->unlock(); |
---|
[7788] | 223 | Clock clock; |
---|
| 224 | clock.capture(); |
---|
[6417] | 225 | |
---|
[3304] | 226 | if(base) |
---|
| 227 | { |
---|
[7163] | 228 | packet::Gamestate *diffed1 = gs->diffVariables(base); |
---|
| 229 | if( diffed1->getDataSize() == 0 ) |
---|
| 230 | { |
---|
| 231 | delete diffed1; |
---|
| 232 | destgamestate = 0; |
---|
| 233 | return; |
---|
| 234 | } |
---|
| 235 | gs = diffed1; |
---|
[1907] | 236 | } |
---|
[7163] | 237 | else |
---|
| 238 | { |
---|
[1907] | 239 | gs = new packet::Gamestate(*gs); |
---|
| 240 | } |
---|
[6417] | 241 | |
---|
| 242 | |
---|
[7788] | 243 | // bool b = gs->compressData(); |
---|
| 244 | // assert(b); |
---|
| 245 | clock.capture(); |
---|
| 246 | COUT(4) << "diff and compress time: " << clock.getDeltaTime() << endl; |
---|
[7163] | 247 | // COUT(5) << "sending gamestate with id " << gs->getID(); |
---|
[3304] | 248 | // if(gamestate->isDiffed()) |
---|
[7163] | 249 | // COUT(5) << " and baseid " << gs->getBaseID() << endl; |
---|
[3304] | 250 | // else |
---|
[7163] | 251 | // COUT(5) << endl; |
---|
[7788] | 252 | gs->setPeerID(peerID); |
---|
[7163] | 253 | destgamestate = gs; |
---|
[1705] | 254 | } |
---|
[2087] | 255 | |
---|
| 256 | |
---|
[7788] | 257 | bool GamestateManager::ackGamestate(unsigned int gamestateID, unsigned int peerID) |
---|
| 258 | { |
---|
| 259 | // ClientInformation *temp = ClientInformation::findClient(peerID); |
---|
| 260 | // assert(temp); |
---|
| 261 | std::map<uint32_t, peerInfo>::iterator it = this->peerMap_.find(peerID); |
---|
| 262 | assert(it!=this->peerMap_.end()); |
---|
| 263 | unsigned int curid = it->second.lastAckedGamestateID; |
---|
[2087] | 264 | |
---|
[2662] | 265 | if(gamestateID == ACKID_NACK){ |
---|
[7788] | 266 | it->second.lastAckedGamestateID = GAMESTATEID_INITIAL; |
---|
| 267 | // temp->setGamestateID(GAMESTATEID_INITIAL); |
---|
[2662] | 268 | // now delete all saved gamestates for this client |
---|
[7788] | 269 | std::map<uint32_t, packet::Gamestate*>::iterator it2; |
---|
| 270 | for(it2 = it->second.gamestates.begin(); it2!=it->second.gamestates.end(); ++it2 ){ |
---|
| 271 | delete it2->second; |
---|
[2662] | 272 | } |
---|
[7788] | 273 | it->second.gamestates.clear(); |
---|
[1769] | 274 | return true; |
---|
[1705] | 275 | } |
---|
[2087] | 276 | |
---|
[7788] | 277 | assert(curid==GAMESTATEID_INITIAL || curid<=gamestateID); |
---|
| 278 | COUT(5) << "acking gamestate " << gamestateID << " for peerID: " << peerID << " curid: " << curid << std::endl; |
---|
| 279 | std::map<uint32_t, packet::Gamestate*>::iterator it2; |
---|
| 280 | for( it2=it->second.gamestates.begin(); it2!=it->second.gamestates.end(); ) |
---|
| 281 | { |
---|
| 282 | if( it2->second->getID() < gamestateID ) |
---|
| 283 | { |
---|
| 284 | delete it2->second; |
---|
| 285 | it->second.gamestates.erase(it2++); |
---|
| 286 | } |
---|
| 287 | else |
---|
| 288 | ++it2; |
---|
[1705] | 289 | } |
---|
[7788] | 290 | |
---|
| 291 | // std::map<unsigned int, packet::Gamestate*>::iterator it; |
---|
| 292 | // for(it = gamestateMap_[peerID].begin(); it!=gamestateMap_[peerID].end() && it->first<gamestateID; ){ |
---|
| 293 | // delete it->second; |
---|
| 294 | // gamestateMap_[peerID].erase(it++); |
---|
| 295 | // } |
---|
| 296 | it->second.lastAckedGamestateID = gamestateID; |
---|
| 297 | // temp->setGamestateID(gamestateID); |
---|
| 298 | // TrafficControl::processAck(peerID, gamestateID); |
---|
[1705] | 299 | return true; |
---|
| 300 | } |
---|
[7788] | 301 | |
---|
| 302 | uint32_t GamestateManager::getLastProcessedGamestateID(unsigned int peerID) |
---|
| 303 | { |
---|
| 304 | assert( this->peerMap_.find(peerID)!=this->peerMap_.end() ); |
---|
| 305 | if( this->peerMap_.find(peerID) != this->peerMap_.end() ) |
---|
| 306 | return this->peerMap_[peerID].lastProcessedGamestateID; |
---|
| 307 | else |
---|
| 308 | return GAMESTATEID_INITIAL; |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | |
---|
| 312 | void GamestateManager::addPeer(uint32_t peerID) |
---|
| 313 | { |
---|
| 314 | assert(peerMap_.find(peerID)==peerMap_.end()); |
---|
| 315 | peerMap_[peerID].peerID = peerID; |
---|
| 316 | peerMap_[peerID].lastProcessedGamestateID = GAMESTATEID_INITIAL; |
---|
| 317 | peerMap_[peerID].lastAckedGamestateID = GAMESTATEID_INITIAL; |
---|
| 318 | if( GameMode::isMaster() ) |
---|
| 319 | peerMap_[peerID].isSynched = false; |
---|
| 320 | else |
---|
| 321 | peerMap_[peerID].isSynched = true; |
---|
| 322 | } |
---|
[1705] | 323 | |
---|
[7788] | 324 | void GamestateManager::removePeer(uint32_t peerID) |
---|
| 325 | { |
---|
| 326 | assert(peerMap_.find(peerID)!=peerMap_.end()); |
---|
| 327 | std::map<uint32_t, packet::Gamestate*>::iterator peerIt; |
---|
| 328 | for( peerIt = peerMap_[peerID].gamestates.begin(); peerIt!=peerMap_[peerID].gamestates.end(); ++peerIt ) |
---|
| 329 | { |
---|
| 330 | delete peerIt->second; |
---|
| 331 | } |
---|
| 332 | peerMap_.erase(peerMap_.find(peerID)); |
---|
[1705] | 333 | } |
---|
[2087] | 334 | |
---|
[7788] | 335 | |
---|
| 336 | // void GamestateManager::removeClient(ClientInformation* client){ |
---|
| 337 | // assert(client); |
---|
| 338 | // std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(client->getID()); |
---|
| 339 | // // first delete all remained gamestates |
---|
| 340 | // std::map<unsigned int, packet::Gamestate*>::iterator it; |
---|
| 341 | // for(it=clientMap->second.begin(); it!=clientMap->second.end(); it++) |
---|
| 342 | // delete it->second; |
---|
| 343 | // // now delete the clients gamestatemap |
---|
| 344 | // gamestateMap_.erase(clientMap); |
---|
| 345 | // } |
---|
| 346 | |
---|
| 347 | bool GamestateManager::processGamestate(packet::Gamestate *gs) |
---|
| 348 | { |
---|
[1751] | 349 | if(gs->isCompressed()) |
---|
[1907] | 350 | { |
---|
| 351 | bool b = gs->decompressData(); |
---|
| 352 | assert(b); |
---|
| 353 | } |
---|
[1712] | 354 | assert(!gs->isDiffed()); |
---|
[7788] | 355 | uint8_t gsMode; |
---|
| 356 | if( GameMode::isMaster() ) |
---|
| 357 | gsMode = packet::GAMESTATE_MODE_SERVER; |
---|
| 358 | else |
---|
| 359 | gsMode = packet::GAMESTATE_MODE_CLIENT; |
---|
| 360 | if( gs->spreadData(gsMode) ) |
---|
| 361 | { |
---|
| 362 | this->peerMap_[gs->getPeerID()].lastProcessedGamestateID = gs->getID(); |
---|
| 363 | return true; |
---|
| 364 | } |
---|
| 365 | else |
---|
| 366 | return false; |
---|
[1712] | 367 | } |
---|
[1705] | 368 | |
---|
| 369 | } |
---|