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