| 1 | /* | 
|---|
| 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 | */ | 
|---|
| 27 |  | 
|---|
| 28 | // | 
|---|
| 29 | // C++ Interface: ConnectionManager | 
|---|
| 30 | // | 
|---|
| 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 | 
|---|
| 33 | // connection processes is provided by ... | 
|---|
| 34 | // | 
|---|
| 35 | // | 
|---|
| 36 | // Author:  Oliver Scheuss | 
|---|
| 37 | // | 
|---|
| 38 |  | 
|---|
| 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" | 
|---|
| 46 | #include "ConnectionManager.h" | 
|---|
| 47 |  | 
|---|
| 48 | namespace std | 
|---|
| 49 | { | 
|---|
| 50 |   bool operator< (ENetAddress a, ENetAddress b) { | 
|---|
| 51 |     if(a.host <= b.host) | 
|---|
| 52 |       return true; | 
|---|
| 53 |     else | 
|---|
| 54 |       return false; | 
|---|
| 55 |   } | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | namespace network | 
|---|
| 59 | { | 
|---|
| 60 |   boost::thread_group network_threads; | 
|---|
| 61 |  | 
|---|
| 62 |   ConnectionManager::ConnectionManager(ClientInformation *head) { | 
|---|
| 63 |     quit=false; | 
|---|
| 64 |     bindAddress.host = ENET_HOST_ANY; | 
|---|
| 65 |     bindAddress.port = NETWORK_PORT; | 
|---|
| 66 |     head_ = head; | 
|---|
| 67 |   } | 
|---|
| 68 |  | 
|---|
| 69 |   ConnectionManager::ConnectionManager(int port, std::string address, ClientInformation *head) { | 
|---|
| 70 |     quit=false; | 
|---|
| 71 |     enet_address_set_host (& bindAddress, address.c_str()); | 
|---|
| 72 |     bindAddress.port = NETWORK_PORT; | 
|---|
| 73 |     head_ = head; | 
|---|
| 74 |   } | 
|---|
| 75 |  | 
|---|
| 76 |   ConnectionManager::ConnectionManager(int port, const char *address, ClientInformation *head) { | 
|---|
| 77 |     quit=false; | 
|---|
| 78 |     enet_address_set_host (& bindAddress, address); | 
|---|
| 79 |     bindAddress.port = NETWORK_PORT; | 
|---|
| 80 |     head_ = head; | 
|---|
| 81 |   } | 
|---|
| 82 |  | 
|---|
| 83 |   ENetPacket *ConnectionManager::getPacket(ENetAddress &address) { | 
|---|
| 84 |     if(!buffer.isEmpty()) | 
|---|
| 85 |       return buffer.pop(address); | 
|---|
| 86 |     else | 
|---|
| 87 |       return NULL; | 
|---|
| 88 |   } | 
|---|
| 89 |  | 
|---|
| 90 |   ENetPacket *ConnectionManager::getPacket(int &clientID) { | 
|---|
| 91 |     ENetAddress address; | 
|---|
| 92 |     ENetPacket *packet=getPacket(address); | 
|---|
| 93 |     ClientInformation *temp =head_->findClient(&address); | 
|---|
| 94 |     clientID=temp->getID(); | 
|---|
| 95 |     return packet; | 
|---|
| 96 |   } | 
|---|
| 97 |  | 
|---|
| 98 |   bool ConnectionManager::queueEmpty() { | 
|---|
| 99 |     return buffer.isEmpty(); | 
|---|
| 100 |   } | 
|---|
| 101 |  | 
|---|
| 102 |   void ConnectionManager::createListener() { | 
|---|
| 103 |     network_threads.create_thread(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this)); | 
|---|
| 104 |     //     boost::thread thr(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this)); | 
|---|
| 105 |     return; | 
|---|
| 106 |   } | 
|---|
| 107 |  | 
|---|
| 108 |   bool ConnectionManager::quitListener() { | 
|---|
| 109 |     quit=true; | 
|---|
| 110 |     network_threads.join_all(); | 
|---|
| 111 |     return true; | 
|---|
| 112 |   } | 
|---|
| 113 |  | 
|---|
| 114 |   bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer) { | 
|---|
| 115 |     if(enet_peer_send(peer, head_->findClient(&(peer->address))->getID() , packet)!=0) | 
|---|
| 116 |       return false; | 
|---|
| 117 |     return true; | 
|---|
| 118 |   } | 
|---|
| 119 |  | 
|---|
| 120 |   bool ConnectionManager::addPacket(ENetPacket *packet, int clientID) { | 
|---|
| 121 |     if(enet_peer_send(head_->findClient(clientID)->getPeer(), clientID, packet)!=0) | 
|---|
| 122 |       return false; | 
|---|
| 123 |     return true; | 
|---|
| 124 |   } | 
|---|
| 125 |  | 
|---|
| 126 |   bool ConnectionManager::addPacketAll(ENetPacket *packet) { | 
|---|
| 127 |     for(ClientInformation *i=head_->next(); i!=0; i=i->next()){ | 
|---|
| 128 |       if(enet_peer_send(i->getPeer(), i->getID(), packet)!=0) | 
|---|
| 129 |         return false; | 
|---|
| 130 |     } | 
|---|
| 131 |     return true; | 
|---|
| 132 |   } | 
|---|
| 133 |  | 
|---|
| 134 |   bool ConnectionManager::sendPackets(ENetEvent *event) { | 
|---|
| 135 |     if(server==NULL) | 
|---|
| 136 |       return false; | 
|---|
| 137 |     if(enet_host_service(server, event, NETWORK_SEND_WAIT)>=0) | 
|---|
| 138 |       return true; | 
|---|
| 139 |     else | 
|---|
| 140 |       return false; | 
|---|
| 141 |   } | 
|---|
| 142 |  | 
|---|
| 143 |   bool ConnectionManager::sendPackets() { | 
|---|
| 144 |     ENetEvent event; | 
|---|
| 145 |     if(server==NULL) | 
|---|
| 146 |       return false; | 
|---|
| 147 |     if(enet_host_service(server, &event, NETWORK_SEND_WAIT)>=0) | 
|---|
| 148 |       return true; | 
|---|
| 149 |     else | 
|---|
| 150 |       return false; | 
|---|
| 151 |   } | 
|---|
| 152 |  | 
|---|
| 153 |   void ConnectionManager::receiverThread() { | 
|---|
| 154 |     // what about some error-handling here ? | 
|---|
| 155 |     enet_initialize(); | 
|---|
| 156 |     atexit(enet_deinitialize); | 
|---|
| 157 |     ENetEvent event; | 
|---|
| 158 |     server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0); | 
|---|
| 159 |     if(server==NULL){ | 
|---|
| 160 |       // add some error handling here ========================== | 
|---|
| 161 |       quit=true; | 
|---|
| 162 |       return; | 
|---|
| 163 |     } | 
|---|
| 164 |  | 
|---|
| 165 |     while(!quit){ | 
|---|
| 166 |       if(enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT)<0){ | 
|---|
| 167 |         // we should never reach this point | 
|---|
| 168 |         quit=true; | 
|---|
| 169 |         // add some error handling here ======================== | 
|---|
| 170 |       } | 
|---|
| 171 |       switch(event.type){ | 
|---|
| 172 |         // log handling ================ | 
|---|
| 173 |         case ENET_EVENT_TYPE_CONNECT: | 
|---|
| 174 |           addClient(&event); | 
|---|
| 175 |           break; | 
|---|
| 176 |         case ENET_EVENT_TYPE_RECEIVE: | 
|---|
| 177 |           //std::cout << "received data" << std::endl; | 
|---|
| 178 |           processData(&event); | 
|---|
| 179 |           break; | 
|---|
| 180 |         case ENET_EVENT_TYPE_DISCONNECT: | 
|---|
| 181 |           // add some error/log handling here | 
|---|
| 182 |           clientDisconnect(event.peer); | 
|---|
| 183 |           break; | 
|---|
| 184 |         case ENET_EVENT_TYPE_NONE: | 
|---|
| 185 |           break; | 
|---|
| 186 |       } | 
|---|
| 187 |     } | 
|---|
| 188 |     disconnectClients(); | 
|---|
| 189 |     // if we're finishied, destroy server | 
|---|
| 190 |     enet_host_destroy(server); | 
|---|
| 191 |   } | 
|---|
| 192 |  | 
|---|
| 193 |   void ConnectionManager::disconnectClients() { | 
|---|
| 194 |     ENetEvent event; | 
|---|
| 195 |     ClientInformation *temp = head_->next(); | 
|---|
| 196 |     while(temp!=0){ | 
|---|
| 197 |       enet_peer_disconnect(temp->getPeer(), 0); | 
|---|
| 198 |       temp = temp->next(); | 
|---|
| 199 |     } | 
|---|
| 200 |     temp = temp->next(); | 
|---|
| 201 |     while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) > 0){ | 
|---|
| 202 |       switch (event.type) | 
|---|
| 203 |       { | 
|---|
| 204 |       case ENET_EVENT_TYPE_NONE: | 
|---|
| 205 |       case ENET_EVENT_TYPE_CONNECT: | 
|---|
| 206 |       case ENET_EVENT_TYPE_RECEIVE: | 
|---|
| 207 |         enet_packet_destroy(event.packet); | 
|---|
| 208 |         break; | 
|---|
| 209 |       case ENET_EVENT_TYPE_DISCONNECT: | 
|---|
| 210 |         std::cout << "disconnecting client" << std::endl; | 
|---|
| 211 |         delete head_->findClient(&(event.peer->address)); | 
|---|
| 212 |         temp = temp->next(); | 
|---|
| 213 |         break; | 
|---|
| 214 |       } | 
|---|
| 215 |     } | 
|---|
| 216 |     return; | 
|---|
| 217 |   } | 
|---|
| 218 |  | 
|---|
| 219 |   bool ConnectionManager::processData(ENetEvent *event) { | 
|---|
| 220 |     // just add packet to the buffer | 
|---|
| 221 |     // this can be extended with some preprocessing | 
|---|
| 222 |     return buffer.push(event); | 
|---|
| 223 |   } | 
|---|
| 224 |  | 
|---|
| 225 |   //bool ConnectionManager::clientDisconnect(ENetPeer *peer) { | 
|---|
| 226 |   //  return clientDisconnect(*peer); | 
|---|
| 227 |   //} | 
|---|
| 228 |  | 
|---|
| 229 |   bool ConnectionManager::clientDisconnect(ENetPeer *peer) { | 
|---|
| 230 |     return head_->removeClient(peer); | 
|---|
| 231 |   } | 
|---|
| 232 |  | 
|---|
| 233 |   bool ConnectionManager::addClient(ENetEvent *event) { | 
|---|
| 234 |     ClientInformation *temp = head_->insertBack(new ClientInformation); | 
|---|
| 235 |     if(temp->prev()->head) | 
|---|
| 236 |       temp->setID(1); | 
|---|
| 237 |     else | 
|---|
| 238 |       temp->setID(temp->prev()->getID()+1); | 
|---|
| 239 |     temp->setPeer(event->peer); | 
|---|
| 240 |     std::cout << "added client id: " << temp->getID() << std::endl; | 
|---|
| 241 |     syncClassid(temp->getID()); | 
|---|
| 242 |     temp->setSynched(true); | 
|---|
| 243 |     return true; | 
|---|
| 244 |   } | 
|---|
| 245 |  | 
|---|
| 246 |   int ConnectionManager::getClientID(ENetPeer peer) { | 
|---|
| 247 |     return getClientID(peer.address); | 
|---|
| 248 |   } | 
|---|
| 249 |  | 
|---|
| 250 |   int ConnectionManager::getClientID(ENetAddress address) { | 
|---|
| 251 |     return head_->findClient(&address)->getID(); | 
|---|
| 252 |   } | 
|---|
| 253 |  | 
|---|
| 254 |   ENetPeer *ConnectionManager::getClientPeer(int clientID) { | 
|---|
| 255 |     return head_->findClient(clientID)->getPeer(); | 
|---|
| 256 |   } | 
|---|
| 257 |  | 
|---|
| 258 |   void ConnectionManager::syncClassid(int clientID) { | 
|---|
| 259 |     int i=0; | 
|---|
| 260 |     std::string classname; | 
|---|
| 261 |     bool abort=false; | 
|---|
| 262 |     orxonox::Identifier *id; | 
|---|
| 263 |     while(!abort){ | 
|---|
| 264 |       id = ID(i); | 
|---|
| 265 |       std::cout << "syncid: " << i << ", ID(id): " << id << std::endl; | 
|---|
| 266 |       if(id == NULL){ | 
|---|
| 267 |         if(i!=0) | 
|---|
| 268 |           abort=true; | 
|---|
| 269 |         else{ | 
|---|
| 270 |           ++i; | 
|---|
| 271 |           continue; | 
|---|
| 272 |         } | 
|---|
| 273 |       } | 
|---|
| 274 |       else{ | 
|---|
| 275 |         classname = id->getName(); | 
|---|
| 276 |         addPacket(packet_gen.clid( i, classname ),clientID); | 
|---|
| 277 |       } | 
|---|
| 278 |       ++i; | 
|---|
| 279 |     } | 
|---|
| 280 |     sendPackets(); | 
|---|
| 281 |   } | 
|---|
| 282 |  | 
|---|
| 283 | } | 
|---|