Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Oct 10, 2018, 3:06:55 PM (6 years ago)
Author:
merholzl
Message:

Merged Masterserver, refresh button had to be removed

Location:
code/branches/mergeFS18
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/mergeFS18

  • code/branches/mergeFS18/src/libraries/network/Connection.cc

    r11071 r12027  
    3838
    3939#include "packet/Packet.h"
     40#include "util/Output.h"
    4041#include <util/Sleep.h>
    4142
     
    4546  const unsigned int                NETWORK_DISCONNECT_TIMEOUT = 500;
    4647
     48  /**
     49   * Constructor
     50   * @param firstPeerId The initial value of nextPeerID_
     51   */
    4752  Connection::Connection(uint32_t firstPeerID):
    4853    host_(nullptr), bCommunicationThreadRunning_(false), nextPeerID_(firstPeerID)
    4954  {
     55    // Global initialization of ENet
    5056    enet_initialize();
     57
     58    // Register enet_deinitialize to be executed when the program exits normally
    5159    atexit(enet_deinitialize);
     60
     61    // Create mutexes for incoming and outgoing events
    5262    this->incomingEventsMutex_ = new boost::mutex;
    5363    this->outgoingEventsMutex_ = new boost::mutex;
    54 //     this->overallMutex_ = new boost::mutex;
    55   }
    56 
     64  }
     65
     66  /**
     67   * Destructor
     68   */
    5769  Connection::~Connection()
    5870  {
     71    // Delete the mutexes
    5972    delete this->incomingEventsMutex_;
    6073    delete this->outgoingEventsMutex_;
    6174  }
    6275
     76  /**
     77   * Start the main communication thread.
     78   */
    6379  void Connection::startCommunicationThread()
    6480  {
     
    6783  }
    6884 
     85  /**
     86   * Stop the main communication thread.
     87   */
    6988  void Connection::stopCommunicationThread()
    7089  {
    7190    this->bCommunicationThreadRunning_ = false;
    72     if( !this->communicationThread_->timed_join(NETWORK_COMMUNICATION_THREAD_WAIT_TIME) )
    73     {
    74       // force thread to stop
     91    // Wait for peaceful termination
     92    if(!this->communicationThread_->timed_join(NETWORK_COMMUNICATION_THREAD_WAIT_TIME))
     93    {
     94      // Force thread to stop if the waiting time runs out.
    7595      this->communicationThread_->interrupt();
    7696    }
     
    7898  }
    7999
     100  /**
     101   * Send an outgoing event of type 'disconnectPeer'.
     102   * @param peerID The peer to which the event is sent
     103   */
    80104  void Connection::disconnectPeer(uint32_t peerID)
    81105  {
    82 //     this->overallMutex_->lock();
    83106    outgoingEvent outEvent = { peerID, OutgoingEventType::disconnectPeer, nullptr, 0 };
    84107   
     
    86109    this->outgoingEvents_.push_back(outEvent);
    87110    this->outgoingEventsMutex_->unlock();
    88 //     this->overallMutex_->unlock();
    89   }
    90  
     111  }
     112
     113  /**
     114   * Send an outgoing event of type 'disconnectPeers'.
     115   */
    91116  void Connection::disconnectPeers()
    92117  {
     
    98123  }
    99124
     125  /**
     126   * Send a packet.
     127   * @param packet Pointer to the packet to send
     128   * @param peerID The peer to which the event is sent
     129   * @param channelId The channel ID
     130   */
    100131  void Connection::addPacket(ENetPacket* packet, uint32_t peerID, uint8_t channelID)
    101132  {
    102 //     this->overallMutex_->lock();
    103133    outgoingEvent outEvent = { peerID, OutgoingEventType::sendPacket, packet, channelID };
    104134   
     
    106136    this->outgoingEvents_.push_back(outEvent);
    107137    this->outgoingEventsMutex_->unlock();
    108 //     this->overallMutex_->unlock();
    109   }
    110  
     138  }
     139 
     140  /**
     141   * Send a broadcast packet.
     142   * @param packet Pointer to the packet to send
     143   * @param channelId The channel ID
     144   */
    111145  void Connection::broadcastPacket(ENetPacket* packet, uint8_t channelID)
    112146  {
    113 //     this->overallMutex_->lock();
    114147    outgoingEvent outEvent = { 0, OutgoingEventType::broadcastPacket, packet, channelID };
    115148   
     
    117150    this->outgoingEvents_.push_back(outEvent);
    118151    this->outgoingEventsMutex_->unlock();
    119 //     this->overallMutex_->unlock();
    120   }
    121 
    122  
     152  }
     153
     154 
     155  /**
     156   * Main communication thread
     157   */
    123158  void Connection::communicationThread()
    124159  {
    125160    ENetEvent event;
    126161   
    127 //     this->overallMutex_->lock();
    128     while( bCommunicationThreadRunning_ )
     162    while(this->bCommunicationThreadRunning_)
    129163    {
    130164      // Receive all pending incoming Events (such as packets, connects and disconnects)
    131       while( enet_host_check_events( this->host_, &event ) > 0 )
     165      while(enet_host_check_events(this->host_, &event ) > 0)
    132166      {
    133         processIncomingEvent(event);
     167        this->processIncomingEvent(event);
    134168      }
    135169     
    136 //       this->overallMutex_->unlock();
     170      // Sleep for 1ms
    137171      msleep(1);
    138 //       this->overallMutex_->lock();
    139172     
    140173      // Send all waiting outgoing packets
     
    142175      uint32_t outgoingEventsCount = this->outgoingEvents_.size();
    143176      this->outgoingEventsMutex_->unlock();
    144       while( outgoingEventsCount > 0 )
     177
     178      while(outgoingEventsCount > 0)
    145179      {
    146 //         orxout(verbose, context::network) << "outgoing event" << endl;
    147180        this->outgoingEventsMutex_->lock();
    148181        outgoingEvent outEvent = this->outgoingEvents_.front();
     
    150183        this->outgoingEventsMutex_->unlock();
    151184       
    152         processOutgoingEvent(outEvent);
     185        this->processOutgoingEvent(outEvent);
    153186       
    154187        this->outgoingEventsMutex_->lock();
     
    158191     
    159192      // Wait for incoming events (at most NETWORK_WAIT_TIMEOUT ms)
    160       if( enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0 )
     193      if(enet_host_service(this->host_, &event, NETWORK_WAIT_TIMEOUT) > 0)
    161194      {
    162         processIncomingEvent(event);
     195        this->processIncomingEvent(event);
    163196      }
    164197    }
    165 //     this->overallMutex_->unlock();
    166   }
    167  
     198  }
     199 
     200  /**
     201   * Handle an incoming event.
     202   * @param event The incoming event
     203   */
    168204  void Connection::processIncomingEvent(ENetEvent& event)
    169205  {
    170206    incomingEvent inEvent;
    171207    // preprocess event
    172     switch( event.type )
     208    switch(event.type)
    173209    {
    174210      case ENET_EVENT_TYPE_CONNECT:
     
    192228  }
    193229 
     230  /**
     231   * Send an event.
     232   * @param event The event to send
     233   */
    194234  void Connection::processOutgoingEvent(outgoingEvent& event)
    195235  {
    196236    ENetPeer* peer;
    197     switch( event.type )
     237    switch(event.type)
    198238    {
    199239      case OutgoingEventType::sendPacket:
    200240        // check whether the peer is still/already in the peer list
    201         if( this->peerMap_.find(event.peerID) != this->peerMap_.end() )
     241        if(this->peerMap_.find(event.peerID) != this->peerMap_.end())
    202242        {
    203243          peer = this->peerMap_[event.peerID];
    204           enet_peer_send( peer, event.channelID, event.packet );
     244          enet_peer_send(peer, event.channelID, event.packet);
    205245        }
    206246        else
    207247        {
    208248          // peer probably already disconnected so just discard packet
    209           assert(event.peerID<this->nextPeerID_);
     249          orxout(message) << "Trying to send packet to peer that is not in peer list. Ignoring packet." << endl;
    210250          enet_packet_destroy(event.packet);
    211251        }
    212252        break;
    213253      case OutgoingEventType::disconnectPeer:
    214         if( this->peerMap_.find(event.peerID) != this->peerMap_.end() )
     254        if(this->peerMap_.find(event.peerID) != this->peerMap_.end())
    215255        {
    216256          peer = this->peerMap_[event.peerID];
     
    220260        {
    221261          // peer probably already disconnected so just discard disconnect event
    222           assert(event.peerID<this->nextPeerID_);
     262          assert(event.peerID < this->nextPeerID_);
    223263        }
    224264        break;
    225265      case OutgoingEventType::disconnectPeers:
    226         disconnectPeersInternal();
     266        this->disconnectPeersInternal();
    227267        break;
    228268      case OutgoingEventType::broadcastPacket:
     
    234274  }
    235275
    236 
    237276  void Connection::disconnectPeersInternal()
    238277  {
     
    241280      enet_peer_disconnect(mapEntry.second, 0);
    242281    }
    243     uint32_t iterations = NETWORK_DISCONNECT_TIMEOUT/NETWORK_WAIT_TIMEOUT;
     282    uint32_t iterations = NETWORK_DISCONNECT_TIMEOUT / NETWORK_WAIT_TIMEOUT;
    244283    uint32_t i = 0;
    245284    while( this->peerMap_.size() && i++ < iterations )
     
    361400  }
    362401
    363 
    364402}
Note: See TracChangeset for help on using the changeset viewer.