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