[1168] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Oliver Scheuss, (C) 2007 |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
[1232] | 28 | |
---|
| 29 | // |
---|
| 30 | // C++ Interface: ConnectionManager |
---|
| 31 | // |
---|
| 32 | // Description: The Class ConnectionManager manages the servers conenctions to the clients. |
---|
| 33 | // each connection is provided by a new process. communication between master process and |
---|
| 34 | // connection processes is provided by ... |
---|
| 35 | // |
---|
| 36 | // |
---|
| 37 | // Author: Oliver Scheuss |
---|
| 38 | // |
---|
| 39 | |
---|
| 40 | #include <iostream> |
---|
| 41 | // boost.thread library for multithreading support |
---|
| 42 | #include <boost/thread/thread.hpp> |
---|
| 43 | #include <boost/bind.hpp> |
---|
| 44 | |
---|
| 45 | #include "core/CoreIncludes.h" |
---|
| 46 | #include "core/BaseObject.h" |
---|
| 47 | #include "util/Math.h" |
---|
| 48 | #include "objects/SpaceShip.h" |
---|
| 49 | #include "ClientInformation.h" |
---|
| 50 | #include "ConnectionManager.h" |
---|
| 51 | #include "Synchronisable.h" |
---|
| 52 | |
---|
| 53 | namespace std |
---|
| 54 | { |
---|
| 55 | bool operator< (ENetAddress a, ENetAddress b) { |
---|
| 56 | if(a.host <= b.host) |
---|
| 57 | return true; |
---|
| 58 | else |
---|
| 59 | return false; |
---|
| 60 | } |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | namespace network |
---|
| 64 | { |
---|
| 65 | boost::thread_group network_threads; |
---|
| 66 | |
---|
| 67 | ConnectionManager::ConnectionManager(){} |
---|
| 68 | |
---|
| 69 | ConnectionManager::ConnectionManager(ClientInformation *head) { |
---|
| 70 | quit=false; |
---|
| 71 | bindAddress.host = ENET_HOST_ANY; |
---|
| 72 | bindAddress.port = NETWORK_PORT; |
---|
| 73 | head_ = head; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | ConnectionManager::ConnectionManager(int port, std::string address, ClientInformation *head) { |
---|
| 77 | quit=false; |
---|
| 78 | enet_address_set_host (& bindAddress, address.c_str()); |
---|
| 79 | bindAddress.port = NETWORK_PORT; |
---|
| 80 | head_ = head; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | ConnectionManager::ConnectionManager(int port, const char *address, ClientInformation *head) { |
---|
| 84 | quit=false; |
---|
| 85 | enet_address_set_host (& bindAddress, address); |
---|
| 86 | bindAddress.port = NETWORK_PORT; |
---|
| 87 | head_ = head; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | ENetPacket *ConnectionManager::getPacket(ENetAddress &address) { |
---|
| 91 | if(!buffer.isEmpty()) |
---|
| 92 | return buffer.pop(address); |
---|
| 93 | else |
---|
| 94 | return NULL; |
---|
| 95 | } |
---|
| 96 | /** |
---|
| 97 | This function only pops the first element in PacketBuffer (first in first out) |
---|
| 98 | used by processQueue in Server.cc |
---|
| 99 | */ |
---|
| 100 | ENetPacket *ConnectionManager::getPacket(int &clientID) { |
---|
| 101 | ENetAddress address; |
---|
| 102 | ENetPacket *packet=getPacket(address); |
---|
| 103 | ClientInformation *temp =head_->findClient(&address); |
---|
| 104 | clientID=temp->getID(); |
---|
| 105 | return packet; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | bool ConnectionManager::queueEmpty() { |
---|
| 109 | return buffer.isEmpty(); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | void ConnectionManager::createListener() { |
---|
| 113 | network_threads.create_thread(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this)); |
---|
| 114 | // boost::thread thr(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this)); |
---|
| 115 | return; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | bool ConnectionManager::quitListener() { |
---|
| 119 | quit=true; |
---|
| 120 | network_threads.join_all(); |
---|
| 121 | return true; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer) { |
---|
| 125 | if(enet_peer_send(peer, (enet_uint8)head_->findClient(&(peer->address))->getID() , packet)!=0) |
---|
| 126 | return false; |
---|
| 127 | return true; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | bool ConnectionManager::addPacket(ENetPacket *packet, int clientID) { |
---|
| 131 | if(enet_peer_send(head_->findClient(clientID)->getPeer(), (enet_uint8)clientID, packet)!=0) |
---|
| 132 | return false; |
---|
| 133 | return true; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | bool ConnectionManager::addPacketAll(ENetPacket *packet) { |
---|
| 137 | for(ClientInformation *i=head_->next(); i!=0; i=i->next()){ |
---|
| 138 | if(enet_peer_send(i->getPeer(), (enet_uint8)i->getID(), packet)!=0) |
---|
| 139 | return false; |
---|
| 140 | } |
---|
| 141 | return true; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | bool ConnectionManager::sendPackets(ENetEvent *event) { |
---|
| 145 | if(server==NULL) |
---|
| 146 | return false; |
---|
| 147 | if(enet_host_service(server, event, NETWORK_SEND_WAIT)>=0) |
---|
| 148 | return true; |
---|
| 149 | else |
---|
| 150 | return false; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | bool ConnectionManager::sendPackets() { |
---|
| 154 | ENetEvent event; |
---|
| 155 | if(server==NULL) |
---|
| 156 | return false; |
---|
| 157 | if(enet_host_service(server, &event, NETWORK_SEND_WAIT)>=0) |
---|
| 158 | return true; |
---|
| 159 | else |
---|
| 160 | return false; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | void ConnectionManager::receiverThread() { |
---|
| 164 | // what about some error-handling here ? |
---|
| 165 | enet_initialize(); |
---|
| 166 | atexit(enet_deinitialize); |
---|
[1245] | 167 | ENetEvent *event = new ENetEvent; |
---|
[1232] | 168 | server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0); |
---|
| 169 | if(server==NULL){ |
---|
| 170 | // add some error handling here ========================== |
---|
| 171 | quit=true; |
---|
| 172 | return; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | while(!quit){ |
---|
[1245] | 176 | if(enet_host_service(server, event, NETWORK_WAIT_TIMEOUT)<0){ |
---|
[1232] | 177 | // we should never reach this point |
---|
| 178 | quit=true; |
---|
| 179 | // add some error handling here ======================== |
---|
| 180 | } |
---|
[1245] | 181 | switch(event->type){ |
---|
[1232] | 182 | // log handling ================ |
---|
| 183 | case ENET_EVENT_TYPE_CONNECT: |
---|
[1245] | 184 | addClient(event); |
---|
[1232] | 185 | //this is a workaround to ensure thread safety |
---|
[1233] | 186 | /*if(!addFakeConnectRequest(&event)) |
---|
| 187 | COUT(3) << "Problem pushing fakeconnectRequest to queue" << std::endl;*/ |
---|
[1232] | 188 | COUT(5) << "Con.Man: connection event has occured" << std::endl; |
---|
| 189 | break; |
---|
| 190 | case ENET_EVENT_TYPE_RECEIVE: |
---|
| 191 | //std::cout << "received data" << std::endl; |
---|
| 192 | COUT(5) << "Con.Man: receive event has occured" << std::endl; |
---|
[1261] | 193 | // only add, if client has connected yet and not been disconnected |
---|
| 194 | if(head_->findClient(&event->peer->address)) |
---|
| 195 | processData(event); |
---|
[1232] | 196 | break; |
---|
| 197 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
[1245] | 198 | clientDisconnect(event->peer); |
---|
[1232] | 199 | break; |
---|
| 200 | case ENET_EVENT_TYPE_NONE: |
---|
| 201 | break; |
---|
| 202 | } |
---|
[1250] | 203 | // usleep(100); |
---|
[1245] | 204 | //yield(); //TODO: find apropriate |
---|
[1232] | 205 | } |
---|
| 206 | disconnectClients(); |
---|
| 207 | // if we're finishied, destroy server |
---|
| 208 | enet_host_destroy(server); |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | //### added some bugfixes here, but we cannot test them because |
---|
| 212 | //### the server crashes everytime because of some gamestates |
---|
| 213 | //### (trying to resolve that now) |
---|
| 214 | void ConnectionManager::disconnectClients() { |
---|
| 215 | ENetEvent event; |
---|
| 216 | ClientInformation *temp = head_->next(); |
---|
| 217 | while(temp!=0){ |
---|
| 218 | enet_peer_disconnect(temp->getPeer(), 0); |
---|
| 219 | temp = temp->next(); |
---|
| 220 | } |
---|
| 221 | //bugfix: might be the reason why server crashes when clients disconnects |
---|
| 222 | //temp = temp->next(); |
---|
| 223 | temp = head_->next(); |
---|
| 224 | while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) > 0){ |
---|
| 225 | switch (event.type) |
---|
| 226 | { |
---|
| 227 | case ENET_EVENT_TYPE_NONE: break; |
---|
| 228 | case ENET_EVENT_TYPE_CONNECT: break; |
---|
| 229 | case ENET_EVENT_TYPE_RECEIVE: |
---|
| 230 | enet_packet_destroy(event.packet); |
---|
| 231 | break; |
---|
| 232 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
| 233 | COUT(4) << "disconnecting all clients" << std::endl; |
---|
| 234 | delete head_->findClient(&(event.peer->address)); |
---|
| 235 | //maybe needs bugfix: might also be a reason for the server to crash |
---|
| 236 | temp = temp->next(); |
---|
| 237 | break; |
---|
| 238 | } |
---|
| 239 | } |
---|
| 240 | return; |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | bool ConnectionManager::processData(ENetEvent *event) { |
---|
| 244 | // just add packet to the buffer |
---|
| 245 | // this can be extended with some preprocessing |
---|
| 246 | return buffer.push(event); |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | bool ConnectionManager::clientDisconnect(ENetPeer *peer) { |
---|
| 250 | COUT(4) << "removing client from list" << std::endl; |
---|
[1245] | 251 | return removeClient(head_->findClient(&(peer->address))->getID()); |
---|
[1232] | 252 | } |
---|
| 253 | /** |
---|
| 254 | This function adds a client that connects to the clientlist of the server |
---|
| 255 | NOTE: if you change this, don't forget to change the test function |
---|
| 256 | addClientTest in diffTest.cc since addClient is not good for testing because of syncClassid |
---|
| 257 | */ |
---|
| 258 | bool ConnectionManager::addClient(ENetEvent *event) { |
---|
| 259 | ClientInformation *temp = head_->insertBack(new ClientInformation); |
---|
| 260 | if(temp->prev()->head) { //not good if you use anything else than insertBack |
---|
| 261 | temp->prev()->setID(0); //bugfix: not necessary but usefull |
---|
| 262 | temp->setID(1); |
---|
| 263 | } |
---|
| 264 | else |
---|
| 265 | temp->setID(temp->prev()->getID()+1); |
---|
| 266 | temp->setPeer(event->peer); |
---|
| 267 | COUT(4) << "Con.Man: added client id: " << temp->getID() << std::endl; |
---|
| 268 | return true; |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | int ConnectionManager::getClientID(ENetPeer peer) { |
---|
| 272 | return getClientID(peer.address); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | int ConnectionManager::getClientID(ENetAddress address) { |
---|
| 276 | return head_->findClient(&address)->getID(); |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | ENetPeer *ConnectionManager::getClientPeer(int clientID) { |
---|
| 280 | return head_->findClient(clientID)->getPeer(); |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | void ConnectionManager::syncClassid(int clientID) { |
---|
| 284 | unsigned int network_id=0; |
---|
| 285 | std::string classname; |
---|
| 286 | orxonox::Identifier *id; |
---|
| 287 | std::map<std::string, orxonox::Identifier*>::const_iterator it = orxonox::Factory::getFactoryBegin(); |
---|
| 288 | while(it != orxonox::Factory::getFactoryEnd()){ |
---|
| 289 | id = (*it).second; |
---|
| 290 | if(id == NULL) |
---|
| 291 | continue; |
---|
| 292 | classname = id->getName(); |
---|
| 293 | network_id = id->getNetworkID(); |
---|
| 294 | COUT(4) << "Con.Man:syncClassid:\tnetwork_id: " << network_id << ", classname: " << classname << std::endl; |
---|
| 295 | |
---|
| 296 | addPacket(packet_gen.clid( (int)network_id, classname ), clientID); |
---|
| 297 | |
---|
| 298 | ++it; |
---|
| 299 | } |
---|
| 300 | sendPackets(); |
---|
| 301 | COUT(4) << "syncClassid:\tall synchClassID packets have been sent" << std::endl; |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | bool ConnectionManager::createClient(int clientID){ |
---|
| 305 | ClientInformation *temp = head_->findClient(clientID); |
---|
| 306 | COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl; |
---|
| 307 | syncClassid(temp->getID()); |
---|
| 308 | COUT(4) << "creating spaceship for clientid: " << temp->getID() << std::endl; |
---|
| 309 | // TODO: this is only a hack, untill we have a possibility to define default player-join actions |
---|
| 310 | createShip(temp); |
---|
| 311 | COUT(4) << "created spaceship" << std::endl; |
---|
| 312 | temp->setSynched(true); |
---|
| 313 | COUT(4) << "sending welcome" << std::endl; |
---|
| 314 | sendWelcome(temp->getID(), temp->getShipID(), true); |
---|
| 315 | return true; |
---|
| 316 | } |
---|
| 317 | |
---|
[1245] | 318 | bool ConnectionManager::removeClient(int clientID){ |
---|
| 319 | orxonox::Iterator<orxonox::SpaceShip> it = orxonox::ObjectList<orxonox::SpaceShip>::start(); |
---|
| 320 | while(it){ |
---|
| 321 | if(it->objectID!=head_->findClient(clientID)->getShipID()){ |
---|
| 322 | ++it; |
---|
| 323 | continue; |
---|
| 324 | } |
---|
| 325 | orxonox::Iterator<orxonox::SpaceShip> temp=it; |
---|
| 326 | ++it; |
---|
| 327 | delete *temp; |
---|
| 328 | return head_->removeClient(clientID); |
---|
| 329 | } |
---|
| 330 | return false; |
---|
| 331 | } |
---|
| 332 | |
---|
[1232] | 333 | bool ConnectionManager::createShip(ClientInformation *client){ |
---|
| 334 | orxonox::Identifier* id = ID("SpaceShip"); |
---|
| 335 | if(!id){ |
---|
| 336 | COUT(4) << "We could not create the SpaceShip for client: " << client->getID() << std::endl; |
---|
| 337 | return false; |
---|
| 338 | } |
---|
| 339 | orxonox::SpaceShip *no = dynamic_cast<orxonox::SpaceShip *>(id->fabricate()); |
---|
| 340 | no->setPosition(orxonox::Vector3(0,80,0)); |
---|
| 341 | no->setScale(10); |
---|
| 342 | no->setYawPitchRoll(orxonox::Degree(-90),orxonox::Degree(-90),orxonox::Degree(0)); |
---|
| 343 | no->setMesh("assf3.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("cam_"+client->getID()); |
---|
| 352 | no->create(); |
---|
| 353 | |
---|
| 354 | client->setShipID(no->objectID); |
---|
| 355 | return true; |
---|
| 356 | } |
---|
| 357 | |
---|
[1261] | 358 | bool ConnectionManager::removeShip(ClientInformation *client){ |
---|
| 359 | int id=client->getShipID(); |
---|
| 360 | orxonox::Iterator<orxonox::SpaceShip> it; |
---|
| 361 | for(it = orxonox::ObjectList<orxonox::SpaceShip>::start(); it; ++it){ |
---|
| 362 | if(it->objectID!=id) |
---|
| 363 | continue; |
---|
| 364 | delete *it; |
---|
| 365 | } |
---|
| 366 | return true; |
---|
| 367 | } |
---|
| 368 | |
---|
[1232] | 369 | bool ConnectionManager::sendWelcome(int clientID, int shipID, bool allowed){ |
---|
| 370 | addPacket(packet_gen.generateWelcome(clientID, shipID, allowed),clientID); |
---|
| 371 | sendPackets(); |
---|
[1245] | 372 | return true; |
---|
[1232] | 373 | } |
---|
| 374 | |
---|
[1261] | 375 | void ConnectionManager::disconnectClient(ClientInformation *client){ |
---|
| 376 | enet_peer_disconnect(client->getPeer(), 0); |
---|
| 377 | removeShip(client); |
---|
| 378 | } |
---|
| 379 | |
---|
[1232] | 380 | bool ConnectionManager::addFakeConnectRequest(ENetEvent *ev){ |
---|
| 381 | ENetEvent event; |
---|
| 382 | event.peer=ev->peer; |
---|
| 383 | event.packet = packet_gen.generateConnectRequest(); |
---|
| 384 | return buffer.push(&event); |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | |
---|
| 388 | // int ConnectionManager::getNumberOfClients() { |
---|
| 389 | // |
---|
| 390 | // return clientsShip.size(); |
---|
| 391 | // } |
---|
| 392 | |
---|
| 393 | /*void ConnectionManager::addClientsObjectID( int clientID, int objectID ) { |
---|
| 394 | COUT(4) << "ship of client: " << clientID << ": " << objectID << " mapped" << std::endl; |
---|
| 395 | clientsShip.insert( std::make_pair( clientID, objectID ) ); |
---|
| 396 | } |
---|
| 397 | |
---|
| 398 | int ConnectionManager::getClientsShipID( int clientID ) { |
---|
| 399 | return clientsShip[clientID]; |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | int ConnectionManager::getObjectsClientID( int objectID ) { |
---|
| 403 | std::map<int, int>::iterator iter; |
---|
| 404 | for( iter = clientsShip.begin(); iter != clientsShip.end(); iter++ ) { |
---|
| 405 | if( iter->second == objectID ) return iter->first; |
---|
| 406 | } |
---|
| 407 | return -99; |
---|
| 408 | } |
---|
| 409 | |
---|
| 410 | void ConnectionManager::deleteClientIDReg( int clientID ) { |
---|
| 411 | clientsShip.erase( clientID ); |
---|
| 412 | } |
---|
| 413 | |
---|
| 414 | void ConnectionManager::deleteObjectIDReg( int objectID ) { |
---|
| 415 | std::map<int, int>::iterator iter = clientsShip.begin(); |
---|
| 416 | for( iter = clientsShip.begin(); iter != clientsShip.end(); iter++ ) { |
---|
| 417 | if( iter->second == objectID ) break; |
---|
| 418 | } |
---|
| 419 | clientsShip.erase( iter->first ); |
---|
| 420 | }*/ |
---|
| 421 | } |
---|