Index: /branches/network/src/lib/network/handshake.cc
===================================================================
--- /branches/network/src/lib/network/handshake.cc	(revision 7730)
+++ /branches/network/src/lib/network/handshake.cc	(revision 7731)
@@ -31,12 +31,9 @@
 
   this->setIsServer(server);
-  this->localState.hostId = clientId;
-  this->localState.networkManagerId = networkGameManagerId;
-  this->localState.messageManagerId = messageManagerId;
  
   orxId_handler = registerVarId( new SynchronizeableInt( &localState.orxId, &remoteState.orxId, "orxonoxId", PERMISSION_ALL ) );
   version_handler = registerVarId( new SynchronizeableInt( &localState.version, &remoteState.version, "version", PERMISSION_ALL ) );
   netManId_handler = registerVarId( new SynchronizeableInt( &localState.networkManagerId, &remoteState.networkManagerId, "networkManagerId", PERMISSION_ALL ) );
-  netManId_handler = registerVarId( new SynchronizeableInt( &localState.messageManagerId, &remoteState.messageManagerId, "networkManagerId", PERMISSION_ALL ) );
+  msgManId_handler = registerVarId( new SynchronizeableInt( &localState.messageManagerId, &remoteState.messageManagerId, "messageManagerId", PERMISSION_ALL ) );
   hostId_handler = registerVarId( new SynchronizeableInt( &localState.hostId, &remoteState.hostId, "hostId", PERMISSION_ALL ) );
   completed_handler = registerVarId( new SynchronizeableInt( &localState.completed, &remoteState.completed, "completed", PERMISSION_ALL ) );
@@ -49,4 +46,5 @@
   localState.hostId = clientId;
   localState.networkManagerId = networkGameManagerId;
+  this->localState.messageManagerId = messageManagerId;
   localState.orxId = _ORXONOX_ID;
   localState.version = _ORXONOX_VERSION;
@@ -57,4 +55,5 @@
   remoteState.hostId = -1;
   remoteState.networkManagerId = -1;
+  remoteState.messageManagerId = -1;
   remoteState.orxId = 0;
   remoteState.version = 0;
Index: /branches/network/src/lib/network/handshake.h
===================================================================
--- /branches/network/src/lib/network/handshake.h	(revision 7730)
+++ /branches/network/src/lib/network/handshake.h	(revision 7731)
@@ -47,4 +47,5 @@
     
     int netManId_handler;
+    int msgManId_handler;
     int hostId_handler;
     int completed_handler;
Index: /branches/network/src/lib/network/network_stream.cc
===================================================================
--- /branches/network/src/lib/network/network_stream.cc	(revision 7730)
+++ /branches/network/src/lib/network/network_stream.cc	(revision 7731)
@@ -412,5 +412,5 @@
       offset += INTSIZE;
 
-      n = sync.getStateDiff( peer->second.userId, buf + offset, UDP_PACKET_SIZE-offset, currentState, peer->second.lastAckedState, 0 );
+      n = sync.getStateDiff( peer->second.userId, buf + offset, UDP_PACKET_SIZE-offset, currentState, peer->second.lastAckedState, -1000 );
       offset += n;
       
@@ -421,4 +421,5 @@
     
     assert( peer->second.socket->writePacket( buf, offset ) );
+    peer->second.sentStateTicks[currentState] = SDL_GetTicks();
     PRINTF(0)("send packet: %d userId = %d\n", offset, peer->second.userId);
   }
@@ -461,4 +462,5 @@
       assert( Converter::byteArrayToInt( buf + 2*INTSIZE, &fromState ) == INTSIZE );
       assert( Converter::byteArrayToInt( buf + 3*INTSIZE, &ackedState ) == INTSIZE );
+      PRINTF(0)("ackedstate: %d\n", ackedState);
       offset = 4*INTSIZE;
 
@@ -475,5 +477,5 @@
         continue;
       }
-
+      
       while ( offset < length )
       {
@@ -485,5 +487,5 @@
       
         Synchronizeable * sync = NULL;
-      
+        
         for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ )
         { 
@@ -495,5 +497,5 @@
           }
         }
-      
+        
         if ( sync == NULL )
         {
@@ -503,5 +505,5 @@
             continue;
           }
-        
+
         //TODO dont accept new object from all peers (probably only servers)
           int leafClassId;
@@ -511,5 +513,5 @@
             continue;
           }
-        
+
           Converter::byteArrayToInt( buf + offset, &leafClassId );
         
@@ -532,5 +534,5 @@
             continue;
           }
-        
+
           if ( b->isA(CL_SYNCHRONIZEABLE) )
           {
@@ -550,13 +552,39 @@
         }
 
-        offset += sync->setStateDiff( peer->second.userId, buf+offset, length-offset, state, fromState );
+        offset += sync->setStateDiff( peer->second.userId, buf+offset, syncDataLength, state, fromState ); 
       }
-    
+      
       if ( offset != length )
       {
+        PRINTF(0)("offset (%d) != length (%d)\n", offset, length);
         peer->second.socket->disconnectServer();
       }
+      
+      if ( peer->second.sentStateTicks.find( ackedState ) != peer->second.sentStateTicks.end() )
+      {
+        peer->second.ackDelay.push_back( SDL_GetTicks() - peer->second.sentStateTicks[ackedState] );
+      }
+      
+      while ( peer->second.sentStateTicks.begin()->first <= ackedState )
+        peer->second.sentStateTicks.erase( peer->second.sentStateTicks.begin() );
+      
+      while ( peer->second.ackDelay.size() > 20 )
+        peer->second.ackDelay.erase( peer->second.ackDelay.begin() );
+      
+      peer->second.ping = 0;
+      
+      for ( std::list<int>::iterator it = peer->second.ackDelay.begin(); it != peer->second.ackDelay.end(); it++ )
+        peer->second.ping += *it;
+      
+      if ( peer->second.ackDelay.size() == 0 )
+        peer->second.ping = -1;
+      else
+        peer->second.ping /= peer->second.ackDelay.size();
+      
+      PRINTF(0)("PING: user: %d ping: %d\n", peer->second.userId, peer->second.ping );
     
       peer->second.lastAckedState = ackedState;
+      peer->second.lastRecvedState = state;
+      
     }
   
Index: /branches/network/src/lib/network/network_stream.h
===================================================================
--- /branches/network/src/lib/network/network_stream.h	(revision 7730)
+++ /branches/network/src/lib/network/network_stream.h	(revision 7731)
@@ -28,10 +28,13 @@
     PeerInfo() { clear(); }
     void clear() { userId = 0; isServer = false; socket = NULL; handshake = NULL; lastAckedState = 0; lastRecvedState = 0; }
-    int             userId;
-    bool            isServer;
-    NetworkSocket * socket;
-    Handshake *     handshake;
-    int             lastAckedState;
-    int             lastRecvedState;
+    int               userId;
+    bool              isServer;
+    NetworkSocket *   socket;
+    Handshake *       handshake;
+    int               lastAckedState;
+    int               lastRecvedState;
+    std::map<int,int> sentStateTicks;
+    std::list<int>    ackDelay;
+    int               ping;
 };
 
Index: /branches/network/src/lib/network/synchronizeable.cc
===================================================================
--- /branches/network/src/lib/network/synchronizeable.cc	(revision 7730)
+++ /branches/network/src/lib/network/synchronizeable.cc	(revision 7731)
@@ -123,5 +123,5 @@
       if ( (*it2)->data != NULL )
       {
-        delete (*it2)->data;
+        delete [] (*it2)->data;
         (*it2)->data = NULL;
       }
@@ -154,5 +154,5 @@
   stateTo->stateId = stateId;
   stateTo->dataLength = neededSize;
-  stateTo->data = (byte*)malloc( neededSize );
+  stateTo->data = new byte[ neededSize ];
 
   std::list<int>::iterator sizeIter = stateFrom->sizeList.begin();
@@ -178,14 +178,4 @@
       stateTo->sizeList.push_back( n );
       i += n;
-    }
-    else if ( ! hasPermission )
-    {
-      for ( int j = 0; j < (*it)->getSize(); j++ )
-      {
-        //TODO this should be called!!!!!!!!!!1
-        assert(false);
-        stateTo->data[i] = 0;
-        i++;
-      }
     }
     else
@@ -240,5 +230,5 @@
   stateTo->stateId = stateId;
   stateTo->dataLength = length;
-  stateTo->data = (byte*)malloc( length );
+  stateTo->data = new byte[ length ];
 
   //remove old states
@@ -254,5 +244,5 @@
       if ( (*it2)->data != NULL )
       {
-        delete (*it2)->data;
+        delete [] (*it2)->data;
         (*it2)->data = NULL;
       }
@@ -290,5 +280,5 @@
     
   }
-
+  
   //add state to state history
   recvStates[userId].push_back( stateTo );
@@ -366,8 +356,8 @@
     {
       if ( (*it2)->data )
-        delete (*it2)->data;
+        delete [] (*it2)->data;
       (*it2)->data = NULL;
       
-      delete *it2;
+      delete [] *it2;
     }
   }
@@ -380,8 +370,8 @@
     {
       if ( (*it2)->data )
-        delete (*it2)->data;
+        delete [] (*it2)->data;
       (*it2)->data = NULL;
       
-      delete *it2;
+      delete [] *it2;
     }
   }
Index: /branches/network/src/lib/network/synchronizeable_var/synchronizeable_var.cc
===================================================================
--- /branches/network/src/lib/network/synchronizeable_var/synchronizeable_var.cc	(revision 7730)
+++ /branches/network/src/lib/network/synchronizeable_var/synchronizeable_var.cc	(revision 7731)
@@ -36,4 +36,5 @@
   this->name = name;
   this->bWatched = false;
+  this->changed = false;
 }
 
Index: /branches/network/src/subprojects/network/network_unit_test.cc
===================================================================
--- /branches/network/src/subprojects/network/network_unit_test.cc	(revision 7730)
+++ /branches/network/src/subprojects/network/network_unit_test.cc	(revision 7731)
@@ -220,5 +220,5 @@
   for( int i = 0; i < 3; i++)
   {
-    nm->synchronize();
+    nm->synchronize( 1000 );
     /* simulate the network delay */
     SDL_Delay(50);
@@ -237,7 +237,8 @@
 }
 
-void testCB( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
+bool testCB( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
 {
   printf("GOT MESSAGE: %s\n", data);
+  return true;
 }
 
@@ -273,5 +274,5 @@
   {
     MessageManager::getInstance()->sendMessage( TESTMESSAGEID, (byte*)"server to client", 18, RT_ALL, 0, MP_HIGHBANDWIDTH );
-    netMan->synchronize();
+    netMan->synchronize( 1000 );
     SDL_Delay(1000);
     ss->debug();
@@ -311,5 +312,5 @@
   for(;;)
   {
-    netMan->synchronize();
+    netMan->synchronize( 1000 );
     MessageManager::getInstance()->sendMessage( TESTMESSAGEID, (byte*)"client to server", 18, RT_ALL, 0, MP_HIGHBANDWIDTH );
     ss = dynamic_cast<SimpleSync*>(ClassList::getObject( "Server", CL_SIMPLE_SYNC ) );
@@ -355,5 +356,5 @@
   for(;;)
   {
-    netMan->synchronize();
+    netMan->synchronize( 1000 );
     SDL_Delay(10);
   }
