Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

still problems with chatserver/client

File size: 6.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 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    clientID=head_->findClient(&address)->getID();
59    return packet;
60  }
61
62  bool ConnectionManager::queueEmpty(){
63    return buffer.isEmpty();
64  }
65
66  void ConnectionManager::createListener(){
67    network_threads.create_thread(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this));
68//     boost::thread thr(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this));
69    return;
70  }
71
72  bool ConnectionManager::quitListener(){
73    quit=true;
74    network_threads.join_all();
75    return true;
76  }
77
78  bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer){
79    if(enet_peer_send(peer, head_->findClient(&(peer->address))->getID() , packet)!=0)
80      return false;
81    return true;
82  }
83 
84  bool ConnectionManager::addPacket(ENetPacket *packet, int clientID){
85    if(enet_peer_send(head_->findClient(clientID)->getPeer(), clientID, packet)!=0)
86      return false;
87    return true;
88  }
89 
90  bool ConnectionManager::addPacketAll(ENetPacket *packet){
91    for(ClientInformation *i=head_->next(); i!=0; i=i->next()){
92      if(enet_peer_send(i->getPeer(), i->getID(), 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  bool ConnectionManager::sendPackets(){
108    ENetEvent event;
109    if(server==NULL)
110      return false;
111    if(enet_host_service(server, &event, NETWORK_SEND_WAIT)>=0)
112      return true;
113    else
114      return false;
115  }
116
117  void ConnectionManager::receiverThread(){
118    // what about some error-handling here ?
119    enet_initialize();
120    atexit(enet_deinitialize);
121    ENetEvent event;
122    server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0);
123    if(server==NULL){
124      // add some error handling here ==========================
125      quit=true;
126      return;
127    }
128
129    while(!quit){
130      if(enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT)<0){
131        // we should never reach this point
132        quit=true;
133        // add some error handling here ========================
134      }
135      switch(event.type){
136        // log handling ================
137        case ENET_EVENT_TYPE_CONNECT:
138        addClient(&event);
139        break;
140      case ENET_EVENT_TYPE_RECEIVE:
141        processData(&event);
142        break;
143      case ENET_EVENT_TYPE_DISCONNECT:
144        // add some error/log handling here
145        clientDisconnect(event.peer);
146        break;
147      case ENET_EVENT_TYPE_NONE:
148        break;
149      }
150    }
151    disconnectClients();
152    // if we're finishied, destroy server
153    enet_host_destroy(server);
154  }
155 
156  void ConnectionManager::disconnectClients(){
157    ENetEvent event;
158    ClientInformation *temp = head_->next();
159    while(temp!=0){
160      enet_peer_disconnect(temp->getPeer(), 0);
161      temp = temp->next();
162    }
163    temp = temp->next();
164    while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) > 0){
165      switch (event.type)
166      {
167        case ENET_EVENT_TYPE_NONE:
168        case ENET_EVENT_TYPE_CONNECT:
169        case ENET_EVENT_TYPE_RECEIVE:
170          enet_packet_destroy(event.packet);
171          break;
172        case ENET_EVENT_TYPE_DISCONNECT:
173          delete head_->findClient(&(event.peer->address));
174          temp = temp->next();
175          break;
176      }
177    }
178    return;
179  }
180
181  bool ConnectionManager::processData(ENetEvent *event){
182    // just add packet to the buffer
183    // this can be extended with some preprocessing
184    return buffer.push(event);
185  }
186
187//   bool ConnectionManager::clientDisconnect(ENetPeer *peer){
188//     return clientDisconnect(*peer);
189//   }
190 
191 
192 
193  bool ConnectionManager::clientDisconnect(ENetPeer *peer){
194    return head_->removeClient(peer);
195    return true;
196  }
197
198  bool ConnectionManager::addClient(ENetEvent *event){
199    ClientInformation *temp = head_->insertBack(new ClientInformation);
200    temp->setID(temp->prev()->getID()+1);
201    temp->setPeer(event->peer);
202    std::cout << "added client id: " << temp->prev()->getID() << std::endl;
203    return true;
204  }
205 
206  int ConnectionManager::getClientID(ENetPeer peer){
207    return getClientID(peer.address);
208  }
209 
210  int ConnectionManager::getClientID(ENetAddress address){
211    return head_->findClient(&address)->getID();
212  }
213 
214  ENetPeer *ConnectionManager::getClientPeer(int clientID){
215    return head_->findClient(clientID)->getPeer();
216  }
217 
218  void ConnectionManager::syncClassid(int clientID){
219    int i=0;
220    std::string classname;
221    bool abort=false;
222    orxonox::Identifier *id;
223    while(!abort){
224      id = orxonox::ID(i);
225      if(id == NULL)
226        abort=true;
227      else{
228        classname = id->getName();
229        addPacket(packet_gen.clid( i, classname ),clientID);
230      }
231      ++i;
232    }
233    sendPackets();
234  }
235 
236}
Note: See TracBrowser for help on using the repository browser.