Changeset 11829 for code/branches/Masterserver_FS18/src
- Timestamp:
- Mar 22, 2018, 4:12:23 PM (7 years ago)
- Location:
- code/branches/Masterserver_FS18/src/libraries/network
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/Masterserver_FS18/src/libraries/network/Connection.cc
r11071 r11829 45 45 const unsigned int NETWORK_DISCONNECT_TIMEOUT = 500; 46 46 47 /** 48 * Constructor 49 * @param firstPeerId The initial value of nextPeerID_ 50 */ 47 51 Connection::Connection(uint32_t firstPeerID): 48 52 host_(nullptr), bCommunicationThreadRunning_(false), nextPeerID_(firstPeerID) 49 53 { 54 // Global initialization of ENet 50 55 enet_initialize(); 56 57 // Register enet_deinitialize to be executed when the program exits normally 51 58 atexit(enet_deinitialize); 59 60 // Create mutexes for incoming and outgoing events 52 61 this->incomingEventsMutex_ = new boost::mutex; 53 62 this->outgoingEventsMutex_ = new boost::mutex; 54 // this->overallMutex_ = new boost::mutex; 55 } 56 63 } 64 65 /** 66 * Destructor 67 */ 57 68 Connection::~Connection() 58 69 { 70 // Delete the mutexes 59 71 delete this->incomingEventsMutex_; 60 72 delete this->outgoingEventsMutex_; 61 } 62 73 74 // TODO: Why is enet_deinitialize() not called here? 75 // Would make sense, since its counterpart, enet_initialize(), is called in the constructor. 76 } 77 78 /** 79 * Start the main communication thread. 80 */ 63 81 void Connection::startCommunicationThread() 64 82 { … … 67 85 } 68 86 87 /** 88 * Stop the main communication thread. 89 */ 69 90 void Connection::stopCommunicationThread() 70 91 { 71 92 this->bCommunicationThreadRunning_ = false; 72 if( !this->communicationThread_->timed_join(NETWORK_COMMUNICATION_THREAD_WAIT_TIME) ) 73 { 74 // force thread to stop 93 // Wait for peaceful termination 94 if(!this->communicationThread_->timed_join(NETWORK_COMMUNICATION_THREAD_WAIT_TIME)) 95 { 96 // Force thread to stop if the waiting time runs out. 75 97 this->communicationThread_->interrupt(); 76 98 } … … 78 100 } 79 101 102 /** 103 * Send an outgoing event of type 'disconnectPeer'. 104 * @param peerID The peer to which the event is sent 105 */ 80 106 void Connection::disconnectPeer(uint32_t peerID) 81 107 { 82 // this->overallMutex_->lock();83 108 outgoingEvent outEvent = { peerID, OutgoingEventType::disconnectPeer, nullptr, 0 }; 84 109 … … 86 111 this->outgoingEvents_.push_back(outEvent); 87 112 this->outgoingEventsMutex_->unlock(); 88 // this->overallMutex_->unlock(); 89 } 90 113 } 114 115 /** 116 * Send an outgoing event of type 'disconnectPeers'. 117 */ 91 118 void Connection::disconnectPeers() 92 119 { … … 98 125 } 99 126 127 /** 128 * Send a packet. 129 * @param packet Pointer to the packet to send 130 * @param peerID The peer to which the event is sent 131 * @param channelId The channel ID 132 */ 100 133 void Connection::addPacket(ENetPacket* packet, uint32_t peerID, uint8_t channelID) 101 134 { 102 // this->overallMutex_->lock();103 135 outgoingEvent outEvent = { peerID, OutgoingEventType::sendPacket, packet, channelID }; 104 136 … … 106 138 this->outgoingEvents_.push_back(outEvent); 107 139 this->outgoingEventsMutex_->unlock(); 108 // this->overallMutex_->unlock(); 109 } 110 140 } 141 142 /** 143 * Send a broadcast packet. 144 * @param packet Pointer to the packet to send 145 * @param channelId The channel ID 146 */ 111 147 void Connection::broadcastPacket(ENetPacket* packet, uint8_t channelID) 112 148 { 113 // this->overallMutex_->lock();114 149 outgoingEvent outEvent = { 0, OutgoingEventType::broadcastPacket, packet, channelID }; 115 150 … … 117 152 this->outgoingEvents_.push_back(outEvent); 118 153 this->outgoingEventsMutex_->unlock(); 119 // this->overallMutex_->unlock(); 120 } 121 122 154 } 155 156 157 /** 158 * Main communication thread 159 */ 123 160 void Connection::communicationThread() 124 161 { 125 162 ENetEvent event; 126 163 127 // this->overallMutex_->lock(); 128 while( bCommunicationThreadRunning_ ) 164 while(this->bCommunicationThreadRunning_) 129 165 { 130 166 // Receive all pending incoming Events (such as packets, connects and disconnects) 131 while( enet_host_check_events( this->host_, &event ) > 0)167 while(enet_host_check_events(this->host_, &event ) > 0) 132 168 { 133 processIncomingEvent(event);169 this->processIncomingEvent(event); 134 170 } 135 171 136 // this->overallMutex_->unlock(); 172 // Sleep for 1ms 173 // TODO: Why? 137 174 msleep(1); 138 // this->overallMutex_->lock();139 175 140 176 // Send all waiting outgoing packets 177 // TODO: Why do we need a mutex to read a single variable? 141 178 this->outgoingEventsMutex_->lock(); 142 179 uint32_t outgoingEventsCount = this->outgoingEvents_.size(); 143 180 this->outgoingEventsMutex_->unlock(); 144 while( outgoingEventsCount > 0 ) 181 182 // TODO: Not convinced about how mutexes are used here, seems kinda pointless 183 while(outgoingEventsCount > 0) 145 184 { 146 // orxout(verbose, context::network) << "outgoing event" << endl;147 185 this->outgoingEventsMutex_->lock(); 148 186 outgoingEvent outEvent = this->outgoingEvents_.front(); … … 150 188 this->outgoingEventsMutex_->unlock(); 151 189 152 processOutgoingEvent(outEvent);190 this->processOutgoingEvent(outEvent); 153 191 154 192 this->outgoingEventsMutex_->lock(); … … 158 196 159 197 // Wait for incoming events (at most NETWORK_WAIT_TIMEOUT ms) 160 if( enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0)198 if(enet_host_service(this->host_, &event, NETWORK_WAIT_TIMEOUT) > 0) 161 199 { 162 processIncomingEvent(event);200 this->processIncomingEvent(event); 163 201 } 164 202 } 165 // this->overallMutex_->unlock(); 166 } 167 203 } 204 205 /** 206 * Handle an incoming event. 207 * @param event The incoming event 208 */ 168 209 void Connection::processIncomingEvent(ENetEvent& event) 169 210 { 170 211 incomingEvent inEvent; 171 212 // preprocess event 172 switch( event.type)213 switch(event.type) 173 214 { 174 215 case ENET_EVENT_TYPE_CONNECT: … … 192 233 } 193 234 235 /** 236 * Send an event. 237 * @param event The event to send 238 */ 194 239 void Connection::processOutgoingEvent(outgoingEvent& event) 195 240 { 196 241 ENetPeer* peer; 197 switch( event.type)242 switch(event.type) 198 243 { 199 244 case OutgoingEventType::sendPacket: 200 245 // check whether the peer is still/already in the peer list 201 if( this->peerMap_.find(event.peerID) != this->peerMap_.end())246 if(this->peerMap_.find(event.peerID) != this->peerMap_.end()) 202 247 { 203 248 peer = this->peerMap_[event.peerID]; 204 enet_peer_send( peer, event.channelID, event.packet);249 enet_peer_send(peer, event.channelID, event.packet); 205 250 } 206 251 else 207 252 { 208 253 // peer probably already disconnected so just discard packet 209 assert(event.peerID <this->nextPeerID_);254 assert(event.peerID < this->nextPeerID_); 210 255 enet_packet_destroy(event.packet); 211 256 } 212 257 break; 213 258 case OutgoingEventType::disconnectPeer: 214 if( this->peerMap_.find(event.peerID) != this->peerMap_.end())259 if(this->peerMap_.find(event.peerID) != this->peerMap_.end()) 215 260 { 216 261 peer = this->peerMap_[event.peerID]; … … 220 265 { 221 266 // peer probably already disconnected so just discard disconnect event 222 assert(event.peerID <this->nextPeerID_);267 assert(event.peerID < this->nextPeerID_); 223 268 } 224 269 break; 225 270 case OutgoingEventType::disconnectPeers: 226 disconnectPeersInternal();271 this->disconnectPeersInternal(); 227 272 break; 228 273 case OutgoingEventType::broadcastPacket: … … 234 279 } 235 280 236 237 281 void Connection::disconnectPeersInternal() 238 282 { … … 241 285 enet_peer_disconnect(mapEntry.second, 0); 242 286 } 243 uint32_t iterations = NETWORK_DISCONNECT_TIMEOUT /NETWORK_WAIT_TIMEOUT;287 uint32_t iterations = NETWORK_DISCONNECT_TIMEOUT / NETWORK_WAIT_TIMEOUT; 244 288 uint32_t i = 0; 245 289 while( this->peerMap_.size() && i++ < iterations ) -
code/branches/Masterserver_FS18/src/libraries/network/Connection.h
r11071 r11829 96 96 97 97 protected: 98 Connection(uint32_t firstPeerID = NETWORK_PEER_ID_SERVER +1);98 Connection(uint32_t firstPeerID = NETWORK_PEER_ID_SERVER + 1); 99 99 100 100 void startCommunicationThread(); … … 110 110 void processQueue(); 111 111 void waitOutgoingQueue(); // wait for the outgoing queue to become empty (everything processed by communication thread) 112 virtual void addPeer(uint32_t peerID) =0;113 virtual void removePeer(uint32_t peerID) =0;114 virtual void processPacket( packet::Packet* packet) =0;112 virtual void addPeer(uint32_t peerID) = 0; 113 virtual void removePeer(uint32_t peerID) = 0; 114 virtual void processPacket( packet::Packet* packet) = 0; 115 115 116 116 incomingEvent preprocessConnectEvent(ENetEvent& event); … … 124 124 125 125 ENetHost* host_; 126 126 127 private: 127 128 void communicationThread(); … … 130 131 bool bCommunicationThreadRunning_; 131 132 ENetAddress* bindAddress_; 133 134 // Queue for incoming events 132 135 std::deque<incomingEvent> incomingEvents_; 136 137 // Queue for outgoing events 133 138 std::deque<outgoingEvent> outgoingEvents_; 139 134 140 boost::mutex* incomingEventsMutex_; 135 141 boost::mutex* outgoingEventsMutex_; -
code/branches/Masterserver_FS18/src/libraries/network/GamestateManager.cc
r11071 r11829 43 43 #include <cassert> 44 44 #include <queue> 45 // #include <boost/thread/mutex.hpp>46 45 47 46 #include "packet/Acknowledgement.h" … … 55 54 #include "util/Clock.h" 56 55 #include "util/OrxAssert.h" 57 // #include "TrafficControl.h"58 56 59 57 namespace orxonox … … 62 60 currentGamestate_(nullptr), id_(0) 63 61 { 64 // trafficControl_ = new TrafficControl();65 // threadMutex_ = new boost::mutex();66 // threadPool_ = new ThreadPool();67 62 } 68 63 69 64 GamestateManager::~GamestateManager() 70 65 { 71 if( this->currentGamestate_ ) 72 delete this->currentGamestate_; 73 for( const auto& mapEntry : gamestateQueue ) 74 delete mapEntry.second; 75 for( const auto& mapEntryPeer : peerMap_ ) 76 { 77 for( const auto& mapEntryGamestate : mapEntryPeer.second.gamestates ) 78 delete mapEntryGamestate.second; 79 } 80 // this->trafficControl_->destroy(); 81 // delete this->threadMutex_; 82 // delete this->threadPool_; 83 } 84 85 bool GamestateManager::update(){ 86 // cleanup(); 66 if(this->currentGamestate_) 67 { 68 delete this->currentGamestate_; 69 } 70 71 for(const auto& gsPair : this->gamestateQueue) 72 { 73 delete gsPair.second; 74 } 75 76 for(const auto& peerPair : this->peerMap_) 77 { 78 for(const auto& gsPair : peerPair.second.gamestates) 79 { 80 delete gsPair.second; 81 } 82 } 83 } 84 85 bool GamestateManager::update() 86 { 87 87 return getSnapshot(); 88 88 } … … 91 91 { 92 92 assert(gs); 93 std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateQueue.find(clientID); 94 if(it!=gamestateQueue.end()) 93 // Search the queue for a gamestate for the client 94 std::map<unsigned int, packet::Gamestate*>::iterator it = this->gamestateQueue.find(clientID); 95 if(it != this->gamestateQueue.end()) 95 96 { 96 97 // delete obsolete gamestate 97 98 delete it->second; 98 99 } 99 gamestateQueue[clientID] = gs; 100 // update the client's gamestate 101 this->gamestateQueue[clientID] = gs; 102 100 103 return true; 101 104 } 102 105 106 /** 107 * Process the queued gamestates. 108 */ 103 109 bool GamestateManager::processGamestates() 104 110 { 105 if( this->gamestateQueue.empty() ) 111 // Queue is empty, nothing to do 112 if(this->gamestateQueue.empty()) 113 { 106 114 return true; 115 } 116 107 117 // now push only the most recent gamestates we received (ignore obsolete ones) 108 for(const auto& mapEntry :gamestateQueue)109 { 110 OrxVerify(processGamestate( mapEntry.second), "ERROR: could not process Gamestate");111 sendAck( mapEntry.second->getID(), mapEntry.second->getPeerID());112 delete mapEntry.second;118 for(const auto& gsPair : this->gamestateQueue) 119 { 120 OrxVerify(processGamestate(gsPair.second), "ERROR: could not process Gamestate"); 121 sendAck(gsPair.second->getID(), gsPair.second->getPeerID()); 122 delete gsPair.second; 113 123 } 114 124 // now clear the queue 115 gamestateQueue.clear(); 125 this->gamestateQueue.clear(); 126 116 127 //and call all queued callbacks 117 128 NetworkCallbackManager::callCallbacks(); 129 118 130 return true; 119 131 } 120 132 133 /** 134 * Send Acknowledgement packet. 135 * @param gamestateId The gamestate ID we want to acknowledge 136 * @param peerID The ID of the peer we want to send the Acknowledgement to 137 */ 121 138 bool GamestateManager::sendAck(unsigned int gamestateID, uint32_t peerID) 122 139 { 123 assert( gamestateID != ACKID_NACK);140 assert(gamestateID != ACKID_NACK); 124 141 packet::Acknowledgement *ack = new packet::Acknowledgement(gamestateID, peerID); 125 if( 142 if(!this->sendPacket(ack)) 126 143 { 127 144 orxout(internal_warning, context::network) << "could not ack gamestate: " << gamestateID << endl; … … 135 152 } 136 153 137 138 bool GamestateManager::getSnapshot(){ 139 if ( currentGamestate_ != nullptr ) 140 delete currentGamestate_; 154 /** 155 * Update the current gamestate. 156 */ 157 bool GamestateManager::getSnapshot() 158 { 159 // Delete current gamestate 160 if (this->currentGamestate_ != nullptr) 161 { 162 delete this->currentGamestate_; 163 } 164 141 165 uint8_t gsMode; 142 if( GameMode::isMaster() ) 166 if(GameMode::isMaster()) 167 { 143 168 gsMode = packet::GAMESTATE_MODE_SERVER; 144 else 169 } 170 else 171 { 145 172 gsMode = packet::GAMESTATE_MODE_CLIENT; 173 } 174 146 175 uint32_t newID; 147 if( GameMode::isMaster() ) 148 newID = ++id_; 149 else 150 { 151 assert(peerMap_.size()!=0); 152 newID = peerMap_[NETWORK_PEER_ID_SERVER].lastReceivedGamestateID; 153 if( newID == GAMESTATEID_INITIAL ) 176 if(GameMode::isMaster()) 177 { 178 newID = ++this->id_; 179 } 180 else 181 { 182 assert(this->peerMap_.size() != 0); 183 newID = this->peerMap_[NETWORK_PEER_ID_SERVER].lastReceivedGamestateID; 184 if(newID == GAMESTATEID_INITIAL) 154 185 { 155 186 return false; … … 157 188 } 158 189 159 currentGamestate_ = new packet::Gamestate(); 160 161 if(!currentGamestate_->collectData(newID, gsMode)) 162 { //we have no data to send 163 delete currentGamestate_; 164 currentGamestate_=nullptr; 190 // Create a new gamestate 191 this->currentGamestate_ = new packet::Gamestate(); 192 if(!this->currentGamestate_->collectData(newID, gsMode)) 193 { 194 // we have no data to send 195 delete this->currentGamestate_; 196 this->currentGamestate_ = nullptr; 165 197 return false; 166 198 } 199 167 200 return true; 168 201 } 169 202 203 /** 204 * Return a vector with the gamestates of all peers. 205 */ 170 206 std::vector<packet::Gamestate*> GamestateManager::getGamestates() 171 207 { 172 if(!currentGamestate_) 208 // Current gamestate is empty 209 if(!this->currentGamestate_){ 173 210 return std::vector<packet::Gamestate*>(); 211 } 212 174 213 std::vector<packet::Gamestate*> peerGamestates; 175 176 for( const auto& mapEntry : peerMap_)177 { 178 if( !mapEntry.second.isSynched)214 // TODO: mapEntry is an incredibly bad name 215 for(const auto& mapEntry : this->peerMap_) 216 { 217 if(!mapEntry.second.isSynched) 179 218 { 180 219 orxout(verbose_more, context::network) << "Server: not sending gamestate" << endl; … … 198 237 peerGamestates.push_back(nullptr); // insert an empty gamestate* to be changed 199 238 finishGamestate( peerID, peerGamestates.back(), baseGamestate, currentGamestate_ ); 200 if( peerGamestates.back()==nullptr ) 239 if(peerGamestates.back() == nullptr) 240 { 201 241 // nothing to send to remove pointer from vector 202 242 peerGamestates.pop_back(); 203 //FunctorMember<GamestateManager>* functor = 204 // ExecutorMember<GamestateManager>* executor = createExecutor( createFunctor(&GamestateManager::finishGamestate, this) ); 205 // executor->setDefaultValues( cid, &clientGamestates.back(), client, currentGamestate_ ); 206 // (*static_cast<Executor*>(executor))(); 207 // this->threadPool_->passFunction( executor, true ); 208 // (*functor)( cid, &(clientGamestates.back()), client, currentGamestate_ ); 209 } 210 211 // threadPool_->synchronise(); 243 } 244 } 212 245 213 246 return peerGamestates; … … 215 248 216 249 217 void GamestateManager::finishGamestate( unsigned int peerID, packet::Gamestate*& destgamestate, packet::Gamestate* base, packet::Gamestate* gamestate ) { 218 //why are we searching the same client's gamestate id as we searched in 219 //Server::sendGameState? 250 void GamestateManager::finishGamestate(unsigned int peerID, packet::Gamestate*& destgamestate, packet::Gamestate* base, packet::Gamestate* gamestate) 251 { 220 252 // save the (undiffed) gamestate in the clients gamestate map 221 //chose wheather the next gamestate is the first or not 222 223 // packet::Gamestate *gs = gamestate->doSelection(clientID, 20000); 224 // packet::Gamestate* gs = new packet::Gamestate(*gamestate); 225 // packet::Gamestate* gs = gamestate; 253 // choose whether the next gamestate is the first or not 254 255 // Create a copy of the gamestate 256 // TODO: Does this actually create a copy of the gamestate? 226 257 packet::Gamestate *gs = new packet::Gamestate(*gamestate); //this is neccessary because the gamestate are being kept (to diff them later on) for each client seperately 227 // packet::Gamestate *gs = new packet::Gamestate(); 228 // gs->collectData( id_, 0x1 ); 229 // this->threadMutex_->lock(); 230 peerMap_[peerID].gamestates[gamestate->getID()]=gs; 231 // this->threadMutex_->unlock(); 258 this->peerMap_[peerID].gamestates[gamestate->getID()] = gs; 259 260 // Start the clock 232 261 Clock clock; 233 262 clock.capture(); … … 236 265 { 237 266 packet::Gamestate *diffed1 = gs->diffVariables(base); 238 if( diffed1->getDataSize() == 0)267 if(diffed1->getDataSize() == 0) 239 268 { 240 269 delete diffed1; … … 249 278 } 250 279 251 252 // OrxVerify(gs->compressData(), ""); 280 // Stop the clock 253 281 clock.capture(); 254 282 orxout(verbose_more, context::network) << "diff and compress time: " << clock.getDeltaTime() << endl; 255 // orxout(verbose_more, context::network) << "sending gamestate with id " << gs->getID(); 256 // if(gamestate->isDiffed()) 257 // orxout(verbose_more, context::network) << " and baseid " << gs->getBaseID() << endl; 258 // else 259 // orxout(verbose_more, context::network) << endl; 283 284 260 285 gs->setPeerID(peerID); 261 286 destgamestate = gs; … … 263 288 264 289 290 /** 291 * Acknowledge a received gamestate. 292 * @param gamestateID The ID of the gamestate to be acknowledged 293 * @param peerID The ID of the peer to send the Acknowledgement to 294 */ 265 295 bool GamestateManager::ackGamestate(unsigned int gamestateID, unsigned int peerID) 266 296 { 267 // ClientInformation *temp = ClientInformation::findClient(peerID); 268 // assert(temp); 297 // Search for the peer in the peer map 269 298 std::map<uint32_t, peerInfo>::iterator it = this->peerMap_.find(peerID); 270 assert(it!=this->peerMap_.end()); 299 assert(it != this->peerMap_.end()); 300 271 301 unsigned int curid = it->second.lastAckedGamestateID; 272 302 273 303 assert(gamestateID != ACKID_NACK); 274 // if(gamestateID == ACKID_NACK){ 275 // it->second.lastAckedGamestateID = GAMESTATEID_INITIAL; 276 // // temp->setGamestateID(GAMESTATEID_INITIAL); 277 // // now delete all saved gamestates for this client 278 // std::map<uint32_t, packet::Gamestate*>::iterator it2; 279 // for(it2 = it->second.gamestates.begin(); it2!=it->second.gamestates.end(); ++it2 ){ 280 // delete it2->second; 281 // } 282 // it->second.gamestates.clear(); 283 // return true; 284 // } 285 286 // assert(curid==GAMESTATEID_INITIAL || curid<=gamestateID); // this line is commented out because acknowledgements are unreliable and may arrive in distorted order 287 if( gamestateID <= curid && curid != GAMESTATEID_INITIAL ) 304 305 // The gamestate has already been acknowledged, nothing to do 306 if(gamestateID <= curid && curid != GAMESTATEID_INITIAL) 307 { 288 308 return true; 289 orxout(verbose, context::network) << "acking gamestate " << gamestateID << " for peerID: " << peerID << " curid: " << curid << endl; 309 } 310 311 orxout(verbose, context::network) << "acking gamestate " << gamestateID << " for peerID: " << peerID << " curid: " << curid << endl; 290 312 std::map<uint32_t, packet::Gamestate*>::iterator it2; 291 for ( it2=it->second.gamestates.begin(); it2!=it->second.gamestates.end();)292 { 293 if( it2->second->getID() < gamestateID)313 for (it2 = it->second.gamestates.begin(); it2 != it->second.gamestates.end();) 314 { 315 if(it2->second->getID() < gamestateID) 294 316 { 295 317 delete it2->second; … … 297 319 } 298 320 else 321 { 299 322 ++it2; 323 } 300 324 } 301 325 302 // std::map<unsigned int, packet::Gamestate*>::iterator it; 303 // for(it = gamestateMap_[peerID].begin(); it!=gamestateMap_[peerID].end() && it->first<gamestateID; ){ 304 // delete it->second; 305 // gamestateMap_[peerID].erase(it++); 306 // } 326 // update the last acked gamestate 307 327 it->second.lastAckedGamestateID = gamestateID; 308 // temp->setGamestateID(gamestateID); 309 // TrafficControl::processAck(peerID, gamestateID); 328 310 329 return true; 311 330 } 312 331 332 /** 333 * Return the ID of the last received gamestate for a certain peer 334 * @param peerID The ID of the peer\ 335 */ 313 336 uint32_t GamestateManager::getLastReceivedGamestateID(unsigned int peerID) 314 337 { 315 assert( this->peerMap_.find(peerID)!=this->peerMap_.end() );316 if( this->peerMap_.find(peerID) != this->peerMap_.end() )338 if(this->peerMap_.find(peerID) != this->peerMap_.end()) 339 { 317 340 return this->peerMap_[peerID].lastReceivedGamestateID; 318 else 341 } 342 else 343 { 319 344 return GAMESTATEID_INITIAL; 345 } 320 346 } 321 347 322 348 /** 349 * Add a peer to the game. 350 * @param peerID The ID of the peer to add. 351 */ 323 352 void GamestateManager::addPeer(uint32_t peerID) 324 353 { 325 assert(peerMap_.find(peerID)==peerMap_.end()); 326 peerMap_[peerID].peerID = peerID; 327 peerMap_[peerID].lastReceivedGamestateID = GAMESTATEID_INITIAL; 328 peerMap_[peerID].lastAckedGamestateID = GAMESTATEID_INITIAL; 329 if( GameMode::isMaster() ) 330 peerMap_[peerID].isSynched = false; 331 else 332 peerMap_[peerID].isSynched = true; 333 } 334 354 // Ensure that the peer doesn't already exist. 355 assert(this->peerMap_.find(peerID) == this->peerMap_.end()); 356 357 // Create the peerInfo struct for the peer 358 this->peerMap_[peerID].peerID = peerID; 359 this->peerMap_[peerID].lastReceivedGamestateID = GAMESTATEID_INITIAL; 360 this->peerMap_[peerID].lastAckedGamestateID = GAMESTATEID_INITIAL; 361 if(GameMode::isMaster()) 362 { 363 this->peerMap_[peerID].isSynched = false; 364 } 365 else 366 { 367 this->peerMap_[peerID].isSynched = true; 368 } 369 } 370 371 /** 372 * Remove a peer from the game. 373 * @param peerID The ID of the peer to be removed 374 */ 335 375 void GamestateManager::removePeer(uint32_t peerID) 336 376 { 337 assert(peerMap_.find(peerID)!=peerMap_.end()); 338 for( const auto& mapEntry : peerMap_[peerID].gamestates ) 377 // Make sure that the peer exists 378 assert(this->peerMap_.find(peerID) != this->peerMap_.end()); 379 380 for (const auto& mapEntry : this->peerMap_[peerID].gamestates) 339 381 { 340 382 delete mapEntry.second; 341 383 } 342 peerMap_.erase(peerMap_.find(peerID)); 343 } 344 345 346 // void GamestateManager::removeClient(ClientInformation* client){ 347 // assert(client); 348 // std::map<unsigned int, std::map<unsigned int, packet::Gamestate*>>::iterator clientMap = gamestateMap_.find(client->getID()); 349 // // first delete all remained gamestates 350 // std::map<unsigned int, packet::Gamestate*>::iterator it; 351 // for(it=clientMap->second.begin(); it!=clientMap->second.end(); it++) 352 // delete it->second; 353 // // now delete the clients gamestatemap 354 // gamestateMap_.erase(clientMap); 355 // } 356 384 this->peerMap_.erase(this->peerMap_.find(peerID)); 385 } 386 387 /** 388 * Process an incoming Gamestate packet. 389 * @param gs Pointer to the incoming Gamestate packet 390 */ 357 391 bool GamestateManager::processGamestate(packet::Gamestate *gs) 358 392 { 393 // Decompress if necessary 359 394 if(gs->isCompressed()) 360 395 { … … 362 397 } 363 398 assert(!gs->isDiffed()); 399 364 400 uint8_t gsMode; 365 if( GameMode::isMaster() ) 401 if(GameMode::isMaster()) 402 { 366 403 gsMode = packet::GAMESTATE_MODE_SERVER; 367 else 404 } 405 else 406 { 368 407 gsMode = packet::GAMESTATE_MODE_CLIENT; 369 if( gs->spreadData(gsMode) ) 408 } 409 410 if(gs->spreadData(gsMode)) 370 411 { 371 412 this->peerMap_[gs->getPeerID()].lastReceivedGamestateID = gs->getID(); … … 373 414 } 374 415 else 416 { 375 417 return false; 418 } 376 419 } 377 420 -
code/branches/Masterserver_FS18/src/libraries/network/GamestateManager.h
r11071 r11829 62 62 * b: new Gamestate 63 63 * x: diffed and compressed gamestate 64 * x =(a^b)65 * b =(a^x)66 * diff(a, diff(a,x))=x (hope this is correct)64 * x = (a ^ b) 65 * b = (a ^ x) 66 * diff(a, diff(a, x)) = x (hope this is correct) 67 67 * @author Oliver Scheuss 68 68 */ … … 75 75 uint32_t lastAckedGamestateID; //!< id of the last gamestate on which we received an ack from the peer 76 76 bool isSynched; 77 std::map< uint32_t, packet::Gamestate*> gamestates;77 std::map<uint32_t, packet::Gamestate*> gamestates; 78 78 }; 79 79 … … 86 86 virtual bool ackGamestate(unsigned int gamestateID, unsigned int peerID) override; 87 87 virtual uint32_t getLastReceivedGamestateID( unsigned int peerID ) override; 88 virtual uint32_t getCurrentGamestateID() override { if( currentGamestate_) return currentGamestate_->getID(); else return GAMESTATEID_INITIAL; }88 virtual uint32_t getCurrentGamestateID() override { if(currentGamestate) return currentGamestate_->getID(); else return GAMESTATEID_INITIAL; } 89 89 90 90 bool processGamestates(); … … 98 98 void addPeer( uint32_t peerID ); 99 99 void setSynched( uint32_t peerID ) 100 { assert(peerMap_.find(peerID) !=peerMap_.end()); peerMap_[peerID].isSynched = true; }100 { assert(peerMap_.find(peerID) != peerMap_.end()); peerMap_[peerID].isSynched = true; } 101 101 void removePeer( uint32_t peerID ); 102 bool hasPeers(){ return this->peerMap_.size() !=0; }103 // void removeClient(ClientInformation *client); 102 bool hasPeers(){ return this->peerMap_.size() != 0; } 103 104 104 protected: 105 105 virtual bool sendPacket( packet::Packet* packet ) = 0; 106 106 107 private: 107 108 bool processGamestate(packet::Gamestate *gs); 108 109 109 // std::map<unsigned int, std::map<unsigned int, packet::Gamestate*>> gamestateMap_; 110 // TODO: What is the purpose of the gamestateQueue? 110 111 std::map<unsigned int, packet::Gamestate*> gamestateQueue; 111 // std::map<unsigned int, uint32_t> lastProcessedGamestateID_; 112 113 // Each peerID maps to a peerInfo struct 112 114 std::map<uint32_t, peerInfo> peerMap_; 115 116 // always contains the latest gamestate 113 117 packet::Gamestate* currentGamestate_; 114 // TrafficControl *trafficControl_;115 118 unsigned int id_; 116 // boost::mutex* threadMutex_; 117 ThreadPool* /*thread*/Pool_; 119 ThreadPool* Pool_; 118 120 }; 119 121 -
code/branches/Masterserver_FS18/src/libraries/network/Host.cc
r11071 r11829 44 44 SetConsoleCommand(__CC_printRTT_group, __CC_printRTT_name, &Host::printRTT); 45 45 46 // Host* Host::instance_=nullptr; 47 uint32_t Host::clientID_s=0; 48 // uint32_t Host::shipID_s=-1; 46 uint32_t Host::clientID_s = 0; 49 47 std::vector<Host*> Host::instances_s; 50 48 … … 54 52 Host::Host() 55 53 { 56 // assert(instance_==nullptr); 57 instances_s.push_back(this); 54 Host::instances_s.push_back(this); 55 56 // TODO: What does this do? 58 57 ModifyConsoleCommand(__CC_printRTT_group, __CC_printRTT_name).setObject(this); 58 59 59 this->bIsActive_ = false; 60 60 } … … 66 66 Host::~Host() 67 67 { 68 assert( std::find( instances_s.begin(), instances_s.end(), this )!=instances_s.end());69 instances_s.erase(std::find( instances_s.begin(), instances_s.end(), this ));68 assert(std::find( instances_s.begin(), instances_s.end(), this ) != instances_s.end()); 69 Host::instances_s.erase(std::find( instances_s.begin(), instances_s.end(), this )); 70 70 ModifyConsoleCommand(__CC_printRTT_group, __CC_printRTT_name).setObject(nullptr); 71 71 } … … 80 80 void Host::addPacket(ENetPacket *packet, int clientID, uint8_t channelID) 81 81 { 82 for (Host* host : instances_s)82 for (Host* host : instances_s) 83 83 { 84 if ( host->isActive())84 if (host->isActive()) 85 85 { 86 86 host->queuePacket(packet, clientID, channelID); … … 97 97 void Host::sendChat(const std::string& message, unsigned int sourceID, unsigned int targetID) 98 98 { 99 for(Host* host : instances_s) 100 if( host->isActive() ) 99 for (Host* host : instances_s) 100 { 101 if (host->isActive()) 102 { 101 103 host->doSendChat(message, sourceID, targetID); 104 } 105 } 102 106 } 103 107 … … 108 112 { 109 113 for (NetworkChatListener* listener : ObjectList<NetworkChatListener>()) 114 { 110 115 listener->incomingChat(message, sourceID); 116 } 111 117 } 112 118 … … 114 120 bool Host::isServer() 115 121 { 116 for (Host* host : instances_s)122 for (Host* host : Host::instances_s) 117 123 { 118 if ( host->isActive())124 if (host->isActive()) 119 125 { 120 if( host->isServer_() ) 126 if (host->isServer_()) 127 { 121 128 return true; 129 } 122 130 } 123 131 } … … 125 133 } 126 134 135 /** 136 * Singleton implementation. Return the first active instance. 137 */ 127 138 Host* Host::getActiveInstance() 128 139 { 129 140 std::vector<Host*>::iterator it = Host::instances_s.begin(); 130 while ( it != Host::instances_s.end())141 while (it != Host::instances_s.end()) 131 142 { 132 if( (*it)->isActive() ) 143 if ((*it)->isActive()) 144 { 133 145 return *it; 146 } 134 147 else 148 { 135 149 ++it; 150 } 136 151 } 137 152 return nullptr; -
code/branches/Masterserver_FS18/src/libraries/network/Host.h
r8858 r11829 40 40 const unsigned int CLIENTID_SERVER = 0; 41 41 const unsigned int NETWORK_FREQUENCY = 25; 42 const float NETWORK_PERIOD = 1.0f /NETWORK_FREQUENCY;42 const float NETWORK_PERIOD = 1.0f / NETWORK_FREQUENCY; 43 43 44 44 /** … … 55 55 56 56 private: 57 virtual void queuePacket(ENetPacket *packet, int clientID, uint8_t channelID) =0;57 virtual void queuePacket(ENetPacket *packet, int clientID, uint8_t channelID) = 0; 58 58 virtual bool isServer_()=0; 59 59 … … 63 63 void setActive( bool bActive ){ bIsActive_ = bActive; } 64 64 65 virtual void doSendChat(const std::string& message, unsigned int sourceID, unsigned int targetID) =0;66 virtual void doReceiveChat(const std::string& message, unsigned int sourceID, unsigned int targetID) =0;65 virtual void doSendChat(const std::string& message, unsigned int sourceID, unsigned int targetID) = 0; 66 virtual void doReceiveChat(const std::string& message, unsigned int sourceID, unsigned int targetID) = 0; 67 67 68 68 public: … … 74 74 static bool isServer(); 75 75 static void sendChat(const std::string& message, unsigned int sourceID, unsigned int targetID); 76 virtual void printRTT() =0;76 virtual void printRTT() = 0; 77 77 bool isActive(){ return bIsActive_; } 78 78 79 private: 79 80 static uint32_t clientID_s; -
code/branches/Masterserver_FS18/src/libraries/network/Server.cc
r11103 r11829 55 55 #include "packet/Gamestate.h" 56 56 #include "packet/Welcome.h" 57 // #include "ClientInformation.h"58 57 #include "FunctionCallManager.h" 59 58 #include "GamestateManager.h" … … 69 68 Server::Server() 70 69 { 71 this->timeSinceLastUpdate_=0; 72 } 73 70 this->timeSinceLastUpdate_ = 0; 71 } 72 73 /** 74 * Constructor 75 * @param port Port to listen on 76 */ 74 77 Server::Server(int port) 75 78 { 76 this->setPort( port ); 77 this->timeSinceLastUpdate_=0; 78 } 79 /* 80 Server::Server(int port, const std::string name) 81 { 82 this->setPort( port ); 83 this->timeSinceLastUpdate_=0; 84 this->serverName_=name; 85 }*/ 79 this->setPort(port); 80 this->timeSinceLastUpdate_ = 0; 81 } 82 86 83 /** 87 84 * Constructor … … 91 88 Server::Server(int port, const std::string& bindAddress) 92 89 { 93 this->setPort( port);94 this->setBindAddress( bindAddress);95 this->timeSinceLastUpdate_ =0;90 this->setPort(port); 91 this->setBindAddress(bindAddress); 92 this->timeSinceLastUpdate_ = 0; 96 93 } 97 94 … … 156 153 LANDiscoverable::update(); 157 154 158 if ( GamestateManager::hasPeers())155 if (GamestateManager::hasPeers()) 159 156 { 160 157 // process incoming gamestates … … 163 160 164 161 // send function calls to clients 165 FunctionCallManager::sendCalls( static_cast<Host*>(this));166 167 // this steers our network frequency168 timeSinceLastUpdate_ +=time.getDeltaTime();169 if(timeSinceLastUpdate_ >=NETWORK_PERIOD)162 FunctionCallManager::sendCalls(static_cast<Host*>(this)); 163 164 // this steers our network frequency 165 timeSinceLastUpdate_ += time.getDeltaTime(); 166 if(timeSinceLastUpdate_ >= NETWORK_PERIOD) 170 167 { 171 timeSinceLastUpdate_ -= static_cast<unsigned int>( timeSinceLastUpdate_ / NETWORK_PERIOD) * NETWORK_PERIOD;168 timeSinceLastUpdate_ -= static_cast<unsigned int>(timeSinceLastUpdate_ / NETWORK_PERIOD) * NETWORK_PERIOD; 172 169 updateGamestate(); 173 170 } 174 // sendPackets(); // flush the enet queue175 171 } 176 172 } … … 182 178 183 179 /** 184 * @brief: returns ping time to client in milliseconds180 * Return ping time to client in milliseconds. 185 181 */ 186 182 unsigned int Server::getRTT(unsigned int clientID) 187 183 { 188 // assert(ClientInformation::findClient(clientID)); 189 // return ClientInformation::findClient(clientID)->getRTT(); 190 // TODO: reimplement 184 // TODO: Implement 191 185 return 0; 192 186 } 193 187 188 /** 189 * Print ping time to client in milliseconds. 190 */ 194 191 void Server::printRTT() 195 192 { 196 // for( ClientInformation* temp=ClientInformation::getBegin(); temp!=nullptr; temp=temp->next() ) 197 // orxout(message) << "Round trip time to client with ID: " << temp->getID() << " is " << temp->getRTT() << " ms" << endl; 198 } 199 200 /** 201 * @brief: return packet loss ratio to client (scales from 0 to 1) 193 // TODO: Implement 194 } 195 196 /** 197 * Return packet loss ratio to client (scales from 0 to 1). 202 198 */ 203 199 float Server::getPacketLoss(unsigned int clientID) 204 200 { 205 // assert(ClientInformation::findClient(clientID)); 206 // return ClientInformation::findClient(clientID)->getPacketLoss(); 201 // TODO: Implement 207 202 return 0.; 208 203 } 209 204 210 205 /** 211 * takes a new snapshot of the gamestate and sends it to the clients212 */206 * Take a new snapshot of the gamestate and send it to the clients. 207 */ 213 208 void Server::updateGamestate() 214 209 { 215 if( this->clientIDs_.size()==0 ) 216 //no client connected 210 if(this->clientIDs_.size() == 0) 211 { 212 // no client connected 217 213 return; 214 } 218 215 GamestateManager::update(); 219 // orxout(verbose_more, context::network) << "Server: one gamestate update complete, goig to sendGameState" << endl;220 //orxout(verbose_more, context::network) << "updated gamestate, sending it" << endl;221 //if(clients->getGamestateID()!=GAMESTATEID_INITIAL)222 216 sendGameStates(); 223 217 sendObjectDeletes(); 224 // orxout(verbose_more, context::network) << "Server: one sendGameState turn complete, repeat in next tick" << endl; 225 //orxout(verbose_more, context::network) << "sent gamestate" << endl; 226 } 227 228 /** 229 * sends the current gamestate to all peers 230 */ 218 } 219 220 /** 221 * Send the current gamestate to all peers. 222 */ 231 223 bool Server::sendGameStates() 232 224 { 233 225 std::vector<packet::Gamestate*> gamestates = GamestateManager::getGamestates(); 234 for( packet::Gamestate* gamestate : gamestates)226 for(packet::Gamestate* gamestate : gamestates) 235 227 { 236 228 gamestate->send(static_cast<Host*>(this)); … … 240 232 241 233 234 /** 235 * Send 'DeleteObjects' packet 236 */ 242 237 bool Server::sendObjectDeletes() 243 238 { 244 // ClientInformation *temp = ClientInformation::getBegin(); 245 // if( temp == nullptr ) 246 //no client connected 247 if( this->clientIDs_.size()==0 ) 239 // no client connected 240 if(this->clientIDs_.size() == 0) 241 { 248 242 return true; 243 } 244 249 245 packet::DeleteObjects *del = new packet::DeleteObjects(); 250 246 if(!del->fetchIDs()) … … 253 249 return true; //everything ok (no deletes this tick) 254 250 } 255 // orxout(verbose, context::network) << "sending DeleteObjects" << endl; 256 // while(temp != nullptr){ 257 // if( !(temp->getSynched()) ) 258 // { 259 // orxout(verbose_more, context::network) << "Server: not sending gamestate" << endl; 260 // temp=temp->next(); 261 // continue; 262 // } 263 // int cid = temp->getID(); //get client id 264 // packet::DeleteObjects *cd = new packet::DeleteObjects(*del); 265 // assert(cd); 251 266 252 del->setPeerID(NETWORK_PEER_ID_BROADCAST); 267 if ( !del->send( static_cast<Host*>(this) ) ) 253 if (!del->send( static_cast<Host*>(this))) 254 { 268 255 orxout(internal_warning, context::network) << "Server: could not broadcast deleteObjects packet" << endl; 269 // temp=temp->next(); 270 // gs gets automatically deleted by enet callback 271 // } 272 // delete del; 256 } 257 258 // TODO: Possible memory leak? 259 // del is allocated but only deleted if fetchIDs() returns false 260 273 261 return true; 274 262 } 275 263 276 264 /** 265 * Add a new peer to the server. 266 */ 277 267 void Server::addPeer(uint32_t peerID) 278 268 { 279 // static unsigned int newid=1;280 //281 // orxout(internal_info, context::network) << "Server: adding client" << endl;282 // ClientInformation *temp = ClientInformation::insertBack(new ClientInformation);283 // if(!temp)284 // {285 // orxout(internal_warning, context::network) << "Server: could not add client" << endl;286 // }287 // temp->setID(newid);288 // temp->setPeer(event->peer);289 290 269 // inform all the listeners 291 270 this->clientIDs_.push_back(peerID); … … 296 275 GamestateManager::addPeer(peerID); 297 276 298 // ++newid;299 300 277 orxout(internal_info, context::network) << "Server: added client id: " << peerID << endl; 301 278 302 279 createClient(peerID); 303 } 304 280 } 281 282 /** 283 * Remove a peer from the server. 284 */ 305 285 void Server::removePeer(uint32_t peerID) 306 286 { 307 287 orxout(verbose, context::network) << "removing client from list" << endl; 308 // ClientInformation *client = ClientInformation::findClient(&event->peer->address); 309 // if(!client) 310 // return; 311 // else 312 // { 313 std::vector<uint32_t>::iterator it; 314 for( it=this->clientIDs_.begin(); it!=this->clientIDs_.end(); ++it ) 315 { 316 if( *it == peerID ) 317 { 318 this->clientIDs_.erase(it); 319 break; 320 } 321 } 322 WANDiscoverable::updateClientNumber(this->clientIDs_.size()); 323 LANDiscoverable::updateClientNumber(this->clientIDs_.size()); 324 325 ClientConnectionListener::broadcastClientDisconnected(peerID); 326 GamestateManager::removePeer(peerID); 327 //ServerConnection::disconnectClient( client ); 328 //ClientConnectionListener::broadcastClientDisconnected( client->getID() ); //this is done in ClientInformation now 329 // delete client; 330 // } 331 } 332 288 289 // erase the peer from the list 290 std::vector<uint32_t>::iterator it; 291 for(it=this->clientIDs_.begin(); it!=this->clientIDs_.end(); ++it) 292 { 293 if(*it == peerID) 294 { 295 this->clientIDs_.erase(it); 296 break; 297 } 298 } 299 300 // TODO: What happens if no peer with this ID is found? 301 // Should probably at least log 302 303 WANDiscoverable::updateClientNumber(this->clientIDs_.size()); 304 LANDiscoverable::updateClientNumber(this->clientIDs_.size()); 305 306 // Send 'clientDisconnected' message 307 ClientConnectionListener::broadcastClientDisconnected(peerID); 308 309 GamestateManager::removePeer(peerID); 310 } 311 312 /** 313 * Process an incoming packet. 314 */ 333 315 void Server::processPacket(packet::Packet* packet) 334 316 { 335 if( packet->isReliable() ) 336 { 337 if( this->getLastReceivedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID() ) 317 if(packet->isReliable()) 318 { 319 if(this->getLastReceivedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID()) 320 { 338 321 packet->process(static_cast<Host*>(this)); 322 } 339 323 else 324 { 340 325 this->packetQueue_.push_back(packet); 326 } 341 327 } 342 328 else 329 { 343 330 packet->process(static_cast<Host*>(this)); 344 } 345 346 331 } 332 } 333 334 /** 335 * Create a client. 336 */ 347 337 bool Server::createClient(int clientID) 348 338 { 349 // ClientInformation *temp = ClientInformation::findClient(clientID);350 // if(!temp)351 // {352 // orxout(internal_error, context::network) << "Server. could not create client with id: " << clientID << endl;353 // return false;354 // }355 // orxout(verbose, context::network) << "Con.Man: creating client id: " << temp->getID() << endl;356 339 357 340 // synchronise class ids … … 361 344 packet::FunctionIDs *fIDs = new packet::FunctionIDs(); 362 345 fIDs->setPeerID(clientID); 363 bool b = fIDs->send( static_cast<Host*>(this));346 bool b = fIDs->send(static_cast<Host*>(this)); 364 347 assert(b); 365 366 // temp->setSynched(true); 348 // TODO: assert probably isn't the way to go here, as a packet which fails to send will crash the game... 349 367 350 GamestateManager::setSynched(clientID); 368 351 352 // Send 'Welcome' packet to client 369 353 orxout(verbose, context::network) << "sending welcome" << endl; 370 354 packet::Welcome *w = new packet::Welcome(clientID); 371 355 w->setPeerID(clientID); 372 b = w->send( static_cast<Host*>(this));356 b = w->send(static_cast<Host*>(this)); 373 357 assert(b); 358 // TODO: assert probably isn't the way to go here, as a packet which fails to send will crash the game... 359 374 360 (void)b; // avoid compiler warning 375 // packet::Gamestate *g = new packet::Gamestate();376 // g->setPeerID(clientID);377 // b = g->collectData(0,packet::GAMESTATE_MODE_SERVER);378 // assert(b);379 // if(!b)380 // return false; //no data for the client381 // // b = g->compressData();382 // // assert(b);383 // b = g->send( static_cast<Host*>(this) );384 // assert(b);385 361 return true; 386 362 } 387 363 388 void Server::disconnectClient( uint32_t clientID ) 389 { 390 ServerConnection::disconnectClient( clientID ); 391 GamestateManager::removePeer( clientID ); 392 // inform all the listeners 393 // ClientConnectionListener::broadcastClientDisconnected(client->getID()); // this is done in ClientInformation now 364 /** 365 * Disconnect a client. 366 */ 367 void Server::disconnectClient(uint32_t clientID) 368 { 369 ServerConnection::disconnectClient(clientID); 370 GamestateManager::removePeer(clientID); 394 371 } 395 372 … … 404 381 // check if the target exists. just ignore the message otherwise 405 382 if (!this->isValidTarget(targetID)) // TODO: remove this if an invalid clientIDs don't trigger assertions anymore 383 { 406 384 return; 385 } 407 386 408 387 // send the message to the target 409 388 packet::Chat* packet = new packet::Chat(message, sourceID, targetID); 410 389 packet->setPeerID(targetID); 411 packet->send( static_cast<Host*>(this));390 packet->send(static_cast<Host*>(this)); 412 391 413 392 // if the target is (or includes) this host as well, call the parent function which passes the message to the listeners 414 393 if (targetID == NETWORK_PEER_ID_BROADCAST || targetID == Host::getPlayerID()) 394 { 415 395 Host::doReceiveChat(message, sourceID, targetID); 396 } 416 397 } 417 398 … … 431 412 bool Server::isValidTarget(unsigned int targetID) 432 413 { 414 // Broadcast or server ID are okay 433 415 if (targetID == NETWORK_PEER_ID_BROADCAST || targetID == NETWORK_PEER_ID_SERVER) 416 { 434 417 return true; 435 436 for( uint32_t id : this->clientIDs_ ) 437 if( id == targetID ) 418 } 419 420 // IDs in the client list are okay also 421 for(uint32_t id : this->clientIDs_) 422 { 423 if(id == targetID) 424 { 438 425 return true; 426 } 427 } 439 428 440 429 return false; … … 443 432 void Server::syncClassid(unsigned int clientID) 444 433 { 445 int failures=0;446 434 packet::ClassID *classid = new packet::ClassID(); 447 435 classid->setPeerID(clientID); 448 while(!classid->send( static_cast<Host*>(this) ) && failures < 10){ 436 // TODO: Better to do this with a for loop 437 int failures = 0; 438 while(!classid->send(static_cast<Host*>(this)) && failures < 10)\ 439 { 449 440 failures++; 450 441 } 451 442 assert(failures<10); 443 // TODO: assert probably isn't the way to go here, as 10 failed packets will crash the game... 452 444 orxout(verbose, context::network) << "syncClassid:\tall synchClassID packets have been sent" << endl; 453 445 } -
code/branches/Masterserver_FS18/src/libraries/network/ServerConnection.cc
r11071 r11829 36 36 #include "util/Output.h" 37 37 #include <util/Sleep.h> 38 // #include "ClientInformation.h"39 38 40 39 namespace orxonox 41 40 { 42 41 42 // TODO: Calls to the Connection superclass are done as follows: 43 // Connection::startCommunicationThread() 44 // However, these methods are not overridden. 45 // Why can't we just do this->startCommunicationThread()? 46 47 /** 48 * Constructor 49 */ 43 50 ServerConnection::ServerConnection(): 44 51 bListening_(false) 45 52 { 46 53 this->bindAddress_ = new ENetAddress(); 47 // memset(this->bindAddress_, 0, sizeof(ENetAddress));48 54 this->bindAddress_->host = ENET_HOST_ANY; 49 55 this->bindAddress_->port = NETWORK_PORT; … … 51 57 } 52 58 59 /** 60 * Destructor 61 */ 53 62 ServerConnection::~ServerConnection() 54 63 { 55 if ( this->bListening_ ) 56 closeListener(); 64 if (this->bListening_) 65 { 66 this->closeListener(); 67 } 57 68 delete this->bindAddress_; 58 69 } 59 70 60 void ServerConnection::setBindAddress( const std::string& bindAddress ) 71 /** 72 * Set address on which to listen. 73 * @param bindAddress The address on which to listen 74 */ 75 void ServerConnection::setBindAddress(const std::string& bindAddress) 61 76 { 62 if (enet_address_set_host (this->bindAddress_, bindAddress.c_str()) < 0) 63 orxout(internal_error, context::network) << "Could not resolve \"" << bindAddress << "\"." << endl; 77 if (enet_address_set_host(this->bindAddress_, bindAddress.c_str()) < 0) 78 { 79 orxout(internal_error, context::network) << "Could not resolve \"" << bindAddress << "\"." << endl; 80 } 64 81 } 65 82 66 void ServerConnection::setPort( unsigned int port ) { 83 /** 84 * Set port on which to listen on. 85 * @param port The port on which to listen on. 86 */ 87 void ServerConnection::setPort(unsigned int port) { 67 88 this->bindAddress_->port = port; 68 89 } … … 73 94 this->host_ = enet_host_create(this->bindAddress_, NETWORK_MAX_CONNECTIONS, NETWORK_CHANNEL_COUNT, 0, 0); 74 95 75 if ( this->host_ == nullptr)96 if (this->host_ == nullptr) 76 97 { 77 98 orxout(internal_error, context::network) << "ServerConnection: host_ == nullptr" << endl; … … 81 102 // enable compression 82 103 this->enableCompression(); 104 105 // ensure that either IPv4 or IPv6 succeeded 83 106 assert( this->host_->socket4 != ENET_SOCKET_NULL || this->host_->socket6 != ENET_SOCKET_NULL ); 107 84 108 if (this->host_->socket4 == ENET_SOCKET_NULL) 109 { 85 110 orxout(internal_warning, context::network) << "IPv4 Socket failed." << endl; 111 } 86 112 else if (this->host_->socket6 == ENET_SOCKET_NULL) 113 { 87 114 orxout(internal_warning, context::network) << "IPv6 Socket failed." << endl; 115 } 88 116 else 117 { 89 118 orxout(internal_info, context::network) << "Using IPv4 and IPv6 Sockets." << endl; 119 } 90 120 91 121 // start communication thread … … 95 125 } 96 126 127 /** 128 * Stop listening. 129 */ 97 130 bool ServerConnection::closeListener() 98 131 { 99 this->bListening_ =false;132 this->bListening_ = false; 100 133 disconnectClients(); 101 134 Connection::stopCommunicationThread(); … … 104 137 } 105 138 139 /** 140 * Add outgoing packet to queue. 141 * @param packet The packet to send 142 * @param clientID The ID of the recipient 143 * @param channelID The channel ID 144 * TODO: Not sure yet how this actually works 145 */ 106 146 void ServerConnection::addPacket(ENetPacket *packet, unsigned int clientID, uint8_t channelID) 107 147 { 108 if ( clientID == NETWORK_PEER_ID_BROADCAST)148 if (clientID == NETWORK_PEER_ID_BROADCAST) 109 149 { 110 150 broadcastPacket(packet, channelID); … … 112 152 else 113 153 { 114 // ClientInformation *temp = ClientInformation::findClient(clientID);115 // if(!temp){116 // orxout(internal_warning, context::network) << "C.Man: addPacket findClient failed" << endl;117 // }118 154 Connection::addPacket(packet, clientID, channelID); 119 155 } 120 156 } 121 157 122 // void ServerConnection::disconnectClient(ClientInformation *client) 123 // { 124 // Connection::disconnectPeer( client->getPeer() ); 125 // } 126 158 /** 159 * Terminate connection with a peer. 160 * @param clientID The peer with which to terminate the connection. 161 */ 127 162 void ServerConnection::disconnectClient(int clientID) 128 163 { 129 // ClientInformation *client = ClientInformation::findClient(clientID);130 // if(client)131 164 Connection::disconnectPeer(clientID); 132 165 } 133 166 167 /** 168 * Disconnect all peers. 169 */ 134 170 void ServerConnection::disconnectClients() 135 171 { … … 138 174 return; 139 175 } 140 141 142 // int ServerConnection::getClientID(ENetPeer* peer)143 // {144 // return getClientID(&(peer->address));145 // }146 147 // int ServerConnection::getClientID(ENetAddress* address)148 // {149 // return ClientInformation::findClient(address)->getID();150 // }151 //152 // ENetPeer *ServerConnection::getClientPeer(int clientID)153 // {154 // return ClientInformation::findClient(clientID)->getPeer();155 // }156 157 158 176 } -
code/branches/Masterserver_FS18/src/libraries/network/ServerConnection.h
r8327 r11829 47 47 { 48 48 49 class _NetworkExport ServerConnection : public Connection{ 49 class _NetworkExport ServerConnection : public Connection 50 { 50 51 public: 51 52 ~ServerConnection(); … … 57 58 bool closeListener(); 58 59 void addPacket(ENetPacket *packet, unsigned int ID, uint8_t channelID); 59 // virtual void disconnectClient(ClientInformation *client);60 60 void disconnectClient(int clientID); 61 61 62 protected: 62 63 ServerConnection(); … … 64 65 65 66 private: 66 // int getClientID(ENetPeer* peer);67 // int getClientID(ENetAddress* address);68 // ENetPeer* getClientPeer(int clientID);69 70 67 ENetAddress* bindAddress_; 71 72 68 bool bListening_; 73 69
Note: See TracChangeset
for help on using the changeset viewer.