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