Index: /branches/network/src/lib/network/message_manager.cc
===================================================================
--- /branches/network/src/lib/network/message_manager.cc	(revision 7677)
+++ /branches/network/src/lib/network/message_manager.cc	(revision 7678)
@@ -130,4 +130,5 @@
   int nAcks;
   
+  assert( i + INTSIZE <= length );
   n = Converter::byteArrayToInt( data + i, &nAcks );
   assert( n == INTSIZE );
@@ -140,4 +141,5 @@
   for ( int j = 0; j < nAcks; j++ )
   {
+    assert( i + INTSIZE <= length );
     n = Converter::byteArrayToInt( data + i, &number );
     assert( n == INTSIZE );
@@ -149,11 +151,38 @@
   int nMessages;
   
+  assert( i + INTSIZE <= length );
   n = Converter::byteArrayToInt( data + i, &nMessages );
   assert( n == INTSIZE );
   i += n;
+
+  int messageLength, messageId;
   
   for ( int j = 0; j < nMessages; j++ )
   {
-  }
+    assert( i + INTSIZE <= length );
+    n = Converter::byteArrayToInt( data + i, &messageLength );
+    assert( n == INTSIZE );
+    i += n;
+    
+    assert( i + INTSIZE <= length );
+    n = Converter::byteArrayToInt( data + i, &number );
+    assert( n == INTSIZE );
+    i += n;
+  
+    assert( i + INTSIZE <= length );
+    n = Converter::byteArrayToInt( data + i, &messageId );
+    assert( n == INTSIZE );
+    i += n;
+    
+    if ( number > 0 )
+      messageQueue[userId].toAck.push_back( number );
+    
+    assert( i + messageLength <= length );
+    assert( messageHandlerMap.find( (MessageId)messageId ) != messageHandlerMap.end() );
+    (*(messageHandlerMap[(MessageId)messageId].cb))( (MessageId)messageId, data + i, messageLength, messageHandlerMap[(MessageId)messageId].someData, userId );
+    i += messageLength;
+  }
+
+  return i;
 }
 
@@ -164,4 +193,17 @@
 void MessageManager::cleanUpUser( int userId )
 {
+  if ( messageQueue.find( userId ) == messageQueue.end() )
+    return;
+    
+  for ( std::list<NetworkMessage>::iterator it = messageQueue[userId].messages.begin(); it != messageQueue[userId].messages.end(); it++ )
+  {
+    if ( it->data )
+      delete it->data;
+    it->data = NULL;
+  }
+  
+  messageQueue[userId].toAck.clear();
+  
+  messageQueue.erase( userId );
 }
 
@@ -175,4 +217,13 @@
 bool MessageManager::registerMessageHandler( MessageId messageId, MessageCallback cb, void * someData )
 {
+  MessageHandler messageHandler;
+  
+  messageHandler.cb = cb;
+  messageHandler.messageId = messageId;
+  messageHandler.someData = someData;
+  
+  messageHandlerMap[messageId] = messageHandler;
+  
+  return true;
 }
 
@@ -183,3 +234,6 @@
 void MessageManager::initUser( int userId )
 {
-}
+  // just do something so map creates a new entry
+  messageQueue[userId].toAck.clear();
+  assert( messageQueue[userId].messages.size() == 0 );
+}
Index: /branches/network/src/lib/network/message_manager.h
===================================================================
--- /branches/network/src/lib/network/message_manager.h	(revision 7677)
+++ /branches/network/src/lib/network/message_manager.h	(revision 7678)
@@ -20,10 +20,8 @@
       int length
       int number
-      byte MessageId
+      int MessageId
       byte * data
     )[1..nmsg]
 */
-
-typedef void (*MessageCallback)( byte * data, int dataLength, void * someData, int userId );
 
 
@@ -31,4 +29,6 @@
 {
 };
+
+typedef void (*MessageCallback)( MessageId messageId, byte * data, int dataLength, void * someData, int userId );
 
 enum RecieverType
@@ -59,4 +59,5 @@
   MessageCallback cb;
   MessageId       messageId;
+  void *          someData;
 };
 
Index: /branches/network/src/lib/network/synchronizeable.cc
===================================================================
--- /branches/network/src/lib/network/synchronizeable.cc	(revision 7677)
+++ /branches/network/src/lib/network/synchronizeable.cc	(revision 7678)
@@ -174,5 +174,5 @@
     if ( ( hasPermission && (*it)->getPriority() >= priorityTH ) || sizeIter == stateFrom->sizeList.end() )
     {
-      (*it)->debug();
+      //(*it)->debug();
       n = (*it)->writeToBuf( stateTo->data+i, stateTo->dataLength - i );
       stateTo->sizeList.push_back( n );
@@ -183,4 +183,5 @@
       for ( int j = 0; j < (*it)->getSize(); j++ )
       {
+        //TODO this should be called!!!!!!!!!!1
         assert(false);
         stateTo->data[i] = 0;
@@ -310,5 +311,5 @@
     else
     {
-      PRINTF(0)("DONT SET VAR BECAUSE OF PERMISSION: %s %d %d %d %d %d %d\n", (*it)->getName().c_str(), (*it)->checkPermission( PERMISSION_SERVER ), (*it)->checkPermission( PERMISSION_OWNER ), (*it)->checkPermission( PERMISSION_ALL ), networkStream->isUserServer( userId ), this->owner, userId );
+//      PRINTF(0)("DONT SET VAR BECAUSE OF PERMISSION: %s %d %d %d %d %d %d\n", (*it)->getName().c_str(), (*it)->checkPermission( PERMISSION_SERVER ), (*it)->checkPermission( PERMISSION_OWNER ), (*it)->checkPermission( PERMISSION_ALL ), networkStream->isUserServer( userId ), this->owner, userId );
       i += (*it)->getSizeFromBuf( stateTo->data + i, stateTo->dataLength - i );
     }
