Changeset 12027 for code/branches/mergeFS18/src/libraries/network/Server.cc
- Timestamp:
- Oct 10, 2018, 3:06:55 PM (6 years ago)
- Location:
- code/branches/mergeFS18
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/mergeFS18
- Property svn:mergeinfo changed
/code/branches/Masterserver_FS18 (added) merged: 11816,11829,11842,11856,11858,11880,11889,11905,11907,11910,11929-11930,11937,11962-11963,11973,11983,11990
- Property svn:mergeinfo changed
-
code/branches/mergeFS18/src/libraries/network/Server.cc
r11103 r12027 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;172 updateGamestate();168 timeSinceLastUpdate_ -= static_cast<unsigned int>(timeSinceLastUpdate_ / NETWORK_PERIOD) * NETWORK_PERIOD; 169 this->updateGamestate(); 173 170 } 174 // sendPackets(); // flush the enet queue175 171 } 176 172 } … … 182 178 183 179 /** 184 * @brief: returns ping time to client in milliseconds 185 */ 186 unsigned int Server::getRTT(unsigned int clientID) 187 { 188 // assert(ClientInformation::findClient(clientID)); 189 // return ClientInformation::findClient(clientID)->getRTT(); 190 // TODO: reimplement 191 return 0; 192 } 193 180 * Print ping time to client in milliseconds. 181 */ 194 182 void Server::printRTT() 195 183 { 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) 202 */ 203 float Server::getPacketLoss(unsigned int clientID) 204 { 205 // assert(ClientInformation::findClient(clientID)); 206 // return ClientInformation::findClient(clientID)->getPacketLoss(); 207 return 0.; 208 } 209 210 /** 211 * takes a new snapshot of the gamestate and sends it to the clients 212 */ 184 // TODO: Implement 185 } 186 187 /** 188 * Take a new snapshot of the gamestate and send it to the clients. 189 */ 213 190 void Server::updateGamestate() 214 191 { 215 if( this->clientIDs_.size()==0 ) 216 //no client connected 192 if(this->clientIDs_.size() == 0) 193 { 194 // no client connected 217 195 return; 196 } 218 197 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 sendGameStates(); 223 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 */ 198 this->sendGameStates(); 199 this->sendObjectDeletes(); 200 } 201 202 /** 203 * Send the current gamestate to all peers. 204 */ 231 205 bool Server::sendGameStates() 232 206 { 233 207 std::vector<packet::Gamestate*> gamestates = GamestateManager::getGamestates(); 234 for( packet::Gamestate* gamestate : gamestates)208 for(packet::Gamestate* gamestate : gamestates) 235 209 { 236 210 gamestate->send(static_cast<Host*>(this)); … … 240 214 241 215 216 /** 217 * Send 'DeleteObjects' packet 218 */ 242 219 bool Server::sendObjectDeletes() 243 220 { 244 // ClientInformation *temp = ClientInformation::getBegin(); 245 // if( temp == nullptr ) 246 //no client connected 247 if( this->clientIDs_.size()==0 ) 221 // no client connected 222 if(this->clientIDs_.size() == 0) 223 { 248 224 return true; 225 } 226 249 227 packet::DeleteObjects *del = new packet::DeleteObjects(); 250 228 if(!del->fetchIDs()) … … 253 231 return true; //everything ok (no deletes this tick) 254 232 } 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); 233 266 234 del->setPeerID(NETWORK_PEER_ID_BROADCAST); 267 if ( !del->send( static_cast<Host*>(this) ) ) 235 if (!del->send( static_cast<Host*>(this))) 236 { 268 237 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; 238 } 239 273 240 return true; 274 241 } 275 242 276 243 /** 244 * Add a new peer to the server. 245 */ 277 246 void Server::addPeer(uint32_t peerID) 278 247 { 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 248 // inform all the listeners 291 249 this->clientIDs_.push_back(peerID); … … 296 254 GamestateManager::addPeer(peerID); 297 255 298 // ++newid;299 300 256 orxout(internal_info, context::network) << "Server: added client id: " << peerID << endl; 301 257 302 createClient(peerID); 303 } 304 258 this->createClient(peerID); 259 } 260 261 /** 262 * Remove a peer from the server. 263 */ 305 264 void Server::removePeer(uint32_t peerID) 306 265 { 307 266 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 267 268 // erase the peer from the list 269 std::vector<uint32_t>::iterator it; 270 for(it=this->clientIDs_.begin(); it!=this->clientIDs_.end(); ++it) 271 { 272 if(*it == peerID) 273 { 274 this->clientIDs_.erase(it); 275 break; 276 } 277 } 278 279 WANDiscoverable::updateClientNumber(this->clientIDs_.size()); 280 LANDiscoverable::updateClientNumber(this->clientIDs_.size()); 281 282 // Send 'clientDisconnected' message 283 ClientConnectionListener::broadcastClientDisconnected(peerID); 284 285 GamestateManager::removePeer(peerID); 286 } 287 288 /** 289 * Process an incoming packet. 290 */ 333 291 void Server::processPacket(packet::Packet* packet) 334 292 { 335 if( packet->isReliable() ) 336 { 337 if( this->getLastReceivedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID() ) 293 if(packet->isReliable()) 294 { 295 if(this->getLastReceivedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID()) 296 { 338 297 packet->process(static_cast<Host*>(this)); 298 } 339 299 else 300 { 340 301 this->packetQueue_.push_back(packet); 302 } 341 303 } 342 304 else 305 { 343 306 packet->process(static_cast<Host*>(this)); 344 } 345 346 307 } 308 } 309 310 /** 311 * Create a client. 312 */ 347 313 bool Server::createClient(int clientID) 348 314 { 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 315 357 316 // synchronise class ids 358 syncClassid(clientID);317 this->syncClassid(clientID); 359 318 360 319 // now synchronise functionIDs 361 320 packet::FunctionIDs *fIDs = new packet::FunctionIDs(); 362 321 fIDs->setPeerID(clientID); 363 bool b = fIDs->send( static_cast<Host*>(this));322 bool b = fIDs->send(static_cast<Host*>(this)); 364 323 assert(b); 365 324 366 // temp->setSynched(true);367 325 GamestateManager::setSynched(clientID); 368 326 327 // Send 'Welcome' packet to client 369 328 orxout(verbose, context::network) << "sending welcome" << endl; 370 329 packet::Welcome *w = new packet::Welcome(clientID); 371 330 w->setPeerID(clientID); 372 b = w->send( static_cast<Host*>(this));331 b = w->send(static_cast<Host*>(this)); 373 332 assert(b); 333 374 334 (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 335 return true; 386 336 } 387 337 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 338 /** 339 * Disconnect a client. 340 */ 341 void Server::disconnectClient(uint32_t clientID) 342 { 343 ServerConnection::disconnectClient(clientID); 344 GamestateManager::removePeer(clientID); 394 345 } 395 346 … … 403 354 { 404 355 // check if the target exists. just ignore the message otherwise 405 if (!this->isValidTarget(targetID)) // TODO: remove this if an invalid clientIDs don't trigger assertions anymore 356 if (!this->isValidTarget(targetID)) 357 { 406 358 return; 359 } 407 360 408 361 // send the message to the target 409 362 packet::Chat* packet = new packet::Chat(message, sourceID, targetID); 410 363 packet->setPeerID(targetID); 411 packet->send( static_cast<Host*>(this));364 packet->send(static_cast<Host*>(this)); 412 365 413 366 // if the target is (or includes) this host as well, call the parent function which passes the message to the listeners 414 367 if (targetID == NETWORK_PEER_ID_BROADCAST || targetID == Host::getPlayerID()) 368 { 415 369 Host::doReceiveChat(message, sourceID, targetID); 370 } 416 371 } 417 372 … … 431 386 bool Server::isValidTarget(unsigned int targetID) 432 387 { 388 // Broadcast or server ID are okay 433 389 if (targetID == NETWORK_PEER_ID_BROADCAST || targetID == NETWORK_PEER_ID_SERVER) 390 { 434 391 return true; 435 436 for( uint32_t id : this->clientIDs_ ) 437 if( id == targetID ) 392 } 393 394 // IDs in the client list are okay also 395 for(uint32_t id : this->clientIDs_) 396 { 397 if(id == targetID) 398 { 438 399 return true; 400 } 401 } 439 402 440 403 return false; … … 443 406 void Server::syncClassid(unsigned int clientID) 444 407 { 445 int failures=0;446 408 packet::ClassID *classid = new packet::ClassID(); 447 409 classid->setPeerID(clientID); 448 while(!classid->send( static_cast<Host*>(this) ) && failures < 10){ 410 411 int failures = 0; 412 while(!classid->send(static_cast<Host*>(this)) && failures < 10)\ 413 { 449 414 failures++; 450 415 }
Note: See TracChangeset
for help on using the changeset viewer.