Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 351 was 337, checked in by bknecht, 17 years ago

merged merger to FICN branch

File size: 5.1 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){
67    if(client=NULL)
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;
141      }
142    }
143    // if we're finishied, destroy server
144    enet_host_destroy(server);
145  }
[285]146
[196]147  bool ConnectionManager::processData(ENetEvent *event){
148    // just add packet to the buffer
149    // this can be extended with some preprocessing
[204]150    return buffer.push(event);
[173]151  }
[285]152
[196]153  bool ConnectionManager::clientDisconnect(ENetPeer *peer){
[173]154    ClientList *temp=client;
[196]155    // do we have to remove the first item ?
156    if(temp->event->peer->host==peer->host){
[173]157      if(temp->next==NULL){
158        client=NULL;
159      } else{
160        client=temp->next;
161      }
162      delete temp;
163      return true;
164    } else {
165      while(temp->next!=NULL){
[196]166        if(temp->next->event->peer->host==peer->host){
167          // do a correct relink and delete the disconnected client
[173]168          ClientList *temp2=temp->next;
169          temp->next=temp2->next;
170          delete temp2;
171          return true;
172        } else
[196]173          //otherwise keep on searching ;)
[173]174          temp=temp->next;
175      }
176    }
177    return false;
178  }
[285]179
[196]180  bool ConnectionManager::addClient(ENetEvent *event){
[285]181
[196]182    // first client?
[173]183    if(client==NULL){
184      client =new ClientList;
185      client->ID=1;
186      client->next=NULL;
187      client->event = event;
188    } else {
[196]189      // otherwise add a new element to clientlist
190      ClientList *temp = client;
[173]191      int i=1;
192      while(temp->next != NULL){
193        temp=temp->next;
194        i++;
195      }
196      temp->next=new ClientList;
197      temp=temp->next;
198      temp->ID=i+1;
199      temp->event=event;
200      temp->next=NULL;
201    }
202    return true;
203  }
[285]204
205
[173]206}
Note: See TracBrowser for help on using the repository browser.