[Orxonox-commit 3185] r7878 - code/branches/network6/src/libraries/network

scheusso at orxonox.net scheusso at orxonox.net
Sun Feb 13 21:34:23 CET 2011


Author: scheusso
Date: 2011-02-13 21:34:22 +0100 (Sun, 13 Feb 2011)
New Revision: 7878

Modified:
   code/branches/network6/src/libraries/network/Client.cc
   code/branches/network6/src/libraries/network/ClientConnection.cc
   code/branches/network6/src/libraries/network/Connection.cc
   code/branches/network6/src/libraries/network/Connection.h
   code/branches/network6/src/libraries/network/GamestateHandler.h
   code/branches/network6/src/libraries/network/GamestateManager.cc
   code/branches/network6/src/libraries/network/GamestateManager.h
   code/branches/network6/src/libraries/network/Server.cc
   code/branches/network6/src/libraries/network/ServerConnection.cc
Log:
-some cleaning up
-fixing disconnect behaviour
-trying to find a bug


Modified: code/branches/network6/src/libraries/network/Client.cc
===================================================================
--- code/branches/network6/src/libraries/network/Client.cc	2011-02-13 16:57:05 UTC (rev 7877)
+++ code/branches/network6/src/libraries/network/Client.cc	2011-02-13 20:34:22 UTC (rev 7878)
@@ -206,7 +206,7 @@
   {
     if( packet->isReliable() )
     {
-      if( this->getLastProcessedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID() )
+      if( this->getLastReceivedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID() )
         packet->process(static_cast<Host*>(this));
       else
         this->packetQueue_.push_back(packet);

Modified: code/branches/network6/src/libraries/network/ClientConnection.cc
===================================================================
--- code/branches/network6/src/libraries/network/ClientConnection.cc	2011-02-13 16:57:05 UTC (rev 7877)
+++ code/branches/network6/src/libraries/network/ClientConnection.cc	2011-02-13 20:34:22 UTC (rev 7878)
@@ -122,6 +122,8 @@
     if ( !this->established_ )
       return true;
     this->established_ = false;
+    
+    // stop communication thread and disconnect server
     Connection::stopCommunicationThread();
     enet_peer_disconnect(this->server_, 0);
     for( unsigned int i=0; i<NETWORK_CLIENT_CONNECTION_TIMEOUT/NETWORK_CLIENT_WAIT_TIME; i++)

Modified: code/branches/network6/src/libraries/network/Connection.cc
===================================================================
--- code/branches/network6/src/libraries/network/Connection.cc	2011-02-13 16:57:05 UTC (rev 7877)
+++ code/branches/network6/src/libraries/network/Connection.cc	2011-02-13 20:34:22 UTC (rev 7878)
@@ -37,10 +37,12 @@
 #include <boost/date_time.hpp>
 
 #include "packet/Packet.h"
+#include <util/Sleep.h>
 
 namespace orxonox
 {
   const boost::posix_time::millisec NETWORK_COMMUNICATION_THREAD_WAIT_TIME(20);
+  const unsigned int                NETWORK_DISCONNECT_TIMEOUT = 500;
 
   Connection::Connection(uint32_t firstPeerID):
     host_(0), bCommunicationThreadRunning_(false), nextPeerID_(firstPeerID)
@@ -49,6 +51,7 @@
     atexit(enet_deinitialize);
     this->incomingEventsMutex_ = new boost::mutex;
     this->outgoingEventsMutex_ = new boost::mutex;
+    this->overallMutex_ = new boost::mutex;
   }
 
   Connection::~Connection()
@@ -76,11 +79,13 @@
 
   void Connection::disconnectPeer(uint32_t peerID)
   {
+    this->overallMutex_->lock();
     outgoingEvent outEvent = { peerID, outgoingEventType::disconnectPeer, 0, 0 };
     
     this->outgoingEventsMutex_->lock();
     this->outgoingEvents_.push_back(outEvent);
     this->outgoingEventsMutex_->unlock();
+    this->overallMutex_->unlock();
   }
   
   void Connection::disconnectPeers()
@@ -94,20 +99,24 @@
 
   void Connection::addPacket(ENetPacket* packet, uint32_t peerID, uint8_t channelID)
   {
+    this->overallMutex_->lock();
     outgoingEvent outEvent = { peerID, outgoingEventType::sendPacket, packet, channelID };
     
     this->outgoingEventsMutex_->lock();
     this->outgoingEvents_.push_back(outEvent);
     this->outgoingEventsMutex_->unlock();
+    this->overallMutex_->unlock();
   }
   
   void Connection::broadcastPacket(ENetPacket* packet, uint8_t channelID)
   {
+    this->overallMutex_->lock();
     outgoingEvent outEvent = { 0, outgoingEventType::broadcastPacket, packet, channelID };
     
     this->outgoingEventsMutex_->lock();
     this->outgoingEvents_.push_back(outEvent);
     this->outgoingEventsMutex_->unlock();
+    this->overallMutex_->unlock();
   }
 
   
@@ -115,6 +124,7 @@
   {
     ENetEvent event;
     
+    this->overallMutex_->lock();
     while( bCommunicationThreadRunning_ )
     {
       // Receive all pending incoming Events (such as packets, connects and disconnects)
@@ -123,6 +133,10 @@
         processIncomingEvent(event);
       }
       
+      this->overallMutex_->unlock();
+      msleep(10);
+      this->overallMutex_->lock();
+      
       // Send all waiting outgoing packets
       this->outgoingEventsMutex_->lock();
       uint32_t outgoingEventsCount = this->outgoingEvents_.size();
@@ -148,6 +162,7 @@
         processIncomingEvent(event);
       }
     }
+    this->overallMutex_->unlock();
   }
   
   void Connection::processIncomingEvent(ENetEvent& event)
@@ -208,11 +223,7 @@
         }
         break;
       case outgoingEventType::disconnectPeers:
-        while( this->peerMap_.size()!=0 )
-        {
-          peer = this->peerMap_.begin()->second;
-          enet_peer_disconnect(peer, 0);
-        }
+        disconnectPeersInternal();
         break;
       case outgoingEventType::broadcastPacket:
         enet_host_broadcast( this->host_, event.channelID, event.packet );
@@ -223,6 +234,25 @@
   }
 
 
+  void Connection::disconnectPeersInternal()
+  {
+    std::map<uint32_t, ENetPeer*>::iterator it;
+    for( it=this->peerMap_.begin(); it!=this->peerMap_.end(); ++it )
+    {
+      enet_peer_disconnect(it->second, 0);
+    }
+    uint32_t iterations = NETWORK_DISCONNECT_TIMEOUT/NETWORK_WAIT_TIMEOUT;
+    uint32_t i = 0;
+    while( this->peerMap_.size() && i++ < iterations )
+    {
+      ENetEvent event;
+      if( enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0 )
+      {
+        processIncomingEvent(event);
+      }
+    }
+  }
+
   void Connection::processQueue()
   {
     incomingEvent inEvent;
@@ -260,7 +290,23 @@
       this->incomingEventsMutex_->unlock();
     }
   }
+  
+  void Connection::waitOutgoingQueue()
+  {
+    uint32_t outgoingEventsCount;
+    this->outgoingEventsMutex_->lock();
+    outgoingEventsCount = this->outgoingEvents_.size();
+    this->outgoingEventsMutex_->unlock();
+    while( outgoingEventsCount )
+    {
+      msleep(1);
+      this->outgoingEventsMutex_->lock();
+      outgoingEventsCount = this->outgoingEvents_.size();
+      this->outgoingEventsMutex_->unlock();
+    }
+  }
 
+
   incomingEvent Connection::preprocessConnectEvent(ENetEvent& event)
   {
     // make sure this peer doesn't exist

Modified: code/branches/network6/src/libraries/network/Connection.h
===================================================================
--- code/branches/network6/src/libraries/network/Connection.h	2011-02-13 16:57:05 UTC (rev 7877)
+++ code/branches/network6/src/libraries/network/Connection.h	2011-02-13 20:34:22 UTC (rev 7878)
@@ -117,6 +117,7 @@
     void enableCompression();
 
     void processQueue();
+    void waitOutgoingQueue();     // wait for the outgoing queue to become empty (everything processed by communication thread)
     virtual void addPeer(uint32_t peerID)=0;
     virtual void removePeer(uint32_t peerID)=0;
     virtual void processPacket( packet::Packet* packet)=0;
@@ -127,6 +128,8 @@
     
     void processIncomingEvent(ENetEvent& event);
     void processOutgoingEvent(outgoingEvent& event);
+    
+    void disconnectPeersInternal();
 
     ENetHost*                     host_;
   private:
@@ -139,12 +142,11 @@
     std::deque<outgoingEvent>     outgoingEvents_;
     boost::mutex*                 incomingEventsMutex_;
     boost::mutex*                 outgoingEventsMutex_;
+    boost::mutex*                 overallMutex_;
     std::map<uint32_t, ENetPeer*> peerMap_;
     std::map<ENetPeer*, uint32_t> peerIDMap_;
     uint32_t                      nextPeerID_;
 
-//     static Connection *instance_;
-
   };
 
 }

Modified: code/branches/network6/src/libraries/network/GamestateHandler.h
===================================================================
--- code/branches/network6/src/libraries/network/GamestateHandler.h	2011-02-13 16:57:05 UTC (rev 7877)
+++ code/branches/network6/src/libraries/network/GamestateHandler.h	2011-02-13 20:34:22 UTC (rev 7878)
@@ -50,7 +50,7 @@
   public:
     virtual bool      addGamestate(packet::Gamestate* gs, unsigned int clientID) = 0;
     virtual bool      ackGamestate(unsigned int gamestateID, unsigned int clientID) = 0;
-    virtual uint32_t  getLastProcessedGamestateID( unsigned int clientID )=0;
+    virtual uint32_t  getLastReceivedGamestateID( unsigned int clientID )=0;
     virtual uint32_t  getCurrentGamestateID()=0;
 };
 

Modified: code/branches/network6/src/libraries/network/GamestateManager.cc
===================================================================
--- code/branches/network6/src/libraries/network/GamestateManager.cc	2011-02-13 16:57:05 UTC (rev 7877)
+++ code/branches/network6/src/libraries/network/GamestateManager.cc	2011-02-13 20:34:22 UTC (rev 7878)
@@ -109,7 +109,7 @@
     for(it = gamestateQueue.begin(); it!=gamestateQueue.end(); it++){
       bool b = processGamestate(it->second);
       assert(b);
-      sendAck( it->second->getID(), it->second->getPeerID() );
+//       sendAck( it->second->getID(), it->second->getPeerID() );
       delete it->second;
     }
     // now clear the queue
@@ -121,6 +121,7 @@
   
   bool GamestateManager::sendAck(unsigned int gamestateID, uint32_t peerID)
   {
+    assert( gamestateID != ACKID_NACK );
     packet::Acknowledgement *ack = new packet::Acknowledgement(gamestateID, peerID);
     if( !this->sendPacket(ack))
     {
@@ -150,7 +151,7 @@
     else
     {
       assert(peerMap_.size()!=0);
-      newID = peerMap_[NETWORK_PEER_ID_SERVER].lastProcessedGamestateID;
+      newID = peerMap_[NETWORK_PEER_ID_SERVER].lastReceivedGamestateID;
     }
     
     if(!currentGamestate_->collectData(newID, gsMode)){ //we have no data to send
@@ -189,7 +190,7 @@
         baseGamestate = it->second;
       }
 
-      peerGamestates.push_back(0);  // insert an empty gamestate* to change
+      peerGamestates.push_back(0);  // insert an empty gamestate* to be changed
       finishGamestate( peerID, peerGamestates.back(), baseGamestate, currentGamestate_ );
       if( peerGamestates.back()==0 )
         // nothing to send to remove pointer from vector
@@ -265,17 +266,18 @@
     assert(it!=this->peerMap_.end());
     unsigned int curid = it->second.lastAckedGamestateID;
 
-    if(gamestateID == ACKID_NACK){
-      it->second.lastAckedGamestateID = GAMESTATEID_INITIAL;
-//       temp->setGamestateID(GAMESTATEID_INITIAL);
-      // now delete all saved gamestates for this client
-      std::map<uint32_t, packet::Gamestate*>::iterator it2;
-      for(it2 = it->second.gamestates.begin(); it2!=it->second.gamestates.end(); ++it2 ){
-        delete it2->second;
-      }
-      it->second.gamestates.clear();
-      return true;
-    }
+    assert(gamestateID != ACKID_NACK);
+//     if(gamestateID == ACKID_NACK){
+//       it->second.lastAckedGamestateID = GAMESTATEID_INITIAL;
+// //       temp->setGamestateID(GAMESTATEID_INITIAL);
+//       // now delete all saved gamestates for this client
+//       std::map<uint32_t, packet::Gamestate*>::iterator it2;
+//       for(it2 = it->second.gamestates.begin(); it2!=it->second.gamestates.end(); ++it2 ){
+//         delete it2->second;
+//       }
+//       it->second.gamestates.clear();
+//       return true;
+//     }
 
     assert(curid==GAMESTATEID_INITIAL || curid<=gamestateID);
     COUT(5) << "acking gamestate " << gamestateID << " for peerID: " << peerID << " curid: " << curid << std::endl;
@@ -302,11 +304,11 @@
     return true;
   }
   
-  uint32_t GamestateManager::getLastProcessedGamestateID(unsigned int peerID)
+  uint32_t GamestateManager::getLastReceivedGamestateID(unsigned int peerID)
   {
     assert( this->peerMap_.find(peerID)!=this->peerMap_.end() );
     if( this->peerMap_.find(peerID) != this->peerMap_.end() )
-      return this->peerMap_[peerID].lastProcessedGamestateID;
+      return this->peerMap_[peerID].lastReceivedGamestateID;
     else
       return GAMESTATEID_INITIAL;
   }
@@ -316,7 +318,7 @@
   {
     assert(peerMap_.find(peerID)==peerMap_.end());
     peerMap_[peerID].peerID = peerID;
-    peerMap_[peerID].lastProcessedGamestateID = GAMESTATEID_INITIAL;
+    peerMap_[peerID].lastReceivedGamestateID = GAMESTATEID_INITIAL;
     peerMap_[peerID].lastAckedGamestateID = GAMESTATEID_INITIAL;
     if( GameMode::isMaster() )
       peerMap_[peerID].isSynched = false;
@@ -362,7 +364,7 @@
       gsMode = packet::GAMESTATE_MODE_CLIENT;
     if( gs->spreadData(gsMode) )
     {
-      this->peerMap_[gs->getPeerID()].lastProcessedGamestateID = gs->getID();
+      this->peerMap_[gs->getPeerID()].lastReceivedGamestateID = gs->getID();
       return true;
     }
     else

Modified: code/branches/network6/src/libraries/network/GamestateManager.h
===================================================================
--- code/branches/network6/src/libraries/network/GamestateManager.h	2011-02-13 16:57:05 UTC (rev 7877)
+++ code/branches/network6/src/libraries/network/GamestateManager.h	2011-02-13 20:34:22 UTC (rev 7878)
@@ -72,8 +72,8 @@
     struct peerInfo
     {
       uint32_t  peerID;
-      uint32_t  lastProcessedGamestateID;
-      uint32_t  lastAckedGamestateID;
+      uint32_t  lastReceivedGamestateID;  //!< id of the last gamestate which was received (and processed) from the peer
+      uint32_t  lastAckedGamestateID;     //!< id of the last gamestate on which we received an ack from the peer
       bool      isSynched;
       std::map< uint32_t, packet::Gamestate* > gamestates;
     };
@@ -85,7 +85,7 @@
 
     virtual bool      addGamestate(packet::Gamestate *gs, unsigned int peerID);
     virtual bool      ackGamestate(unsigned int gamestateID, unsigned int peerID);
-    virtual uint32_t  getLastProcessedGamestateID( unsigned int peerID );
+    virtual uint32_t  getLastReceivedGamestateID( unsigned int peerID );
     virtual uint32_t  getCurrentGamestateID(){ return currentGamestate_->getID(); }
     
     bool processGamestates();

Modified: code/branches/network6/src/libraries/network/Server.cc
===================================================================
--- code/branches/network6/src/libraries/network/Server.cc	2011-02-13 16:57:05 UTC (rev 7877)
+++ code/branches/network6/src/libraries/network/Server.cc	2011-02-13 20:34:22 UTC (rev 7878)
@@ -379,7 +379,7 @@
   {
     if( packet->isReliable() )
     {
-      if( this->getLastProcessedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID() )
+      if( this->getLastReceivedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID() )
         packet->process(static_cast<Host*>(this));
       else
         this->packetQueue_.push_back(packet);

Modified: code/branches/network6/src/libraries/network/ServerConnection.cc
===================================================================
--- code/branches/network6/src/libraries/network/ServerConnection.cc	2011-02-13 16:57:05 UTC (rev 7877)
+++ code/branches/network6/src/libraries/network/ServerConnection.cc	2011-02-13 20:34:22 UTC (rev 7878)
@@ -34,6 +34,7 @@
 #include <enet/enet.h>
 
 #include "util/Debug.h"
+#include <util/Sleep.h>
 // #include "ClientInformation.h"
 
 namespace orxonox
@@ -132,12 +133,7 @@
   void ServerConnection::disconnectClients()
   {
     Connection::disconnectPeers();
-//     ClientInformation *temp = ClientInformation::getBegin();
-//     while(temp!=0)
-//     {
-//       ServerConnection::disconnectClient( temp );
-//       temp = temp->next();
-//     }
+    Connection::waitOutgoingQueue();
     return;
   }
 




More information about the Orxonox-commit mailing list