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