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
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  void test(){
19    return;
20  }
21
22  ConnectionManager::ConnectionManager(){
23    quit=false;
24    client=NULL;
25    bindAddress.host = ENET_HOST_ANY;
26    bindAddress.port = NETWORK_PORT;
27  }
28
29  ConnectionManager::ConnectionManager(int port, std::string address){
30    quit=false;
31    client=NULL;
32    enet_address_set_host (& bindAddress, address.c_str());
33    bindAddress.port = NETWORK_PORT;
34  }
35
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  }
42
43  ENetPacket *ConnectionManager::getPacket(ENetAddress &address){
44    if(!buffer.isEmpty())
45      return buffer.pop(address);
46    else
47        return NULL;
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(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  }
79
80  bool ConnectionManager::addPacket(ENetPacket *packet, int ID){
81    if(client==NULL)
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  }
94
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  }
103
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;
109    else
110      return false;
111  }
112
113  void ConnectionManager::receiverThread(){
114    // what about some error-handling here ?
115    enet_initialize();
116    atexit(enet_deinitialize);
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;
122
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 ================
131        case ENET_EVENT_TYPE_CONNECT:
132        addClient(&event);
133        break;
134      case ENET_EVENT_TYPE_RECEIVE:
135        processData(&event);
136        break;
137      case ENET_EVENT_TYPE_DISCONNECT:
138        // add some error/log handling here
139        clientDisconnect(event.peer);
140        break;
141      case ENET_EVENT_TYPE_NONE:
142        break;
143      }
144    }
145    // if we're finishied, destroy server
146    enet_host_destroy(server);
147  }
148
149  bool ConnectionManager::processData(ENetEvent *event){
150    // just add packet to the buffer
151    // this can be extended with some preprocessing
152    return buffer.push(event);
153  }
154
155  bool ConnectionManager::clientDisconnect(ENetPeer *peer){
156    ClientList *temp=client;
157    // do we have to remove the first item ?
158    if(temp->event->peer->host==peer->host){
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){
168        if(temp->next->event->peer->host==peer->host){
169          // do a correct relink and delete the disconnected client
170          ClientList *temp2=temp->next;
171          temp->next=temp2->next;
172          delete temp2;
173          return true;
174        } else
175          //otherwise keep on searching ;)
176          temp=temp->next;
177      }
178    }
179    return false;
180  }
181
182  bool ConnectionManager::addClient(ENetEvent *event){
183
184    // first client?
185    if(client==NULL){
186      client =new ClientList;
187      client->ID=1;
188      client->next=NULL;
189      client->event = event;
190    } else {
191      // otherwise add a new element to clientlist
192      ClientList *temp = client;
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  }
206
207
208}
Note: See TracBrowser for help on using the repository browser.