Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 31, 2007, 7:40:23 PM (16 years ago)
Author:
rgrieder
Message:
  • added dll support to the network library
  • improved header file dependency in network
File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/FICN/src/network/ConnectionManager.cc

    r681 r777  
    11/*
    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  */
     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*/
    2727
    2828//
     
    3737//
    3838
     39#include <iostream>
     40// boost.thread library for multithreading support
     41#include <boost/thread/thread.hpp>
     42#include <boost/bind.hpp>
     43
     44#include "core/CoreIncludes.h"
     45#include "ClientInformation.h"
    3946#include "ConnectionManager.h"
    4047
    41 namespace std{
    42   bool operator< (ENetAddress a, ENetAddress b){
     48namespace std
     49{
     50  bool operator< (ENetAddress a, ENetAddress b) {
    4351    if(a.host <= b.host)
    4452      return true;
     
    4856}
    4957
    50 namespace network{
    51 
     58namespace network
     59{
    5260  boost::thread_group network_threads;
    5361
    54   ConnectionManager::ConnectionManager(ClientInformation *head){
     62  ConnectionManager::ConnectionManager(ClientInformation *head) {
    5563    quit=false;
    5664    bindAddress.host = ENET_HOST_ANY;
     
    5967  }
    6068
    61   ConnectionManager::ConnectionManager(int port, std::string address, ClientInformation *head){
     69  ConnectionManager::ConnectionManager(int port, std::string address, ClientInformation *head) {
    6270    quit=false;
    6371    enet_address_set_host (& bindAddress, address.c_str());
     
    6674  }
    6775
    68   ConnectionManager::ConnectionManager(int port, const char *address, ClientInformation *head){
     76  ConnectionManager::ConnectionManager(int port, const char *address, ClientInformation *head) {
    6977    quit=false;
    7078    enet_address_set_host (& bindAddress, address);
     
    7381  }
    7482
    75   ENetPacket *ConnectionManager::getPacket(ENetAddress &address){
     83  ENetPacket *ConnectionManager::getPacket(ENetAddress &address) {
    7684    if(!buffer.isEmpty())
    7785      return buffer.pop(address);
    7886    else
    79         return NULL;
    80   }
    81 
    82   ENetPacket *ConnectionManager::getPacket(int &clientID){
     87      return NULL;
     88  }
     89
     90  ENetPacket *ConnectionManager::getPacket(int &clientID) {
    8391    ENetAddress address;
    8492    ENetPacket *packet=getPacket(address);
     
    8896  }
    8997
    90   bool ConnectionManager::queueEmpty(){
     98  bool ConnectionManager::queueEmpty() {
    9199    return buffer.isEmpty();
    92100  }
    93101
    94   void ConnectionManager::createListener(){
     102  void ConnectionManager::createListener() {
    95103    network_threads.create_thread(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this));
    96 //     boost::thread thr(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this));
     104    //     boost::thread thr(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this));
    97105    return;
    98106  }
    99107
    100   bool ConnectionManager::quitListener(){
     108  bool ConnectionManager::quitListener() {
    101109    quit=true;
    102110    network_threads.join_all();
     
    104112  }
    105113
    106   bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer){
     114  bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer) {
    107115    if(enet_peer_send(peer, head_->findClient(&(peer->address))->getID() , packet)!=0)
    108116      return false;
     
    110118  }
    111119
    112   bool ConnectionManager::addPacket(ENetPacket *packet, int clientID){
     120  bool ConnectionManager::addPacket(ENetPacket *packet, int clientID) {
    113121    if(enet_peer_send(head_->findClient(clientID)->getPeer(), clientID, packet)!=0)
    114122      return false;
     
    116124  }
    117125
    118   bool ConnectionManager::addPacketAll(ENetPacket *packet){
     126  bool ConnectionManager::addPacketAll(ENetPacket *packet) {
    119127    for(ClientInformation *i=head_->next(); i!=0; i=i->next()){
    120128      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){
     129        return false;
     130    }
     131    return true;
     132  }
     133
     134  bool ConnectionManager::sendPackets(ENetEvent *event) {
    127135    if(server==NULL)
    128136      return false;
     
    133141  }
    134142
    135   bool ConnectionManager::sendPackets(){
     143  bool ConnectionManager::sendPackets() {
    136144    ENetEvent event;
    137145    if(server==NULL)
     
    143151  }
    144152
    145   void ConnectionManager::receiverThread(){
     153  void ConnectionManager::receiverThread() {
    146154    // what about some error-handling here ?
    147155    enet_initialize();
     
    164172        // log handling ================
    165173        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;
     174          addClient(&event);
     175          break;
     176        case ENET_EVENT_TYPE_RECEIVE:
     177          //std::cout << "received data" << std::endl;
     178          processData(&event);
     179          break;
     180        case ENET_EVENT_TYPE_DISCONNECT:
     181          // add some error/log handling here
     182          clientDisconnect(event.peer);
     183          break;
     184        case ENET_EVENT_TYPE_NONE:
     185          break;
    178186      }
    179187    }
     
    183191  }
    184192
    185   void ConnectionManager::disconnectClients(){
     193  void ConnectionManager::disconnectClients() {
    186194    ENetEvent event;
    187195    ClientInformation *temp = head_->next();
     
    194202      switch (event.type)
    195203      {
    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;
     204      case ENET_EVENT_TYPE_NONE:
     205      case ENET_EVENT_TYPE_CONNECT:
     206      case ENET_EVENT_TYPE_RECEIVE:
     207        enet_packet_destroy(event.packet);
     208        break;
     209      case ENET_EVENT_TYPE_DISCONNECT:
     210        std::cout << "disconnecting client" << std::endl;
     211        delete head_->findClient(&(event.peer->address));
     212        temp = temp->next();
     213        break;
    206214      }
    207215    }
     
    209217  }
    210218
    211   bool ConnectionManager::processData(ENetEvent *event){
     219  bool ConnectionManager::processData(ENetEvent *event) {
    212220    // just add packet to the buffer
    213221    // this can be extended with some preprocessing
     
    215223  }
    216224
    217 //   bool ConnectionManager::clientDisconnect(ENetPeer *peer){
    218 //     return clientDisconnect(*peer);
    219 //   }
    220 
    221 
    222 
    223   bool ConnectionManager::clientDisconnect(ENetPeer *peer){
     225  //bool ConnectionManager::clientDisconnect(ENetPeer *peer) {
     226  //  return clientDisconnect(*peer);
     227  //}
     228
     229  bool ConnectionManager::clientDisconnect(ENetPeer *peer) {
    224230    return head_->removeClient(peer);
    225231  }
    226232
    227   bool ConnectionManager::addClient(ENetEvent *event){
     233  bool ConnectionManager::addClient(ENetEvent *event) {
    228234    ClientInformation *temp = head_->insertBack(new ClientInformation);
    229235    if(temp->prev()->head)
     
    238244  }
    239245
    240   int ConnectionManager::getClientID(ENetPeer peer){
     246  int ConnectionManager::getClientID(ENetPeer peer) {
    241247    return getClientID(peer.address);
    242248  }
    243249
    244   int ConnectionManager::getClientID(ENetAddress address){
     250  int ConnectionManager::getClientID(ENetAddress address) {
    245251    return head_->findClient(&address)->getID();
    246252  }
    247253
    248   ENetPeer *ConnectionManager::getClientPeer(int clientID){
     254  ENetPeer *ConnectionManager::getClientPeer(int clientID) {
    249255    return head_->findClient(clientID)->getPeer();
    250256  }
    251257
    252   void ConnectionManager::syncClassid(int clientID){
     258  void ConnectionManager::syncClassid(int clientID) {
    253259    int i=0;
    254260    std::string classname;
Note: See TracChangeset for help on using the changeset viewer.