Changeset 3201 for code/branches/netp5/src/network/Server.cc
- Timestamp:
- Jun 20, 2009, 7:21:46 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/netp5/src/network/Server.cc
r3198 r3201 46 46 47 47 48 #include "ConnectionManager.h"48 // #include "ConnectionManager.h" 49 49 #include "ClientConnectionListener.h" 50 50 #include "GamestateManager.h" … … 58 58 #include "packet/Welcome.h" 59 59 #include "packet/DeleteObjects.h" 60 #include "packet/ClassID.h" 60 61 #include "util/Convert.h" 61 62 #include "ChatListener.h" … … 74 75 Server::Server() { 75 76 timeSinceLastUpdate_=0; 76 connection = new ConnectionManager();77 77 gamestates_ = new GamestateManager(); 78 78 } 79 79 80 80 Server::Server(int port){ 81 this->setPort( port ); 81 82 timeSinceLastUpdate_=0; 82 connection = new ConnectionManager(port);83 83 gamestates_ = new GamestateManager(); 84 84 } … … 90 90 */ 91 91 Server::Server(int port, const std::string& bindAddress) { 92 this->setPort( port ); 93 this->setBindAddress( bindAddress ); 92 94 timeSinceLastUpdate_=0; 93 connection = new ConnectionManager(port, bindAddress);94 95 gamestates_ = new GamestateManager(); 95 96 } 96 97 97 98 /** 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 99 * @brief Destructor 110 100 */ 111 101 Server::~Server(){ 112 if(connection)113 delete connection;114 102 if(gamestates_) 115 103 delete gamestates_; … … 120 108 */ 121 109 void Server::open() { 122 connection->createListener(); 110 COUT(4) << "opening server" << endl; 111 this->openListener(); 123 112 return; 124 113 } … … 128 117 */ 129 118 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(); 119 COUT(4) << "closing server" << endl; 120 this->disconnectClients(); 121 this->closeListener(); 140 122 return; 141 123 } … … 162 144 */ 163 145 void Server::update(const Clock& time) { 164 processQueue();146 ServerConnection::processQueue(); 165 147 gamestates_->processGamestates(); 166 148 //this steers our network frequency … … 174 156 175 157 bool Server::queuePacket(ENetPacket *packet, int clientID){ 176 return connection->addPacket(packet, clientID);158 return ServerConnection::addPacket(packet, clientID); 177 159 } 178 160 … … 191 173 assert(ClientInformation::findClient(clientID)); 192 174 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 175 } 227 176 … … 318 267 319 268 320 boolServer::addClient(ENetEvent *event){269 void Server::addClient(ENetEvent *event){ 321 270 static unsigned int newid=1; 322 271 … … 325 274 if(!temp){ 326 275 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;*/ 276 } 334 277 temp->setID(newid); 335 278 temp->setPeer(event->peer); … … 342 285 } 343 286 344 newid++;287 ++newid; 345 288 346 289 COUT(3) << "Server: added client id: " << temp->getID() << std::endl; 347 returncreateClient(temp->getID());290 createClient(temp->getID()); 348 291 } 349 292 … … 357 300 358 301 // synchronise class ids 359 connection->syncClassid(temp->getID());302 syncClassid(temp->getID()); 360 303 361 304 // now synchronise functionIDs … … 382 325 return true; 383 326 } 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 327 404 void Server::disconnectClient( ClientInformation *client ){405 connection->disconnectClient(client);328 void Server::disconnectClient( ClientInformation *client ){ 329 ServerConnection::disconnectClient( client ); 406 330 gamestates_->removeClient(client); 407 331 // inform all the listeners … … 439 363 } 440 364 365 void Server::syncClassid(unsigned int clientID) { 366 int failures=0; 367 packet::ClassID *classid = new packet::ClassID(); 368 classid->setClientID(clientID); 369 while(!classid->send() && failures < 10){ 370 failures++; 371 } 372 assert(failures<10); 373 COUT(4) << "syncClassid:\tall synchClassID packets have been sent" << std::endl; 374 } 375 441 376 }
Note: See TracChangeset
for help on using the changeset viewer.