Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/network/ConnectionManager.cc @ 356

Last change on this file since 356 was 352, checked in by scheusso, 17 years ago

corrected some compiler-warnings

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