Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

created a dummyserver and dummyclient in order to test ConnectionManager and PacketBuffer with enet and boost_thread\n Makefile is used to build server and client

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