- Timestamp:
- Mar 22, 2018, 4:12:23 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note: See TracChangeset
for help on using the changeset viewer.