Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Feb 13, 2011, 9:34:22 PM (14 years ago)
Author:
scheusso
Message:

-some cleaning up
-fixing disconnect behaviour
-trying to find a bug

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/network6/src/libraries/network/Connection.cc

    r7825 r7878  
    3838
    3939#include "packet/Packet.h"
     40#include <util/Sleep.h>
    4041
    4142namespace orxonox
    4243{
    4344  const boost::posix_time::millisec NETWORK_COMMUNICATION_THREAD_WAIT_TIME(20);
     45  const unsigned int                NETWORK_DISCONNECT_TIMEOUT = 500;
    4446
    4547  Connection::Connection(uint32_t firstPeerID):
     
    5052    this->incomingEventsMutex_ = new boost::mutex;
    5153    this->outgoingEventsMutex_ = new boost::mutex;
     54    this->overallMutex_ = new boost::mutex;
    5255  }
    5356
     
    7780  void Connection::disconnectPeer(uint32_t peerID)
    7881  {
     82    this->overallMutex_->lock();
    7983    outgoingEvent outEvent = { peerID, outgoingEventType::disconnectPeer, 0, 0 };
    8084   
     
    8286    this->outgoingEvents_.push_back(outEvent);
    8387    this->outgoingEventsMutex_->unlock();
     88    this->overallMutex_->unlock();
    8489  }
    8590 
     
    95100  void Connection::addPacket(ENetPacket* packet, uint32_t peerID, uint8_t channelID)
    96101  {
     102    this->overallMutex_->lock();
    97103    outgoingEvent outEvent = { peerID, outgoingEventType::sendPacket, packet, channelID };
    98104   
     
    100106    this->outgoingEvents_.push_back(outEvent);
    101107    this->outgoingEventsMutex_->unlock();
     108    this->overallMutex_->unlock();
    102109  }
    103110 
    104111  void Connection::broadcastPacket(ENetPacket* packet, uint8_t channelID)
    105112  {
     113    this->overallMutex_->lock();
    106114    outgoingEvent outEvent = { 0, outgoingEventType::broadcastPacket, packet, channelID };
    107115   
     
    109117    this->outgoingEvents_.push_back(outEvent);
    110118    this->outgoingEventsMutex_->unlock();
     119    this->overallMutex_->unlock();
    111120  }
    112121
     
    116125    ENetEvent event;
    117126   
     127    this->overallMutex_->lock();
    118128    while( bCommunicationThreadRunning_ )
    119129    {
     
    123133        processIncomingEvent(event);
    124134      }
     135     
     136      this->overallMutex_->unlock();
     137      msleep(10);
     138      this->overallMutex_->lock();
    125139     
    126140      // Send all waiting outgoing packets
     
    149163      }
    150164    }
     165    this->overallMutex_->unlock();
    151166  }
    152167 
     
    209224        break;
    210225      case outgoingEventType::disconnectPeers:
    211         while( this->peerMap_.size()!=0 )
    212         {
    213           peer = this->peerMap_.begin()->second;
    214           enet_peer_disconnect(peer, 0);
    215         }
     226        disconnectPeersInternal();
    216227        break;
    217228      case outgoingEventType::broadcastPacket:
     
    223234  }
    224235
     236
     237  void Connection::disconnectPeersInternal()
     238  {
     239    std::map<uint32_t, ENetPeer*>::iterator it;
     240    for( it=this->peerMap_.begin(); it!=this->peerMap_.end(); ++it )
     241    {
     242      enet_peer_disconnect(it->second, 0);
     243    }
     244    uint32_t iterations = NETWORK_DISCONNECT_TIMEOUT/NETWORK_WAIT_TIMEOUT;
     245    uint32_t i = 0;
     246    while( this->peerMap_.size() && i++ < iterations )
     247    {
     248      ENetEvent event;
     249      if( enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0 )
     250      {
     251        processIncomingEvent(event);
     252      }
     253    }
     254  }
    225255
    226256  void Connection::processQueue()
     
    261291    }
    262292  }
     293 
     294  void Connection::waitOutgoingQueue()
     295  {
     296    uint32_t outgoingEventsCount;
     297    this->outgoingEventsMutex_->lock();
     298    outgoingEventsCount = this->outgoingEvents_.size();
     299    this->outgoingEventsMutex_->unlock();
     300    while( outgoingEventsCount )
     301    {
     302      msleep(1);
     303      this->outgoingEventsMutex_->lock();
     304      outgoingEventsCount = this->outgoingEvents_.size();
     305      this->outgoingEventsMutex_->unlock();
     306    }
     307  }
     308
    263309
    264310  incomingEvent Connection::preprocessConnectEvent(ENetEvent& event)
Note: See TracChangeset for help on using the changeset viewer.