Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 28, 2010, 4:46:42 PM (14 years ago)
Author:
scheusso
Message:

again some structural changes in network to increase modularity/encapsulation
precondition for fixing client-disconnect bug

File:
1 edited

Legend:

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

    r7801 r7823  
    4444
    4545  Connection::Connection():
    46     host_(0), bCommunicationThreadRunning_(false)
     46    host_(0), bCommunicationThreadRunning_(false), nextPeerID_(NETWORK_PEER_ID_SERVER+1)
    4747  {
    4848    enet_initialize();
     
    7575  }
    7676
    77 
    78 //   int Connection::service(ENetEvent* event) {
    79 //     return enet_host_service( this->host_, event, NETWORK_WAIT_TIMEOUT );
    80 //   }
    81 
    82   void Connection::disconnectPeer(ENetPeer *peer)
    83   {
    84     assert(peer);
    85     outgoingEvent outEvent = { peer, outgoingEventType::disconnectPeer, (ENetPacket*)10, 15 };
     77  void Connection::disconnectPeer(uint32_t peerID)
     78  {
     79    outgoingEvent outEvent = { peerID, outgoingEventType::disconnectPeer, 0, 0 };
    8680   
    8781    this->outgoingEventsMutex_->lock();
     
    8983    this->outgoingEventsMutex_->unlock();
    9084  }
    91 
    92   void Connection::addPacket(ENetPacket *packet, ENetPeer *peer, uint8_t channelID)
    93   {
    94     assert(peer);
    95     outgoingEvent outEvent = { peer, outgoingEventType::sendPacket, packet, channelID };
     85 
     86  void Connection::disconnectPeers()
     87  {
     88    outgoingEvent outEvent = { 0, outgoingEventType::disconnectPeers, 0, 0 };
    9689   
    9790    this->outgoingEventsMutex_->lock();
     
    9992    this->outgoingEventsMutex_->unlock();
    10093  }
    101  
    102   void Connection::broadcastPacket(ENetPacket* packet, uint8_t channelID)
    103   {
    104     outgoingEvent outEvent = { (ENetPeer*)15, outgoingEventType::broadcastPacket, packet, channelID };
     94
     95  void Connection::addPacket(ENetPacket* packet, uint32_t peerID, uint8_t channelID)
     96  {
     97    outgoingEvent outEvent = { peerID, outgoingEventType::sendPacket, packet, channelID };
    10598   
    10699    this->outgoingEventsMutex_->lock();
     
    108101    this->outgoingEventsMutex_->unlock();
    109102  }
     103 
     104  void Connection::broadcastPacket(ENetPacket* packet, uint8_t channelID)
     105  {
     106    outgoingEvent outEvent = { 0, outgoingEventType::broadcastPacket, packet, channelID };
     107   
     108    this->outgoingEventsMutex_->lock();
     109    this->outgoingEvents_.push_back(outEvent);
     110    this->outgoingEventsMutex_->unlock();
     111  }
    110112
    111113 
     
    119121      while( enet_host_check_events( this->host_, &event ) > 0 )
    120122      {
    121 //         COUT(0) << "incoming event" << endl;
    122         // received an event
    123         this->incomingEventsMutex_->lock();
    124         this->incomingEvents_.push_back(event);
    125         this->incomingEventsMutex_->unlock();
     123        processIncomingEvent(event);
    126124      }
    127125     
     
    138136        this->outgoingEventsMutex_->unlock();
    139137       
    140         switch( outEvent.type )
    141         {
    142           case outgoingEventType::sendPacket:
    143             enet_peer_send( outEvent.peer, outEvent.channelID, outEvent.packet );
    144             break;
    145           case outgoingEventType::disconnectPeer:
    146             enet_peer_disconnect(outEvent.peer, 0);
    147             break;
    148           case outgoingEventType::broadcastPacket:
    149             enet_host_broadcast( this->host_, outEvent.channelID, outEvent.packet );
    150             break;
    151           default:
    152             assert(0);
    153         }
     138        processOutgoingEvent(outEvent);
     139       
    154140        this->outgoingEventsMutex_->lock();
    155141        outgoingEventsCount = this->outgoingEvents_.size();
     
    160146      if( enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0 )
    161147      {
    162 //         COUT(0) << "incoming event after wait" << endl;
    163         //received an event
    164         this->incomingEventsMutex_->lock();
    165         this->incomingEvents_.push_back(event);
    166         this->incomingEventsMutex_->unlock();
     148        processIncomingEvent(event);
    167149      }
    168150    }
    169151  }
     152 
     153  void Connection::processIncomingEvent(ENetEvent& event)
     154  {
     155    incomingEvent inEvent;
     156    // preprocess event
     157    switch( event.type )
     158    {
     159      case ENET_EVENT_TYPE_CONNECT:
     160        inEvent = preprocessConnectEvent(event);
     161        break;
     162      case ENET_EVENT_TYPE_RECEIVE:
     163        inEvent = preprocessReceiveEvent(event);
     164        break;
     165      case ENET_EVENT_TYPE_DISCONNECT:
     166        inEvent = preprocessDisconnectEvent(event);
     167        break;
     168      case ENET_EVENT_TYPE_NONE:
     169      default:
     170        return;
     171    }
     172   
     173    // pushing event to queue
     174    this->incomingEventsMutex_->lock();
     175    this->incomingEvents_.push_back(inEvent);
     176    this->incomingEventsMutex_->unlock();
     177  }
     178 
     179  void Connection::processOutgoingEvent(outgoingEvent& event)
     180  {
     181    ENetPeer* peer;
     182    switch( event.type )
     183    {
     184      case outgoingEventType::sendPacket:
     185        assert(this->peerMap_.find(event.peerID) != this->peerMap_.end());
     186        peer = this->peerMap_[event.peerID];
     187        enet_peer_send( peer, event.channelID, event.packet );
     188        break;
     189      case outgoingEventType::disconnectPeer:
     190        assert(this->peerMap_.find(event.peerID) != this->peerMap_.end());
     191        peer = this->peerMap_[event.peerID];
     192        enet_peer_disconnect(peer, 0);
     193        break;
     194      case outgoingEventType::disconnectPeers:
     195        while( this->peerMap_.size()!=0 )
     196        {
     197          peer = this->peerMap_.begin()->second;
     198          enet_peer_disconnect(peer, 0);
     199        }
     200        break;
     201      case outgoingEventType::broadcastPacket:
     202        enet_host_broadcast( this->host_, event.channelID, event.packet );
     203        break;
     204      default:
     205        assert(0);
     206    }
     207  }
     208
    170209
    171210  void Connection::processQueue()
    172211  {
    173     ENetEvent event;
     212    incomingEvent inEvent;
    174213
    175214    this->incomingEventsMutex_->lock();
     
    178217    while( incomingEventsCount > 0 )
    179218    {
    180       packet::Packet* p;
     219      // pop event from queue
    181220      this->incomingEventsMutex_->lock();
    182       event = this->incomingEvents_.front();
     221      inEvent = this->incomingEvents_.front();
    183222      this->incomingEvents_.pop_front();
    184223      this->incomingEventsMutex_->unlock();
    185224     
    186       switch(event.type)
     225      // process event
     226      switch( inEvent.type )
    187227      {
    188         // log handling ================
    189         case ENET_EVENT_TYPE_CONNECT:
    190           addPeer( &event );
     228        case incomingEventType::peerConnect:
     229          addPeer(inEvent.peerID);
    191230          break;
    192         case ENET_EVENT_TYPE_DISCONNECT:
    193           removePeer( &event );
     231        case incomingEventType::peerDisconnect:
     232          removePeer(inEvent.peerID);
    194233          break;
    195         case ENET_EVENT_TYPE_RECEIVE:
    196 //           COUT(0) << "ENET_EVENT_TYPE_RECEIVE" << endl;
    197           p = createPacket( &event );
    198           processPacket(p);
     234        case incomingEventType::receivePacket:
     235          processPacket(inEvent.packet);
    199236          break;
    200         case ENET_EVENT_TYPE_NONE:
     237        default:
    201238          break;
    202239      }
    203240     
     241      // check whether there are still events in the queue
    204242      this->incomingEventsMutex_->lock();
    205243      incomingEventsCount = this->incomingEvents_.size();
     
    208246  }
    209247
    210   packet::Packet* Connection::createPacket(ENetEvent* event)
    211   {
    212     packet::Packet *p = packet::Packet::createPacket(event->packet, event->peer);
    213     return p;
    214 //     return p->process();
    215   }
     248  incomingEvent Connection::preprocessConnectEvent(ENetEvent& event)
     249  {
     250    // make sure this peer doesn't exist
     251    assert( this->peerMap_.find(this->nextPeerID_) == this->peerMap_.end() );
     252    assert( this->peerIDMap_.find(event.peer) == this->peerIDMap_.end() );
     253   
     254    // give peer a new id and increase peerID for next peer
     255    uint32_t peerID = this->nextPeerID_;
     256    ++this->nextPeerID_;
     257   
     258    // add peer/peerID into peerMap_ and peerIDMap_
     259    this->peerMap_[peerID] = event.peer;
     260    this->peerIDMap_[event.peer] = peerID;
     261   
     262    // create new peerEvent and return it
     263    incomingEvent inEvent = { peerID, incomingEventType::peerConnect, 0 };
     264    return inEvent;
     265  }
     266 
     267  incomingEvent Connection::preprocessDisconnectEvent(ENetEvent& event)
     268  {
     269    // assert that the peer exists and get peerID
     270    assert( this->peerIDMap_.find(event.peer) != this->peerIDMap_.end() );
     271    uint32_t peerID = this->peerIDMap_[event.peer];
     272   
     273    // remove peer/peerID from maps
     274    this->peerIDMap_.erase(this->peerIDMap_.find(event.peer));
     275    this->peerMap_.erase(this->peerMap_.find(peerID));
     276   
     277    // create new peerEvent and return it
     278    incomingEvent inEvent = { peerID, incomingEventType::peerDisconnect, 0 };
     279    return inEvent;
     280  }
     281 
     282  incomingEvent Connection::preprocessReceiveEvent(ENetEvent& event)
     283  {
     284    // assert that the peer exists and get peerID
     285    assert( this->peerIDMap_.find(event.peer) != this->peerIDMap_.end() );
     286    uint32_t peerID = this->peerIDMap_[event.peer];
     287   
     288    // create new Packet from ENetPacket
     289    packet::Packet* p = packet::Packet::createPacket(event.packet, peerID);
     290   
     291    // create new peerEvent and return it
     292    incomingEvent inEvent = { peerID, incomingEventType::receivePacket, p };
     293    return inEvent;
     294  }
     295
    216296 
    217297  void Connection::enableCompression()
Note: See TracChangeset for help on using the changeset viewer.