Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Mar 22, 2018, 4:12:23 PM (6 years ago)
Author:
mdedial
Message:

WIP 22.03.18: Begin documenting server-side code

File:
1 edited

Legend:

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

    r11071 r11829  
    4545  const unsigned int                NETWORK_DISCONNECT_TIMEOUT = 500;
    4646
     47  /**
     48   * Constructor
     49   * @param firstPeerId The initial value of nextPeerID_
     50   */
    4751  Connection::Connection(uint32_t firstPeerID):
    4852    host_(nullptr), bCommunicationThreadRunning_(false), nextPeerID_(firstPeerID)
    4953  {
     54    // Global initialization of ENet
    5055    enet_initialize();
     56
     57    // Register enet_deinitialize to be executed when the program exits normally
    5158    atexit(enet_deinitialize);
     59
     60    // Create mutexes for incoming and outgoing events
    5261    this->incomingEventsMutex_ = new boost::mutex;
    5362    this->outgoingEventsMutex_ = new boost::mutex;
    54 //     this->overallMutex_ = new boost::mutex;
    55   }
    56 
     63  }
     64
     65  /**
     66   * Destructor
     67   */
    5768  Connection::~Connection()
    5869  {
     70    // Delete the mutexes
    5971    delete this->incomingEventsMutex_;
    6072    delete this->outgoingEventsMutex_;
    61   }
    62 
     73
     74    // TODO: Why is enet_deinitialize() not called here?
     75    // Would make sense, since its counterpart, enet_initialize(), is called in the constructor.
     76  }
     77
     78  /**
     79   * Start the main communication thread.
     80   */
    6381  void Connection::startCommunicationThread()
    6482  {
     
    6785  }
    6886 
     87  /**
     88   * Stop the main communication thread.
     89   */
    6990  void Connection::stopCommunicationThread()
    7091  {
    7192    this->bCommunicationThreadRunning_ = false;
    72     if( !this->communicationThread_->timed_join(NETWORK_COMMUNICATION_THREAD_WAIT_TIME) )
    73     {
    74       // force thread to stop
     93    // Wait for peaceful termination
     94    if(!this->communicationThread_->timed_join(NETWORK_COMMUNICATION_THREAD_WAIT_TIME))
     95    {
     96      // Force thread to stop if the waiting time runs out.
    7597      this->communicationThread_->interrupt();
    7698    }
     
    78100  }
    79101
     102  /**
     103   * Send an outgoing event of type 'disconnectPeer'.
     104   * @param peerID The peer to which the event is sent
     105   */
    80106  void Connection::disconnectPeer(uint32_t peerID)
    81107  {
    82 //     this->overallMutex_->lock();
    83108    outgoingEvent outEvent = { peerID, OutgoingEventType::disconnectPeer, nullptr, 0 };
    84109   
     
    86111    this->outgoingEvents_.push_back(outEvent);
    87112    this->outgoingEventsMutex_->unlock();
    88 //     this->overallMutex_->unlock();
    89   }
    90  
     113  }
     114
     115  /**
     116   * Send an outgoing event of type 'disconnectPeers'.
     117   */
    91118  void Connection::disconnectPeers()
    92119  {
     
    98125  }
    99126
     127  /**
     128   * Send a packet.
     129   * @param packet Pointer to the packet to send
     130   * @param peerID The peer to which the event is sent
     131   * @param channelId The channel ID
     132   */
    100133  void Connection::addPacket(ENetPacket* packet, uint32_t peerID, uint8_t channelID)
    101134  {
    102 //     this->overallMutex_->lock();
    103135    outgoingEvent outEvent = { peerID, OutgoingEventType::sendPacket, packet, channelID };
    104136   
     
    106138    this->outgoingEvents_.push_back(outEvent);
    107139    this->outgoingEventsMutex_->unlock();
    108 //     this->overallMutex_->unlock();
    109   }
    110  
     140  }
     141 
     142  /**
     143   * Send a broadcast packet.
     144   * @param packet Pointer to the packet to send
     145   * @param channelId The channel ID
     146   */
    111147  void Connection::broadcastPacket(ENetPacket* packet, uint8_t channelID)
    112148  {
    113 //     this->overallMutex_->lock();
    114149    outgoingEvent outEvent = { 0, OutgoingEventType::broadcastPacket, packet, channelID };
    115150   
     
    117152    this->outgoingEvents_.push_back(outEvent);
    118153    this->outgoingEventsMutex_->unlock();
    119 //     this->overallMutex_->unlock();
    120   }
    121 
    122  
     154  }
     155
     156 
     157  /**
     158   * Main communication thread
     159   */
    123160  void Connection::communicationThread()
    124161  {
    125162    ENetEvent event;
    126163   
    127 //     this->overallMutex_->lock();
    128     while( bCommunicationThreadRunning_ )
     164    while(this->bCommunicationThreadRunning_)
    129165    {
    130166      // Receive all pending incoming Events (such as packets, connects and disconnects)
    131       while( enet_host_check_events( this->host_, &event ) > 0 )
     167      while(enet_host_check_events(this->host_, &event ) > 0)
    132168      {
    133         processIncomingEvent(event);
     169        this->processIncomingEvent(event);
    134170      }
    135171     
    136 //       this->overallMutex_->unlock();
     172      // Sleep for 1ms
     173      // TODO: Why?
    137174      msleep(1);
    138 //       this->overallMutex_->lock();
    139175     
    140176      // Send all waiting outgoing packets
     177      // TODO: Why do we need a mutex to read a single variable?
    141178      this->outgoingEventsMutex_->lock();
    142179      uint32_t outgoingEventsCount = this->outgoingEvents_.size();
    143180      this->outgoingEventsMutex_->unlock();
    144       while( outgoingEventsCount > 0 )
     181
     182      // TODO: Not convinced about how mutexes are used here, seems kinda pointless
     183      while(outgoingEventsCount > 0)
    145184      {
    146 //         orxout(verbose, context::network) << "outgoing event" << endl;
    147185        this->outgoingEventsMutex_->lock();
    148186        outgoingEvent outEvent = this->outgoingEvents_.front();
     
    150188        this->outgoingEventsMutex_->unlock();
    151189       
    152         processOutgoingEvent(outEvent);
     190        this->processOutgoingEvent(outEvent);
    153191       
    154192        this->outgoingEventsMutex_->lock();
     
    158196     
    159197      // Wait for incoming events (at most NETWORK_WAIT_TIMEOUT ms)
    160       if( enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0 )
     198      if(enet_host_service(this->host_, &event, NETWORK_WAIT_TIMEOUT) > 0)
    161199      {
    162         processIncomingEvent(event);
     200        this->processIncomingEvent(event);
    163201      }
    164202    }
    165 //     this->overallMutex_->unlock();
    166   }
    167  
     203  }
     204 
     205  /**
     206   * Handle an incoming event.
     207   * @param event The incoming event
     208   */
    168209  void Connection::processIncomingEvent(ENetEvent& event)
    169210  {
    170211    incomingEvent inEvent;
    171212    // preprocess event
    172     switch( event.type )
     213    switch(event.type)
    173214    {
    174215      case ENET_EVENT_TYPE_CONNECT:
     
    192233  }
    193234 
     235  /**
     236   * Send an event.
     237   * @param event The event to send
     238   */
    194239  void Connection::processOutgoingEvent(outgoingEvent& event)
    195240  {
    196241    ENetPeer* peer;
    197     switch( event.type )
     242    switch(event.type)
    198243    {
    199244      case OutgoingEventType::sendPacket:
    200245        // check whether the peer is still/already in the peer list
    201         if( this->peerMap_.find(event.peerID) != this->peerMap_.end() )
     246        if(this->peerMap_.find(event.peerID) != this->peerMap_.end())
    202247        {
    203248          peer = this->peerMap_[event.peerID];
    204           enet_peer_send( peer, event.channelID, event.packet );
     249          enet_peer_send(peer, event.channelID, event.packet);
    205250        }
    206251        else
    207252        {
    208253          // peer probably already disconnected so just discard packet
    209           assert(event.peerID<this->nextPeerID_);
     254          assert(event.peerID < this->nextPeerID_);
    210255          enet_packet_destroy(event.packet);
    211256        }
    212257        break;
    213258      case OutgoingEventType::disconnectPeer:
    214         if( this->peerMap_.find(event.peerID) != this->peerMap_.end() )
     259        if(this->peerMap_.find(event.peerID) != this->peerMap_.end())
    215260        {
    216261          peer = this->peerMap_[event.peerID];
     
    220265        {
    221266          // peer probably already disconnected so just discard disconnect event
    222           assert(event.peerID<this->nextPeerID_);
     267          assert(event.peerID < this->nextPeerID_);
    223268        }
    224269        break;
    225270      case OutgoingEventType::disconnectPeers:
    226         disconnectPeersInternal();
     271        this->disconnectPeersInternal();
    227272        break;
    228273      case OutgoingEventType::broadcastPacket:
     
    234279  }
    235280
    236 
    237281  void Connection::disconnectPeersInternal()
    238282  {
     
    241285      enet_peer_disconnect(mapEntry.second, 0);
    242286    }
    243     uint32_t iterations = NETWORK_DISCONNECT_TIMEOUT/NETWORK_WAIT_TIMEOUT;
     287    uint32_t iterations = NETWORK_DISCONNECT_TIMEOUT / NETWORK_WAIT_TIMEOUT;
    244288    uint32_t i = 0;
    245289    while( this->peerMap_.size() && i++ < iterations )
Note: See TracChangeset for help on using the changeset viewer.