Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 1, 2008, 3:54:20 PM (16 years ago)
Author:
rgrieder
Message:
  • @everyone: Do not create a branch until I've added the svn:eol-style property correctly. Otherwise this would cost me another 4 hours or so when we want to merge back.
  • merged network branch back to trunk
  • I had to omit the changes from last evening concerning the line endings
  • might not work yet because of the line endings
  • @beni: script branch is the only branch still open. you probably will have to apply a patch because of inconsistent new lines
File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/network/ClientConnection.cc

    r1293 r1502  
    5050{
    5151  //static boost::thread_group network_threads;
     52
     53  boost::recursive_mutex ClientConnection::enet_mutex_;
    5254
    5355  ClientConnection::ClientConnection(int port, std::string address) {
     
    7577
    7678
    77   ENetPacket *ClientConnection::getPacket(ENetAddress &address) {
     79  /*ENetPacket *ClientConnection::getPacket(ENetAddress &address) {
    7880    if(!buffer.isEmpty()) {
    7981      //std::cout << "###BUFFER IS NOT EMPTY###" << std::endl;
     
    8890    ENetAddress address; //sems that address is not needed
    8991    return getPacket(address);
     92  }*/
     93 
     94  ENetEvent *ClientConnection::getEvent(){
     95    if(!buffer.isEmpty())
     96      return buffer.pop();
     97    else
     98      return NULL;
    9099  }
    91100
     
    98107    //network_threads.create_thread(boost::bind(boost::mem_fn(&ClientConnection::receiverThread), this));
    99108    // wait 10 seconds for the connection to be established
    100     return waitEstablished(10000);
     109    return waitEstablished(3000);
    101110  }
    102111
     
    117126      return false;
    118127    }
     128    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
    119129    if(enet_peer_send(server, 0, packet)<0)
    120130      return false;
     
    123133  }
    124134
    125   bool ClientConnection::sendPackets(ENetEvent *event) {
     135  bool ClientConnection::sendPackets() {
    126136    if(server==NULL)
    127137      return false;
    128     if(enet_host_service(client, event, NETWORK_SEND_WAIT)>=0){
    129       return true;}
    130     else
    131       return false;
    132   }
    133 
    134   bool ClientConnection::sendPackets() {
    135     ENetEvent event;
    136     if(server==NULL)
    137       return false;
    138     if(enet_host_service(client, &event, NETWORK_SEND_WAIT)>=0){
    139       return true;}
    140     else
    141       return false;
     138    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     139    enet_host_flush(client);
     140    lock.unlock();
     141    return true;
    142142  }
    143143
    144144  void ClientConnection::receiverThread() {
    145145    // what about some error-handling here ?
    146     enet_initialize();
    147146    atexit(enet_deinitialize);
    148     ENetEvent event;
    149     client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0);
    150     if(client==NULL)
     147    ENetEvent *event;
     148    {
     149      boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     150      enet_initialize();
     151      client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0);
     152      lock.unlock();
     153    }
     154    if(client==NULL) {
     155      COUT(2) << "ClientConnection: could not create client host" << std::endl;
    151156      // add some error handling here ==========================
    152157      quit=true;
     158    }
    153159    //connect to the server
    154160    if(!establishConnection()){
     161      COUT(2) << "clientConn: receiver thread: could not establishConnection" << std::endl;
    155162      quit=true;
    156163      return;
    157164    }
     165    event = new ENetEvent;
    158166    //main loop
    159167    while(!quit){
    160168      //std::cout << "connection loop" << std::endl;
    161       if(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT)<0){
    162         // we should never reach this point
    163         quit=true;
    164         // add some error handling here ========================
     169      {
     170        boost::recursive_mutex::scoped_lock lock(enet_mutex_);
     171        if(enet_host_service(client, event, NETWORK_CLIENT_TIMEOUT)<0){
     172          // we should never reach this point
     173          quit=true;
     174          continue;
     175          // add some error handling here ========================
     176        }
     177        lock.unlock();
    165178      }
    166       switch(event.type){
     179      switch(event->type){
    167180        // log handling ================
    168181      case ENET_EVENT_TYPE_CONNECT:
     182        break;
    169183      case ENET_EVENT_TYPE_RECEIVE:
    170184        COUT(5) << "Cl.Con: receiver-Thread while loop: got new packet" << std::endl;
    171         if ( !processData(&event) ) COUT(2) << "Current packet was not pushed to packetBuffer -> ev ongoing SegFault" << std::endl;
     185        if ( !processData(event) ) COUT(2) << "Current packet was not pushed to packetBuffer -> ev ongoing SegFault" << std::endl;
    172186        COUT(5) << "Cl.Con: processed Data in receiver-thread while loop" << std::endl;
     187        event = new ENetEvent;
    173188        break;
    174189      case ENET_EVENT_TYPE_DISCONNECT:
     
    178193        break;
    179194      case ENET_EVENT_TYPE_NONE:
    180         continue;
     195        //receiverThread_->yield();
     196        usleep(1000);
     197        break;
    181198      }
    182       //receiverThread_->yield();
    183199    }
    184200    // now disconnect
     
    186202    if(!disconnectConnection())
    187203      // if disconnecting failed destroy conn.
     204      boost::recursive_mutex::scoped_lock lock(enet_mutex_);
    188205      enet_peer_reset(server);
    189206    return;
     
    192209  bool ClientConnection::disconnectConnection() {
    193210    ENetEvent event;
     211    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
    194212    enet_peer_disconnect(server, 0);
    195213    while(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT) > 0){
     
    212230    ENetEvent event;
    213231    // connect to peer (server is type ENetPeer*)
     232    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
    214233    server = enet_host_connect(client, &serverAddress, NETWORK_CLIENT_CHANNELS);
    215     if(server==NULL)
     234    if(server==NULL) {
     235      COUT(2) << "ClientConnection: server == NULL" << std::endl;
    216236      // error handling
    217237      return false;
     238    }
    218239    // handshake
    219     if(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT)>0 && event.type == ENET_EVENT_TYPE_CONNECT){
     240    if(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT)>=0 && event.type == ENET_EVENT_TYPE_CONNECT){
    220241      established=true;
    221242      return true;
    222243    }
    223     else
    224       return false;
     244    else {
     245      COUT(2) << "ClientConnection: enet_host_service < 0 or event.type != ENET_EVENT_TYPE_CONNECT # EVENT:" << event.type << std::endl;
     246      return false;
     247    }
    225248  }
    226249
Note: See TracChangeset for help on using the changeset viewer.