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