[1282] | 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 | */ |
---|
| 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 | |
---|
[1842] | 40 | #include "ConnectionManager.h" |
---|
| 41 | |
---|
[1282] | 42 | #include <iostream> |
---|
[1735] | 43 | #include <assert.h> |
---|
[1282] | 44 | // boost.thread library for multithreading support |
---|
[1755] | 45 | #include <boost/thread/thread.hpp> |
---|
[1282] | 46 | #include <boost/bind.hpp> |
---|
| 47 | |
---|
| 48 | #include "core/CoreIncludes.h" |
---|
| 49 | #include "core/BaseObject.h" |
---|
[1747] | 50 | #include "core/Iterator.h" |
---|
[1502] | 51 | #include "objects/SpaceShip.h" |
---|
[1282] | 52 | #include "util/Math.h" |
---|
[1502] | 53 | #include "util/Sleep.h" |
---|
[1282] | 54 | #include "ClientInformation.h" |
---|
| 55 | #include "Synchronisable.h" |
---|
[1735] | 56 | #include "packet/ClassID.h" |
---|
[1282] | 57 | |
---|
| 58 | namespace std |
---|
| 59 | { |
---|
| 60 | bool operator< (ENetAddress a, ENetAddress b) { |
---|
| 61 | if(a.host <= b.host) |
---|
| 62 | return true; |
---|
| 63 | else |
---|
| 64 | return false; |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | namespace network |
---|
| 69 | { |
---|
| 70 | //boost::thread_group network_threads; |
---|
[1747] | 71 | |
---|
[1735] | 72 | ConnectionManager *ConnectionManager::instance_=0; |
---|
[1747] | 73 | |
---|
[1735] | 74 | ConnectionManager::ConnectionManager():receiverThread_(0){ |
---|
| 75 | assert(instance_==0); |
---|
| 76 | instance_=this; |
---|
[1282] | 77 | quit=false; |
---|
| 78 | bindAddress.host = ENET_HOST_ANY; |
---|
| 79 | bindAddress.port = NETWORK_PORT; |
---|
| 80 | } |
---|
[1735] | 81 | boost::recursive_mutex ConnectionManager::enet_mutex; |
---|
[1747] | 82 | |
---|
[1735] | 83 | // ConnectionManager::ConnectionManager(ClientInformation *head) : receiverThread_(0) { |
---|
| 84 | // assert(instance_==0); |
---|
| 85 | // instance_=this; |
---|
| 86 | // quit=false; |
---|
| 87 | // bindAddress.host = ENET_HOST_ANY; |
---|
| 88 | // bindAddress.port = NETWORK_PORT; |
---|
| 89 | // } |
---|
[1747] | 90 | |
---|
[1735] | 91 | ConnectionManager::ConnectionManager(int port){ |
---|
| 92 | assert(instance_==0); |
---|
| 93 | instance_=this; |
---|
[1502] | 94 | quit=false; |
---|
| 95 | bindAddress.host = ENET_HOST_ANY; |
---|
| 96 | bindAddress.port = port; |
---|
| 97 | } |
---|
[1282] | 98 | |
---|
[1735] | 99 | ConnectionManager::ConnectionManager(int port, std::string address) :receiverThread_(0) { |
---|
| 100 | assert(instance_==0); |
---|
| 101 | instance_=this; |
---|
[1282] | 102 | quit=false; |
---|
| 103 | enet_address_set_host (& bindAddress, address.c_str()); |
---|
| 104 | bindAddress.port = NETWORK_PORT; |
---|
| 105 | } |
---|
| 106 | |
---|
[1735] | 107 | ConnectionManager::ConnectionManager(int port, const char *address) : receiverThread_(0) { |
---|
| 108 | assert(instance_==0); |
---|
| 109 | instance_=this; |
---|
[1282] | 110 | quit=false; |
---|
| 111 | enet_address_set_host (& bindAddress, address); |
---|
| 112 | bindAddress.port = NETWORK_PORT; |
---|
| 113 | } |
---|
[1747] | 114 | |
---|
[1735] | 115 | ConnectionManager::~ConnectionManager(){ |
---|
| 116 | instance_=0; |
---|
| 117 | if(!quit) |
---|
| 118 | quitListener(); |
---|
| 119 | } |
---|
[1282] | 120 | |
---|
[1502] | 121 | /*ENetPacket *ConnectionManager::getPacket(ENetAddress &address) { |
---|
[1282] | 122 | if(!buffer.isEmpty()) |
---|
| 123 | return buffer.pop(address); |
---|
| 124 | else |
---|
| 125 | return NULL; |
---|
[1502] | 126 | }*/ |
---|
[1282] | 127 | /** |
---|
| 128 | This function only pops the first element in PacketBuffer (first in first out) |
---|
| 129 | used by processQueue in Server.cc |
---|
| 130 | */ |
---|
[1502] | 131 | /*ENetPacket *ConnectionManager::getPacket(int &clientID) { |
---|
[1282] | 132 | ENetAddress address; |
---|
| 133 | ENetPacket *packet=getPacket(address); |
---|
| 134 | ClientInformation *temp =head_->findClient(&address); |
---|
[1360] | 135 | if(!temp) |
---|
| 136 | return NULL; |
---|
[1282] | 137 | clientID=temp->getID(); |
---|
| 138 | return packet; |
---|
[1502] | 139 | }*/ |
---|
[1747] | 140 | |
---|
[1502] | 141 | ENetEvent *ConnectionManager::getEvent(){ |
---|
| 142 | if(!buffer.isEmpty()) |
---|
| 143 | return buffer.pop(); |
---|
| 144 | else |
---|
| 145 | return NULL; |
---|
[1282] | 146 | } |
---|
| 147 | |
---|
| 148 | bool ConnectionManager::queueEmpty() { |
---|
| 149 | return buffer.isEmpty(); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | void ConnectionManager::createListener() { |
---|
| 153 | receiverThread_ = new boost::thread(boost::bind(&ConnectionManager::receiverThread, this)); |
---|
| 154 | //network_threads.create_thread(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this)); |
---|
| 155 | //boost::thread thr(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this)); |
---|
| 156 | return; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | bool ConnectionManager::quitListener() { |
---|
| 160 | quit=true; |
---|
| 161 | //network_threads.join_all(); |
---|
| 162 | receiverThread_->join(); |
---|
| 163 | return true; |
---|
| 164 | } |
---|
[1747] | 165 | |
---|
[1735] | 166 | // bool ConnectionManager::addPacket(Packet::Packet *packet){ |
---|
| 167 | // ClientInformation *temp = instance_->head_->findClient(packet->getClientID()); |
---|
| 168 | // if(!temp){ |
---|
| 169 | // COUT(3) << "C.Man: addPacket findClient failed" << std::endl; |
---|
| 170 | // return false; |
---|
| 171 | // } |
---|
| 172 | // ENetPacket *packet = new ENetPacket; |
---|
| 173 | // // TODO: finish implementation |
---|
| 174 | // } |
---|
[1747] | 175 | // |
---|
| 176 | |
---|
[1282] | 177 | bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer) { |
---|
[1735] | 178 | boost::recursive_mutex::scoped_lock lock(instance_->enet_mutex); |
---|
| 179 | if(enet_peer_send(peer, NETWORK_DEFAULT_CHANNEL, packet)!=0) |
---|
[1282] | 180 | return false; |
---|
| 181 | return true; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | bool ConnectionManager::addPacket(ENetPacket *packet, int clientID) { |
---|
[1735] | 185 | ClientInformation *temp = ClientInformation::findClient(clientID); |
---|
[1502] | 186 | if(!temp){ |
---|
| 187 | COUT(3) << "C.Man: addPacket findClient failed" << std::endl; |
---|
[1282] | 188 | return false; |
---|
[1502] | 189 | } |
---|
[1735] | 190 | return addPacket(packet, temp->getPeer()); |
---|
[1282] | 191 | } |
---|
| 192 | |
---|
| 193 | bool ConnectionManager::addPacketAll(ENetPacket *packet) { |
---|
[1735] | 194 | if(!instance_) |
---|
| 195 | return false; |
---|
| 196 | boost::recursive_mutex::scoped_lock lock(instance_->enet_mutex); |
---|
| 197 | for(ClientInformation *i=ClientInformation::getBegin()->next(); i!=0; i=i->next()){ |
---|
[1534] | 198 | COUT(3) << "adding broadcast packet for client: " << i->getID() << std::endl; |
---|
| 199 | if(enet_peer_send(i->getPeer(), 0, packet)!=0) |
---|
[1282] | 200 | return false; |
---|
| 201 | } |
---|
| 202 | return true; |
---|
| 203 | } |
---|
| 204 | |
---|
[1502] | 205 | // we actually dont need that function, because host_service does that for us |
---|
[1282] | 206 | bool ConnectionManager::sendPackets() { |
---|
[1735] | 207 | if(server==NULL || !instance_) |
---|
[1282] | 208 | return false; |
---|
[1735] | 209 | boost::recursive_mutex::scoped_lock lock(enet_mutex); |
---|
[1502] | 210 | enet_host_flush(server); |
---|
| 211 | lock.unlock(); |
---|
| 212 | return true; |
---|
[1282] | 213 | } |
---|
| 214 | |
---|
| 215 | void ConnectionManager::receiverThread() { |
---|
| 216 | // what about some error-handling here ? |
---|
[1502] | 217 | ENetEvent *event; |
---|
[1282] | 218 | atexit(enet_deinitialize); |
---|
[1502] | 219 | { //scope of the mutex |
---|
[1735] | 220 | boost::recursive_mutex::scoped_lock lock(enet_mutex); |
---|
[1502] | 221 | enet_initialize(); |
---|
| 222 | server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0); |
---|
| 223 | lock.unlock(); |
---|
| 224 | } |
---|
[1282] | 225 | if(server==NULL){ |
---|
| 226 | // add some error handling here ========================== |
---|
| 227 | quit=true; |
---|
| 228 | return; |
---|
| 229 | } |
---|
| 230 | |
---|
[1502] | 231 | event = new ENetEvent; |
---|
[1282] | 232 | while(!quit){ |
---|
[1502] | 233 | { //mutex scope |
---|
[1735] | 234 | boost::recursive_mutex::scoped_lock lock(enet_mutex); |
---|
[1502] | 235 | if(enet_host_service(server, event, NETWORK_WAIT_TIMEOUT)<0){ |
---|
| 236 | // we should never reach this point |
---|
| 237 | quit=true; |
---|
| 238 | continue; |
---|
| 239 | // add some error handling here ======================== |
---|
| 240 | } |
---|
| 241 | lock.unlock(); |
---|
[1282] | 242 | } |
---|
| 243 | switch(event->type){ |
---|
| 244 | // log handling ================ |
---|
| 245 | case ENET_EVENT_TYPE_CONNECT: |
---|
[1502] | 246 | COUT(3) << "adding event_type_connect to queue" << std::endl; |
---|
| 247 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
| 248 | //addClient(event); |
---|
[1282] | 249 | //this is a workaround to ensure thread safety |
---|
[1502] | 250 | //COUT(5) << "Con.Man: connection event has occured" << std::endl; |
---|
| 251 | //break; |
---|
[1282] | 252 | case ENET_EVENT_TYPE_RECEIVE: |
---|
| 253 | //std::cout << "received data" << std::endl; |
---|
| 254 | COUT(5) << "Con.Man: receive event has occured" << std::endl; |
---|
| 255 | // only add, if client has connected yet and not been disconnected |
---|
[1502] | 256 | //if(head_->findClient(&event->peer->address)) |
---|
[1282] | 257 | processData(event); |
---|
[1502] | 258 | event = new ENetEvent; |
---|
| 259 | // else |
---|
| 260 | // COUT(3) << "received a packet from a client we don't know" << std::endl; |
---|
[1282] | 261 | break; |
---|
[1502] | 262 | //case ENET_EVENT_TYPE_DISCONNECT: |
---|
| 263 | //clientDisconnect(event->peer); |
---|
| 264 | //break; |
---|
[1282] | 265 | case ENET_EVENT_TYPE_NONE: |
---|
[1502] | 266 | //receiverThread_->yield(); |
---|
| 267 | usleep(1000); |
---|
[1282] | 268 | break; |
---|
| 269 | } |
---|
| 270 | // usleep(100); |
---|
[1502] | 271 | //receiverThread_->yield(); //TODO: find apropriate |
---|
[1282] | 272 | } |
---|
| 273 | disconnectClients(); |
---|
| 274 | // if we're finishied, destroy server |
---|
[1502] | 275 | { |
---|
[1735] | 276 | boost::recursive_mutex::scoped_lock lock(enet_mutex); |
---|
[1502] | 277 | enet_host_destroy(server); |
---|
| 278 | lock.unlock(); |
---|
| 279 | } |
---|
[1282] | 280 | } |
---|
[1747] | 281 | |
---|
[1282] | 282 | //### added some bugfixes here, but we cannot test them because |
---|
| 283 | //### the server crashes everytime because of some gamestates |
---|
| 284 | //### (trying to resolve that now) |
---|
| 285 | void ConnectionManager::disconnectClients() { |
---|
| 286 | ENetEvent event; |
---|
[1735] | 287 | ClientInformation *temp = ClientInformation::getBegin()->next(); |
---|
[1282] | 288 | while(temp!=0){ |
---|
[1502] | 289 | { |
---|
[1735] | 290 | boost::recursive_mutex::scoped_lock lock(enet_mutex); |
---|
[1502] | 291 | enet_peer_disconnect(temp->getPeer(), 0); |
---|
| 292 | lock.unlock(); |
---|
| 293 | } |
---|
[1282] | 294 | temp = temp->next(); |
---|
| 295 | } |
---|
| 296 | //bugfix: might be the reason why server crashes when clients disconnects |
---|
[1735] | 297 | temp = ClientInformation::getBegin()->next(); |
---|
| 298 | boost::recursive_mutex::scoped_lock lock(enet_mutex); |
---|
[1502] | 299 | while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) >= 0){ |
---|
[1282] | 300 | switch (event.type) |
---|
| 301 | { |
---|
| 302 | case ENET_EVENT_TYPE_NONE: break; |
---|
| 303 | case ENET_EVENT_TYPE_CONNECT: break; |
---|
| 304 | case ENET_EVENT_TYPE_RECEIVE: |
---|
| 305 | enet_packet_destroy(event.packet); |
---|
| 306 | break; |
---|
| 307 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
| 308 | COUT(4) << "disconnecting all clients" << std::endl; |
---|
[1735] | 309 | if(ClientInformation::findClient(&(event.peer->address))) |
---|
| 310 | delete ClientInformation::findClient(&(event.peer->address)); |
---|
[1282] | 311 | //maybe needs bugfix: might also be a reason for the server to crash |
---|
| 312 | temp = temp->next(); |
---|
| 313 | break; |
---|
| 314 | } |
---|
| 315 | } |
---|
| 316 | return; |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | bool ConnectionManager::processData(ENetEvent *event) { |
---|
| 320 | // just add packet to the buffer |
---|
| 321 | // this can be extended with some preprocessing |
---|
| 322 | return buffer.push(event); |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | |
---|
[1502] | 326 | |
---|
[1282] | 327 | int ConnectionManager::getClientID(ENetPeer peer) { |
---|
| 328 | return getClientID(peer.address); |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | int ConnectionManager::getClientID(ENetAddress address) { |
---|
[1735] | 332 | return ClientInformation::findClient(&address)->getID(); |
---|
[1282] | 333 | } |
---|
| 334 | |
---|
| 335 | ENetPeer *ConnectionManager::getClientPeer(int clientID) { |
---|
[1735] | 336 | return ClientInformation::findClient(clientID)->getPeer(); |
---|
[1282] | 337 | } |
---|
| 338 | |
---|
[1735] | 339 | /** |
---|
[1747] | 340 | * |
---|
| 341 | * @param clientID |
---|
[1735] | 342 | */ |
---|
| 343 | void ConnectionManager::syncClassid(unsigned int clientID) { |
---|
[1360] | 344 | unsigned int network_id=0, failures=0; |
---|
[1282] | 345 | std::string classname; |
---|
| 346 | orxonox::Identifier *id; |
---|
[1856] | 347 | std::map<std::string, orxonox::Identifier*>::const_iterator it = orxonox::Factory::getFactoryMapBegin(); |
---|
| 348 | while(it != orxonox::Factory::getFactoryMapEnd()){ |
---|
[1282] | 349 | id = (*it).second; |
---|
| 350 | if(id == NULL) |
---|
| 351 | continue; |
---|
| 352 | classname = id->getName(); |
---|
| 353 | network_id = id->getNetworkID(); |
---|
[1360] | 354 | if(network_id==0) |
---|
| 355 | COUT(3) << "we got a null class id: " << id->getName() << std::endl; |
---|
[1282] | 356 | COUT(4) << "Con.Man:syncClassid:\tnetwork_id: " << network_id << ", classname: " << classname << std::endl; |
---|
| 357 | |
---|
[1735] | 358 | packet::ClassID *classid = new packet::ClassID( network_id, classname ); |
---|
| 359 | classid->setClientID(clientID); |
---|
| 360 | while(!classid->send() && failures < 10){ |
---|
[1360] | 361 | failures++; |
---|
| 362 | } |
---|
[1282] | 363 | ++it; |
---|
| 364 | } |
---|
[1502] | 365 | //sendPackets(); |
---|
[1282] | 366 | COUT(4) << "syncClassid:\tall synchClassID packets have been sent" << std::endl; |
---|
| 367 | } |
---|
| 368 | |
---|
[1747] | 369 | |
---|
| 370 | |
---|
[1282] | 371 | bool ConnectionManager::removeShip(ClientInformation *client){ |
---|
[1735] | 372 | unsigned int id=client->getShipID(); |
---|
[1747] | 373 | orxonox::ObjectList<orxonox::SpaceShip>::iterator it; |
---|
| 374 | for(it = orxonox::ObjectList<orxonox::SpaceShip>::begin(); it; ++it){ |
---|
[1282] | 375 | if(it->objectID!=id) |
---|
| 376 | continue; |
---|
| 377 | delete *it; |
---|
| 378 | } |
---|
| 379 | return true; |
---|
| 380 | } |
---|
[1747] | 381 | |
---|
| 382 | |
---|
[1282] | 383 | void ConnectionManager::disconnectClient(ClientInformation *client){ |
---|
[1502] | 384 | { |
---|
[1735] | 385 | boost::recursive_mutex::scoped_lock lock(enet_mutex); |
---|
[1502] | 386 | enet_peer_disconnect(client->getPeer(), 0); |
---|
| 387 | lock.unlock(); |
---|
| 388 | } |
---|
[1282] | 389 | removeShip(client); |
---|
| 390 | } |
---|
| 391 | |
---|
[1747] | 392 | |
---|
[1282] | 393 | } |
---|