Changeset 2087 for code/trunk/src/network/Server.cc
- Timestamp:
- Nov 1, 2008, 7:04:09 PM (16 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
-
code/trunk/src/network/Server.cc
r1907 r2087 46 46 47 47 #include "ConnectionManager.h" 48 #include "ClientConnectionListener.h" 48 49 #include "GamestateManager.h" 49 50 #include "ClientInformation.h" 50 51 #include "util/Sleep.h" 51 #include "objects/SpaceShip.h"52 52 #include "core/ConsoleCommand.h" 53 53 #include "core/CoreIncludes.h" … … 58 58 #include "packet/DeleteObjects.h" 59 59 #include <util/Convert.h> 60 #include "ChatListener.h" 60 61 61 62 namespace network 62 63 { 63 const int MAX_FAILURES = 20; 64 const int NETWORK_FREQUENCY = 30; 64 const unsigned int MAX_FAILURES = 20; 65 const unsigned int NETWORK_FREQUENCY = 25; 66 const float NETWORK_PERIOD = (float)1/NETWORK_FREQUENCY; 65 67 66 68 /** … … 85 87 * @param bindAddress Address to listen on 86 88 */ 87 Server::Server(int port, std::stringbindAddress) {89 Server::Server(int port, const std::string& bindAddress) { 88 90 timeSinceLastUpdate_=0; 89 91 connection = new ConnectionManager(port, bindAddress); … … 101 103 gamestates_ = new GamestateManager(); 102 104 } 103 105 104 106 /** 105 107 * @brief Destructor … … 128 130 } 129 131 130 bool Server::processChat( std::stringmessage, unsigned int playerID){132 bool Server::processChat(const std::string& message, unsigned int playerID){ 131 133 ClientInformation *temp = ClientInformation::getBegin(); 132 134 packet::Chat *chat; … … 138 140 temp = temp->next(); 139 141 } 140 COUT(1) << "Player " << playerID << ": " << message << std::endl;142 // COUT(1) << "Player " << playerID << ": " << message << std::endl; 141 143 return true; 142 144 } … … 152 154 //this steers our network frequency 153 155 timeSinceLastUpdate_+=time; 154 if(timeSinceLastUpdate_>= (1./NETWORK_FREQUENCY)){155 timeSinceLastUpdate_ =(float)((int)(timeSinceLastUpdate_*NETWORK_FREQUENCY))/timeSinceLastUpdate_;156 if(timeSinceLastUpdate_>=NETWORK_PERIOD){ 157 timeSinceLastUpdate_ -= static_cast<unsigned int>( timeSinceLastUpdate_ / NETWORK_PERIOD ) * NETWORK_PERIOD; 156 158 gamestates_->processGamestates(); 157 159 updateGamestate(); … … 162 164 return connection->addPacket(packet, clientID); 163 165 } 164 166 167 /** 168 * @brief: returns ping time to client in milliseconds 169 */ 170 unsigned int Server::getPing(unsigned int clientID){ 171 assert(ClientInformation::findClient(clientID)); 172 return ClientInformation::findClient(clientID)->getRTT(); 173 } 174 175 /** 176 * @brief: return packet loss ratio to client (scales from 0 to 1) 177 */ 178 double Server::getPacketLoss(unsigned int clientID){ 179 assert(ClientInformation::findClient(clientID)); 180 return ClientInformation::findClient(clientID)->getPacketLoss(); 181 } 182 165 183 /** 166 184 * processes all the packets waiting in the queue … … 239 257 if(gs==NULL){ 240 258 COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl; 259 temp = temp->next(); 241 260 continue; 242 261 } … … 281 300 282 301 bool Server::addClient(ENetEvent *event){ 302 static unsigned int newid=1; 303 304 COUT(2) << "Server: adding client" << std::endl; 283 305 ClientInformation *temp = ClientInformation::insertBack(new ClientInformation); 284 306 if(!temp){ … … 286 308 return false; 287 309 } 288 if(temp==ClientInformation::getBegin()) { //not good if you use anything else than insertBack289 temp->setID(1);310 /*if(temp==ClientInformation::getBegin()) { //not good if you use anything else than insertBack 311 newid=1; 290 312 } 291 313 else 292 temp->setID(temp->prev()->getID()+1); 314 newid=temp->prev()->getID()+1;*/ 315 temp->setID(newid); 293 316 temp->setPeer(event->peer); 317 318 // inform all the listeners 319 orxonox::ObjectList<ClientConnectionListener>::iterator listener = orxonox::ObjectList<ClientConnectionListener>::begin(); 320 while(listener){ 321 listener->clientConnected(newid); 322 listener++; 323 } 324 325 newid++; 326 294 327 COUT(3) << "Server: added client id: " << temp->getID() << std::endl; 295 328 return createClient(temp->getID()); 296 329 } 297 330 298 331 bool Server::createClient(int clientID){ … … 304 337 COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl; 305 338 connection->syncClassid(temp->getID()); 306 COUT(5) << "creating spaceship for clientid: " << temp->getID() << std::endl;307 // TODO: this is only a hack, untill we have a possibility to define default player-join actions308 if(!createShip(temp))309 COUT(2) << "Con.Man. could not create ship for clientid: " << clientID << std::endl;310 else311 COUT(3) << "created spaceship" << std::endl;312 339 temp->setSynched(true); 313 340 COUT(3) << "sending welcome" << std::endl; … … 319 346 g->setClientID(temp->getID()); 320 347 b = g->collectData(0); 321 assert(b); 348 if(!b) 349 return false; //no data for the client 322 350 b = g->compressData(); 323 351 assert(b); … … 327 355 } 328 356 329 bool Server::createShip(ClientInformation *client){330 if(!client)331 return false;332 orxonox::Identifier* id = ClassByName("SpaceShip");333 if(!id){334 COUT(4) << "We could not create the SpaceShip for client: " << client->getID() << std::endl;335 return false;336 }337 orxonox::SpaceShip *no = dynamic_cast<orxonox::SpaceShip *>(id->fabricate());338 no->classID = id->getNetworkID();339 client->setShipID(no->getObjectID());340 no->setPosition(orxonox::Vector3(0,0,80));341 no->setScale(10);342 //no->setYawPitchRoll(orxonox::Degree(-90),orxonox::Degree(-90),orxonox::Degree(0));343 no->setMesh("assff.mesh");344 no->setMaxSpeed(500);345 no->setMaxSideAndBackSpeed(50);346 no->setMaxRotation(1.0);347 no->setTransAcc(200);348 no->setRotAcc(3.0);349 no->setTransDamp(75);350 no->setRotDamp(1.0);351 no->setCamera(std::string("cam_") + convertToString(client->getID()));352 no->create();353 354 return true;355 }356 357 357 bool Server::disconnectClient(ENetEvent *event){ 358 358 COUT(4) << "removing client from list" << std::endl; … … 360 360 361 361 //boost::recursive_mutex::scoped_lock lock(head_->mutex_); 362 orxonox::ObjectList<orxonox::SpaceShip>::iterator it = orxonox::ObjectList<orxonox::SpaceShip>::begin();363 362 ClientInformation *client = ClientInformation::findClient(&event->peer->address); 364 363 if(!client) 365 364 return false; 366 365 gamestates_->removeClient(client); 367 while(it){ 368 if(it->getObjectID()!=client->getShipID()){ 369 ++it; 370 continue; 371 } 372 orxonox::ObjectList<orxonox::SpaceShip>::iterator temp=it; 373 ++it; 374 delete *temp; 375 return ClientInformation::removeClient(event->peer); 376 } 377 return false; 366 367 // inform all the listeners 368 orxonox::ObjectList<ClientConnectionListener>::iterator listener = orxonox::ObjectList<ClientConnectionListener>::begin(); 369 while(listener){ 370 listener->clientDisconnected(client->getID()); 371 listener++; 372 } 373 374 return ClientInformation::removeClient(event->peer); 378 375 } 379 376 … … 387 384 gamestates_->removeClient(client); 388 385 } 389 390 bool Server::chat(std::string message){ 386 387 bool Server::chat(const std::string& message){ 388 return this->sendChat(message, Host::getPlayerID()); 389 } 390 391 bool Server::broadcast(const std::string& message){ 392 return this->sendChat(message, CLIENTID_UNKNOWN); 393 } 394 395 bool Server::sendChat(const std::string& message, unsigned int clientID){ 391 396 ClientInformation *temp = ClientInformation::getBegin(); 392 397 packet::Chat *chat; 393 398 while(temp){ 394 chat = new packet::Chat(message, Host::getPlayerID());399 chat = new packet::Chat(message, clientID); 395 400 chat->setClientID(temp->getID()); 396 401 if(!chat->send()) … … 398 403 temp = temp->next(); 399 404 } 400 COUT(1) << "Player " << Host::getPlayerID() << ": " << message << std::endl; 405 // COUT(1) << "Player " << Host::getPlayerID() << ": " << message << std::endl; 406 for (orxonox::ObjectList<ChatListener>::iterator it = orxonox::ObjectList<ChatListener>::begin(); it != orxonox::ObjectList<ChatListener>::end(); ++it) 407 it->incomingChat(message, clientID); 408 401 409 return true; 402 410 }
Note: See TracChangeset
for help on using the changeset viewer.