| 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 "ConnectionManager.h" |
|---|
| 41 | |
|---|
| 42 | #include <iostream> |
|---|
| 43 | #include <assert.h> |
|---|
| 44 | // boost.thread library for multithreading support |
|---|
| 45 | #include <boost/thread/thread.hpp> |
|---|
| 46 | #include <boost/bind.hpp> |
|---|
| 47 | |
|---|
| 48 | #include "util/Math.h" |
|---|
| 49 | #include "util/Sleep.h" |
|---|
| 50 | #include "ClientInformation.h" |
|---|
| 51 | #include "synchronisable/Synchronisable.h" |
|---|
| 52 | #include "packet/ClassID.h" |
|---|
| 53 | |
|---|
| 54 | namespace std |
|---|
| 55 | { |
|---|
| 56 | bool operator< (ENetAddress a, ENetAddress b) { |
|---|
| 57 | if(a.host <= b.host) |
|---|
| 58 | return true; |
|---|
| 59 | else |
|---|
| 60 | return false; |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | namespace orxonox |
|---|
| 65 | { |
|---|
| 66 | //boost::thread_group network_threads; |
|---|
| 67 | |
|---|
| 68 | ConnectionManager *ConnectionManager::instance_=0; |
|---|
| 69 | |
|---|
| 70 | ConnectionManager::ConnectionManager():receiverThread_(0){ |
|---|
| 71 | assert(instance_==0); |
|---|
| 72 | instance_=this; |
|---|
| 73 | quit=false; |
|---|
| 74 | bindAddress.host = ENET_HOST_ANY; |
|---|
| 75 | bindAddress.port = NETWORK_PORT; |
|---|
| 76 | } |
|---|
| 77 | boost::recursive_mutex ConnectionManager::enet_mutex; |
|---|
| 78 | |
|---|
| 79 | ConnectionManager::ConnectionManager(int port){ |
|---|
| 80 | assert(instance_==0); |
|---|
| 81 | instance_=this; |
|---|
| 82 | quit=false; |
|---|
| 83 | bindAddress.host = ENET_HOST_ANY; |
|---|
| 84 | bindAddress.port = port; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | ConnectionManager::ConnectionManager(int port, const std::string& address) :receiverThread_(0) { |
|---|
| 88 | assert(instance_==0); |
|---|
| 89 | instance_=this; |
|---|
| 90 | quit=false; |
|---|
| 91 | enet_address_set_host (& bindAddress, address.c_str()); |
|---|
| 92 | bindAddress.port = NETWORK_PORT; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | ConnectionManager::ConnectionManager(int port, const char *address) : receiverThread_(0) { |
|---|
| 96 | assert(instance_==0); |
|---|
| 97 | instance_=this; |
|---|
| 98 | quit=false; |
|---|
| 99 | enet_address_set_host (& bindAddress, address); |
|---|
| 100 | bindAddress.port = NETWORK_PORT; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | ConnectionManager::~ConnectionManager(){ |
|---|
| 104 | if(!quit) |
|---|
| 105 | quitListener(); |
|---|
| 106 | instance_=0; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | ENetEvent *ConnectionManager::getEvent(){ |
|---|
| 111 | if(!buffer.isEmpty()) |
|---|
| 112 | return buffer.pop(); |
|---|
| 113 | else |
|---|
| 114 | return NULL; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | bool ConnectionManager::queueEmpty() { |
|---|
| 118 | return buffer.isEmpty(); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | void ConnectionManager::createListener() { |
|---|
| 122 | receiverThread_ = new boost::thread(boost::bind(&ConnectionManager::receiverThread, this)); |
|---|
| 123 | return; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | bool ConnectionManager::quitListener() { |
|---|
| 127 | quit=true; |
|---|
| 128 | receiverThread_->join(); |
|---|
| 129 | return true; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer) { |
|---|
| 134 | boost::recursive_mutex::scoped_lock lock(ConnectionManager::enet_mutex); |
|---|
| 135 | if(enet_peer_send(peer, NETWORK_DEFAULT_CHANNEL, packet)!=0) |
|---|
| 136 | return false; |
|---|
| 137 | return true; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | bool ConnectionManager::addPacket(ENetPacket *packet, int clientID) { |
|---|
| 141 | ClientInformation *temp = ClientInformation::findClient(clientID); |
|---|
| 142 | if(!temp){ |
|---|
| 143 | COUT(3) << "C.Man: addPacket findClient failed" << std::endl; |
|---|
| 144 | return false; |
|---|
| 145 | } |
|---|
| 146 | return addPacket(packet, temp->getPeer()); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | bool ConnectionManager::addPacketAll(ENetPacket *packet) { |
|---|
| 150 | if(!instance_) |
|---|
| 151 | return false; |
|---|
| 152 | boost::recursive_mutex::scoped_lock lock(ConnectionManager::enet_mutex); |
|---|
| 153 | for(ClientInformation *i=ClientInformation::getBegin()->next(); i!=0; i=i->next()){ |
|---|
| 154 | COUT(3) << "adding broadcast packet for client: " << i->getID() << std::endl; |
|---|
| 155 | if(enet_peer_send(i->getPeer(), 0, packet)!=0) |
|---|
| 156 | return false; |
|---|
| 157 | } |
|---|
| 158 | return true; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | // we actually dont need that function, because host_service does that for us |
|---|
| 162 | bool ConnectionManager::sendPackets() { |
|---|
| 163 | if(server==NULL || !instance_) |
|---|
| 164 | return false; |
|---|
| 165 | boost::recursive_mutex::scoped_lock lock(ConnectionManager::enet_mutex); |
|---|
| 166 | enet_host_flush(server); |
|---|
| 167 | lock.unlock(); |
|---|
| 168 | return true; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | void ConnectionManager::receiverThread() { |
|---|
| 172 | // what about some error-handling here ? |
|---|
| 173 | ENetEvent *event; |
|---|
| 174 | atexit(enet_deinitialize); |
|---|
| 175 | { //scope of the mutex |
|---|
| 176 | boost::recursive_mutex::scoped_lock lock(ConnectionManager::enet_mutex); |
|---|
| 177 | enet_initialize(); |
|---|
| 178 | server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0); |
|---|
| 179 | lock.unlock(); |
|---|
| 180 | } |
|---|
| 181 | if(server==NULL){ |
|---|
| 182 | // add some error handling here ========================== |
|---|
| 183 | quit=true; |
|---|
| 184 | return; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | event = new ENetEvent; |
|---|
| 188 | while(!quit){ |
|---|
| 189 | { //mutex scope |
|---|
| 190 | boost::recursive_mutex::scoped_lock lock(ConnectionManager::enet_mutex); |
|---|
| 191 | if(enet_host_service(server, event, NETWORK_WAIT_TIMEOUT)<0){ |
|---|
| 192 | // we should never reach this point |
|---|
| 193 | quit=true; |
|---|
| 194 | continue; |
|---|
| 195 | // add some error handling here ======================== |
|---|
| 196 | } |
|---|
| 197 | lock.unlock(); |
|---|
| 198 | } |
|---|
| 199 | switch(event->type){ |
|---|
| 200 | // log handling ================ |
|---|
| 201 | case ENET_EVENT_TYPE_CONNECT: |
|---|
| 202 | case ENET_EVENT_TYPE_DISCONNECT: |
|---|
| 203 | case ENET_EVENT_TYPE_RECEIVE: |
|---|
| 204 | processData(event); |
|---|
| 205 | event = new ENetEvent; |
|---|
| 206 | break; |
|---|
| 207 | case ENET_EVENT_TYPE_NONE: |
|---|
| 208 | //receiverThread_->yield(); |
|---|
| 209 | msleep(1); |
|---|
| 210 | break; |
|---|
| 211 | } |
|---|
| 212 | // usleep(100); |
|---|
| 213 | //receiverThread_->yield(); //TODO: find apropriate |
|---|
| 214 | } |
|---|
| 215 | disconnectClients(); |
|---|
| 216 | // if we're finishied, destroy server |
|---|
| 217 | { |
|---|
| 218 | boost::recursive_mutex::scoped_lock lock(ConnectionManager::enet_mutex); |
|---|
| 219 | enet_host_destroy(server); |
|---|
| 220 | lock.unlock(); |
|---|
| 221 | } |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | //### added some bugfixes here, but we cannot test them because |
|---|
| 225 | //### the server crashes everytime because of some gamestates |
|---|
| 226 | //### (trying to resolve that now) |
|---|
| 227 | void ConnectionManager::disconnectClients() { |
|---|
| 228 | ENetEvent event; |
|---|
| 229 | ClientInformation *temp = ClientInformation::getBegin()->next(); |
|---|
| 230 | while(temp!=0){ |
|---|
| 231 | { |
|---|
| 232 | boost::recursive_mutex::scoped_lock lock(ConnectionManager::enet_mutex); |
|---|
| 233 | enet_peer_disconnect(temp->getPeer(), 0); |
|---|
| 234 | lock.unlock(); |
|---|
| 235 | } |
|---|
| 236 | temp = temp->next(); |
|---|
| 237 | } |
|---|
| 238 | //bugfix: might be the reason why server crashes when clients disconnects |
|---|
| 239 | temp = ClientInformation::getBegin()->next(); |
|---|
| 240 | boost::recursive_mutex::scoped_lock lock(ConnectionManager::enet_mutex); |
|---|
| 241 | while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) >= 0){ |
|---|
| 242 | switch (event.type) |
|---|
| 243 | { |
|---|
| 244 | case ENET_EVENT_TYPE_NONE: break; |
|---|
| 245 | case ENET_EVENT_TYPE_CONNECT: break; |
|---|
| 246 | case ENET_EVENT_TYPE_RECEIVE: |
|---|
| 247 | enet_packet_destroy(event.packet); |
|---|
| 248 | break; |
|---|
| 249 | case ENET_EVENT_TYPE_DISCONNECT: |
|---|
| 250 | COUT(4) << "disconnecting all clients" << std::endl; |
|---|
| 251 | if(ClientInformation::findClient(&(event.peer->address))) |
|---|
| 252 | delete ClientInformation::findClient(&(event.peer->address)); |
|---|
| 253 | //maybe needs bugfix: might also be a reason for the server to crash |
|---|
| 254 | temp = temp->next(); |
|---|
| 255 | break; |
|---|
| 256 | } |
|---|
| 257 | } |
|---|
| 258 | return; |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | bool ConnectionManager::processData(ENetEvent *event) { |
|---|
| 262 | // just add packet to the buffer |
|---|
| 263 | // this can be extended with some preprocessing |
|---|
| 264 | return buffer.push(event); |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | int ConnectionManager::getClientID(ENetPeer peer) { |
|---|
| 270 | return getClientID(peer.address); |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | int ConnectionManager::getClientID(ENetAddress address) { |
|---|
| 274 | return ClientInformation::findClient(&address)->getID(); |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | ENetPeer *ConnectionManager::getClientPeer(int clientID) { |
|---|
| 278 | return ClientInformation::findClient(clientID)->getPeer(); |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | void ConnectionManager::syncClassid(unsigned int clientID) { |
|---|
| 283 | int failures=0; |
|---|
| 284 | packet::ClassID *classid = new packet::ClassID(); |
|---|
| 285 | classid->setClientID(clientID); |
|---|
| 286 | while(!classid->send() && failures < 10){ |
|---|
| 287 | failures++; |
|---|
| 288 | } |
|---|
| 289 | assert(failures<10); |
|---|
| 290 | COUT(4) << "syncClassid:\tall synchClassID packets have been sent" << std::endl; |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | void ConnectionManager::disconnectClient(ClientInformation *client){ |
|---|
| 295 | { |
|---|
| 296 | boost::recursive_mutex::scoped_lock lock(ConnectionManager::enet_mutex); |
|---|
| 297 | enet_peer_disconnect(client->getPeer(), 0); |
|---|
| 298 | lock.unlock(); |
|---|
| 299 | } |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | } |
|---|