[173] | 1 | // |
---|
| 2 | // C++ Interface: ConnectionManager |
---|
| 3 | // |
---|
| 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 |
---|
| 6 | // connection processes is provided by ... |
---|
| 7 | // |
---|
| 8 | // |
---|
[196] | 9 | // Author: Oliver Scheuss |
---|
[173] | 10 | // |
---|
| 11 | |
---|
| 12 | #include "network/ConnectionManager.h" |
---|
| 13 | |
---|
| 14 | namespace network{ |
---|
[196] | 15 | |
---|
| 16 | boost::thread_group network_threads; |
---|
| 17 | |
---|
| 18 | void test(){ |
---|
| 19 | return; |
---|
| 20 | } |
---|
| 21 | |
---|
[173] | 22 | ConnectionManager::ConnectionManager(){ |
---|
| 23 | quit=false; |
---|
| 24 | client=NULL; |
---|
[196] | 25 | bindAddress.host = ENET_HOST_ANY; |
---|
[173] | 26 | bindAddress.port = NETWORK_PORT; |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | ConnectionManager::ConnectionManager(int port, int address){ |
---|
| 30 | quit=false; |
---|
| 31 | client=NULL; |
---|
| 32 | bindAddress.host = address; |
---|
| 33 | bindAddress.port = NETWORK_PORT; |
---|
| 34 | } |
---|
| 35 | |
---|
[196] | 36 | |
---|
[204] | 37 | ENetPacket *ConnectionManager::getPacket(ENetAddress &address){ |
---|
[196] | 38 | if(!buffer.isEmpty()) |
---|
[204] | 39 | return buffer.pop(address); |
---|
[196] | 40 | else |
---|
| 41 | return NULL; |
---|
[173] | 42 | } |
---|
| 43 | |
---|
[196] | 44 | bool ConnectionManager::queueEmpty(){ |
---|
| 45 | return buffer.isEmpty(); |
---|
[173] | 46 | } |
---|
| 47 | |
---|
[196] | 48 | void ConnectionManager::createListener(){ |
---|
| 49 | network_threads.create_thread(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this)); |
---|
| 50 | // boost::thread thr(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this)); |
---|
| 51 | return; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | bool ConnectionManager::quitListener(){ |
---|
[173] | 55 | quit=true; |
---|
[196] | 56 | network_threads.join_all(); |
---|
[173] | 57 | return true; |
---|
| 58 | } |
---|
| 59 | |
---|
[196] | 60 | bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer){ |
---|
| 61 | if(client=NULL) |
---|
| 62 | return false; |
---|
| 63 | ClientList *temp=client; |
---|
| 64 | while(peer->host != temp->event->peer->host){ |
---|
| 65 | temp=temp->next; |
---|
| 66 | if(temp==NULL) |
---|
| 67 | return false; |
---|
| 68 | } |
---|
| 69 | if(enet_peer_send(temp->event->peer, temp->ID, packet)!=0) |
---|
| 70 | return false; |
---|
| 71 | return true; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | bool ConnectionManager::addPacket(ENetPacket *packet, int ID){ |
---|
[229] | 75 | if(client==NULL) |
---|
[196] | 76 | return false; |
---|
| 77 | ClientList *temp=client; |
---|
| 78 | while(ID != temp->ID){ |
---|
| 79 | temp=temp->next; |
---|
| 80 | if(temp==NULL) |
---|
| 81 | return false; |
---|
| 82 | } |
---|
| 83 | if(enet_peer_send(temp->event->peer, temp->ID, packet)!=0) |
---|
| 84 | return false; |
---|
| 85 | else |
---|
| 86 | return true; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | bool ConnectionManager::addPacketAll(ENetPacket *packet){ |
---|
| 90 | ClientList *temp=client; |
---|
| 91 | while(temp!=NULL){ |
---|
| 92 | if(enet_peer_send(temp->event->peer, temp->ID, packet)!=0) |
---|
| 93 | return false; |
---|
| 94 | } |
---|
| 95 | return true; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | bool ConnectionManager::sendPackets(ENetEvent *event){ |
---|
| 99 | if(server==NULL) |
---|
| 100 | return false; |
---|
| 101 | if(enet_host_service(server, event, NETWORK_SEND_WAIT)>=0) |
---|
| 102 | return true; |
---|
| 103 | else |
---|
| 104 | return false; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | void ConnectionManager::receiverThread(){ |
---|
| 108 | // what about some error-handling here ? |
---|
| 109 | enet_initialize(); |
---|
| 110 | atexit(enet_deinitialize); |
---|
[173] | 111 | ENetEvent event; |
---|
| 112 | server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0); |
---|
| 113 | if(server==NULL) |
---|
| 114 | // add some error handling here ========================== |
---|
| 115 | quit=true; |
---|
| 116 | |
---|
| 117 | while(!quit){ |
---|
| 118 | if(enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT)<0){ |
---|
| 119 | // we should never reach this point |
---|
| 120 | quit=true; |
---|
| 121 | // add some error handling here ======================== |
---|
| 122 | } |
---|
| 123 | switch(event.type){ |
---|
| 124 | // log handling ================ |
---|
[196] | 125 | case ENET_EVENT_TYPE_CONNECT: |
---|
| 126 | addClient(&event); |
---|
[173] | 127 | break; |
---|
[196] | 128 | case ENET_EVENT_TYPE_RECEIVE: |
---|
| 129 | processData(&event); |
---|
[173] | 130 | break; |
---|
[196] | 131 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
[173] | 132 | // add some error/log handling here |
---|
| 133 | clientDisconnect(event.peer); |
---|
| 134 | break; |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | // if we're finishied, destroy server |
---|
| 138 | enet_host_destroy(server); |
---|
| 139 | } |
---|
| 140 | |
---|
[196] | 141 | bool ConnectionManager::processData(ENetEvent *event){ |
---|
| 142 | // just add packet to the buffer |
---|
| 143 | // this can be extended with some preprocessing |
---|
[204] | 144 | return buffer.push(event); |
---|
[173] | 145 | } |
---|
| 146 | |
---|
[196] | 147 | bool ConnectionManager::clientDisconnect(ENetPeer *peer){ |
---|
[173] | 148 | ClientList *temp=client; |
---|
[196] | 149 | // do we have to remove the first item ? |
---|
| 150 | if(temp->event->peer->host==peer->host){ |
---|
[173] | 151 | if(temp->next==NULL){ |
---|
| 152 | client=NULL; |
---|
| 153 | } else{ |
---|
| 154 | client=temp->next; |
---|
| 155 | } |
---|
| 156 | delete temp; |
---|
| 157 | return true; |
---|
| 158 | } else { |
---|
| 159 | while(temp->next!=NULL){ |
---|
[196] | 160 | if(temp->next->event->peer->host==peer->host){ |
---|
| 161 | // do a correct relink and delete the disconnected client |
---|
[173] | 162 | ClientList *temp2=temp->next; |
---|
| 163 | temp->next=temp2->next; |
---|
| 164 | delete temp2; |
---|
| 165 | return true; |
---|
| 166 | } else |
---|
[196] | 167 | //otherwise keep on searching ;) |
---|
[173] | 168 | temp=temp->next; |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | return false; |
---|
| 172 | } |
---|
| 173 | |
---|
[196] | 174 | bool ConnectionManager::addClient(ENetEvent *event){ |
---|
| 175 | |
---|
| 176 | // first client? |
---|
[173] | 177 | if(client==NULL){ |
---|
| 178 | client =new ClientList; |
---|
| 179 | client->ID=1; |
---|
| 180 | client->next=NULL; |
---|
| 181 | client->event = event; |
---|
| 182 | } else { |
---|
[196] | 183 | // otherwise add a new element to clientlist |
---|
| 184 | ClientList *temp = client; |
---|
[173] | 185 | int i=1; |
---|
| 186 | while(temp->next != NULL){ |
---|
| 187 | temp=temp->next; |
---|
| 188 | i++; |
---|
| 189 | } |
---|
| 190 | temp->next=new ClientList; |
---|
| 191 | temp=temp->next; |
---|
| 192 | temp->ID=i+1; |
---|
| 193 | temp->event=event; |
---|
| 194 | temp->next=NULL; |
---|
| 195 | } |
---|
| 196 | return true; |
---|
| 197 | } |
---|
| 198 | |
---|
[196] | 199 | |
---|
[173] | 200 | } |
---|