[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> |
---|
[7163] | 45 | #include "util/Clock.h" |
---|
[3304] | 46 | // #include <boost/thread/mutex.hpp> |
---|
[1705] | 47 | |
---|
[3214] | 48 | #include "util/Debug.h" |
---|
[3304] | 49 | #include "core/ThreadPool.h" |
---|
[7204] | 50 | #include "core/command/Executor.h" |
---|
[1705] | 51 | #include "ClientInformation.h" |
---|
[3214] | 52 | #include "packet/Acknowledgement.h" |
---|
| 53 | #include "packet/Gamestate.h" |
---|
[2662] | 54 | #include "synchronisable/NetworkCallbackManager.h" |
---|
[3214] | 55 | #include "TrafficControl.h" |
---|
[1705] | 56 | |
---|
[2171] | 57 | namespace orxonox |
---|
[1705] | 58 | { |
---|
[2662] | 59 | GamestateManager::GamestateManager() : |
---|
| 60 | reference(0), id_(0) |
---|
| 61 | { |
---|
| 62 | trafficControl_ = new TrafficControl(); |
---|
[3304] | 63 | // threadMutex_ = new boost::mutex(); |
---|
| 64 | // threadPool_ = new ThreadPool(); |
---|
[1705] | 65 | } |
---|
| 66 | |
---|
[2662] | 67 | GamestateManager::~GamestateManager() |
---|
| 68 | { |
---|
[3198] | 69 | if( this->reference ) |
---|
[3304] | 70 | delete this->reference;std::map<unsigned int, packet::Gamestate*>::iterator it; |
---|
| 71 | for( it = gamestateQueue.begin(); it != gamestateQueue.end(); ++it ) |
---|
[6417] | 72 | delete it->second; |
---|
[3304] | 73 | std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator it1; |
---|
| 74 | std::map<unsigned 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 ) |
---|
[6417] | 78 | delete it2->second; |
---|
[3304] | 79 | } |
---|
[5929] | 80 | this->trafficControl_->destroy(); |
---|
[3304] | 81 | // delete this->threadMutex_; |
---|
| 82 | // delete this->threadPool_; |
---|
[1705] | 83 | } |
---|
| 84 | |
---|
| 85 | bool GamestateManager::update(){ |
---|
[1907] | 86 | // cleanup(); |
---|
[1730] | 87 | return getSnapshot(); |
---|
[1705] | 88 | } |
---|
[2087] | 89 | |
---|
| 90 | bool GamestateManager::add(packet::Gamestate *gs, unsigned int clientID){ |
---|
[1705] | 91 | assert(gs); |
---|
[2087] | 92 | std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateQueue.find(clientID); |
---|
[1705] | 93 | if(it!=gamestateQueue.end()){ |
---|
| 94 | // delete obsolete gamestate |
---|
| 95 | delete it->second; |
---|
| 96 | } |
---|
| 97 | gamestateQueue[clientID] = gs; |
---|
| 98 | return true; |
---|
| 99 | } |
---|
[2087] | 100 | |
---|
[1705] | 101 | bool GamestateManager::processGamestates(){ |
---|
[3304] | 102 | if( this->gamestateQueue.empty() ) |
---|
| 103 | return true; |
---|
[2087] | 104 | std::map<unsigned int, packet::Gamestate*>::iterator it; |
---|
[1705] | 105 | // now push only the most recent gamestates we received (ignore obsolete ones) |
---|
| 106 | for(it = gamestateQueue.begin(); it!=gamestateQueue.end(); it++){ |
---|
[2087] | 107 | bool b = processGamestate(it->second); |
---|
| 108 | assert(b); |
---|
[1705] | 109 | delete it->second; |
---|
| 110 | } |
---|
| 111 | // now clear the queue |
---|
| 112 | gamestateQueue.clear(); |
---|
[2662] | 113 | //and call all queued callbacks |
---|
| 114 | NetworkCallbackManager::callCallbacks(); |
---|
[1705] | 115 | return true; |
---|
| 116 | } |
---|
[2087] | 117 | |
---|
| 118 | |
---|
[1705] | 119 | bool GamestateManager::getSnapshot(){ |
---|
[2662] | 120 | if ( reference != 0 ) |
---|
| 121 | delete reference; |
---|
[1705] | 122 | reference = new packet::Gamestate(); |
---|
[3102] | 123 | if(!reference->collectData(++id_, 0x1)){ //we have no data to send |
---|
[2087] | 124 | delete reference; |
---|
| 125 | reference=0; |
---|
| 126 | } |
---|
[1705] | 127 | return true; |
---|
| 128 | } |
---|
[6417] | 129 | |
---|
[3304] | 130 | void GamestateManager::sendGamestates() |
---|
| 131 | { |
---|
| 132 | ClientInformation *temp = ClientInformation::getBegin(); |
---|
| 133 | std::queue<packet::Gamestate*> clientGamestates; |
---|
| 134 | while(temp != NULL){ |
---|
| 135 | if( !(temp->getSynched()) ){ |
---|
| 136 | COUT(5) << "Server: not sending gamestate" << std::endl; |
---|
| 137 | temp=temp->next(); |
---|
| 138 | if(!temp) |
---|
| 139 | break; |
---|
| 140 | continue; |
---|
| 141 | } |
---|
| 142 | COUT(4) << "client id: " << temp->getID() << " RTT: " << temp->getRTT() << " loss: " << temp->getPacketLoss() << std::endl; |
---|
| 143 | COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl; |
---|
| 144 | int cid = temp->getID(); //get client id |
---|
[6417] | 145 | |
---|
[3304] | 146 | unsigned int gID = temp->getGamestateID(); |
---|
| 147 | if(!reference) |
---|
| 148 | return; |
---|
[6417] | 149 | |
---|
[3304] | 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 | } |
---|
[6417] | 159 | |
---|
[3304] | 160 | clientGamestates.push(0); |
---|
[7163] | 161 | finishGamestate( cid, clientGamestates.back(), client, reference ); |
---|
[6417] | 162 | //FunctorMember<GamestateManager>* functor = |
---|
[5929] | 163 | // ExecutorMember<GamestateManager>* executor = createExecutor( createFunctor(&GamestateManager::finishGamestate, this) ); |
---|
[3304] | 164 | // executor->setDefaultValues( cid, &clientGamestates.back(), client, reference ); |
---|
| 165 | // (*static_cast<Executor*>(executor))(); |
---|
| 166 | // this->threadPool_->passFunction( executor, true ); |
---|
| 167 | // (*functor)( cid, &(clientGamestates.back()), client, reference ); |
---|
[6417] | 168 | |
---|
[3304] | 169 | temp = temp->next(); |
---|
| 170 | } |
---|
[6417] | 171 | |
---|
[3304] | 172 | // threadPool_->synchronise(); |
---|
[6417] | 173 | |
---|
[3304] | 174 | while( !clientGamestates.empty() ) |
---|
| 175 | { |
---|
| 176 | if(clientGamestates.front()) |
---|
| 177 | clientGamestates.front()->send(); |
---|
| 178 | clientGamestates.pop(); |
---|
| 179 | } |
---|
| 180 | } |
---|
[2087] | 181 | |
---|
[1705] | 182 | |
---|
[7163] | 183 | void GamestateManager::finishGamestate( unsigned int clientID, packet::Gamestate*& destgamestate, packet::Gamestate* base, packet::Gamestate* gamestate ) { |
---|
[1705] | 184 | //why are we searching the same client's gamestate id as we searched in |
---|
| 185 | //Server::sendGameState? |
---|
[1907] | 186 | // save the (undiffed) gamestate in the clients gamestate map |
---|
[1705] | 187 | //chose wheather the next gamestate is the first or not |
---|
[6417] | 188 | |
---|
[7163] | 189 | // packet::Gamestate *gs = gamestate->doSelection(clientID, 20000); |
---|
| 190 | // packet::Gamestate* gs = new packet::Gamestate(*gamestate); |
---|
| 191 | // packet::Gamestate* gs = gamestate; |
---|
| 192 | packet::Gamestate *gs = new packet::Gamestate(*gamestate); //TODO: is this neccessary ? |
---|
[3304] | 193 | // packet::Gamestate *gs = new packet::Gamestate(); |
---|
| 194 | // gs->collectData( id_, 0x1 ); |
---|
| 195 | // this->threadMutex_->lock(); |
---|
| 196 | gamestateMap_[clientID][gamestate->getID()]=gs; |
---|
| 197 | // this->threadMutex_->unlock(); |
---|
[7163] | 198 | Clock clock; |
---|
| 199 | clock.capture(); |
---|
[6417] | 200 | |
---|
[3304] | 201 | if(base) |
---|
| 202 | { |
---|
[7163] | 203 | packet::Gamestate *diffed1 = gs->diffVariables(base); |
---|
| 204 | if( diffed1->getDataSize() == 0 ) |
---|
| 205 | { |
---|
| 206 | delete diffed1; |
---|
| 207 | destgamestate = 0; |
---|
| 208 | return; |
---|
| 209 | } |
---|
| 210 | gs = diffed1; |
---|
[1907] | 211 | } |
---|
[7163] | 212 | else |
---|
| 213 | { |
---|
[1907] | 214 | gs = new packet::Gamestate(*gs); |
---|
| 215 | } |
---|
[6417] | 216 | |
---|
| 217 | |
---|
[1907] | 218 | bool b = gs->compressData(); |
---|
| 219 | assert(b); |
---|
[7163] | 220 | clock.capture(); |
---|
| 221 | COUT(0) << "diff time: " << clock.getDeltaTime() << endl; |
---|
| 222 | // COUT(5) << "sending gamestate with id " << gs->getID(); |
---|
[3304] | 223 | // if(gamestate->isDiffed()) |
---|
[7163] | 224 | // COUT(5) << " and baseid " << gs->getBaseID() << endl; |
---|
[3304] | 225 | // else |
---|
[7163] | 226 | // COUT(5) << endl; |
---|
[3304] | 227 | gs->setClientID(clientID); |
---|
[7163] | 228 | destgamestate = gs; |
---|
[1705] | 229 | } |
---|
[2087] | 230 | |
---|
| 231 | |
---|
| 232 | bool GamestateManager::ack(unsigned int gamestateID, unsigned int clientID) { |
---|
[1705] | 233 | ClientInformation *temp = ClientInformation::findClient(clientID); |
---|
[1907] | 234 | assert(temp); |
---|
[2087] | 235 | unsigned int curid = temp->getGamestateID(); |
---|
| 236 | |
---|
[2662] | 237 | if(gamestateID == ACKID_NACK){ |
---|
[1705] | 238 | temp->setGamestateID(GAMESTATEID_INITIAL); |
---|
[2662] | 239 | // 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; |
---|
[3084] | 243 | |
---|
[2662] | 244 | gamestateMap_[clientID].erase(it++); |
---|
| 245 | } |
---|
[1769] | 246 | return true; |
---|
[1705] | 247 | } |
---|
[2087] | 248 | |
---|
[3301] | 249 | assert(curid==GAMESTATEID_INITIAL || curid<gamestateID); |
---|
[3304] | 250 | COUT(5) << "acking gamestate " << gamestateID << " for clientid: " << clientID << " curid: " << curid << std::endl; |
---|
[2662] | 251 | std::map<unsigned int, packet::Gamestate*>::iterator it; |
---|
| 252 | for(it = gamestateMap_[clientID].begin(); it!=gamestateMap_[clientID].end() && it->first<gamestateID; ){ |
---|
[1907] | 253 | delete it->second; |
---|
[2662] | 254 | gamestateMap_[clientID].erase(it++); |
---|
[1705] | 255 | } |
---|
[1907] | 256 | temp->setGamestateID(gamestateID); |
---|
[2662] | 257 | TrafficControl::processAck(clientID, gamestateID); |
---|
[1705] | 258 | return true; |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | void GamestateManager::removeClient(ClientInformation* client){ |
---|
[1907] | 262 | assert(client); |
---|
[2087] | 263 | std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(client->getID()); |
---|
[1907] | 264 | // first delete all remained gamestates |
---|
[2087] | 265 | std::map<unsigned int, packet::Gamestate*>::iterator it; |
---|
[1907] | 266 | for(it=clientMap->second.begin(); it!=clientMap->second.end(); it++) |
---|
| 267 | delete it->second; |
---|
| 268 | // now delete the clients gamestatemap |
---|
| 269 | gamestateMap_.erase(clientMap); |
---|
[1705] | 270 | } |
---|
[2087] | 271 | |
---|
[1712] | 272 | bool GamestateManager::processGamestate(packet::Gamestate *gs){ |
---|
[1751] | 273 | if(gs->isCompressed()) |
---|
[1907] | 274 | { |
---|
| 275 | bool b = gs->decompressData(); |
---|
| 276 | assert(b); |
---|
| 277 | } |
---|
[1712] | 278 | assert(!gs->isDiffed()); |
---|
[3102] | 279 | return gs->spreadData(0x1); |
---|
[1712] | 280 | } |
---|
[1705] | 281 | |
---|
| 282 | } |
---|