Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/network/ConnectionManager.cc @ 1119

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