Changeset 1502 for code/trunk/src/network/Server.cc
- Timestamp:
- Jun 1, 2008, 3:54:20 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/network/Server.cc
r1360 r1502 42 42 43 43 #include <iostream> 44 44 45 45 46 #include "ConnectionManager.h" … … 49 50 //#include "NetworkFrameListener.h" 50 51 #include "util/Sleep.h" 52 #include "objects/SpaceShip.h" 51 53 52 54 … … 56 58 57 59 #define MAX_FAILURES 20; 58 60 #define NETWORK_FREQUENCY 30 59 61 60 62 /** … … 63 65 */ 64 66 Server::Server() { 67 timeSinceLastUpdate_=0; 65 68 packet_gen = PacketGenerator(); 66 69 clients = new ClientInformation(true); … … 68 71 gamestates = new GameStateManager(clients); 69 72 } 73 74 Server::Server(int port){ 75 timeSinceLastUpdate_=0; 76 packet_gen = PacketGenerator(); 77 clients = new ClientInformation(true); 78 connection = new ConnectionManager(clients, port); 79 gamestates = new GameStateManager(clients); 80 } 70 81 71 82 /** … … 75 86 */ 76 87 Server::Server(int port, std::string bindAddress) { 88 timeSinceLastUpdate_=0; 77 89 packet_gen = PacketGenerator(); 78 90 clients = new ClientInformation(); … … 87 99 */ 88 100 Server::Server(int port, const char *bindAddress) { 101 timeSinceLastUpdate_=0; 89 102 packet_gen = PacketGenerator(); 90 103 clients = new ClientInformation(); … … 117 130 ENetPacket *packet = packet_gen.chatMessage(msg.c_str()); 118 131 //std::cout <<"adding packets" << std::endl; 119 if(connection->addPacketAll(packet)) 120 //std::cout <<"added packets" << std::endl; 121 return connection->sendPackets(); 122 else 123 return false; 132 return connection->addPacketAll(packet); 124 133 } 125 134 … … 132 141 ENetPacket *packet = packet_gen.chatMessage(msg); 133 142 COUT(4) <<"Server: adding Packets" << std::endl; 134 connection->addPacketAll(packet); 135 //std::cout <<"added packets" << std::endl; 136 if (connection->sendPackets()){ 137 COUT(4) << "Server: Sucessfully" << std::endl; 138 return true; 139 } 140 return false; 143 return connection->addPacketAll(packet); 141 144 } 142 145 … … 148 151 void Server::tick(float time) { 149 152 processQueue(); 150 updateGamestate(); 151 // usleep(500000); // TODO remove 153 //this steers our network frequency 154 timeSinceLastUpdate_+=time; 155 if(timeSinceLastUpdate_>=(1./NETWORK_FREQUENCY)){ 156 timeSinceLastUpdate_-=(1./NETWORK_FREQUENCY); 157 gamestates->processGameStates(); 158 updateGamestate(); 159 } 160 // usleep(5000); // TODO remove 152 161 return; 153 162 } … … 157 166 */ 158 167 void Server::processQueue() { 159 ENet Packet *packet;168 ENetEvent *event; 160 169 int clientID=-1; 161 170 while(!connection->queueEmpty()){ 162 171 //std::cout << "Client " << clientID << " sent: " << std::endl; 163 172 //clientID here is a reference to grab clientID from ClientInformation 164 packet = connection->getPacket(clientID); 165 if(!packet) 166 continue; 173 event = connection->getEvent(); 174 if(!event) 175 continue; 176 assert(event->type != ENET_EVENT_TYPE_NONE); 177 switch( event->type ) { 178 case ENET_EVENT_TYPE_CONNECT: 179 COUT(3) << "processing event_Type_connect" << std::endl; 180 addClient(event); 181 break; 182 case ENET_EVENT_TYPE_DISCONNECT: 183 if(clients->findClient(&event->peer->address)) 184 disconnectClient(event); 185 break; 186 case ENET_EVENT_TYPE_RECEIVE: 187 if(clients->findClient(&event->peer->address)){ 188 clientID = clients->findClient(&event->peer->address)->getID(); 189 if( !elaborate(event->packet, clientID) ) 190 COUT(3) << "Server: could not elaborate" << std::endl; 191 } 192 break; 193 } 194 delete event; 167 195 //if statement to catch case that packetbuffer is empty 168 if( !elaborate(packet, clientID) )169 COUT(3) << "Server: could not elaborate" << std::endl;170 196 } 171 197 } … … 210 236 if(gs==NULL){ 211 237 COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl; 212 return false;238 continue; 213 239 } 214 240 //std::cout << "adding gamestate" << std::endl; 215 if ( !(connection->addPacket(packet_gen.gstate(gs), cid)) ){ 241 ENetPacket *packet = packet_gen.gstate(gs); 242 if(!packet) 243 continue; 244 if ( !(connection->addPacket(packet, cid)) ){ 216 245 COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl; 217 246 temp->addFailure(); 218 if(temp->getFailures() > 20 )219 disconnectClient(temp); 247 /*if(temp->getFailures() > 0 ) 248 disconnectClient(temp);*/ 220 249 //std::cout << "added gamestate" << std::endl; 221 } 250 }else 251 temp->resetFailures(); 222 252 added=true; 223 253 temp=temp->next(); … … 226 256 delete gs; 227 257 } 228 if(added) {258 /*if(added) { 229 259 //std::cout << "send gamestates from server.cc in sendGameState" << std::endl; 230 260 return connection->sendPackets(); 231 } 232 COUT(5) << "Server: had no gamestates to send" << std::endl;233 return false;261 }*/ 262 //COUT(5) << "Server: had no gamestates to send" << std::endl; 263 return true; 234 264 } 235 265 236 266 void Server::processAck( ack *data, int clientID) { 237 COUT(4) << " \b\b\b\n\n\n\n\nServer: processing ack from client: " << clientID << "; ack-id: " << data->a << std::endl;267 COUT(4) << "Server: processing ack from client: " << clientID << "; ack-id: " << data->a << std::endl; 238 268 gamestates->ackGameState(clientID, data->a); 239 269 delete data; … … 241 271 242 272 bool Server::processConnectRequest( connectRequest *con, int clientID ){ 243 COUT(3) << "processing connectRequest " << std::endl;273 //(COUT(3) << "processing connectRequest " << std::endl; 244 274 //connection->addPacket(packet_gen.gstate(gamestates->popGameState(clientID)) , clientID); 245 connection->createClient(clientID);275 //createClient(clientID); 246 276 delete con; 247 277 return true; … … 250 280 void Server::processGamestate( GameStateCompressed *data, int clientID){ 251 281 COUT(4) << "processing partial gamestate from client " << clientID << std::endl; 252 if(!gamestates->pushGameState(data, clientID))253 COUT(3) << "Could not push gamestate\t\t\t\t=====" << std::endl;282 gamestates->addGameState(data, clientID); 283 /*COUT(3) << "Could not push gamestate\t\t\t\t=====" << std::endl; 254 284 else 255 285 if(clients->findClient(clientID)) 256 clients->findClient(clientID)->resetFailures(); 286 clients->findClient(clientID)->resetFailures();*/ 287 } 288 289 bool Server::addClient(ENetEvent *event){ 290 ClientInformation *temp = clients->insertBack(new ClientInformation); 291 if(!temp){ 292 COUT(2) << "Server: could not add client" << std::endl; 293 return false; 294 } 295 if(temp->prev()->getHead()) { //not good if you use anything else than insertBack 296 temp->prev()->setID(0); //bugfix: not necessary but usefull 297 temp->setID(1); 298 } 299 else 300 temp->setID(temp->prev()->getID()+1); 301 temp->setPeer(event->peer); 302 COUT(3) << "Server: added client id: " << temp->getID() << std::endl; 303 return createClient(temp->getID()); 304 } 305 306 bool Server::createClient(int clientID){ 307 ClientInformation *temp = clients->findClient(clientID); 308 if(!temp){ 309 COUT(2) << "Conn.Man. could not create client with id: " << clientID << std::endl; 310 return false; 311 } 312 COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl; 313 connection->syncClassid(temp->getID()); 314 COUT(4) << "creating spaceship for clientid: " << temp->getID() << std::endl; 315 // TODO: this is only a hack, untill we have a possibility to define default player-join actions 316 if(!createShip(temp)) 317 COUT(2) << "Con.Man. could not create ship for clientid: " << clientID << std::endl; 318 else 319 COUT(3) << "created spaceship" << std::endl; 320 temp->setSynched(true); 321 COUT(3) << "sending welcome" << std::endl; 322 connection->sendWelcome(temp->getID(), temp->getShipID(), true); 323 return true; 324 } 325 326 bool Server::createShip(ClientInformation *client){ 327 if(!client) 328 return false; 329 orxonox::Identifier* id = ID("SpaceShip"); 330 if(!id){ 331 COUT(4) << "We could not create the SpaceShip for client: " << client->getID() << std::endl; 332 return false; 333 } 334 orxonox::SpaceShip *no = dynamic_cast<orxonox::SpaceShip *>(id->fabricate()); 335 no->setPosition(orxonox::Vector3(0,0,80)); 336 no->setScale(10); 337 //no->setYawPitchRoll(orxonox::Degree(-90),orxonox::Degree(-90),orxonox::Degree(0)); 338 no->setMesh("assff.mesh"); 339 no->setMaxSpeed(500); 340 no->setMaxSideAndBackSpeed(50); 341 no->setMaxRotation(1.0); 342 no->setTransAcc(200); 343 no->setRotAcc(3.0); 344 no->setTransDamp(75); 345 no->setRotDamp(1.0); 346 no->setCamera("cam_"+client->getID()); 347 no->classID = id->getNetworkID(); 348 no->create(); 349 350 client->setShipID(no->objectID); 351 return true; 352 } 353 354 bool Server::disconnectClient(ENetEvent *event){ 355 COUT(4) << "removing client from list" << std::endl; 356 //return removeClient(head_->findClient(&(peer->address))->getID()); 357 358 //boost::recursive_mutex::scoped_lock lock(head_->mutex_); 359 orxonox::Iterator<orxonox::SpaceShip> it = orxonox::ObjectList<orxonox::SpaceShip>::start(); 360 ClientInformation *client = clients->findClient(&event->peer->address); 361 if(!client) 362 return false; 363 while(it){ 364 if(it->objectID!=client->getShipID()){ 365 ++it; 366 continue; 367 } 368 orxonox::Iterator<orxonox::SpaceShip> temp=it; 369 ++it; 370 delete *temp; 371 return clients->removeClient(event->peer); 372 } 373 return false; 257 374 } 258 375
Note: See TracChangeset
for help on using the changeset viewer.