Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

problem in connectionmanager

File size: 5.7 KB
Line 
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//
9// Author:  Oliver Scheuss
10//
11
12#include "ConnectionManager.h"
13
14namespace network{
15
16  boost::thread_group network_threads;
17
18  ConnectionManager::ConnectionManager(){
19    quit=false;
20    bindAddress.host = ENET_HOST_ANY;
21    bindAddress.port = NETWORK_PORT;
22  }
23
24  ConnectionManager::ConnectionManager(int port, std::string address){
25    quit=false;
26    enet_address_set_host (& bindAddress, address.c_str());
27    bindAddress.port = NETWORK_PORT;
28  }
29
30  ConnectionManager::ConnectionManager(int port, const char *address){
31    quit=false;
32    enet_address_set_host (& bindAddress, address);
33    bindAddress.port = NETWORK_PORT;
34  }
35
36  ENetPacket *ConnectionManager::getPacket(ENetAddress &address){
37    if(!buffer.isEmpty())
38      return buffer.pop(address);
39    else
40        return NULL;
41  }
42 
43  ENetPacket *ConnectionManager::getPacket(int &clientID){
44    ENetAddress address;
45    ENetPacket *packet=getPacket(address);
46    clientID=clientMap.find(address)->second;
47    return packet;
48  }
49
50  bool ConnectionManager::queueEmpty(){
51    return buffer.isEmpty();
52  }
53
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  }
59
60  bool ConnectionManager::quitListener(){
61    quit=true;
62    network_threads.join_all();
63    return true;
64  }
65
66  bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer){
67    if(clientVector.size()==0)
68      return false;
69    if(enet_peer_send(peer, clientMap.find(peer->address)->second , packet)!=0)
70      return false;
71    return true;
72  }
73 
74  bool ConnectionManager::addPacket(ENetPacket *packet, int clientID){
75    if(clientVector.size()==0)
76      return false;
77    if(enet_peer_send(&(peerMap.find(clientVector[clientID])->second), clientID, packet)!=0)
78      return false;
79    return true;
80  }
81 
82  bool ConnectionManager::addPacketAll(ENetPacket *packet){
83    for(int i=0; i<clientVector.size(); i++){
84      if(enet_peer_send(&(peerMap.find(clientVector[i])->second), i, packet)!=0)
85         return false;
86    }
87    return true;
88  }
89
90  bool ConnectionManager::sendPackets(ENetEvent *event){
91    if(server==NULL)
92      return false;
93    if(enet_host_service(server, event, NETWORK_SEND_WAIT)>=0)
94      return true;
95    else
96      return false;
97  }
98 
99  bool ConnectionManager::sendPackets(){
100    ENetEvent event;
101    if(server==NULL)
102      return false;
103    if(enet_host_service(server, &event, NETWORK_SEND_WAIT)>=0)
104      return true;
105    else
106      return false;
107  }
108
109  void ConnectionManager::receiverThread(){
110    // what about some error-handling here ?
111    enet_initialize();
112    atexit(enet_deinitialize);
113    ENetEvent event;
114    server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0);
115    if(server==NULL){
116      // add some error handling here ==========================
117      quit=true;
118      return;
119    }
120
121    while(!quit){
122      if(enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT)<0){
123        // we should never reach this point
124        quit=true;
125        // add some error handling here ========================
126      }
127      switch(event.type){
128        // log handling ================
129        case ENET_EVENT_TYPE_CONNECT:
130        addClient(&event);
131        break;
132      case ENET_EVENT_TYPE_RECEIVE:
133        processData(&event);
134        break;
135      case ENET_EVENT_TYPE_DISCONNECT:
136        // add some error/log handling here
137        clientDisconnect(event.peer);
138        break;
139      case ENET_EVENT_TYPE_NONE:
140        break;
141      }
142    }
143    disconnectClients();
144    // if we're finishied, destroy server
145    enet_host_destroy(server);
146  }
147 
148  void ConnectionManager::disconnectClients(){
149    bool disconnected=false;
150    ENetEvent event;
151    for(int i=0; i<clientVector.size(); i++){
152      enet_peer_disconnect(&(peerMap.find(clientVector[i])->second), 0);
153      while( !disconnected && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) > 0){
154        switch (event.type)
155        {
156          case ENET_EVENT_TYPE_NONE:
157          case ENET_EVENT_TYPE_CONNECT:
158          case ENET_EVENT_TYPE_RECEIVE:
159            enet_packet_destroy(event.packet);
160            break;
161          case ENET_EVENT_TYPE_DISCONNECT:
162            disconnected=true;
163            break;
164        }
165      }
166      disconnected=false;
167    }
168    return;
169  }
170
171  bool ConnectionManager::processData(ENetEvent *event){
172    // just add packet to the buffer
173    // this can be extended with some preprocessing
174    return buffer.push(event);
175  }
176
177  bool ConnectionManager::clientDisconnect(ENetPeer *peer){
178    return clientDisconnect(*peer);
179  }
180 
181  bool ConnectionManager::clientDisconnect(ENetPeer peer){
182    std::map<ENetAddress, int>::iterator it;
183    it=clientMap.find(peer.address);
184    clientVector.erase(clientVector.begin()+it->second);
185    clientMap.erase(it);
186    peerMap.erase(peerMap.find(peer.address));
187    return true;
188  }
189
190  bool ConnectionManager::addClient(ENetEvent *event){
191    clientVector.push_back((event->peer->address));
192    clientMap[event->peer->address]=clientVector.size()-1;
193    peerMap[event->peer->address]=*(event->peer);
194    return true;
195  }
196 
197  int ConnectionManager::getClientID(ENetPeer peer){
198    return clientMap.find(peer.address)->second;
199  }
200 
201  int ConnectionManager::getClientID(ENetAddress address){
202    return clientMap.find(address)->second;
203  }
204 
205  ENetPeer ConnectionManager::getClientPeer(int clientID){
206    return peerMap.find(clientVector[clientID])->second;
207  }
208 
209}
Note: See TracBrowser for help on using the repository browser.