[1056] | 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 | */ |
---|
[514] | 28 | |
---|
[173] | 29 | // |
---|
| 30 | // C++ Interface: ConnectionManager |
---|
| 31 | // |
---|
[285] | 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 |
---|
[173] | 34 | // connection processes is provided by ... |
---|
| 35 | // |
---|
| 36 | // |
---|
[196] | 37 | // Author: Oliver Scheuss |
---|
[173] | 38 | // |
---|
| 39 | |
---|
[777] | 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 "ClientInformation.h" |
---|
[285] | 47 | #include "ConnectionManager.h" |
---|
[173] | 48 | |
---|
[777] | 49 | namespace std |
---|
| 50 | { |
---|
| 51 | bool operator< (ENetAddress a, ENetAddress b) { |
---|
[381] | 52 | if(a.host <= b.host) |
---|
| 53 | return true; |
---|
| 54 | else |
---|
| 55 | return false; |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | |
---|
[777] | 59 | namespace network |
---|
| 60 | { |
---|
[196] | 61 | boost::thread_group network_threads; |
---|
[1098] | 62 | |
---|
| 63 | ConnectionManager::ConnectionManager(){} |
---|
| 64 | |
---|
[777] | 65 | ConnectionManager::ConnectionManager(ClientInformation *head) { |
---|
[173] | 66 | quit=false; |
---|
[196] | 67 | bindAddress.host = ENET_HOST_ANY; |
---|
[173] | 68 | bindAddress.port = NETWORK_PORT; |
---|
[436] | 69 | head_ = head; |
---|
[173] | 70 | } |
---|
[285] | 71 | |
---|
[777] | 72 | ConnectionManager::ConnectionManager(int port, std::string address, ClientInformation *head) { |
---|
[173] | 73 | quit=false; |
---|
[230] | 74 | enet_address_set_host (& bindAddress, address.c_str()); |
---|
[173] | 75 | bindAddress.port = NETWORK_PORT; |
---|
[436] | 76 | head_ = head; |
---|
[173] | 77 | } |
---|
[285] | 78 | |
---|
[777] | 79 | ConnectionManager::ConnectionManager(int port, const char *address, ClientInformation *head) { |
---|
[230] | 80 | quit=false; |
---|
| 81 | enet_address_set_host (& bindAddress, address); |
---|
| 82 | bindAddress.port = NETWORK_PORT; |
---|
[436] | 83 | head_ = head; |
---|
[230] | 84 | } |
---|
[285] | 85 | |
---|
[777] | 86 | ENetPacket *ConnectionManager::getPacket(ENetAddress &address) { |
---|
[196] | 87 | if(!buffer.isEmpty()) |
---|
[204] | 88 | return buffer.pop(address); |
---|
[196] | 89 | else |
---|
[777] | 90 | return NULL; |
---|
[173] | 91 | } |
---|
[1098] | 92 | /** |
---|
| 93 | This function only pops the first element in PacketBuffer (first in first out) |
---|
| 94 | used by processQueue in Server.cc |
---|
| 95 | */ |
---|
[777] | 96 | ENetPacket *ConnectionManager::getPacket(int &clientID) { |
---|
[380] | 97 | ENetAddress address; |
---|
[446] | 98 | ENetPacket *packet=getPacket(address); |
---|
[444] | 99 | ClientInformation *temp =head_->findClient(&address); |
---|
| 100 | clientID=temp->getID(); |
---|
[380] | 101 | return packet; |
---|
| 102 | } |
---|
[285] | 103 | |
---|
[777] | 104 | bool ConnectionManager::queueEmpty() { |
---|
[196] | 105 | return buffer.isEmpty(); |
---|
[173] | 106 | } |
---|
[285] | 107 | |
---|
[777] | 108 | void ConnectionManager::createListener() { |
---|
[196] | 109 | network_threads.create_thread(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this)); |
---|
[777] | 110 | // boost::thread thr(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this)); |
---|
[196] | 111 | return; |
---|
| 112 | } |
---|
[285] | 113 | |
---|
[777] | 114 | bool ConnectionManager::quitListener() { |
---|
[173] | 115 | quit=true; |
---|
[196] | 116 | network_threads.join_all(); |
---|
[173] | 117 | return true; |
---|
| 118 | } |
---|
[285] | 119 | |
---|
[777] | 120 | bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer) { |
---|
[1064] | 121 | if(enet_peer_send(peer, (enet_uint8)head_->findClient(&(peer->address))->getID() , packet)!=0) |
---|
[196] | 122 | return false; |
---|
| 123 | return true; |
---|
| 124 | } |
---|
[514] | 125 | |
---|
[777] | 126 | bool ConnectionManager::addPacket(ENetPacket *packet, int clientID) { |
---|
[1064] | 127 | if(enet_peer_send(head_->findClient(clientID)->getPeer(), (enet_uint8)clientID, packet)!=0) |
---|
[196] | 128 | return false; |
---|
[380] | 129 | return true; |
---|
[196] | 130 | } |
---|
[514] | 131 | |
---|
[777] | 132 | bool ConnectionManager::addPacketAll(ENetPacket *packet) { |
---|
[436] | 133 | for(ClientInformation *i=head_->next(); i!=0; i=i->next()){ |
---|
[1064] | 134 | if(enet_peer_send(i->getPeer(), (enet_uint8)i->getID(), packet)!=0) |
---|
[777] | 135 | return false; |
---|
[196] | 136 | } |
---|
| 137 | return true; |
---|
| 138 | } |
---|
[285] | 139 | |
---|
[777] | 140 | bool ConnectionManager::sendPackets(ENetEvent *event) { |
---|
[196] | 141 | if(server==NULL) |
---|
| 142 | return false; |
---|
| 143 | if(enet_host_service(server, event, NETWORK_SEND_WAIT)>=0) |
---|
| 144 | return true; |
---|
[285] | 145 | else |
---|
[196] | 146 | return false; |
---|
| 147 | } |
---|
[514] | 148 | |
---|
[777] | 149 | bool ConnectionManager::sendPackets() { |
---|
[369] | 150 | ENetEvent event; |
---|
| 151 | if(server==NULL) |
---|
| 152 | return false; |
---|
| 153 | if(enet_host_service(server, &event, NETWORK_SEND_WAIT)>=0) |
---|
| 154 | return true; |
---|
| 155 | else |
---|
| 156 | return false; |
---|
| 157 | } |
---|
[285] | 158 | |
---|
[777] | 159 | void ConnectionManager::receiverThread() { |
---|
[196] | 160 | // what about some error-handling here ? |
---|
| 161 | enet_initialize(); |
---|
| 162 | atexit(enet_deinitialize); |
---|
[173] | 163 | ENetEvent event; |
---|
| 164 | server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0); |
---|
[369] | 165 | if(server==NULL){ |
---|
[173] | 166 | // add some error handling here ========================== |
---|
| 167 | quit=true; |
---|
[369] | 168 | return; |
---|
| 169 | } |
---|
[285] | 170 | |
---|
[173] | 171 | while(!quit){ |
---|
| 172 | if(enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT)<0){ |
---|
| 173 | // we should never reach this point |
---|
| 174 | quit=true; |
---|
| 175 | // add some error handling here ======================== |
---|
| 176 | } |
---|
| 177 | switch(event.type){ |
---|
| 178 | // log handling ================ |
---|
[196] | 179 | case ENET_EVENT_TYPE_CONNECT: |
---|
[777] | 180 | addClient(&event); |
---|
[1098] | 181 | COUT(5) << "Con.Man: connection event has occured" << std::endl; |
---|
[777] | 182 | break; |
---|
| 183 | case ENET_EVENT_TYPE_RECEIVE: |
---|
| 184 | //std::cout << "received data" << std::endl; |
---|
[1098] | 185 | COUT(5) << "Con.Man: receive event has occured" << std::endl; |
---|
[777] | 186 | processData(&event); |
---|
| 187 | break; |
---|
| 188 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
| 189 | // add some error/log handling here |
---|
| 190 | clientDisconnect(event.peer); |
---|
| 191 | break; |
---|
| 192 | case ENET_EVENT_TYPE_NONE: |
---|
| 193 | break; |
---|
[173] | 194 | } |
---|
| 195 | } |
---|
[369] | 196 | disconnectClients(); |
---|
[173] | 197 | // if we're finishied, destroy server |
---|
| 198 | enet_host_destroy(server); |
---|
| 199 | } |
---|
[1098] | 200 | |
---|
| 201 | //### added some bugfixes here, but we cannot test them because |
---|
| 202 | //### the server crashes everytime because of some gamestates |
---|
| 203 | //### (trying to resolve that now) |
---|
[777] | 204 | void ConnectionManager::disconnectClients() { |
---|
[369] | 205 | ENetEvent event; |
---|
[436] | 206 | ClientInformation *temp = head_->next(); |
---|
| 207 | while(temp!=0){ |
---|
| 208 | enet_peer_disconnect(temp->getPeer(), 0); |
---|
| 209 | temp = temp->next(); |
---|
| 210 | } |
---|
[1098] | 211 | //bugfix: might be the reason why server crashes when clients disconnects |
---|
| 212 | //temp = temp->next(); |
---|
| 213 | temp = head_->next(); |
---|
[436] | 214 | while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) > 0){ |
---|
| 215 | switch (event.type) |
---|
| 216 | { |
---|
[1098] | 217 | case ENET_EVENT_TYPE_NONE: break; |
---|
| 218 | case ENET_EVENT_TYPE_CONNECT: break; |
---|
[777] | 219 | case ENET_EVENT_TYPE_RECEIVE: |
---|
| 220 | enet_packet_destroy(event.packet); |
---|
| 221 | break; |
---|
| 222 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
[1098] | 223 | COUT(4) << "disconnecting all clients" << std::endl; |
---|
[777] | 224 | delete head_->findClient(&(event.peer->address)); |
---|
[1098] | 225 | //maybe needs bugfix: might also be a reason for the server to crash |
---|
[777] | 226 | temp = temp->next(); |
---|
| 227 | break; |
---|
[369] | 228 | } |
---|
| 229 | } |
---|
| 230 | return; |
---|
| 231 | } |
---|
[285] | 232 | |
---|
[777] | 233 | bool ConnectionManager::processData(ENetEvent *event) { |
---|
[196] | 234 | // just add packet to the buffer |
---|
| 235 | // this can be extended with some preprocessing |
---|
[204] | 236 | return buffer.push(event); |
---|
[173] | 237 | } |
---|
[285] | 238 | |
---|
[777] | 239 | //bool ConnectionManager::clientDisconnect(ENetPeer *peer) { |
---|
| 240 | // return clientDisconnect(*peer); |
---|
| 241 | //} |
---|
[514] | 242 | |
---|
[777] | 243 | bool ConnectionManager::clientDisconnect(ENetPeer *peer) { |
---|
[1098] | 244 | COUT(4) << "removing client from list" << std::endl; |
---|
[436] | 245 | return head_->removeClient(peer); |
---|
[380] | 246 | } |
---|
[1098] | 247 | /** |
---|
| 248 | This function adds a client that connects to the clientlist of the server |
---|
| 249 | NOTE: if you change this, don't forget to change the test function |
---|
| 250 | addClientTest in diffTest.cc since addClient is not good for testing because of syncClassid |
---|
| 251 | */ |
---|
[777] | 252 | bool ConnectionManager::addClient(ENetEvent *event) { |
---|
[436] | 253 | ClientInformation *temp = head_->insertBack(new ClientInformation); |
---|
[1098] | 254 | if(temp->prev()->head) { //not good if you use anything else than insertBack |
---|
| 255 | temp->prev()->setID(0); //bugfix: not necessary but usefull |
---|
[620] | 256 | temp->setID(1); |
---|
[1098] | 257 | } |
---|
[620] | 258 | else |
---|
| 259 | temp->setID(temp->prev()->getID()+1); |
---|
[436] | 260 | temp->setPeer(event->peer); |
---|
[1098] | 261 | COUT(4) << "Con.Man: added client id: " << temp->getID() << std::endl; |
---|
[636] | 262 | syncClassid(temp->getID()); |
---|
| 263 | temp->setSynched(true); |
---|
[173] | 264 | return true; |
---|
| 265 | } |
---|
[514] | 266 | |
---|
[777] | 267 | int ConnectionManager::getClientID(ENetPeer peer) { |
---|
[436] | 268 | return getClientID(peer.address); |
---|
[380] | 269 | } |
---|
[514] | 270 | |
---|
[777] | 271 | int ConnectionManager::getClientID(ENetAddress address) { |
---|
[436] | 272 | return head_->findClient(&address)->getID(); |
---|
[380] | 273 | } |
---|
[514] | 274 | |
---|
[777] | 275 | ENetPeer *ConnectionManager::getClientPeer(int clientID) { |
---|
[436] | 276 | return head_->findClient(clientID)->getPeer(); |
---|
[380] | 277 | } |
---|
[514] | 278 | |
---|
[777] | 279 | void ConnectionManager::syncClassid(int clientID) { |
---|
[1021] | 280 | unsigned int network_id=0; |
---|
[400] | 281 | std::string classname; |
---|
| 282 | orxonox::Identifier *id; |
---|
[1021] | 283 | std::map<std::string, orxonox::Identifier*>::const_iterator it = orxonox::Factory::getFactoryBegin(); |
---|
| 284 | while(it != orxonox::Factory::getFactoryEnd()){ |
---|
| 285 | id = (*it).second; |
---|
| 286 | if(id == NULL) |
---|
| 287 | continue; |
---|
| 288 | classname = id->getName(); |
---|
| 289 | network_id = id->getNetworkID(); |
---|
[1098] | 290 | COUT(4) << "Con.Man:syncClassid:\tnetwork_id: " << network_id << ", classname: " << classname << std::endl; |
---|
[1056] | 291 | |
---|
[1021] | 292 | addPacket(packet_gen.clid( (int)network_id, classname ), clientID); |
---|
[1056] | 293 | |
---|
[1021] | 294 | ++it; |
---|
[400] | 295 | } |
---|
| 296 | sendPackets(); |
---|
[1098] | 297 | COUT(4) << "syncClassid:\tall synchClassID packets have been sent" << std::endl; |
---|
[400] | 298 | } |
---|
[514] | 299 | |
---|
[1056] | 300 | |
---|
| 301 | |
---|
[1021] | 302 | void ConnectionManager::addClientsObjectID( int clientID, int objectID ) { |
---|
| 303 | COUT(4) << "ship of client: " << clientID << ": " << objectID << " mapped" << std::endl; |
---|
| 304 | clientsShip.insert( std::make_pair( clientID, objectID ) ); |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | int ConnectionManager::getClientsShipID( int clientID ) { |
---|
| 308 | return clientsShip[clientID]; |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | int ConnectionManager::getObjectsClientID( int objectID ) { |
---|
[1098] | 312 | std::map<int, int>::iterator iter; |
---|
| 313 | for( iter = clientsShip.begin(); iter != clientsShip.end(); iter++ ) { |
---|
[1021] | 314 | if( iter->second == objectID ) return iter->first; |
---|
| 315 | } |
---|
| 316 | return -99; |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | void ConnectionManager::deleteClientIDReg( int clientID ) { |
---|
| 320 | clientsShip.erase( clientID ); |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | void ConnectionManager::deleteObjectIDReg( int objectID ) { |
---|
| 324 | std::map<int, int>::iterator iter = clientsShip.begin(); |
---|
[1098] | 325 | for( iter = clientsShip.begin(); iter != clientsShip.end(); iter++ ) { |
---|
[1056] | 326 | if( iter->second == objectID ) break; |
---|
[1021] | 327 | } |
---|
| 328 | clientsShip.erase( iter->first ); |
---|
| 329 | } |
---|
[1098] | 330 | int ConnectionManager::getNumberOfClients() { |
---|
| 331 | return clientsShip.size(); |
---|
| 332 | } |
---|
[173] | 333 | } |
---|