Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1907 was 1907, checked in by scheusso, 16 years ago

merged network branch back to trunk

  • Property svn:eol-style set to native
File size: 10.6 KB
RevLine 
[1282]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 */
28
29//
30// C++ Interface: ConnectionManager
31//
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
34// connection processes is provided by ...
35//
36//
37// Author:  Oliver Scheuss
38//
39
[1842]40#include "ConnectionManager.h"
41
[1282]42#include <iostream>
[1735]43#include <assert.h>
[1282]44// boost.thread library for multithreading support
[1755]45#include <boost/thread/thread.hpp>
[1282]46#include <boost/bind.hpp>
47
48#include "core/CoreIncludes.h"
49#include "core/BaseObject.h"
[1747]50#include "core/Iterator.h"
[1502]51#include "objects/SpaceShip.h"
[1282]52#include "util/Math.h"
[1502]53#include "util/Sleep.h"
[1282]54#include "ClientInformation.h"
55#include "Synchronisable.h"
[1735]56#include "packet/ClassID.h"
[1282]57
58namespace std
59{
60  bool operator< (ENetAddress a, ENetAddress b) {
61    if(a.host <= b.host)
62      return true;
63    else
64      return false;
65  }
66}
67
68namespace network
69{
70  //boost::thread_group network_threads;
[1747]71
[1735]72  ConnectionManager *ConnectionManager::instance_=0;
[1747]73
[1735]74  ConnectionManager::ConnectionManager():receiverThread_(0){
75    assert(instance_==0);
76    instance_=this;
[1282]77    quit=false;
78    bindAddress.host = ENET_HOST_ANY;
79    bindAddress.port = NETWORK_PORT;
80  }
[1735]81  boost::recursive_mutex ConnectionManager::enet_mutex;
[1747]82
[1735]83  ConnectionManager::ConnectionManager(int port){
84    assert(instance_==0);
85    instance_=this;
[1502]86    quit=false;
87    bindAddress.host = ENET_HOST_ANY;
88    bindAddress.port = port;
89  }
[1282]90
[1735]91  ConnectionManager::ConnectionManager(int port, std::string address) :receiverThread_(0) {
92    assert(instance_==0);
93    instance_=this;
[1282]94    quit=false;
95    enet_address_set_host (& bindAddress, address.c_str());
96    bindAddress.port = NETWORK_PORT;
97  }
98
[1735]99  ConnectionManager::ConnectionManager(int port, const char *address) : receiverThread_(0) {
100    assert(instance_==0);
101    instance_=this;
[1282]102    quit=false;
103    enet_address_set_host (& bindAddress, address);
104    bindAddress.port = NETWORK_PORT;
105  }
[1747]106
[1735]107  ConnectionManager::~ConnectionManager(){
108    if(!quit)
109      quitListener();
[1907]110    instance_=0;
[1735]111  }
[1282]112
[1747]113
[1502]114  ENetEvent *ConnectionManager::getEvent(){
115    if(!buffer.isEmpty())
116      return buffer.pop();
117    else
118      return NULL;
[1282]119  }
120
121  bool ConnectionManager::queueEmpty() {
122    return buffer.isEmpty();
123  }
124
125  void ConnectionManager::createListener() {
126    receiverThread_ = new boost::thread(boost::bind(&ConnectionManager::receiverThread, this));
127    //network_threads.create_thread(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this));
128         //boost::thread thr(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this));
129    return;
130  }
131
132  bool ConnectionManager::quitListener() {
133    quit=true;
134    //network_threads.join_all();
135    receiverThread_->join();
136    return true;
137  }
[1747]138
139
[1282]140  bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer) {
[1735]141    boost::recursive_mutex::scoped_lock lock(instance_->enet_mutex);
142    if(enet_peer_send(peer, NETWORK_DEFAULT_CHANNEL, packet)!=0)
[1282]143      return false;
144    return true;
145  }
146
147  bool ConnectionManager::addPacket(ENetPacket *packet, int clientID) {
[1735]148    ClientInformation *temp = ClientInformation::findClient(clientID);
[1502]149    if(!temp){
150      COUT(3) << "C.Man: addPacket findClient failed" << std::endl;
[1282]151      return false;
[1502]152    }
[1735]153    return addPacket(packet, temp->getPeer());
[1282]154  }
155
156  bool ConnectionManager::addPacketAll(ENetPacket *packet) {
[1735]157    if(!instance_)
158      return false;
159    boost::recursive_mutex::scoped_lock lock(instance_->enet_mutex);
160    for(ClientInformation *i=ClientInformation::getBegin()->next(); i!=0; i=i->next()){
[1534]161      COUT(3) << "adding broadcast packet for client: " << i->getID() << std::endl;
162      if(enet_peer_send(i->getPeer(), 0, packet)!=0)
[1282]163        return false;
164    }
165    return true;
166  }
167
[1502]168  // we actually dont need that function, because host_service does that for us
[1282]169  bool ConnectionManager::sendPackets() {
[1735]170    if(server==NULL || !instance_)
[1282]171      return false;
[1735]172    boost::recursive_mutex::scoped_lock lock(enet_mutex);
[1502]173    enet_host_flush(server);
174    lock.unlock();
175    return true;
[1282]176  }
177
178  void ConnectionManager::receiverThread() {
179    // what about some error-handling here ?
[1502]180    ENetEvent *event;
[1282]181    atexit(enet_deinitialize);
[1502]182    { //scope of the mutex
[1735]183      boost::recursive_mutex::scoped_lock lock(enet_mutex);
[1502]184      enet_initialize();
185      server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0);
186      lock.unlock();
187    }
[1282]188    if(server==NULL){
189      // add some error handling here ==========================
190      quit=true;
191      return;
192    }
193
[1502]194    event = new ENetEvent;
[1282]195    while(!quit){
[1502]196      { //mutex scope
[1735]197        boost::recursive_mutex::scoped_lock lock(enet_mutex);
[1502]198        if(enet_host_service(server, event, NETWORK_WAIT_TIMEOUT)<0){
199          // we should never reach this point
200          quit=true;
201          continue;
202          // add some error handling here ========================
203        }
204        lock.unlock();
[1282]205      }
206      switch(event->type){
207        // log handling ================
208        case ENET_EVENT_TYPE_CONNECT:
[1502]209          COUT(3) << "adding event_type_connect to queue" << std::endl;
210        case ENET_EVENT_TYPE_DISCONNECT:
211          //addClient(event);
[1282]212          //this is a workaround to ensure thread safety
[1502]213          //COUT(5) << "Con.Man: connection event has occured" << std::endl;
214          //break;
[1282]215        case ENET_EVENT_TYPE_RECEIVE:
216          //std::cout << "received data" << std::endl;
217          COUT(5) << "Con.Man: receive event has occured" << std::endl;
218          // only add, if client has connected yet and not been disconnected
[1502]219          //if(head_->findClient(&event->peer->address))
[1282]220            processData(event);
[1502]221            event = new ENetEvent;
222//           else
223//             COUT(3) << "received a packet from a client we don't know" << std::endl;
[1282]224          break;
[1502]225        //case ENET_EVENT_TYPE_DISCONNECT:
226          //clientDisconnect(event->peer);
227          //break;
[1282]228        case ENET_EVENT_TYPE_NONE:
[1502]229          //receiverThread_->yield();
230          usleep(1000);
[1282]231          break;
232      }
233//       usleep(100);
[1502]234      //receiverThread_->yield(); //TODO: find apropriate
[1282]235    }
236    disconnectClients();
237    // if we're finishied, destroy server
[1502]238    {
[1735]239      boost::recursive_mutex::scoped_lock lock(enet_mutex);
[1502]240      enet_host_destroy(server);
241      lock.unlock();
242    }
[1282]243  }
[1747]244
[1282]245  //### added some bugfixes here, but we cannot test them because
246  //### the server crashes everytime because of some gamestates
247  //### (trying to resolve that now)
248  void ConnectionManager::disconnectClients() {
249    ENetEvent event;
[1735]250    ClientInformation *temp = ClientInformation::getBegin()->next();
[1282]251    while(temp!=0){
[1502]252      {
[1735]253        boost::recursive_mutex::scoped_lock lock(enet_mutex);
[1502]254        enet_peer_disconnect(temp->getPeer(), 0);
255        lock.unlock();
256      }
[1282]257      temp = temp->next();
258    }
259    //bugfix: might be the reason why server crashes when clients disconnects
[1735]260    temp = ClientInformation::getBegin()->next();
261    boost::recursive_mutex::scoped_lock lock(enet_mutex);
[1502]262    while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) >= 0){
[1282]263      switch (event.type)
264      {
265      case ENET_EVENT_TYPE_NONE: break;
266      case ENET_EVENT_TYPE_CONNECT: break;
267      case ENET_EVENT_TYPE_RECEIVE:
268        enet_packet_destroy(event.packet);
269        break;
270      case ENET_EVENT_TYPE_DISCONNECT:
271        COUT(4) << "disconnecting all clients" << std::endl;
[1735]272        if(ClientInformation::findClient(&(event.peer->address)))
273          delete ClientInformation::findClient(&(event.peer->address));
[1282]274        //maybe needs bugfix: might also be a reason for the server to crash
275        temp = temp->next();
276        break;
277      }
278    }
279    return;
280  }
281
282  bool ConnectionManager::processData(ENetEvent *event) {
283    // just add packet to the buffer
284    // this can be extended with some preprocessing
285    return buffer.push(event);
286  }
287
288
[1502]289
[1282]290  int ConnectionManager::getClientID(ENetPeer peer) {
291    return getClientID(peer.address);
292  }
293
294  int ConnectionManager::getClientID(ENetAddress address) {
[1735]295    return ClientInformation::findClient(&address)->getID();
[1282]296  }
297
298  ENetPeer *ConnectionManager::getClientPeer(int clientID) {
[1735]299    return ClientInformation::findClient(clientID)->getPeer();
[1282]300  }
301
[1735]302  /**
[1747]303   *
304   * @param clientID
[1735]305   */
306  void ConnectionManager::syncClassid(unsigned int clientID) {
[1360]307    unsigned int network_id=0, failures=0;
[1282]308    std::string classname;
309    orxonox::Identifier *id;
[1856]310    std::map<std::string, orxonox::Identifier*>::const_iterator it = orxonox::Factory::getFactoryMapBegin();
311    while(it != orxonox::Factory::getFactoryMapEnd()){
[1282]312      id = (*it).second;
313      if(id == NULL)
314        continue;
315      classname = id->getName();
316      network_id = id->getNetworkID();
[1360]317      if(network_id==0)
318        COUT(3) << "we got a null class id: " << id->getName() << std::endl;
[1282]319      COUT(4) << "Con.Man:syncClassid:\tnetwork_id: " << network_id << ", classname: " << classname << std::endl;
320
[1735]321      packet::ClassID *classid = new packet::ClassID( network_id, classname );
322      classid->setClientID(clientID);
323      while(!classid->send() && failures < 10){
[1360]324        failures++;
325      }
[1282]326      ++it;
327    }
[1502]328    //sendPackets();
[1282]329    COUT(4) << "syncClassid:\tall synchClassID packets have been sent" << std::endl;
330  }
331
[1747]332
333
[1282]334  bool ConnectionManager::removeShip(ClientInformation *client){
[1735]335    unsigned int id=client->getShipID();
[1747]336    orxonox::ObjectList<orxonox::SpaceShip>::iterator it;
337    for(it = orxonox::ObjectList<orxonox::SpaceShip>::begin(); it; ++it){
[1907]338      if(it->getObjectID()!=id)
[1282]339        continue;
340      delete *it;
341    }
342    return true;
343  }
[1747]344
345
[1282]346  void ConnectionManager::disconnectClient(ClientInformation *client){
[1502]347    {
[1735]348      boost::recursive_mutex::scoped_lock lock(enet_mutex);
[1502]349      enet_peer_disconnect(client->getPeer(), 0);
350      lock.unlock();
351    }
[1282]352    removeShip(client);
353  }
354
[1747]355
[1282]356}
Note: See TracBrowser for help on using the repository browser.