[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 | |
---|
| 37 | ENetPacket *ConnectionManager::getPacket(){ |
---|
| 38 | if(!buffer.isEmpty()) |
---|
| 39 | return buffer.pop(); |
---|
| 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){ |
---|
| 75 | if(client=NULL) |
---|
| 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 | std::cout << event.packet->data << std::endl; |
---|
| 130 | processData(&event); |
---|
[173] | 131 | break; |
---|
[196] | 132 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
[173] | 133 | // add some error/log handling here |
---|
| 134 | clientDisconnect(event.peer); |
---|
| 135 | break; |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | // if we're finishied, destroy server |
---|
| 139 | enet_host_destroy(server); |
---|
| 140 | } |
---|
| 141 | |
---|
[196] | 142 | bool ConnectionManager::processData(ENetEvent *event){ |
---|
| 143 | // just add packet to the buffer |
---|
| 144 | // this can be extended with some preprocessing |
---|
| 145 | return buffer.push(event->packet); |
---|
[173] | 146 | } |
---|
| 147 | |
---|
[196] | 148 | bool ConnectionManager::clientDisconnect(ENetPeer *peer){ |
---|
[173] | 149 | ClientList *temp=client; |
---|
[196] | 150 | // do we have to remove the first item ? |
---|
| 151 | if(temp->event->peer->host==peer->host){ |
---|
[173] | 152 | if(temp->next==NULL){ |
---|
| 153 | client=NULL; |
---|
| 154 | } else{ |
---|
| 155 | client=temp->next; |
---|
| 156 | } |
---|
| 157 | delete temp; |
---|
| 158 | return true; |
---|
| 159 | } else { |
---|
| 160 | while(temp->next!=NULL){ |
---|
[196] | 161 | if(temp->next->event->peer->host==peer->host){ |
---|
| 162 | // do a correct relink and delete the disconnected client |
---|
[173] | 163 | ClientList *temp2=temp->next; |
---|
| 164 | temp->next=temp2->next; |
---|
| 165 | delete temp2; |
---|
| 166 | return true; |
---|
| 167 | } else |
---|
[196] | 168 | //otherwise keep on searching ;) |
---|
[173] | 169 | temp=temp->next; |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | return false; |
---|
| 173 | } |
---|
| 174 | |
---|
[196] | 175 | bool ConnectionManager::addClient(ENetEvent *event){ |
---|
| 176 | |
---|
| 177 | // first client? |
---|
[173] | 178 | if(client==NULL){ |
---|
| 179 | client =new ClientList; |
---|
| 180 | client->ID=1; |
---|
| 181 | client->next=NULL; |
---|
| 182 | client->event = event; |
---|
| 183 | } else { |
---|
[196] | 184 | // otherwise add a new element to clientlist |
---|
| 185 | ClientList *temp = client; |
---|
[173] | 186 | int i=1; |
---|
| 187 | while(temp->next != NULL){ |
---|
| 188 | temp=temp->next; |
---|
| 189 | i++; |
---|
| 190 | } |
---|
| 191 | temp->next=new ClientList; |
---|
| 192 | temp=temp->next; |
---|
| 193 | temp->ID=i+1; |
---|
| 194 | temp->event=event; |
---|
| 195 | temp->next=NULL; |
---|
| 196 | } |
---|
| 197 | return true; |
---|
| 198 | } |
---|
| 199 | |
---|
[196] | 200 | |
---|
[173] | 201 | } |
---|