Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 604 was 514, checked in by nicolasc, 17 years ago

added copyright notice

File size: 7.1 KB
RevLine 
[514]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
[173]28//
29// C++ Interface: ConnectionManager
30//
[285]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
[173]33// connection processes is provided by ...
34//
35//
[196]36// Author:  Oliver Scheuss
[173]37//
38
[285]39#include "ConnectionManager.h"
[173]40
[381]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
[173]50namespace network{
[285]51
[196]52  boost::thread_group network_threads;
[514]53
[436]54  ConnectionManager::ConnectionManager(ClientInformation *head){
[173]55    quit=false;
[196]56    bindAddress.host = ENET_HOST_ANY;
[173]57    bindAddress.port = NETWORK_PORT;
[436]58    head_ = head;
[173]59  }
[285]60
[436]61  ConnectionManager::ConnectionManager(int port, std::string address, ClientInformation *head){
[173]62    quit=false;
[230]63    enet_address_set_host (& bindAddress, address.c_str());
[173]64    bindAddress.port = NETWORK_PORT;
[436]65    head_ = head;
[173]66  }
[285]67
[436]68  ConnectionManager::ConnectionManager(int port, const char *address, ClientInformation *head){
[230]69    quit=false;
70    enet_address_set_host (& bindAddress, address);
71    bindAddress.port = NETWORK_PORT;
[436]72    head_ = head;
[230]73  }
[285]74
[204]75  ENetPacket *ConnectionManager::getPacket(ENetAddress &address){
[196]76    if(!buffer.isEmpty())
[204]77      return buffer.pop(address);
[196]78    else
79        return NULL;
[173]80  }
[514]81
[380]82  ENetPacket *ConnectionManager::getPacket(int &clientID){
83    ENetAddress address;
[446]84    ENetPacket *packet=getPacket(address);
[444]85    ClientInformation *temp =head_->findClient(&address);
86    clientID=temp->getID();
[380]87    return packet;
88  }
[285]89
[196]90  bool ConnectionManager::queueEmpty(){
91    return buffer.isEmpty();
[173]92  }
[285]93
[196]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  }
[285]99
[196]100  bool ConnectionManager::quitListener(){
[173]101    quit=true;
[196]102    network_threads.join_all();
[173]103    return true;
104  }
[285]105
[196]106  bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer){
[436]107    if(enet_peer_send(peer, head_->findClient(&(peer->address))->getID() , packet)!=0)
[196]108      return false;
109    return true;
110  }
[514]111
[380]112  bool ConnectionManager::addPacket(ENetPacket *packet, int clientID){
[436]113    if(enet_peer_send(head_->findClient(clientID)->getPeer(), clientID, packet)!=0)
[196]114      return false;
[380]115    return true;
[196]116  }
[514]117
[196]118  bool ConnectionManager::addPacketAll(ENetPacket *packet){
[436]119    for(ClientInformation *i=head_->next(); i!=0; i=i->next()){
120      if(enet_peer_send(i->getPeer(), i->getID(), packet)!=0)
[196]121         return false;
122    }
123    return true;
124  }
[285]125
[196]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;
[285]131    else
[196]132      return false;
133  }
[514]134
[369]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  }
[285]144
[196]145  void ConnectionManager::receiverThread(){
146    // what about some error-handling here ?
147    enet_initialize();
148    atexit(enet_deinitialize);
[173]149    ENetEvent event;
150    server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0);
[369]151    if(server==NULL){
[173]152      // add some error handling here ==========================
153      quit=true;
[369]154      return;
155    }
[285]156
[173]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 ================
[196]165        case ENET_EVENT_TYPE_CONNECT:
166        addClient(&event);
[173]167        break;
[196]168      case ENET_EVENT_TYPE_RECEIVE:
[505]169        //std::cout << "received data" << std::endl;
[196]170        processData(&event);
[173]171        break;
[196]172      case ENET_EVENT_TYPE_DISCONNECT:
[173]173        // add some error/log handling here
174        clientDisconnect(event.peer);
175        break;
[352]176      case ENET_EVENT_TYPE_NONE:
177        break;
[173]178      }
179    }
[369]180    disconnectClients();
[173]181    // if we're finishied, destroy server
182    enet_host_destroy(server);
183  }
[514]184
[369]185  void ConnectionManager::disconnectClients(){
186    ENetEvent event;
[436]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          delete head_->findClient(&(event.peer->address));
203          temp = temp->next();
204          break;
[369]205      }
206    }
207    return;
208  }
[285]209
[196]210  bool ConnectionManager::processData(ENetEvent *event){
211    // just add packet to the buffer
212    // this can be extended with some preprocessing
[204]213    return buffer.push(event);
[173]214  }
[285]215
[436]216//   bool ConnectionManager::clientDisconnect(ENetPeer *peer){
217//     return clientDisconnect(*peer);
218//   }
[514]219
220
221
[196]222  bool ConnectionManager::clientDisconnect(ENetPeer *peer){
[436]223    return head_->removeClient(peer);
[380]224    return true;
225  }
[285]226
[196]227  bool ConnectionManager::addClient(ENetEvent *event){
[436]228    ClientInformation *temp = head_->insertBack(new ClientInformation);
[442]229    temp->setID(temp->prev()->getID()+1);
[436]230    temp->setPeer(event->peer);
[442]231    std::cout << "added client id: " << temp->prev()->getID() << std::endl;
[173]232    return true;
233  }
[514]234
[380]235  int ConnectionManager::getClientID(ENetPeer peer){
[436]236    return getClientID(peer.address);
[380]237  }
[514]238
[380]239  int ConnectionManager::getClientID(ENetAddress address){
[436]240    return head_->findClient(&address)->getID();
[380]241  }
[514]242
[436]243  ENetPeer *ConnectionManager::getClientPeer(int clientID){
244    return head_->findClient(clientID)->getPeer();
[380]245  }
[514]246
[400]247  void ConnectionManager::syncClassid(int clientID){
248    int i=0;
249    std::string classname;
250    bool abort=false;
251    orxonox::Identifier *id;
252    while(!abort){
[496]253      id = ID(i);
[400]254      if(id == NULL)
255        abort=true;
256      else{
257        classname = id->getName();
258        addPacket(packet_gen.clid( i, classname ),clientID);
259      }
[401]260      ++i;
[400]261    }
262    sendPackets();
263  }
[514]264
[173]265}
Note: See TracBrowser for help on using the repository browser.