Changeset 3214 for code/trunk/src/network/Server.cc
- Timestamp:
- Jun 23, 2009, 6:02:25 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/network/Server.cc
r3198 r3214 42 42 43 43 #include <enet/enet.h> 44 #include <iostream>45 44 #include <cassert> 46 47 48 #include "ConnectionManager.h" 49 #include "ClientConnectionListener.h" 45 #include <string> 46 47 #include "util/Debug.h" 48 #include "core/Clock.h" 49 #include "core/ObjectList.h" 50 #include "packet/Chat.h" 51 #include "packet/ClassID.h" 52 #include "packet/DeleteObjects.h" 53 #include "packet/FunctionIDs.h" 54 #include "packet/Gamestate.h" 55 #include "packet/Welcome.h" 56 #include "ChatListener.h" 57 #include "ClientInformation.h" 58 #include "FunctionCallManager.h" 50 59 #include "GamestateManager.h" 51 #include "ClientInformation.h"52 #include "util/Sleep.h"53 #include "core/Clock.h"54 #include "core/ConsoleCommand.h"55 #include "core/CoreIncludes.h"56 #include "packet/Chat.h"57 #include "packet/Packet.h"58 #include "packet/Welcome.h"59 #include "packet/DeleteObjects.h"60 #include "util/Convert.h"61 #include "ChatListener.h"62 #include "FunctionCallManager.h"63 #include "packet/FunctionIDs.h"64 65 60 66 61 namespace orxonox … … 74 69 Server::Server() { 75 70 timeSinceLastUpdate_=0; 76 connection = new ConnectionManager();77 71 gamestates_ = new GamestateManager(); 78 72 } 79 73 80 74 Server::Server(int port){ 75 this->setPort( port ); 81 76 timeSinceLastUpdate_=0; 82 connection = new ConnectionManager(port);83 77 gamestates_ = new GamestateManager(); 84 78 } … … 90 84 */ 91 85 Server::Server(int port, const std::string& bindAddress) { 86 this->setPort( port ); 87 this->setBindAddress( bindAddress ); 92 88 timeSinceLastUpdate_=0; 93 connection = new ConnectionManager(port, bindAddress);94 89 gamestates_ = new GamestateManager(); 95 90 } 96 91 97 92 /** 98 * Constructor99 * @param port Port to listen on100 * @param bindAddress Address to listen on101 */102 Server::Server(int port, const char *bindAddress) {103 timeSinceLastUpdate_=0;104 connection = new ConnectionManager(port, bindAddress);105 gamestates_ = new GamestateManager();106 }107 108 /**109 93 * @brief Destructor 110 94 */ 111 95 Server::~Server(){ 112 if(connection)113 delete connection;114 96 if(gamestates_) 115 97 delete gamestates_; … … 120 102 */ 121 103 void Server::open() { 122 connection->createListener(); 104 COUT(4) << "opening server" << endl; 105 this->openListener(); 123 106 return; 124 107 } … … 128 111 */ 129 112 void Server::close() { 130 ClientInformation *temp = ClientInformation::getBegin(); 131 ClientInformation *temp2; 132 // disconnect all connected clients 133 while( temp ) 134 { 135 temp2 = temp; 136 temp = temp->next(); 137 disconnectClient( temp2 ); 138 } 139 connection->quitListener(); 113 COUT(4) << "closing server" << endl; 114 this->disconnectClients(); 115 this->closeListener(); 140 116 return; 141 117 } … … 162 138 */ 163 139 void Server::update(const Clock& time) { 164 processQueue();140 Connection::processQueue(); 165 141 gamestates_->processGamestates(); 166 142 //this steers our network frequency 167 143 timeSinceLastUpdate_+=time.getDeltaTime(); 168 if(timeSinceLastUpdate_>=NETWORK_PERIOD){ 144 if(timeSinceLastUpdate_>=NETWORK_PERIOD) 145 { 169 146 timeSinceLastUpdate_ -= static_cast<unsigned int>( timeSinceLastUpdate_ / NETWORK_PERIOD ) * NETWORK_PERIOD; 170 147 updateGamestate(); 171 148 FunctionCallManager::sendCalls(); 172 149 } 150 sendPackets(); // flush the enet queue 173 151 } 174 152 175 153 bool Server::queuePacket(ENetPacket *packet, int clientID){ 176 return connection->addPacket(packet, clientID);154 return ServerConnection::addPacket(packet, clientID); 177 155 } 178 156 … … 191 169 assert(ClientInformation::findClient(clientID)); 192 170 return ClientInformation::findClient(clientID)->getPacketLoss(); 193 }194 195 /**196 * processes all the packets waiting in the queue197 */198 void Server::processQueue() {199 ENetEvent *event;200 while(!connection->queueEmpty()){201 //std::cout << "Client " << clientID << " sent: " << std::endl;202 //clientID here is a reference to grab clientID from ClientInformation203 event = connection->getEvent();204 if(!event)205 continue;206 assert(event->type != ENET_EVENT_TYPE_NONE);207 switch( event->type ) {208 case ENET_EVENT_TYPE_CONNECT:209 COUT(4) << "processing event_Type_connect" << std::endl;210 addClient(event);211 break;212 case ENET_EVENT_TYPE_DISCONNECT:213 if(ClientInformation::findClient(&event->peer->address))214 disconnectClient(event);215 break;216 case ENET_EVENT_TYPE_RECEIVE:217 if(!processPacket(event->packet, event->peer))218 COUT(3) << "processing incoming packet failed" << std::endl;219 break;220 default:221 break;222 }223 delete event;224 //if statement to catch case that packetbuffer is empty225 }226 171 } 227 172 … … 318 263 319 264 320 boolServer::addClient(ENetEvent *event){265 void Server::addClient(ENetEvent *event){ 321 266 static unsigned int newid=1; 322 267 … … 325 270 if(!temp){ 326 271 COUT(2) << "Server: could not add client" << std::endl; 327 return false; 328 } 329 /*if(temp==ClientInformation::getBegin()) { //not good if you use anything else than insertBack 330 newid=1; 331 } 332 else 333 newid=temp->prev()->getID()+1;*/ 272 } 334 273 temp->setID(newid); 335 274 temp->setPeer(event->peer); … … 342 281 } 343 282 344 newid++;283 ++newid; 345 284 346 285 COUT(3) << "Server: added client id: " << temp->getID() << std::endl; 347 returncreateClient(temp->getID());286 createClient(temp->getID()); 348 287 } 349 288 … … 357 296 358 297 // synchronise class ids 359 connection->syncClassid(temp->getID());298 syncClassid(temp->getID()); 360 299 361 300 // now synchronise functionIDs … … 382 321 return true; 383 322 } 384 385 bool Server::disconnectClient(ENetEvent *event){386 COUT(4) << "removing client from list" << std::endl;387 //return removeClient(head_->findClient(&(peer->address))->getID());388 389 //boost::recursive_mutex::scoped_lock lock(head_->mutex_);390 ClientInformation *client = ClientInformation::findClient(&event->peer->address);391 if(!client)392 return false;393 else394 disconnectClient( client );395 return true;396 }397 398 void Server::disconnectClient(int clientID){399 ClientInformation *client = ClientInformation::findClient(clientID);400 if(client)401 disconnectClient(client);402 }403 323 404 void Server::disconnectClient( ClientInformation *client ){405 connection->disconnectClient(client);324 void Server::disconnectClient( ClientInformation *client ){ 325 ServerConnection::disconnectClient( client ); 406 326 gamestates_->removeClient(client); 407 327 // inform all the listeners … … 439 359 } 440 360 361 void Server::syncClassid(unsigned int clientID) { 362 int failures=0; 363 packet::ClassID *classid = new packet::ClassID(); 364 classid->setClientID(clientID); 365 while(!classid->send() && failures < 10){ 366 failures++; 367 } 368 assert(failures<10); 369 COUT(4) << "syncClassid:\tall synchClassID packets have been sent" << std::endl; 370 } 371 441 372 }
Note: See TracChangeset
for help on using the changeset viewer.