Index: code/branches/PresentationFS18/src/libraries/network/Connection.cc
===================================================================
--- code/branches/PresentationFS18/src/libraries/network/Connection.cc	(revision 12012)
+++ code/branches/PresentationFS18/src/libraries/network/Connection.cc	(revision 12020)
@@ -38,4 +38,5 @@
 
 #include "packet/Packet.h"
+#include "util/Output.h"
 #include <util/Sleep.h>
 
@@ -45,20 +46,35 @@
   const unsigned int                NETWORK_DISCONNECT_TIMEOUT = 500;
 
+  /**
+   * Constructor
+   * @param firstPeerId The initial value of nextPeerID_
+   */
   Connection::Connection(uint32_t firstPeerID):
     host_(nullptr), bCommunicationThreadRunning_(false), nextPeerID_(firstPeerID)
   {
+    // Global initialization of ENet
     enet_initialize();
+
+    // Register enet_deinitialize to be executed when the program exits normally
     atexit(enet_deinitialize);
+
+    // Create mutexes for incoming and outgoing events
     this->incomingEventsMutex_ = new boost::mutex;
     this->outgoingEventsMutex_ = new boost::mutex;
-//     this->overallMutex_ = new boost::mutex;
-  }
-
+  }
+
+  /**
+   * Destructor
+   */
   Connection::~Connection()
   {
+    // Delete the mutexes
     delete this->incomingEventsMutex_;
     delete this->outgoingEventsMutex_;
   }
 
+  /**
+   * Start the main communication thread.
+   */
   void Connection::startCommunicationThread()
   {
@@ -67,10 +83,14 @@
   }
   
+  /**
+   * Stop the main communication thread.
+   */
   void Connection::stopCommunicationThread()
   {
     this->bCommunicationThreadRunning_ = false;
-    if( !this->communicationThread_->timed_join(NETWORK_COMMUNICATION_THREAD_WAIT_TIME) )
-    {
-      // force thread to stop
+    // Wait for peaceful termination
+    if(!this->communicationThread_->timed_join(NETWORK_COMMUNICATION_THREAD_WAIT_TIME))
+    {
+      // Force thread to stop if the waiting time runs out.
       this->communicationThread_->interrupt();
     }
@@ -78,7 +98,10 @@
   }
 
+  /**
+   * Send an outgoing event of type 'disconnectPeer'.
+   * @param peerID The peer to which the event is sent
+   */
   void Connection::disconnectPeer(uint32_t peerID)
   {
-//     this->overallMutex_->lock();
     outgoingEvent outEvent = { peerID, OutgoingEventType::disconnectPeer, nullptr, 0 };
     
@@ -86,7 +109,9 @@
     this->outgoingEvents_.push_back(outEvent);
     this->outgoingEventsMutex_->unlock();
-//     this->overallMutex_->unlock();
-  }
-  
+  }
+
+  /**
+   * Send an outgoing event of type 'disconnectPeers'.
+   */
   void Connection::disconnectPeers()
   {
@@ -98,7 +123,12 @@
   }
 
+  /**
+   * Send a packet.
+   * @param packet Pointer to the packet to send
+   * @param peerID The peer to which the event is sent
+   * @param channelId The channel ID
+   */
   void Connection::addPacket(ENetPacket* packet, uint32_t peerID, uint8_t channelID)
   {
-//     this->overallMutex_->lock();
     outgoingEvent outEvent = { peerID, OutgoingEventType::sendPacket, packet, channelID };
     
@@ -106,10 +136,13 @@
     this->outgoingEvents_.push_back(outEvent);
     this->outgoingEventsMutex_->unlock();
-//     this->overallMutex_->unlock();
-  }
-  
+  }
+  
+  /**
+   * Send a broadcast packet.
+   * @param packet Pointer to the packet to send
+   * @param channelId The channel ID
+   */
   void Connection::broadcastPacket(ENetPacket* packet, uint8_t channelID)
   {
-//     this->overallMutex_->lock();
     outgoingEvent outEvent = { 0, OutgoingEventType::broadcastPacket, packet, channelID };
     
@@ -117,24 +150,24 @@
     this->outgoingEvents_.push_back(outEvent);
     this->outgoingEventsMutex_->unlock();
-//     this->overallMutex_->unlock();
-  }
-
-  
+  }
+
+  
+  /**
+   * Main communication thread
+   */
   void Connection::communicationThread()
   {
     ENetEvent event;
     
-//     this->overallMutex_->lock();
-    while( bCommunicationThreadRunning_ )
+    while(this->bCommunicationThreadRunning_)
     {
       // Receive all pending incoming Events (such as packets, connects and disconnects)
-      while( enet_host_check_events( this->host_, &event ) > 0 )
+      while(enet_host_check_events(this->host_, &event ) > 0)
       {
-        processIncomingEvent(event);
+        this->processIncomingEvent(event);
       }
       
-//       this->overallMutex_->unlock();
+      // Sleep for 1ms
       msleep(1);
-//       this->overallMutex_->lock();
       
       // Send all waiting outgoing packets
@@ -142,7 +175,7 @@
       uint32_t outgoingEventsCount = this->outgoingEvents_.size();
       this->outgoingEventsMutex_->unlock();
-      while( outgoingEventsCount > 0 )
+
+      while(outgoingEventsCount > 0)
       {
-//         orxout(verbose, context::network) << "outgoing event" << endl;
         this->outgoingEventsMutex_->lock();
         outgoingEvent outEvent = this->outgoingEvents_.front();
@@ -150,5 +183,5 @@
         this->outgoingEventsMutex_->unlock();
         
-        processOutgoingEvent(outEvent);
+        this->processOutgoingEvent(outEvent);
         
         this->outgoingEventsMutex_->lock();
@@ -158,17 +191,20 @@
       
       // Wait for incoming events (at most NETWORK_WAIT_TIMEOUT ms)
-      if( enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0 )
+      if(enet_host_service(this->host_, &event, NETWORK_WAIT_TIMEOUT) > 0)
       {
-        processIncomingEvent(event);
+        this->processIncomingEvent(event);
       }
     }
-//     this->overallMutex_->unlock();
-  }
-  
+  }
+  
+  /**
+   * Handle an incoming event.
+   * @param event The incoming event
+   */
   void Connection::processIncomingEvent(ENetEvent& event)
   {
     incomingEvent inEvent;
     // preprocess event
-    switch( event.type )
+    switch(event.type)
     {
       case ENET_EVENT_TYPE_CONNECT:
@@ -192,25 +228,29 @@
   }
   
+  /**
+   * Send an event.
+   * @param event The event to send
+   */
   void Connection::processOutgoingEvent(outgoingEvent& event)
   {
     ENetPeer* peer;
-    switch( event.type )
+    switch(event.type)
     {
       case OutgoingEventType::sendPacket:
         // check whether the peer is still/already in the peer list
-        if( this->peerMap_.find(event.peerID) != this->peerMap_.end() )
+        if(this->peerMap_.find(event.peerID) != this->peerMap_.end())
         {
           peer = this->peerMap_[event.peerID];
-          enet_peer_send( peer, event.channelID, event.packet );
+          enet_peer_send(peer, event.channelID, event.packet);
         }
         else
         {
           // peer probably already disconnected so just discard packet
-          assert(event.peerID<this->nextPeerID_);
+          orxout(message) << "Trying to send packet to peer that is not in peer list. Ignoring packet." << endl;
           enet_packet_destroy(event.packet);
         }
         break;
       case OutgoingEventType::disconnectPeer:
-        if( this->peerMap_.find(event.peerID) != this->peerMap_.end() )
+        if(this->peerMap_.find(event.peerID) != this->peerMap_.end())
         {
           peer = this->peerMap_[event.peerID];
@@ -220,9 +260,9 @@
         {
           // peer probably already disconnected so just discard disconnect event
-          assert(event.peerID<this->nextPeerID_);
+          assert(event.peerID < this->nextPeerID_);
         }
         break;
       case OutgoingEventType::disconnectPeers:
-        disconnectPeersInternal();
+        this->disconnectPeersInternal();
         break;
       case OutgoingEventType::broadcastPacket:
@@ -234,5 +274,4 @@
   }
 
-
   void Connection::disconnectPeersInternal()
   {
@@ -241,5 +280,5 @@
       enet_peer_disconnect(mapEntry.second, 0);
     }
-    uint32_t iterations = NETWORK_DISCONNECT_TIMEOUT/NETWORK_WAIT_TIMEOUT;
+    uint32_t iterations = NETWORK_DISCONNECT_TIMEOUT / NETWORK_WAIT_TIMEOUT;
     uint32_t i = 0;
     while( this->peerMap_.size() && i++ < iterations )
@@ -361,4 +400,3 @@
   }
 
-
 }
