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

Location:
code/branches/network6/src/libraries/network
Files:
9 edited

Legend:

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

    r7801 r7878  
    207207    if( packet->isReliable() )
    208208    {
    209       if( this->getLastProcessedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID() )
     209      if( this->getLastReceivedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID() )
    210210        packet->process(static_cast<Host*>(this));
    211211      else
  • code/branches/network6/src/libraries/network/ClientConnection.cc

    r7825 r7878  
    123123      return true;
    124124    this->established_ = false;
     125   
     126    // stop communication thread and disconnect server
    125127    Connection::stopCommunicationThread();
    126128    enet_peer_disconnect(this->server_, 0);
  • 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)
  • code/branches/network6/src/libraries/network/Connection.h

    r7825 r7878  
    118118
    119119    void processQueue();
     120    void waitOutgoingQueue();     // wait for the outgoing queue to become empty (everything processed by communication thread)
    120121    virtual void addPeer(uint32_t peerID)=0;
    121122    virtual void removePeer(uint32_t peerID)=0;
     
    128129    void processIncomingEvent(ENetEvent& event);
    129130    void processOutgoingEvent(outgoingEvent& event);
     131   
     132    void disconnectPeersInternal();
    130133
    131134    ENetHost*                     host_;
     
    140143    boost::mutex*                 incomingEventsMutex_;
    141144    boost::mutex*                 outgoingEventsMutex_;
     145    boost::mutex*                 overallMutex_;
    142146    std::map<uint32_t, ENetPeer*> peerMap_;
    143147    std::map<ENetPeer*, uint32_t> peerIDMap_;
    144148    uint32_t                      nextPeerID_;
    145 
    146 //     static Connection *instance_;
    147149
    148150  };
  • code/branches/network6/src/libraries/network/GamestateHandler.h

    r7801 r7878  
    5151    virtual bool      addGamestate(packet::Gamestate* gs, unsigned int clientID) = 0;
    5252    virtual bool      ackGamestate(unsigned int gamestateID, unsigned int clientID) = 0;
    53     virtual uint32_t  getLastProcessedGamestateID( unsigned int clientID )=0;
     53    virtual uint32_t  getLastReceivedGamestateID( unsigned int clientID )=0;
    5454    virtual uint32_t  getCurrentGamestateID()=0;
    5555};
  • code/branches/network6/src/libraries/network/GamestateManager.cc

    r7825 r7878  
    110110      bool b = processGamestate(it->second);
    111111      assert(b);
    112       sendAck( it->second->getID(), it->second->getPeerID() );
     112//       sendAck( it->second->getID(), it->second->getPeerID() );
    113113      delete it->second;
    114114    }
     
    122122  bool GamestateManager::sendAck(unsigned int gamestateID, uint32_t peerID)
    123123  {
     124    assert( gamestateID != ACKID_NACK );
    124125    packet::Acknowledgement *ack = new packet::Acknowledgement(gamestateID, peerID);
    125126    if( !this->sendPacket(ack))
     
    151152    {
    152153      assert(peerMap_.size()!=0);
    153       newID = peerMap_[NETWORK_PEER_ID_SERVER].lastProcessedGamestateID;
     154      newID = peerMap_[NETWORK_PEER_ID_SERVER].lastReceivedGamestateID;
    154155    }
    155156   
     
    190191      }
    191192
    192       peerGamestates.push_back(0);  // insert an empty gamestate* to change
     193      peerGamestates.push_back(0);  // insert an empty gamestate* to be changed
    193194      finishGamestate( peerID, peerGamestates.back(), baseGamestate, currentGamestate_ );
    194195      if( peerGamestates.back()==0 )
     
    266267    unsigned int curid = it->second.lastAckedGamestateID;
    267268
    268     if(gamestateID == ACKID_NACK){
    269       it->second.lastAckedGamestateID = GAMESTATEID_INITIAL;
    270 //       temp->setGamestateID(GAMESTATEID_INITIAL);
    271       // now delete all saved gamestates for this client
    272       std::map<uint32_t, packet::Gamestate*>::iterator it2;
    273       for(it2 = it->second.gamestates.begin(); it2!=it->second.gamestates.end(); ++it2 ){
    274         delete it2->second;
    275       }
    276       it->second.gamestates.clear();
    277       return true;
    278     }
     269    assert(gamestateID != ACKID_NACK);
     270//     if(gamestateID == ACKID_NACK){
     271//       it->second.lastAckedGamestateID = GAMESTATEID_INITIAL;
     272// //       temp->setGamestateID(GAMESTATEID_INITIAL);
     273//       // now delete all saved gamestates for this client
     274//       std::map<uint32_t, packet::Gamestate*>::iterator it2;
     275//       for(it2 = it->second.gamestates.begin(); it2!=it->second.gamestates.end(); ++it2 ){
     276//         delete it2->second;
     277//       }
     278//       it->second.gamestates.clear();
     279//       return true;
     280//     }
    279281
    280282    assert(curid==GAMESTATEID_INITIAL || curid<=gamestateID);
     
    303305  }
    304306 
    305   uint32_t GamestateManager::getLastProcessedGamestateID(unsigned int peerID)
     307  uint32_t GamestateManager::getLastReceivedGamestateID(unsigned int peerID)
    306308  {
    307309    assert( this->peerMap_.find(peerID)!=this->peerMap_.end() );
    308310    if( this->peerMap_.find(peerID) != this->peerMap_.end() )
    309       return this->peerMap_[peerID].lastProcessedGamestateID;
     311      return this->peerMap_[peerID].lastReceivedGamestateID;
    310312    else
    311313      return GAMESTATEID_INITIAL;
     
    317319    assert(peerMap_.find(peerID)==peerMap_.end());
    318320    peerMap_[peerID].peerID = peerID;
    319     peerMap_[peerID].lastProcessedGamestateID = GAMESTATEID_INITIAL;
     321    peerMap_[peerID].lastReceivedGamestateID = GAMESTATEID_INITIAL;
    320322    peerMap_[peerID].lastAckedGamestateID = GAMESTATEID_INITIAL;
    321323    if( GameMode::isMaster() )
     
    363365    if( gs->spreadData(gsMode) )
    364366    {
    365       this->peerMap_[gs->getPeerID()].lastProcessedGamestateID = gs->getID();
     367      this->peerMap_[gs->getPeerID()].lastReceivedGamestateID = gs->getID();
    366368      return true;
    367369    }
  • code/branches/network6/src/libraries/network/GamestateManager.h

    r7823 r7878  
    7373    {
    7474      uint32_t  peerID;
    75       uint32_t  lastProcessedGamestateID;
    76       uint32_t  lastAckedGamestateID;
     75      uint32_t  lastReceivedGamestateID;  //!< id of the last gamestate which was received (and processed) from the peer
     76      uint32_t  lastAckedGamestateID;     //!< id of the last gamestate on which we received an ack from the peer
    7777      bool      isSynched;
    7878      std::map< uint32_t, packet::Gamestate* > gamestates;
     
    8686    virtual bool      addGamestate(packet::Gamestate *gs, unsigned int peerID);
    8787    virtual bool      ackGamestate(unsigned int gamestateID, unsigned int peerID);
    88     virtual uint32_t  getLastProcessedGamestateID( unsigned int peerID );
     88    virtual uint32_t  getLastReceivedGamestateID( unsigned int peerID );
    8989    virtual uint32_t  getCurrentGamestateID(){ return currentGamestate_->getID(); }
    9090   
  • code/branches/network6/src/libraries/network/Server.cc

    r7823 r7878  
    380380    if( packet->isReliable() )
    381381    {
    382       if( this->getLastProcessedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID() )
     382      if( this->getLastReceivedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID() )
    383383        packet->process(static_cast<Host*>(this));
    384384      else
  • code/branches/network6/src/libraries/network/ServerConnection.cc

    r7823 r7878  
    3535
    3636#include "util/Debug.h"
     37#include <util/Sleep.h>
    3738// #include "ClientInformation.h"
    3839
     
    133134  {
    134135    Connection::disconnectPeers();
    135 //     ClientInformation *temp = ClientInformation::getBegin();
    136 //     while(temp!=0)
    137 //     {
    138 //       ServerConnection::disconnectClient( temp );
    139 //       temp = temp->next();
    140 //     }
     136    Connection::waitOutgoingQueue();
    141137    return;
    142138  }
Note: See TracChangeset for help on using the changeset viewer.