Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

synchronising of classid↔classname works now

File size: 7.5 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:
[620]202          std::cout << "disconnecting client" << std::endl;
[436]203          delete head_->findClient(&(event.peer->address));
204          temp = temp->next();
205          break;
[369]206      }
207    }
208    return;
209  }
[285]210
[196]211  bool ConnectionManager::processData(ENetEvent *event){
212    // just add packet to the buffer
213    // this can be extended with some preprocessing
[204]214    return buffer.push(event);
[173]215  }
[285]216
[436]217//   bool ConnectionManager::clientDisconnect(ENetPeer *peer){
218//     return clientDisconnect(*peer);
219//   }
[514]220
221
222
[196]223  bool ConnectionManager::clientDisconnect(ENetPeer *peer){
[436]224    return head_->removeClient(peer);
[380]225    return true;
226  }
[285]227
[196]228  bool ConnectionManager::addClient(ENetEvent *event){
[436]229    ClientInformation *temp = head_->insertBack(new ClientInformation);
[620]230    if(temp->prev()->head)
231      temp->setID(1);
232    else
233      temp->setID(temp->prev()->getID()+1);
[436]234    temp->setPeer(event->peer);
[636]235    std::cout << "added client id: " << temp->getID() << std::endl;
236    syncClassid(temp->getID());
237    temp->setSynched(true);
[173]238    return true;
239  }
[514]240
[380]241  int ConnectionManager::getClientID(ENetPeer peer){
[436]242    return getClientID(peer.address);
[380]243  }
[514]244
[380]245  int ConnectionManager::getClientID(ENetAddress address){
[436]246    return head_->findClient(&address)->getID();
[380]247  }
[514]248
[436]249  ENetPeer *ConnectionManager::getClientPeer(int clientID){
250    return head_->findClient(clientID)->getPeer();
[380]251  }
[514]252
[400]253  void ConnectionManager::syncClassid(int clientID){
254    int i=0;
255    std::string classname;
256    bool abort=false;
257    orxonox::Identifier *id;
258    while(!abort){
[496]259      id = ID(i);
[636]260      std::cout << "syncid: " << i << ", ID(id): " << id << std::endl;
261      if(id == NULL){
262        if(i!=0)
263          abort=true;
264        else{
265          ++i;
266          continue;
267        }
268      }
[400]269      else{
270        classname = id->getName();
271        addPacket(packet_gen.clid( i, classname ),clientID);
272      }
[401]273      ++i;
[400]274    }
275    sendPackets();
276  }
[514]277
[173]278}
Note: See TracBrowser for help on using the repository browser.