Index: trunk/src/lib/network/netdefs.h
===================================================================
--- trunk/src/lib/network/netdefs.h	(revision 9235)
+++ trunk/src/lib/network/netdefs.h	(revision 9246)
@@ -25,4 +25,5 @@
 
 
+//!< enum indicating the type of the node
 typedef enum {
   NET_SERVER,
@@ -30,4 +31,5 @@
 } NodeType;
 
+//!< enum indicating the type of the network connection (2 protocols supported)
 typedef enum ConnectionType {
   NET_UDP = 0,
@@ -35,5 +37,5 @@
 };
 
-
+//!< the type of the user id (special number never used elsewhere)
 typedef enum {
   NET_UID_UNASSIGNED = -1,
Index: trunk/src/lib/network/network_stream.cc
===================================================================
--- trunk/src/lib/network/network_stream.cc	(revision 9235)
+++ trunk/src/lib/network/network_stream.cc	(revision 9246)
@@ -10,12 +10,9 @@
 
 ### File Specific:
-   main-programmer: claudio
-   co-programmer:
+   main-programmer: Christoph Renner rennerc@ee.ethz.ch
+   co-programmer:   Patrick Boenzli  boenzlip@orxonox.ethz.ch
 */
 
 
-/* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module
-   For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput
-*/
 #define DEBUG_MODULE_NETWORK
 
@@ -64,4 +61,9 @@
 
 
+/**
+ * connect to a server as a client
+ *  @param host: host name (address)
+ *  @param port: port number
+ */
 NetworkStream::NetworkStream( std::string host, int port )
 {
@@ -75,4 +77,8 @@
 
 
+/**
+ * start as a server
+ *  @param port: at this port
+ */
 NetworkStream::NetworkStream( int port )
 {
@@ -84,4 +90,7 @@
 
 
+/**
+ * generic init functions
+ */
 void NetworkStream::init()
 {
@@ -93,7 +102,7 @@
   myHostId = 0;
   currentState = 0;
-  
+
   remainingBytesToWriteToDict = Preferences::getInstance()->getInt( "compression", "writedict", 0 );
-  
+
   assert( Zip::getInstance()->loadDictionary( "testdict" ) >= 0 );
   this->dictClient = Zip::getInstance()->loadDictionary( "dict2pl_client" );
@@ -104,4 +113,7 @@
 
 
+/**
+ * deconstructor
+ */
 NetworkStream::~NetworkStream()
 {
@@ -120,5 +132,5 @@
       i->second.socket = NULL;
     }
-    
+
     if ( i->second.handshake )
     {
@@ -126,5 +138,5 @@
       i->second.handshake = NULL;
     }
-    
+
     if ( i->second.connectionMonitor )
     {
@@ -138,4 +150,7 @@
 
 
+/**
+ * creates a new instance of the network game manager
+ */
 void NetworkStream::createNetworkGameManager()
 {
@@ -148,4 +163,7 @@
 
 
+/**
+ * starts the network handshake
+ */
 void NetworkStream::startHandshake()
 {
@@ -154,7 +172,7 @@
   assert( peers[0].handshake == NULL );
   peers[0].handshake = hs;
-  
+
   hs->setPreferedNickName( Preferences::getInstance()->getString( "multiplayer", "nickname", "Player" ) );
-  
+
 //   peers[0].handshake->setSynchronized( true );
   //this->connectSynchronizeable(*hs);
@@ -164,4 +182,9 @@
 
 
+/**
+ * this functions connects a synchronizeable to the networkstream, therefore synchronizeing
+ * it all over the network and creating it on the other platforms (if and only if it is a
+ * server
+ */
 void NetworkStream::connectSynchronizeable(Synchronizeable& sync)
 {
@@ -173,4 +196,7 @@
 
 
+/**
+ * removes the synchronizeable from the list of synchronized entities
+ */
 void NetworkStream::disconnectSynchronizeable(Synchronizeable& sync)
 {
@@ -179,24 +205,28 @@
   if (disconnectSynchro != this->synchronizeables.end())
     this->synchronizeables.erase(disconnectSynchro);
-  
+
   oldSynchronizeables[sync.getUniqueID()] = SDL_GetTicks();
 }
 
 
+/**
+ * this is called to process data from the network socket to the synchronizeable and vice versa
+ */
 void NetworkStream::processData()
 {
   int tick = SDL_GetTicks();
-  
+
   currentState++;
-  
+
   if ( this->type == NET_SERVER )
   {
     if ( serverSocket )
       serverSocket->update();
-    
+
     this->updateConnectionList();
   }
   else
   {
+    // check if the connection is ok else terminate and remove
     if ( peers[0].socket && ( !peers[0].socket->isOk() || peers[0].connectionMonitor->hasTimedOut() ) )
     {
@@ -210,5 +240,5 @@
         delete peers[0].handshake;
       peers[0].handshake = NULL;
-      
+
       if ( peers[0].connectionMonitor )
         delete peers[0].connectionMonitor;
@@ -219,12 +249,16 @@
   cleanUpOldSyncList();
   handleHandshakes();
-  
+
   // order of up/downstream is important!!!!
   // don't change it
   handleDownstream( tick );
   handleUpstream( tick );
-
-}
-
+}
+
+
+/**
+ * if we are a server update the connection list to accept new connections (clients)
+ * also start the handsake for the new clients
+ */
 void NetworkStream::updateConnectionList( )
 {
@@ -233,8 +267,10 @@
   NetworkSocket* tempNetworkSocket = serverSocket->getNewSocket();
 
+  // we got new network node
   if ( tempNetworkSocket )
   {
     int clientId;
-    if ( freeSocketSlots.size() >0 )
+    // if there is a list of free client id slots, take these
+    if ( freeSocketSlots.size() > 0 )
     {
       clientId = freeSocketSlots.back();
@@ -246,12 +282,13 @@
       peers[clientId].userId = clientId;
       peers[clientId].isServer = false;
-    } else
+    }
+    else
     {
       clientId = 1;
-      
+
       for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ )
         if ( it->first >= clientId )
           clientId = it->first + 1;
-      
+
       peers[clientId].socket = tempNetworkSocket;
       peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID());
@@ -260,8 +297,9 @@
       peers[clientId].userId = clientId;
       peers[clientId].isServer = false;
-      
+
       PRINTF(0)("num sync: %d\n", synchronizeables.size());
     }
 
+    // check if there are too many clients connected
     if ( clientId > MAX_CONNECTIONS )
     {
@@ -270,16 +308,19 @@
     }
     else
-
-    PRINTF(0)("New Client: %d\n", clientId);
+    {
+      PRINTF(0)("New Client: %d\n", clientId);
+    }
 
     //this->connectSynchronizeable(*handshakes[clientId]);
   }
+
+
 
   //check if connections are ok else remove them
   for ( PeerList::iterator it = peers.begin(); it != peers.end(); )
   {
-    if (  
+    if (
           it->second.socket &&
-          ( 
+          (
             !it->second.socket->isOk()  ||
             it->second.connectionMonitor->hasTimedOut()
@@ -291,7 +332,7 @@
         reason = "timeout";
       PRINTF(0)("Client is gone: %d (%s)\n", it->second.userId, reason.c_str());
-      
-      //assert(false);
-
+
+
+      // clean up the network data
       it->second.socket->disconnectServer();
       delete it->second.socket;
@@ -301,9 +342,9 @@
         delete it->second.connectionMonitor;
       it->second.connectionMonitor = NULL;
-      
+
       if ( it->second.handshake )
         delete it->second.handshake;
       it->second.handshake = NULL;
-      
+
       for ( SynchronizeableList::iterator it2 = synchronizeables.begin(); it2 != synchronizeables.end(); it2++ )
       {
@@ -314,13 +355,13 @@
 
       freeSocketSlots.push_back( it->second.userId );
-      
+
       PeerList::iterator delit = it;
       it++;
-      
+
       peers.erase( delit );
-      
+
       continue;
     }
-    
+
     it++;
   }
@@ -328,4 +369,5 @@
 
 }
+
 
 void NetworkStream::debug()
@@ -348,4 +390,7 @@
 
 
+/**
+ * @returns the number of synchronizeables registered to this stream
+ */
 int NetworkStream::getSyncCount()
 {
@@ -359,6 +404,7 @@
 }
 
-/**
- * check if handshakes completed
+
+/**
+ * check if handshakes completed. if so create the network game manager else remove it again
  */
 void NetworkStream::handleHandshakes( )
@@ -383,5 +429,5 @@
               MessageManager::getInstance()->setUniqueID( it->second.handshake->getMessageManagerId() );
             }
-              
+
 
             PRINT(0)("handshake finished id=%d\n", it->second.handshake->getNetworkGameManagerId());
@@ -396,5 +442,5 @@
               {
                 handleNewClient( it->second.userId );
-                
+
                 if ( PlayerStats::getStats( it->second.userId ) && it->second.handshake->getPreferedNickName() != "" )
                 {
@@ -402,5 +448,5 @@
                 }
               }
-              
+
               PRINT(0)("handshake finished delete it\n");
               delete it->second.handshake;
@@ -420,4 +466,5 @@
 }
 
+
 /**
  * handle upstream network traffic
@@ -427,92 +474,102 @@
   int offset;
   int n;
-  
+
   for ( PeerList::reverse_iterator peer = peers.rbegin(); peer != peers.rend(); peer++ )
   {
-    offset = INTSIZE; //make already space for length 
-    
+    offset = INTSIZE; // reserve enough space for the packet length
+
+    // continue with the next peer if this peer has no socket assigned (therefore no network)
     if ( !peer->second.socket )
       continue;
-    
+
+    // header informations: current state
     n = Converter::intToByteArray( currentState, buf + offset, UDP_PACKET_SIZE - offset );
     assert( n == INTSIZE );
     offset += n;
-    
+
+    // header informations: last acked state
     n = Converter::intToByteArray( peer->second.lastAckedState, buf + offset, UDP_PACKET_SIZE - offset );
     assert( n == INTSIZE );
     offset += n;
-    
+
+    // header informations: last recved state
     n = Converter::intToByteArray( peer->second.lastRecvedState, buf + offset, UDP_PACKET_SIZE - offset );
     assert( n == INTSIZE );
     offset += n;
-    
+
+    // now write all synchronizeables in the packet
     for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ )
     {
       int oldOffset = offset;
       Synchronizeable & sync = **it;
-      
+
+      // do not include synchronizeables with uninit id and syncs that don't want to be synchronized
       if ( !sync.beSynchronized() || sync.getUniqueID() < 0 )
         continue;
 
-      //if handshake not finished only sync handshake
+      // if handshake not finished only sync handshake
       if ( peer->second.handshake && sync.getLeafClassID() != CL_HANDSHAKE )
         continue;
-      
+
+      // if we are a server and this is not our handshake
       if ( isServer() && sync.getLeafClassID() == CL_HANDSHAKE && sync.getUniqueID() != peer->second.userId )
         continue;
-      
-      //do not sync null parent
+
+      /* list of synchronizeables that will never be synchronized over the network: */
+      // do not sync null parent
       if ( sync.getLeafClassID() == CL_NULL_PARENT )
         continue;
 
       assert( offset + INTSIZE <= UDP_PACKET_SIZE );
-      
-      //server fakes uniqueid=0 for handshake
+
+      // server fakes uniqueid == 0 for handshake
       if ( this->isServer() && sync.getUniqueID() < MAX_CONNECTIONS - 1 )
         n = Converter::intToByteArray( 0, buf + offset, UDP_PACKET_SIZE - offset );
       else
         n = Converter::intToByteArray( sync.getUniqueID(), buf + offset, UDP_PACKET_SIZE - offset );
+
       assert( n == INTSIZE );
       offset += n;
-      
-      //make space for size
+
+      // make space for size
       offset += INTSIZE;
 
       n = sync.getStateDiff( peer->second.userId, buf + offset, UDP_PACKET_SIZE-offset, currentState, peer->second.lastAckedState, -1000 );
       offset += n;
-      //NETPRINTF(0)("GGGGGEEEEETTTTT: %s (%d) %d\n",sync.getClassName(), sync.getUniqueID(), n);
-      
+
       assert( Converter::intToByteArray( n, buf + offset - n - INTSIZE, INTSIZE ) == INTSIZE );
-      
-      //check if all bytes == 0 -> remove data 
-      //TODO not all synchronizeables like this maybe add Synchronizeable::canRemoveZeroDiff()
-      bool allZero = true; 
-      for ( int i = 0; i < n; i++ ) 
-      { 
-         if ( buf[i+oldOffset+2*INTSIZE] != 0 ) 
-           allZero = false; 
-      } 
-
-      if ( allZero ) 
-      { 
-        //NETPRINTF(n)("REMOVE ZERO DIFF: %s (%d)\n", sync.getClassName(), sync.getUniqueID()); 
-        offset = oldOffset; 
-      } 
-
-      
-    }
-    
+
+      // check if all data bytes == 0 -> remove data and the synchronizeable from the sync process since there is no update
+      // TODO not all synchronizeables like this maybe add Synchronizeable::canRemoveZeroDiff()
+      bool allZero = true;
+      for ( int i = 0; i < n; i++ )
+      {
+         if ( buf[i+oldOffset+2*INTSIZE] != 0 )
+           allZero = false;
+      }
+      // if there is no new data in this synchronizeable reset the data offset to the last state -> dont synchronizes
+      // data that hast not changed
+      if ( allZero )
+      {
+        offset = oldOffset;
+      }
+    } // all synchronizeables written
+
+
+
     for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ )
     {
       Synchronizeable & sync = **it;
-      
+
       if ( !sync.beSynchronized() || sync.getUniqueID() < 0 )
         continue;
-      
+
       sync.handleSentState( peer->second.userId, currentState, peer->second.lastAckedState );
     }
-    
+
+
     assert( Converter::intToByteArray( offset, buf, INTSIZE ) == INTSIZE );
-    
+
+    // now compress the data with the zip library
     int compLength = 0;
     if ( this->isServer() )
@@ -520,5 +577,5 @@
     else
       compLength = Zip::getInstance()->zip( buf, offset, compBuf, UDP_PACKET_SIZE, dictClient );
-    
+
     if ( compLength <= 0 )
     {
@@ -526,14 +583,13 @@
       continue;
     }
-    
+
     assert( peer->second.socket->writePacket( compBuf, compLength ) );
-    
+
     if ( this->remainingBytesToWriteToDict > 0 )
       writeToNewDict( buf, offset, true );
-    
+
     peer->second.connectionMonitor->processUnzippedOutgoingPacket( tick, buf, offset, currentState );
     peer->second.connectionMonitor->processZippedOutgoingPacket( tick, compBuf, compLength, currentState );
-    
-    //NETPRINTF(n)("send packet: %d userId = %d\n", offset, peer->second.userId);
+
   }
 }
@@ -545,5 +601,5 @@
 {
   int offset = 0;
-  
+
   int length = 0;
   int packetLength = 0;
@@ -554,8 +610,8 @@
   int fromState = 0;
   int syncDataLength = 0;
-  
+
   for ( PeerList::iterator peer = peers.begin(); peer != peers.end(); peer++ )
   {
-    
+
     if ( !peer->second.socket )
       continue;
@@ -564,6 +620,5 @@
     {
       peer->second.connectionMonitor->processZippedIncomingPacket( tick, compBuf, compLength );
-      
-      //PRINTF(0)("GGGGGOOOOOOOOOOTTTTTTTT: %d\n", compLength);
+
       packetLength = Zip::getInstance()->unZip( compBuf, compLength, buf, UDP_PACKET_SIZE );
 
@@ -574,8 +629,8 @@
         continue;
       }
-      
+
       if ( this->remainingBytesToWriteToDict > 0 )
         writeToNewDict( buf, packetLength, false );
-    
+
       assert( Converter::byteArrayToInt( buf, &length ) == INTSIZE );
       assert( Converter::byteArrayToInt( buf + INTSIZE, &state ) == INTSIZE );
@@ -584,13 +639,13 @@
       //NETPRINTF(n)("ackedstate: %d\n", ackedState);
       offset = 4*INTSIZE;
-      
+
       peer->second.connectionMonitor->processUnzippedIncomingPacket( tick, buf, packetLength, state, ackedState );
 
       //NETPRINTF(n)("got packet: %d, %d\n", length, packetLength);
-    
+
     //if this is an old state drop it
       if ( state <= peer->second.lastRecvedState )
         continue;
-    
+
       if ( packetLength != length )
       {
@@ -599,5 +654,5 @@
         continue;
       }
-      
+
       while ( offset + 2*INTSIZE < length )
       {
@@ -605,15 +660,15 @@
         assert( Converter::byteArrayToInt( buf + offset, &uniqueId ) == INTSIZE );
         offset += INTSIZE;
-      
+
         assert( Converter::byteArrayToInt( buf + offset, &syncDataLength ) == INTSIZE );
         offset += INTSIZE;
-        
+
         assert( syncDataLength > 0 );
         assert( syncDataLength < 10000 );
-      
+
         Synchronizeable * sync = NULL;
-        
+
         for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ )
-        { 
+        {
         //                                        client thinks his handshake has id 0!!!!!
           if ( (*it)->getUniqueID() == uniqueId || ( uniqueId == 0 && (*it)->getUniqueID() == peer->second.userId ) )
@@ -623,5 +678,5 @@
           }
         }
-        
+
         if ( sync == NULL )
         {
@@ -632,5 +687,5 @@
             continue;
           }
-          
+
           if ( !peers[peer->second.userId].isServer )
           {
@@ -638,5 +693,5 @@
             continue;
           }
-          
+
           int leafClassId;
           if ( INTSIZE > length - offset )
@@ -647,7 +702,7 @@
 
           Converter::byteArrayToInt( buf + offset, &leafClassId );
-          
+
           assert( leafClassId != 0 );
-        
+
           BaseObject * b = NULL;
           /* These are some small exeptions in creation: Not all objects can/should be created via Factory */
@@ -674,5 +729,5 @@
             sync->setUniqueID( uniqueId );
             sync->setSynchronized(true);
-  
+
             PRINTF(0)("Fabricated %s with id %d\n", sync->getClassName(), sync->getUniqueID());
           }
@@ -685,12 +740,12 @@
           }
         }
-        
-
-        int n = sync->setStateDiff( peer->second.userId, buf+offset, syncDataLength, state, fromState ); 
+
+
+        int n = sync->setStateDiff( peer->second.userId, buf+offset, syncDataLength, state, fromState );
         offset += n;
         //NETPRINTF(0)("SSSSSEEEEETTTTT: %s %d\n",sync->getClassName(), n);
 
       }
-      
+
       if ( offset != length )
       {
@@ -698,26 +753,26 @@
         peer->second.socket->disconnectServer();
       }
-      
-      
+
+
       for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ )
       {
         Synchronizeable & sync = **it;
-      
+
         if ( !sync.beSynchronized() || sync.getUniqueID() < 0 )
           continue;
-      
+
         sync.handleRecvState( peer->second.userId, state, fromState );
       }
-      
+
       assert( peer->second.lastAckedState <= ackedState );
       peer->second.lastAckedState = ackedState;
-      
+
       assert( peer->second.lastRecvedState < state );
       peer->second.lastRecvedState = state;
 
     }
-  
-  }
-  
+
+  }
+
 }
 
@@ -729,5 +784,5 @@
 {
   MessageManager::getInstance()->initUser( userId );
-  
+
   networkGameManager->signalNewPlayer( userId );
 }
@@ -739,5 +794,5 @@
 {
   int now = SDL_GetTicks();
-  
+
   for ( std::map<int,int>::iterator it = oldSynchronizeables.begin(); it != oldSynchronizeables.end();  )
   {
@@ -762,18 +817,18 @@
   if ( remainingBytesToWriteToDict <= 0 )
     return;
-  
+
   if ( length > remainingBytesToWriteToDict )
     length = remainingBytesToWriteToDict;
-  
+
   std::string fileName = ResourceManager::getInstance()->getDataDir();
   fileName += "/dicts/newdict";
-  
+
   if ( upstream )
     fileName += "_upstream";
   else
     fileName += "_downstream";
-  
+
   FILE * f = fopen( fileName.c_str(), "a" );
-  
+
   if ( !f )
   {
@@ -782,5 +837,5 @@
     return;
   }
-  
+
   if ( fwrite( data, 1, length, f ) != length )
   {
@@ -789,13 +844,13 @@
     return;
   }
-  
+
   fclose( f );
-  
-  remainingBytesToWriteToDict -= length;  
-}
-
-
-
-
-
-
+
+  remainingBytesToWriteToDict -= length;
+}
+
+
+
+
+
+
Index: trunk/src/lib/network/network_stream.h
===================================================================
--- trunk/src/lib/network/network_stream.h	(revision 9235)
+++ trunk/src/lib/network/network_stream.h	(revision 9246)
@@ -22,4 +22,5 @@
 class NetworkGameManager;
 
+//!< this structure contains informations about the network node
 class PeerInfo
 {
@@ -27,4 +28,7 @@
     PeerInfo() { clear(); }
     void clear() { userId = 0; isServer = false; socket = NULL; handshake = NULL; lastAckedState = 0; lastRecvedState = 0; connectionMonitor = NULL; }
+
+
+  public:
     int                 userId;
     bool                isServer;
@@ -72,5 +76,5 @@
 
     void debug();
-    
+
     inline PeerInfo & getPeerInfo( int userId ) { return peers[userId]; }
 
@@ -80,8 +84,8 @@
     void handleHandshakes();
     void handleUpstream( int tick );
-    void handleDownstream(int tick ); 
+    void handleDownstream(int tick );
     void handleNewClient( int userId );
     void cleanUpOldSyncList();
-    
+
     void writeToNewDict( byte * data, int length, bool upstream );
 
@@ -96,5 +100,5 @@
 
     int                        myHostId;
-    
+
     int                        currentState;                //!< current state id
 
@@ -102,5 +106,5 @@
 
     std::map<int,int>          oldSynchronizeables;         //!< used to save recently deleted sync ids to not recreate them
-    
+
     byte                       buf[UDP_PACKET_SIZE];        //!< used by handleUp/Downstream
     byte                       compBuf[UDP_PACKET_SIZE];    //!< used by handleUp/Downstream
Index: trunk/src/lib/network/udp_server_socket.h
===================================================================
--- trunk/src/lib/network/udp_server_socket.h	(revision 9235)
+++ trunk/src/lib/network/udp_server_socket.h	(revision 9246)
@@ -47,15 +47,14 @@
   public:
     UdpServerSocket( int port);
-
     virtual ~UdpServerSocket();
 
     virtual bool listen( unsigned int port );
-    
+
     virtual NetworkSocket* getNewSocket( void );
-    
+
     virtual void close();
-    
+
     virtual void update();
-    
+
     void removeUserPackets( int userId );
     void removeUser( int userId );
@@ -64,5 +63,5 @@
     int getPacketCount( int childId );
     void initUser( int childId, IPaddress ip, byte randomByte );
-    
+
   private:
     UDPsocket     socket;           //!< will be uses to send/recieve data
