Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/merger/src/network/ConnectionManager.cc @ 489

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

corrected clientinformation::removeClient(enet event)

File size: 6.3 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 std{
15  bool operator< (ENetAddress a, ENetAddress b){
16    if(a.host <= b.host)
17      return true;
18    else
19      return false;
20  }
21}
22
23namespace network{
24
25  boost::thread_group network_threads;
26 
27  ConnectionManager::ConnectionManager(ClientInformation *head){
28    quit=false;
29    bindAddress.host = ENET_HOST_ANY;
30    bindAddress.port = NETWORK_PORT;
31    head_ = head;
32  }
33
34  ConnectionManager::ConnectionManager(int port, std::string address, ClientInformation *head){
35    quit=false;
36    enet_address_set_host (& bindAddress, address.c_str());
37    bindAddress.port = NETWORK_PORT;
38    head_ = head;
39  }
40
41  ConnectionManager::ConnectionManager(int port, const char *address, ClientInformation *head){
42    quit=false;
43    enet_address_set_host (& bindAddress, address);
44    bindAddress.port = NETWORK_PORT;
45    head_ = head;
46  }
47
48  ENetPacket *ConnectionManager::getPacket(ENetAddress &address){
49    if(!buffer.isEmpty())
50      return buffer.pop(address);
51    else
52        return NULL;
53  }
54 
55  ENetPacket *ConnectionManager::getPacket(int &clientID){
56    ENetAddress address;
57    ENetPacket *packet=getPacket(address);
58    ClientInformation *temp =head_->findClient(&address);
59    clientID=temp->getID();
60    return packet;
61  }
62
63  bool ConnectionManager::queueEmpty(){
64    return buffer.isEmpty();
65  }
66
67  void ConnectionManager::createListener(){
68    network_threads.create_thread(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this));
69//     boost::thread thr(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this));
70    return;
71  }
72
73  bool ConnectionManager::quitListener(){
74    quit=true;
75    network_threads.join_all();
76    return true;
77  }
78
79  bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer){
80    if(enet_peer_send(peer, head_->findClient(&(peer->address))->getID() , packet)!=0)
81      return false;
82    return true;
83  }
84 
85  bool ConnectionManager::addPacket(ENetPacket *packet, int clientID){
86    if(enet_peer_send(head_->findClient(clientID)->getPeer(), clientID, packet)!=0)
87      return false;
88    return true;
89  }
90 
91  bool ConnectionManager::addPacketAll(ENetPacket *packet){
92    for(ClientInformation *i=head_->next(); i!=0; i=i->next()){
93      if(enet_peer_send(i->getPeer(), i->getID(), packet)!=0)
94         return false;
95    }
96    return true;
97  }
98
99  bool ConnectionManager::sendPackets(ENetEvent *event){
100    if(server==NULL)
101      return false;
102    if(enet_host_service(server, event, NETWORK_SEND_WAIT)>=0)
103      return true;
104    else
105      return false;
106  }
107 
108  bool ConnectionManager::sendPackets(){
109    ENetEvent event;
110    if(server==NULL)
111      return false;
112    if(enet_host_service(server, &event, NETWORK_SEND_WAIT)>=0)
113      return true;
114    else
115      return false;
116  }
117
118  void ConnectionManager::receiverThread(){
119    // what about some error-handling here ?
120    enet_initialize();
121    atexit(enet_deinitialize);
122    ENetEvent event;
123    server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0);
124    if(server==NULL){
125      // add some error handling here ==========================
126      quit=true;
127      return;
128    }
129
130    while(!quit){
131      if(enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT)<0){
132        // we should never reach this point
133        quit=true;
134        // add some error handling here ========================
135      }
136      switch(event.type){
137        // log handling ================
138        case ENET_EVENT_TYPE_CONNECT:
139        addClient(&event);
140        break;
141      case ENET_EVENT_TYPE_RECEIVE:
142        std::cout << "received data" << std::endl;
143        processData(&event);
144        break;
145      case ENET_EVENT_TYPE_DISCONNECT:
146        // add some error/log handling here
147        clientDisconnect(event.peer);
148        break;
149      case ENET_EVENT_TYPE_NONE:
150        break;
151      }
152    }
153    disconnectClients();
154    // if we're finishied, destroy server
155    enet_host_destroy(server);
156  }
157 
158  void ConnectionManager::disconnectClients(){
159    ENetEvent event;
160    ClientInformation *temp = head_->next();
161    while(temp!=0){
162      enet_peer_disconnect(temp->getPeer(), 0);
163      temp = temp->next();
164    }
165    temp = temp->next();
166    while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) > 0){
167      switch (event.type)
168      {
169        case ENET_EVENT_TYPE_NONE:
170        case ENET_EVENT_TYPE_CONNECT:
171        case ENET_EVENT_TYPE_RECEIVE:
172          enet_packet_destroy(event.packet);
173          break;
174        case ENET_EVENT_TYPE_DISCONNECT:
175          delete head_->findClient(&(event.peer->address));
176          temp = temp->next();
177          break;
178      }
179    }
180    return;
181  }
182
183  bool ConnectionManager::processData(ENetEvent *event){
184    // just add packet to the buffer
185    // this can be extended with some preprocessing
186    return buffer.push(event);
187  }
188
189//   bool ConnectionManager::clientDisconnect(ENetPeer *peer){
190//     return clientDisconnect(*peer);
191//   }
192 
193 
194 
195  bool ConnectionManager::clientDisconnect(ENetPeer *peer){
196    return head_->removeClient(peer);
197    return true;
198  }
199
200  bool ConnectionManager::addClient(ENetEvent *event){
201    ClientInformation *temp = head_->insertBack(new ClientInformation);
202    temp->setID(temp->prev()->getID()+1);
203    temp->setPeer(event->peer);
204    std::cout << "added client id: " << temp->prev()->getID() << std::endl;
205    return true;
206  }
207 
208  int ConnectionManager::getClientID(ENetPeer peer){
209    return getClientID(peer.address);
210  }
211 
212  int ConnectionManager::getClientID(ENetAddress address){
213    return head_->findClient(&address)->getID();
214  }
215 
216  ENetPeer *ConnectionManager::getClientPeer(int clientID){
217    return head_->findClient(clientID)->getPeer();
218  }
219 
220  void ConnectionManager::syncClassid(int clientID){
221    int i=0;
222    std::string classname;
223    bool abort=false;
224    orxonox::Identifier *id;
225    while(!abort){
226      id = orxonox::ID(i);
227      if(id == NULL)
228        abort=true;
229      else{
230        classname = id->getName();
231        addPacket(packet_gen.clid( i, classname ),clientID);
232      }
233      ++i;
234    }
235    sendPackets();
236  }
237 
238}
Note: See TracBrowser for help on using the repository browser.