Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/network/ConnectionManager.cc @ 204

Last change on this file since 204 was 204, checked in by scheusso, 16 years ago

changed packetbuffer and conenctionmanager to remember the client id/adjustet dummyserver/client

File size: 4.9 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 "network/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, int address){
30    quit=false;
31    client=NULL;
32    bindAddress.host = address;
33    bindAddress.port = NETWORK_PORT;
34  }
35 
36 
37  ENetPacket *ConnectionManager::getPacket(ENetAddress &address){
38    if(!buffer.isEmpty())
39      return buffer.pop(address);
40    else
41        return NULL;
42  }
43 
44  bool ConnectionManager::queueEmpty(){
45    return buffer.isEmpty();
46  }
47 
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(){
55    quit=true;
56    network_threads.join_all();
57    return true;
58  }
59 
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);
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 ================
125        case ENET_EVENT_TYPE_CONNECT:
126        addClient(&event);
127        break;
128      case ENET_EVENT_TYPE_RECEIVE:
129        processData(&event);
130        break;
131      case ENET_EVENT_TYPE_DISCONNECT:
132        // add some error/log handling here
133        clientDisconnect(event.peer);
134        break;
135      }
136    }
137    // if we're finishied, destroy server
138    enet_host_destroy(server);
139  }
140 
141  bool ConnectionManager::processData(ENetEvent *event){
142    // just add packet to the buffer
143    // this can be extended with some preprocessing
144    return buffer.push(event);
145  }
146 
147  bool ConnectionManager::clientDisconnect(ENetPeer *peer){
148    ClientList *temp=client;
149    // do we have to remove the first item ?
150    if(temp->event->peer->host==peer->host){
151      if(temp->next==NULL){
152        client=NULL;
153      } else{
154        client=temp->next;
155      }
156      delete temp;
157      return true;
158    } else {
159      while(temp->next!=NULL){
160        if(temp->next->event->peer->host==peer->host){
161          // do a correct relink and delete the disconnected client
162          ClientList *temp2=temp->next;
163          temp->next=temp2->next;
164          delete temp2;
165          return true;
166        } else
167          //otherwise keep on searching ;)
168          temp=temp->next;
169      }
170    }
171    return false;
172  }
173 
174  bool ConnectionManager::addClient(ENetEvent *event){
175   
176    // first client?
177    if(client==NULL){
178      client =new ClientList;
179      client->ID=1;
180      client->next=NULL;
181      client->event = event;
182    } else {
183      // otherwise add a new element to clientlist
184      ClientList *temp = client;
185      int i=1;
186      while(temp->next != NULL){
187        temp=temp->next;
188        i++;
189      }
190      temp->next=new ClientList;
191      temp=temp->next;
192      temp->ID=i+1;
193      temp->event=event;
194      temp->next=NULL;
195    }
196    return true;
197  }
198 
199 
200}
Note: See TracBrowser for help on using the repository browser.