Index: /branches/network/src/lib/network/network_stream.cc
===================================================================
--- /branches/network/src/lib/network/network_stream.cc	(revision 7658)
+++ /branches/network/src/lib/network/network_stream.cc	(revision 7659)
@@ -581,122 +581,125 @@
       continue;
 
-    packetLength = peer->second.socket->readPacket( buf, UDP_PACKET_SIZE );
-
-    if ( packetLength < 4*INTSIZE )
-    {
-      if ( packetLength != 0 )
-        PRINTF(1)("got too small packet: %d\n", packetLength);
-      continue;
-    }
-    
-    assert( Converter::byteArrayToInt( buf, &length ) == INTSIZE );
-    assert( Converter::byteArrayToInt( buf + INTSIZE, &state ) == INTSIZE );
-    assert( Converter::byteArrayToInt( buf + 2*INTSIZE, &fromState ) == INTSIZE );
-    assert( Converter::byteArrayToInt( buf + 3*INTSIZE, &ackedState ) == INTSIZE );
-    offset = 4*INTSIZE;
-
-    PRINTF(0)("got packet: %d, %d\n", length, packetLength);
+    while ( packetLength = peer->second.socket->readPacket( buf, UDP_PACKET_SIZE ) > 0 )
+    {
+
+      if ( packetLength < 4*INTSIZE )
+      {
+        if ( packetLength != 0 )
+          PRINTF(1)("got too small packet: %d\n", packetLength);
+        continue;
+      }
+    
+      assert( Converter::byteArrayToInt( buf, &length ) == INTSIZE );
+      assert( Converter::byteArrayToInt( buf + INTSIZE, &state ) == INTSIZE );
+      assert( Converter::byteArrayToInt( buf + 2*INTSIZE, &fromState ) == INTSIZE );
+      assert( Converter::byteArrayToInt( buf + 3*INTSIZE, &ackedState ) == INTSIZE );
+      offset = 4*INTSIZE;
+
+      PRINTF(0)("got packet: %d, %d\n", length, packetLength);
     
     //if this is an old state drop it
-    if ( state <= peer->second.lastRecvedState )
-      continue;
-    
-    if ( packetLength != length )
-    {
-      PRINTF(1)("real packet length (%d) and transmitted packet length (%d) do not match!\n", packetLength, length);
-      peer->second.socket->disconnectServer();
-      continue;
-    }
-
-    while ( offset < length )
-    {
-      assert( Converter::byteArrayToInt( buf + offset, &uniqueId ) == INTSIZE );
-      offset += INTSIZE;
-      
-      assert( Converter::byteArrayToInt( buf + offset, &syncDataLength ) == INTSIZE );
-      offset += INTSIZE;
-      
-      Synchronizeable * sync = NULL;
-      
-      for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ )
-      { 
+      if ( state <= peer->second.lastRecvedState )
+        continue;
+    
+      if ( packetLength != length )
+      {
+        PRINTF(1)("real packet length (%d) and transmitted packet length (%d) do not match!\n", packetLength, length);
+        peer->second.socket->disconnectServer();
+        continue;
+      }
+
+      while ( offset < length )
+      {
+        assert( Converter::byteArrayToInt( buf + offset, &uniqueId ) == INTSIZE );
+        offset += INTSIZE;
+      
+        assert( Converter::byteArrayToInt( buf + offset, &syncDataLength ) == INTSIZE );
+        offset += INTSIZE;
+      
+        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 ) )
-        {
-          sync = *it;
-          break;
-        }
-      }
-      
-      if ( sync == NULL )
-      {
-        if ( oldSynchronizeables.find( uniqueId ) != oldSynchronizeables.end() )
-        {
-          offset += syncDataLength;
-          continue;
-        }
+          if ( (*it)->getUniqueID() == uniqueId || ( uniqueId == 0 && (*it)->getUniqueID() == peer->second.userId ) )
+          {
+            sync = *it;
+            break;
+          }
+        }
+      
+        if ( sync == NULL )
+        {
+          if ( oldSynchronizeables.find( uniqueId ) != oldSynchronizeables.end() )
+          {
+            offset += syncDataLength;
+            continue;
+          }
         
         //TODO dont accept new object from all peers (probably only servers)
-        int leafClassId;
-        if ( INTSIZE > length - offset )
-        {
-          offset += syncDataLength;
-          continue;
-        }
+          int leafClassId;
+          if ( INTSIZE > length - offset )
+          {
+            offset += syncDataLength;
+            continue;
+          }
         
-        Converter::byteArrayToInt( buf + offset, &leafClassId );
+          Converter::byteArrayToInt( buf + offset, &leafClassId );
         
-        BaseObject * b;
-        /* These are some small exeptions in creation: Not all objects can/should be created via Factory */
-        /* Exception 1: NullParent */
-        if( leafClassId == CL_NULL_PARENT || leafClassId == CL_SYNCHRONIZEABLE )
-        {
-          PRINTF(1)("Can not create Class with ID %x!\n", (int)leafClassId);
-          offset += syncDataLength;
-          continue;
-        }
-        else
-          b = Factory::fabricate( (ClassID)leafClassId );
-
-        if ( !b )
-        {
-          PRINTF(1)("Could not fabricate Object with classID %x\n", leafClassId);
-          offset += syncDataLength;
-          continue;
-        }
+          BaseObject * b;
+          /* These are some small exeptions in creation: Not all objects can/should be created via Factory */
+          /* Exception 1: NullParent */
+          if( leafClassId == CL_NULL_PARENT || leafClassId == CL_SYNCHRONIZEABLE )
+          {
+            PRINTF(1)("Can not create Class with ID %x!\n", (int)leafClassId);
+            offset += syncDataLength;
+            continue;
+          }
+          else
+            b = Factory::fabricate( (ClassID)leafClassId );
+
+          if ( !b )
+          {
+            PRINTF(1)("Could not fabricate Object with classID %x\n", leafClassId);
+            offset += syncDataLength;
+            continue;
+          }
         
-        if ( b->isA(CL_SYNCHRONIZEABLE) )
-        {
-          sync = dynamic_cast<Synchronizeable*>(b);
-          sync->setUniqueID( uniqueId );
-          sync->setSynchronized(true);
-  
-          PRINTF(0)("Fabricated %s with id %d\n", sync->getClassName(), sync->getUniqueID());
-        }
-        else
-        {
-          PRINTF(1)("Class with ID %x is not a synchronizeable!\n", (int)leafClassId);
-          delete b;
-          offset += syncDataLength;
-          continue;
-        }
+          if ( b->isA(CL_SYNCHRONIZEABLE) )
+          {
+            sync = dynamic_cast<Synchronizeable*>(b);
+            sync->setUniqueID( uniqueId );
+            sync->setSynchronized(true);
+  
+            PRINTF(0)("Fabricated %s with id %d\n", sync->getClassName(), sync->getUniqueID());
+          }
+          else
+          {
+            PRINTF(1)("Class with ID %x is not a synchronizeable!\n", (int)leafClassId);
+            delete b;
+            offset += syncDataLength;
+            continue;
+          }
+        }
+
+        offset += sync->setStateDiff( peer->second.userId, buf+offset, length-offset, state, fromState );
       }
-
-      offset += sync->setStateDiff( peer->second.userId, buf+offset, length-offset, state, fromState );
-    }
-    
-    if ( offset != length )
-    {
-      peer->second.socket->disconnectServer();
-    }
-    
-    peer->second.lastAckedState = ackedState;
-  }
-  
-}
-
-
-
-
-
-
+    
+      if ( offset != length )
+      {
+        peer->second.socket->disconnectServer();
+      }
+    
+      peer->second.lastAckedState = ackedState;
+    }
+  
+  }
+  
+}
+
+
+
+
+
+
