[173] | 1 | // |
---|
| 2 | // C++ Interface: ConnectionManager |
---|
| 3 | // |
---|
[285] | 4 | // Description: The Class ConnectionManager manages the servers conenctions to the clients. |
---|
| 5 | // each connection is provided by a new process. communication between master process and |
---|
[173] | 6 | // connection processes is provided by ... |
---|
| 7 | // |
---|
| 8 | // |
---|
[196] | 9 | // Author: Oliver Scheuss |
---|
[173] | 10 | // |
---|
| 11 | |
---|
[285] | 12 | #include "ConnectionManager.h" |
---|
[173] | 13 | |
---|
[381] | 14 | namespace std{ |
---|
| 15 | bool operator< (ENetAddress a, ENetAddress b){ |
---|
| 16 | if(a.host <= b.host) |
---|
| 17 | return true; |
---|
| 18 | else |
---|
| 19 | return false; |
---|
| 20 | } |
---|
| 21 | } |
---|
| 22 | |
---|
[173] | 23 | namespace network{ |
---|
[285] | 24 | |
---|
[196] | 25 | boost::thread_group network_threads; |
---|
[381] | 26 | |
---|
[173] | 27 | ConnectionManager::ConnectionManager(){ |
---|
| 28 | quit=false; |
---|
[196] | 29 | bindAddress.host = ENET_HOST_ANY; |
---|
[173] | 30 | bindAddress.port = NETWORK_PORT; |
---|
| 31 | } |
---|
[285] | 32 | |
---|
[230] | 33 | ConnectionManager::ConnectionManager(int port, std::string address){ |
---|
[173] | 34 | quit=false; |
---|
[230] | 35 | enet_address_set_host (& bindAddress, address.c_str()); |
---|
[173] | 36 | bindAddress.port = NETWORK_PORT; |
---|
| 37 | } |
---|
[285] | 38 | |
---|
[230] | 39 | ConnectionManager::ConnectionManager(int port, const char *address){ |
---|
| 40 | quit=false; |
---|
| 41 | enet_address_set_host (& bindAddress, address); |
---|
| 42 | bindAddress.port = NETWORK_PORT; |
---|
| 43 | } |
---|
[285] | 44 | |
---|
[204] | 45 | ENetPacket *ConnectionManager::getPacket(ENetAddress &address){ |
---|
[196] | 46 | if(!buffer.isEmpty()) |
---|
[204] | 47 | return buffer.pop(address); |
---|
[196] | 48 | else |
---|
| 49 | return NULL; |
---|
[173] | 50 | } |
---|
[380] | 51 | |
---|
| 52 | ENetPacket *ConnectionManager::getPacket(int &clientID){ |
---|
| 53 | ENetAddress address; |
---|
| 54 | ENetPacket *packet=getPacket(address); |
---|
| 55 | clientID=clientMap.find(address)->second; |
---|
| 56 | return packet; |
---|
| 57 | } |
---|
[285] | 58 | |
---|
[196] | 59 | bool ConnectionManager::queueEmpty(){ |
---|
| 60 | return buffer.isEmpty(); |
---|
[173] | 61 | } |
---|
[285] | 62 | |
---|
[196] | 63 | void ConnectionManager::createListener(){ |
---|
| 64 | network_threads.create_thread(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this)); |
---|
| 65 | // boost::thread thr(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this)); |
---|
| 66 | return; |
---|
| 67 | } |
---|
[285] | 68 | |
---|
[196] | 69 | bool ConnectionManager::quitListener(){ |
---|
[173] | 70 | quit=true; |
---|
[196] | 71 | network_threads.join_all(); |
---|
[173] | 72 | return true; |
---|
| 73 | } |
---|
[285] | 74 | |
---|
[196] | 75 | bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer){ |
---|
[380] | 76 | if(clientVector.size()==0) |
---|
[196] | 77 | return false; |
---|
[380] | 78 | if(enet_peer_send(peer, clientMap.find(peer->address)->second , packet)!=0) |
---|
[196] | 79 | return false; |
---|
| 80 | return true; |
---|
| 81 | } |
---|
[380] | 82 | |
---|
| 83 | bool ConnectionManager::addPacket(ENetPacket *packet, int clientID){ |
---|
| 84 | if(clientVector.size()==0) |
---|
[196] | 85 | return false; |
---|
[380] | 86 | if(enet_peer_send(&(peerMap.find(clientVector[clientID])->second), clientID, packet)!=0) |
---|
[196] | 87 | return false; |
---|
[380] | 88 | return true; |
---|
[196] | 89 | } |
---|
[380] | 90 | |
---|
[196] | 91 | bool ConnectionManager::addPacketAll(ENetPacket *packet){ |
---|
[415] | 92 | for(unsigned int i=0; i<clientVector.size(); i++){ |
---|
[380] | 93 | if(enet_peer_send(&(peerMap.find(clientVector[i])->second), i, packet)!=0) |
---|
[196] | 94 | return false; |
---|
| 95 | } |
---|
| 96 | return true; |
---|
| 97 | } |
---|
[285] | 98 | |
---|
[196] | 99 | bool ConnectionManager::sendPackets(ENetEvent *event){ |
---|
| 100 | if(server==NULL) |
---|
| 101 | return false; |
---|
| 102 | if(enet_host_service(server, event, NETWORK_SEND_WAIT)>=0) |
---|
| 103 | return true; |
---|
[285] | 104 | else |
---|
[196] | 105 | return false; |
---|
| 106 | } |
---|
[369] | 107 | |
---|
| 108 | bool ConnectionManager::sendPackets(){ |
---|
| 109 | ENetEvent event; |
---|
| 110 | if(server==NULL) |
---|
| 111 | return false; |
---|
| 112 | if(enet_host_service(server, &event, NETWORK_SEND_WAIT)>=0) |
---|
| 113 | return true; |
---|
| 114 | else |
---|
| 115 | return false; |
---|
| 116 | } |
---|
[285] | 117 | |
---|
[196] | 118 | void ConnectionManager::receiverThread(){ |
---|
| 119 | // what about some error-handling here ? |
---|
| 120 | enet_initialize(); |
---|
| 121 | atexit(enet_deinitialize); |
---|
[173] | 122 | ENetEvent event; |
---|
| 123 | server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0); |
---|
[369] | 124 | if(server==NULL){ |
---|
[173] | 125 | // add some error handling here ========================== |
---|
| 126 | quit=true; |
---|
[369] | 127 | return; |
---|
| 128 | } |
---|
[285] | 129 | |
---|
[173] | 130 | while(!quit){ |
---|
| 131 | if(enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT)<0){ |
---|
| 132 | // we should never reach this point |
---|
| 133 | quit=true; |
---|
| 134 | // add some error handling here ======================== |
---|
| 135 | } |
---|
| 136 | switch(event.type){ |
---|
| 137 | // log handling ================ |
---|
[196] | 138 | case ENET_EVENT_TYPE_CONNECT: |
---|
| 139 | addClient(&event); |
---|
[173] | 140 | break; |
---|
[196] | 141 | case ENET_EVENT_TYPE_RECEIVE: |
---|
| 142 | processData(&event); |
---|
[173] | 143 | break; |
---|
[196] | 144 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
[173] | 145 | // add some error/log handling here |
---|
| 146 | clientDisconnect(event.peer); |
---|
| 147 | break; |
---|
[352] | 148 | case ENET_EVENT_TYPE_NONE: |
---|
| 149 | break; |
---|
[173] | 150 | } |
---|
| 151 | } |
---|
[369] | 152 | disconnectClients(); |
---|
[173] | 153 | // if we're finishied, destroy server |
---|
| 154 | enet_host_destroy(server); |
---|
| 155 | } |
---|
[369] | 156 | |
---|
| 157 | void ConnectionManager::disconnectClients(){ |
---|
| 158 | bool disconnected=false; |
---|
| 159 | ENetEvent event; |
---|
[415] | 160 | for(unsigned int i=0; i<clientVector.size(); i++){ |
---|
[380] | 161 | enet_peer_disconnect(&(peerMap.find(clientVector[i])->second), 0); |
---|
[369] | 162 | while( !disconnected && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) > 0){ |
---|
| 163 | switch (event.type) |
---|
| 164 | { |
---|
| 165 | case ENET_EVENT_TYPE_NONE: |
---|
| 166 | case ENET_EVENT_TYPE_CONNECT: |
---|
| 167 | case ENET_EVENT_TYPE_RECEIVE: |
---|
| 168 | enet_packet_destroy(event.packet); |
---|
| 169 | break; |
---|
| 170 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
| 171 | disconnected=true; |
---|
| 172 | break; |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | disconnected=false; |
---|
| 176 | } |
---|
| 177 | return; |
---|
| 178 | } |
---|
[285] | 179 | |
---|
[196] | 180 | bool ConnectionManager::processData(ENetEvent *event){ |
---|
| 181 | // just add packet to the buffer |
---|
| 182 | // this can be extended with some preprocessing |
---|
[204] | 183 | return buffer.push(event); |
---|
[173] | 184 | } |
---|
[285] | 185 | |
---|
[196] | 186 | bool ConnectionManager::clientDisconnect(ENetPeer *peer){ |
---|
[380] | 187 | return clientDisconnect(*peer); |
---|
[173] | 188 | } |
---|
[380] | 189 | |
---|
| 190 | bool ConnectionManager::clientDisconnect(ENetPeer peer){ |
---|
| 191 | std::map<ENetAddress, int>::iterator it; |
---|
| 192 | it=clientMap.find(peer.address); |
---|
| 193 | clientVector.erase(clientVector.begin()+it->second); |
---|
| 194 | clientMap.erase(it); |
---|
| 195 | peerMap.erase(peerMap.find(peer.address)); |
---|
| 196 | return true; |
---|
| 197 | } |
---|
[285] | 198 | |
---|
[196] | 199 | bool ConnectionManager::addClient(ENetEvent *event){ |
---|
[400] | 200 | int id=clientVector.size(); |
---|
[380] | 201 | clientVector.push_back((event->peer->address)); |
---|
[400] | 202 | clientMap[event->peer->address]=id; |
---|
[380] | 203 | peerMap[event->peer->address]=*(event->peer); |
---|
[400] | 204 | syncClassid(id); |
---|
[173] | 205 | return true; |
---|
| 206 | } |
---|
[380] | 207 | |
---|
| 208 | int ConnectionManager::getClientID(ENetPeer peer){ |
---|
| 209 | return clientMap.find(peer.address)->second; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | int ConnectionManager::getClientID(ENetAddress address){ |
---|
| 213 | return clientMap.find(address)->second; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | ENetPeer ConnectionManager::getClientPeer(int clientID){ |
---|
| 217 | return peerMap.find(clientVector[clientID])->second; |
---|
| 218 | } |
---|
| 219 | |
---|
[400] | 220 | void ConnectionManager::syncClassid(int clientID){ |
---|
| 221 | int i=0; |
---|
| 222 | std::string classname; |
---|
| 223 | bool abort=false; |
---|
| 224 | orxonox::Identifier *id; |
---|
| 225 | while(!abort){ |
---|
| 226 | id = orxonox::ID(i); |
---|
| 227 | if(id == NULL) |
---|
| 228 | abort=true; |
---|
| 229 | else{ |
---|
| 230 | classname = id->getName(); |
---|
| 231 | addPacket(packet_gen.clid( i, classname ),clientID); |
---|
| 232 | } |
---|
[401] | 233 | ++i; |
---|
[400] | 234 | } |
---|
| 235 | sendPackets(); |
---|
| 236 | } |
---|
| 237 | |
---|
[173] | 238 | } |
---|