[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; |
---|
[514] | 61 | |
---|
[777] | 62 | ConnectionManager::ConnectionManager(ClientInformation *head) { |
---|
[173] | 63 | quit=false; |
---|
[196] | 64 | bindAddress.host = ENET_HOST_ANY; |
---|
[173] | 65 | bindAddress.port = NETWORK_PORT; |
---|
[436] | 66 | head_ = head; |
---|
[173] | 67 | } |
---|
[285] | 68 | |
---|
[777] | 69 | ConnectionManager::ConnectionManager(int port, std::string address, ClientInformation *head) { |
---|
[173] | 70 | quit=false; |
---|
[230] | 71 | enet_address_set_host (& bindAddress, address.c_str()); |
---|
[173] | 72 | bindAddress.port = NETWORK_PORT; |
---|
[436] | 73 | head_ = head; |
---|
[173] | 74 | } |
---|
[285] | 75 | |
---|
[777] | 76 | ConnectionManager::ConnectionManager(int port, const char *address, ClientInformation *head) { |
---|
[230] | 77 | quit=false; |
---|
| 78 | enet_address_set_host (& bindAddress, address); |
---|
| 79 | bindAddress.port = NETWORK_PORT; |
---|
[436] | 80 | head_ = head; |
---|
[230] | 81 | } |
---|
[285] | 82 | |
---|
[777] | 83 | ENetPacket *ConnectionManager::getPacket(ENetAddress &address) { |
---|
[196] | 84 | if(!buffer.isEmpty()) |
---|
[204] | 85 | return buffer.pop(address); |
---|
[196] | 86 | else |
---|
[777] | 87 | return NULL; |
---|
[173] | 88 | } |
---|
[514] | 89 | |
---|
[777] | 90 | ENetPacket *ConnectionManager::getPacket(int &clientID) { |
---|
[380] | 91 | ENetAddress address; |
---|
[446] | 92 | ENetPacket *packet=getPacket(address); |
---|
[444] | 93 | ClientInformation *temp =head_->findClient(&address); |
---|
| 94 | clientID=temp->getID(); |
---|
[380] | 95 | return packet; |
---|
| 96 | } |
---|
[285] | 97 | |
---|
[777] | 98 | bool ConnectionManager::queueEmpty() { |
---|
[196] | 99 | return buffer.isEmpty(); |
---|
[173] | 100 | } |
---|
[285] | 101 | |
---|
[777] | 102 | void ConnectionManager::createListener() { |
---|
[196] | 103 | network_threads.create_thread(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this)); |
---|
[777] | 104 | // boost::thread thr(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this)); |
---|
[196] | 105 | return; |
---|
| 106 | } |
---|
[285] | 107 | |
---|
[777] | 108 | bool ConnectionManager::quitListener() { |
---|
[173] | 109 | quit=true; |
---|
[196] | 110 | network_threads.join_all(); |
---|
[173] | 111 | return true; |
---|
| 112 | } |
---|
[285] | 113 | |
---|
[777] | 114 | bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer) { |
---|
[436] | 115 | if(enet_peer_send(peer, head_->findClient(&(peer->address))->getID() , packet)!=0) |
---|
[196] | 116 | return false; |
---|
| 117 | return true; |
---|
| 118 | } |
---|
[514] | 119 | |
---|
[777] | 120 | bool ConnectionManager::addPacket(ENetPacket *packet, int clientID) { |
---|
[436] | 121 | if(enet_peer_send(head_->findClient(clientID)->getPeer(), clientID, packet)!=0) |
---|
[196] | 122 | return false; |
---|
[380] | 123 | return true; |
---|
[196] | 124 | } |
---|
[514] | 125 | |
---|
[777] | 126 | bool ConnectionManager::addPacketAll(ENetPacket *packet) { |
---|
[436] | 127 | for(ClientInformation *i=head_->next(); i!=0; i=i->next()){ |
---|
| 128 | if(enet_peer_send(i->getPeer(), i->getID(), packet)!=0) |
---|
[777] | 129 | return false; |
---|
[196] | 130 | } |
---|
| 131 | return true; |
---|
| 132 | } |
---|
[285] | 133 | |
---|
[777] | 134 | bool ConnectionManager::sendPackets(ENetEvent *event) { |
---|
[196] | 135 | if(server==NULL) |
---|
| 136 | return false; |
---|
| 137 | if(enet_host_service(server, event, NETWORK_SEND_WAIT)>=0) |
---|
| 138 | return true; |
---|
[285] | 139 | else |
---|
[196] | 140 | return false; |
---|
| 141 | } |
---|
[514] | 142 | |
---|
[777] | 143 | bool ConnectionManager::sendPackets() { |
---|
[369] | 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 | } |
---|
[285] | 152 | |
---|
[777] | 153 | void ConnectionManager::receiverThread() { |
---|
[196] | 154 | // what about some error-handling here ? |
---|
| 155 | enet_initialize(); |
---|
| 156 | atexit(enet_deinitialize); |
---|
[173] | 157 | ENetEvent event; |
---|
| 158 | server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0); |
---|
[369] | 159 | if(server==NULL){ |
---|
[173] | 160 | // add some error handling here ========================== |
---|
| 161 | quit=true; |
---|
[369] | 162 | return; |
---|
| 163 | } |
---|
[285] | 164 | |
---|
[173] | 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 ================ |
---|
[196] | 173 | case ENET_EVENT_TYPE_CONNECT: |
---|
[777] | 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; |
---|
[173] | 186 | } |
---|
| 187 | } |
---|
[369] | 188 | disconnectClients(); |
---|
[173] | 189 | // if we're finishied, destroy server |
---|
| 190 | enet_host_destroy(server); |
---|
| 191 | } |
---|
[514] | 192 | |
---|
[777] | 193 | void ConnectionManager::disconnectClients() { |
---|
[369] | 194 | ENetEvent event; |
---|
[436] | 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 | { |
---|
[777] | 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; |
---|
[369] | 214 | } |
---|
| 215 | } |
---|
| 216 | return; |
---|
| 217 | } |
---|
[285] | 218 | |
---|
[777] | 219 | bool ConnectionManager::processData(ENetEvent *event) { |
---|
[196] | 220 | // just add packet to the buffer |
---|
| 221 | // this can be extended with some preprocessing |
---|
[204] | 222 | return buffer.push(event); |
---|
[173] | 223 | } |
---|
[285] | 224 | |
---|
[777] | 225 | //bool ConnectionManager::clientDisconnect(ENetPeer *peer) { |
---|
| 226 | // return clientDisconnect(*peer); |
---|
| 227 | //} |
---|
[514] | 228 | |
---|
[777] | 229 | bool ConnectionManager::clientDisconnect(ENetPeer *peer) { |
---|
[436] | 230 | return head_->removeClient(peer); |
---|
[380] | 231 | } |
---|
[285] | 232 | |
---|
[777] | 233 | bool ConnectionManager::addClient(ENetEvent *event) { |
---|
[436] | 234 | ClientInformation *temp = head_->insertBack(new ClientInformation); |
---|
[620] | 235 | if(temp->prev()->head) |
---|
| 236 | temp->setID(1); |
---|
| 237 | else |
---|
| 238 | temp->setID(temp->prev()->getID()+1); |
---|
[436] | 239 | temp->setPeer(event->peer); |
---|
[636] | 240 | std::cout << "added client id: " << temp->getID() << std::endl; |
---|
| 241 | syncClassid(temp->getID()); |
---|
| 242 | temp->setSynched(true); |
---|
[173] | 243 | return true; |
---|
| 244 | } |
---|
[514] | 245 | |
---|
[777] | 246 | int ConnectionManager::getClientID(ENetPeer peer) { |
---|
[436] | 247 | return getClientID(peer.address); |
---|
[380] | 248 | } |
---|
[514] | 249 | |
---|
[777] | 250 | int ConnectionManager::getClientID(ENetAddress address) { |
---|
[436] | 251 | return head_->findClient(&address)->getID(); |
---|
[380] | 252 | } |
---|
[514] | 253 | |
---|
[777] | 254 | ENetPeer *ConnectionManager::getClientPeer(int clientID) { |
---|
[436] | 255 | return head_->findClient(clientID)->getPeer(); |
---|
[380] | 256 | } |
---|
[514] | 257 | |
---|
[777] | 258 | void ConnectionManager::syncClassid(int clientID) { |
---|
[400] | 259 | int i=0; |
---|
| 260 | std::string classname; |
---|
| 261 | bool abort=false; |
---|
| 262 | orxonox::Identifier *id; |
---|
| 263 | while(!abort){ |
---|
[496] | 264 | id = ID(i); |
---|
[636] | 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 | } |
---|
[400] | 274 | else{ |
---|
| 275 | classname = id->getName(); |
---|
| 276 | addPacket(packet_gen.clid( i, classname ),clientID); |
---|
| 277 | } |
---|
[401] | 278 | ++i; |
---|
[400] | 279 | } |
---|
| 280 | sendPackets(); |
---|
| 281 | } |
---|
[514] | 282 | |
---|
[173] | 283 | } |
---|