Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 757 was 681, checked in by rgrieder, 17 years ago
  • removed a warning and added on FIXME
File size: 7.4 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Oliver Scheuss, (C) 2007
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28//
29// C++ Interface: ConnectionManager
30//
31// Description: The Class ConnectionManager manages the servers conenctions to the clients.
32// each connection is provided by a new process. communication between master process and
33// connection processes is provided by ...
34//
35//
36// Author:  Oliver Scheuss
37//
38
39#include "ConnectionManager.h"
40
41namespace std{
42  bool operator< (ENetAddress a, ENetAddress b){
43    if(a.host <= b.host)
44      return true;
45    else
46      return false;
47  }
48}
49
50namespace network{
51
52  boost::thread_group network_threads;
53
54  ConnectionManager::ConnectionManager(ClientInformation *head){
55    quit=false;
56    bindAddress.host = ENET_HOST_ANY;
57    bindAddress.port = NETWORK_PORT;
58    head_ = head;
59  }
60
61  ConnectionManager::ConnectionManager(int port, std::string address, ClientInformation *head){
62    quit=false;
63    enet_address_set_host (& bindAddress, address.c_str());
64    bindAddress.port = NETWORK_PORT;
65    head_ = head;
66  }
67
68  ConnectionManager::ConnectionManager(int port, const char *address, ClientInformation *head){
69    quit=false;
70    enet_address_set_host (& bindAddress, address);
71    bindAddress.port = NETWORK_PORT;
72    head_ = head;
73  }
74
75  ENetPacket *ConnectionManager::getPacket(ENetAddress &address){
76    if(!buffer.isEmpty())
77      return buffer.pop(address);
78    else
79        return NULL;
80  }
81
82  ENetPacket *ConnectionManager::getPacket(int &clientID){
83    ENetAddress address;
84    ENetPacket *packet=getPacket(address);
85    ClientInformation *temp =head_->findClient(&address);
86    clientID=temp->getID();
87    return packet;
88  }
89
90  bool ConnectionManager::queueEmpty(){
91    return buffer.isEmpty();
92  }
93
94  void ConnectionManager::createListener(){
95    network_threads.create_thread(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this));
96//     boost::thread thr(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this));
97    return;
98  }
99
100  bool ConnectionManager::quitListener(){
101    quit=true;
102    network_threads.join_all();
103    return true;
104  }
105
106  bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer){
107    if(enet_peer_send(peer, head_->findClient(&(peer->address))->getID() , packet)!=0)
108      return false;
109    return true;
110  }
111
112  bool ConnectionManager::addPacket(ENetPacket *packet, int clientID){
113    if(enet_peer_send(head_->findClient(clientID)->getPeer(), clientID, packet)!=0)
114      return false;
115    return true;
116  }
117
118  bool ConnectionManager::addPacketAll(ENetPacket *packet){
119    for(ClientInformation *i=head_->next(); i!=0; i=i->next()){
120      if(enet_peer_send(i->getPeer(), i->getID(), packet)!=0)
121         return false;
122    }
123    return true;
124  }
125
126  bool ConnectionManager::sendPackets(ENetEvent *event){
127    if(server==NULL)
128      return false;
129    if(enet_host_service(server, event, NETWORK_SEND_WAIT)>=0)
130      return true;
131    else
132      return false;
133  }
134
135  bool ConnectionManager::sendPackets(){
136    ENetEvent event;
137    if(server==NULL)
138      return false;
139    if(enet_host_service(server, &event, NETWORK_SEND_WAIT)>=0)
140      return true;
141    else
142      return false;
143  }
144
145  void ConnectionManager::receiverThread(){
146    // what about some error-handling here ?
147    enet_initialize();
148    atexit(enet_deinitialize);
149    ENetEvent event;
150    server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0);
151    if(server==NULL){
152      // add some error handling here ==========================
153      quit=true;
154      return;
155    }
156
157    while(!quit){
158      if(enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT)<0){
159        // we should never reach this point
160        quit=true;
161        // add some error handling here ========================
162      }
163      switch(event.type){
164        // log handling ================
165        case ENET_EVENT_TYPE_CONNECT:
166        addClient(&event);
167        break;
168      case ENET_EVENT_TYPE_RECEIVE:
169        //std::cout << "received data" << std::endl;
170        processData(&event);
171        break;
172      case ENET_EVENT_TYPE_DISCONNECT:
173        // add some error/log handling here
174        clientDisconnect(event.peer);
175        break;
176      case ENET_EVENT_TYPE_NONE:
177        break;
178      }
179    }
180    disconnectClients();
181    // if we're finishied, destroy server
182    enet_host_destroy(server);
183  }
184
185  void ConnectionManager::disconnectClients(){
186    ENetEvent event;
187    ClientInformation *temp = head_->next();
188    while(temp!=0){
189      enet_peer_disconnect(temp->getPeer(), 0);
190      temp = temp->next();
191    }
192    temp = temp->next();
193    while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) > 0){
194      switch (event.type)
195      {
196        case ENET_EVENT_TYPE_NONE:
197        case ENET_EVENT_TYPE_CONNECT:
198        case ENET_EVENT_TYPE_RECEIVE:
199          enet_packet_destroy(event.packet);
200          break;
201        case ENET_EVENT_TYPE_DISCONNECT:
202          std::cout << "disconnecting client" << std::endl;
203          delete head_->findClient(&(event.peer->address));
204          temp = temp->next();
205          break;
206      }
207    }
208    return;
209  }
210
211  bool ConnectionManager::processData(ENetEvent *event){
212    // just add packet to the buffer
213    // this can be extended with some preprocessing
214    return buffer.push(event);
215  }
216
217//   bool ConnectionManager::clientDisconnect(ENetPeer *peer){
218//     return clientDisconnect(*peer);
219//   }
220
221
222
223  bool ConnectionManager::clientDisconnect(ENetPeer *peer){
224    return head_->removeClient(peer);
225  }
226
227  bool ConnectionManager::addClient(ENetEvent *event){
228    ClientInformation *temp = head_->insertBack(new ClientInformation);
229    if(temp->prev()->head)
230      temp->setID(1);
231    else
232      temp->setID(temp->prev()->getID()+1);
233    temp->setPeer(event->peer);
234    std::cout << "added client id: " << temp->getID() << std::endl;
235    syncClassid(temp->getID());
236    temp->setSynched(true);
237    return true;
238  }
239
240  int ConnectionManager::getClientID(ENetPeer peer){
241    return getClientID(peer.address);
242  }
243
244  int ConnectionManager::getClientID(ENetAddress address){
245    return head_->findClient(&address)->getID();
246  }
247
248  ENetPeer *ConnectionManager::getClientPeer(int clientID){
249    return head_->findClient(clientID)->getPeer();
250  }
251
252  void ConnectionManager::syncClassid(int clientID){
253    int i=0;
254    std::string classname;
255    bool abort=false;
256    orxonox::Identifier *id;
257    while(!abort){
258      id = ID(i);
259      std::cout << "syncid: " << i << ", ID(id): " << id << std::endl;
260      if(id == NULL){
261        if(i!=0)
262          abort=true;
263        else{
264          ++i;
265          continue;
266        }
267      }
268      else{
269        classname = id->getName();
270        addPacket(packet_gen.clid( i, classname ),clientID);
271      }
272      ++i;
273    }
274    sendPackets();
275  }
276
277}
Note: See TracBrowser for help on using the repository browser.