Index: branches/network/src/lib/network/handshake.cc
===================================================================
--- branches/network/src/lib/network/handshake.cc	(revision 7559)
+++ branches/network/src/lib/network/handshake.cc	(revision 7565)
@@ -41,5 +41,20 @@
   error_handler = registerVarId( new SynchronizeableInt( &localState.error, &remoteState.error, "error" ) );
   errorString_handler = registerVarId( new SynchronizeableString( &localState.errorString, &remoteState.errorString, "errorString" ) );
-
+  
+  localState.completed = false;
+  localState.error = 0;
+  localState.errorString = "";
+  localState.hostId = clientId;
+  localState.networkManagerId = networkGameManagerId;
+  localState.orxId = _ORXONOX_ID;
+  localState.version = _ORXONOX_VERSION;
+  
+  remoteState.completed = false;
+  remoteState.error = 0;
+  remoteState.errorString = "";
+  remoteState.hostId = -1;
+  remoteState.networkManagerId = -1;
+  remoteState.orxId = 0;
+  remoteState.version = 0;
 
   this->setSynchronized(true);
@@ -53,4 +68,29 @@
 void Handshake::varChangeHandler( std::list< int > & id )
 {
+  for ( std::list<int>::iterator it = id.begin(); it != id.end(); it++ )
+  {
+    if ( *it == orxId_handler )
+    {
+      if ( remoteState.orxId != _ORXONOX_ID )
+      {
+        localState.error = 1;
+        localState.completed = true;
+        localState.errorString = "Seems not to be orxonox!";
+        return;
+      }
+    }
+     
+    if ( *it == version_handler )
+    {
+      if ( remoteState.version != _ORXONOX_VERSION )
+      {
+        localState.error = 2;
+        localState.completed = true;
+        localState.errorString = "Versions of server and client do not match!";
+        return;
+      }
+    }
+    
+  }
 }
 
Index: branches/network/src/lib/network/handshake.h
===================================================================
--- branches/network/src/lib/network/handshake.h	(revision 7559)
+++ branches/network/src/lib/network/handshake.h	(revision 7565)
@@ -10,5 +10,5 @@
 #include "synchronizeable.h"
 
-#define _INITIAL_DATA 0xF91337A0
+#define _ORXONOX_ID 0xF91337A0
 
 #define _ORXONOX_VERSION 1
Index: branches/network/src/lib/network/network_stream.cc
===================================================================
--- branches/network/src/lib/network/network_stream.cc	(revision 7559)
+++ branches/network/src/lib/network/network_stream.cc	(revision 7565)
@@ -23,10 +23,12 @@
 #include "base_object.h"
 #include "network_protocol.h"
-#include "tcp_socket.h"
-#include "tcp_server_socket.h"
+#include "udp_socket.h"
+#include "udp_server_socket.h"
 #include "connection_monitor.h"
 #include "synchronizeable.h"
 #include "network_game_manager.h"
 #include "shared_network_data.h"
+
+#include "lib/util/loading/factory.h"
 
 #include "debug.h"
@@ -59,21 +61,18 @@
   this->type = NET_CLIENT;
   this->init();
-  this->networkSockets.push_back(new TcpSocket( host, port ));
+  this->peers[0].socket = new UdpSocket( host, port );
   this->networkProtocol = new NetworkProtocol();
   this->connectionMonitor = new ConnectionMonitor();
-  this->maxConnections = 1;
-}
-
-
-NetworkStream::NetworkStream( int port)
+  this->maxConnections = MAX_CONNECTIONS;
+}
+
+
+NetworkStream::NetworkStream( int port )
 {
   this->type = NET_SERVER;
   this->init();
-  this->serverSocket = new TcpServerSocket(port);
+  this->serverSocket = new UdpServerSocket(port);
   this->networkProtocol = new NetworkProtocol();
   this->connectionMonitor = new ConnectionMonitor();
-  this->networkSockets.push_back( NULL );
-  this->networkSockets[0] = NULL; //TODO: remove this
-  this->handshakes.push_back( NULL );
   this->bActive = true;
 }
@@ -88,4 +87,5 @@
   this->networkGameManager = NULL;
   myHostId = 0;
+  currentState = 0;
 }
 
@@ -99,18 +99,24 @@
   }
 
-  for (NetworkSocketVector::iterator i = networkSockets.begin(); i!=networkSockets.end(); i++)
-  {
-    if ( *i )
-    {
-      (*i)->disconnectServer();
-    }
-  }
-
-  for (HandshakeVector::iterator i = handshakes.begin(); i!=handshakes.end(); i++)
-  {
-    if ( *i )
-    {
-      delete (*i);
-    }
+  for ( PeerList::iterator i = peers.begin(); i!=peers.end(); i++)
+  {
+    if ( i->second.socket )
+    {
+      i->second.socket->disconnectServer();
+      delete i->second.socket;
+      i->second.socket = NULL;
+    }
+    
+    if ( i->second.handshake )
+    {
+      delete i->second.handshake;
+      i->second.handshake = NULL;
+    }
+  }
+  
+  if ( serverSocket )
+  {
+    delete serverSocket;
+    serverSocket = NULL;
   }
 
@@ -126,6 +132,5 @@
   // and one for handshake to reject client maxCon+1
   this->networkGameManager->setUniqueID( this->maxConnections + 2 );
-  //this->connectSynchronizeable( *(this->networkGameManager) );
-  this->setMaxConnections( 10 );
+
 }
 
@@ -135,5 +140,6 @@
   Handshake* hs = new Handshake(false);
   hs->setUniqueID( 0 );
-  this->handshakes.push_back(hs);
+  assert( peers[0].handshake == NULL );
+  peers[0].handshake = hs;
   //this->connectSynchronizeable(*hs);
   PRINTF(0)("NetworkStream: %s\n", hs->getName());
@@ -146,6 +152,5 @@
   sync.setNetworkStream( this );
 
-  if( this->networkSockets.size()>0 )
-    this->bActive = true;
+  this->bActive = true;
 }
 
@@ -158,6 +163,5 @@
     this->synchronizeables.erase(disconnectSynchro);
 
-  if( this->networkSockets.size()<=0 )
-    this->bActive = false;
+  this->bActive = false;
 }
 
@@ -165,64 +169,29 @@
 void NetworkStream::processData()
 {
+  currentState++;
+  
   if ( this->type == NET_SERVER )
     this->updateConnectionList();
   else
   {
-    if ( networkSockets[0] && !networkSockets[0]->isOk() )
+    if ( peers[0].socket && !peers[0].socket->isOk() )
     {
       PRINTF(1)("lost connection to server\n");
 
-      //delete networkSockets[i];
-      networkSockets[0]->disconnectServer();
-      networkSockets[0] = NULL;
-
-      if ( handshakes[0] )
-        delete handshakes[0];
-      handshakes[0] = NULL;
-    }
-  }
-
-  for (int i = 0; i<handshakes.size(); i++)
-  {
-    if ( handshakes[i] )
-    {
-      if ( handshakes[i]->completed() )
-      {
-        if ( handshakes[i]->ok() )
-        {
-          if ( type != NET_SERVER )
-          {
-            SharedNetworkData::getInstance()->setHostID( handshakes[i]->getHostId() );
-            myHostId = SharedNetworkData::getInstance()->getHostID();
-
-            this->networkGameManager = NetworkGameManager::getInstance();
-            this->networkGameManager->setUniqueID( handshakes[i]->getNetworkGameManagerId() );
-            //this->connectSynchronizeable( *(this->networkGameManager) );
-          }
-          else
-          {
-
-          }
-          PRINT(0)("handshake finished id=%d\n", handshakes[i]->getNetworkGameManagerId());
-
-
-          delete handshakes[i];
-          handshakes[i] = NULL;
-        }
-        else
-        {
-          PRINT(1)("handshake failed!\n");
-          networkSockets[i]->disconnectServer();
-          delete handshakes[i];
-          handshakes[i] = NULL;
-          //TODO: handle error
-        }
-      }
-    }
-  }
+      peers[0].socket->disconnectServer();
+      delete peers[0].socket;
+      peers[0].socket = NULL;
+
+      if ( peers[0].handshake )
+        delete peers[0].handshake;
+      peers[0].handshake = NULL;
+    }
+  }
+
+
 
 
   /* DOWNSTREAM */
-
+#if 0
 
 
@@ -341,5 +310,5 @@
         {
 #warning fix this
-#if 0
+
           if ( *it && (*it)->getUniqueID()==header.synchronizeableID )
           {
@@ -351,10 +320,12 @@
             continue;
           }
+
+        }
+
+      } while ( dataLength>0 );
+    }
+
+  }
 #endif
-        }
-
-      } while ( dataLength>0 );
-    }
-  }
 }
 
@@ -372,19 +343,25 @@
       clientId = freeSocketSlots.back();
       freeSocketSlots.pop_back();
-      networkSockets[clientId] = tempNetworkSocket;
-      handshakes[clientId] = new Handshake(true, clientId, this->networkGameManager->getUniqueID());
-      handshakes[clientId]->setUniqueID(clientId);
+      peers[clientId].socket = tempNetworkSocket;
+      peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID());
+      peers[clientId].handshake->setUniqueID(clientId);
+      peers[clientId].userId = clientId;
     } else
     {
-      clientId = networkSockets.size();
-      networkSockets.push_back(tempNetworkSocket);
-      Handshake* tHs = new Handshake(true, clientId, this->networkGameManager->getUniqueID());
-      tHs->setUniqueID(clientId);
-      handshakes.push_back(tHs);
+      clientId = 0;
+      
+      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());
+      peers[clientId].handshake->setUniqueID(clientId);
+      peers[clientId].userId = clientId;
     }
 
     if ( clientId > this->maxConnections )
     {
-      handshakes[clientId]->doReject( "too many connections" );
+      peers[clientId].handshake->doReject( "too many connections" );
       PRINTF(0)("Will reject client %d because there are to many connections!\n", clientId);
     }
@@ -398,60 +375,28 @@
 
   //check if connections are ok else remove them
-  for ( int i = 1; i<networkSockets.size(); i++)
-  {
-    if ( networkSockets[i] && !networkSockets[i]->isOk() )
-    {
-      //TODO: tell EntityManager that this player left the game
-      PRINTF(0)("Client is gone: %d\n", i);
-
-      //delete networkSockets[i];
-      networkSockets[i]->disconnectServer();
-      networkSockets[i] = NULL;
-
-      if ( handshakes[i] )
-        delete handshakes[i];
-      handshakes[i] = NULL;
-
-
-      NetworkGameManager::getInstance()->signalLeftPlayer(i);
-
-      if ( i == networkSockets.size()-1 )
-      {
-        networkSockets.pop_back();
-        handshakes.pop_back();
-      }
-      else
-      {
-        freeSocketSlots.push_back(i);
-      }
-    }
-  }
-
-
-}
-
-void NetworkStream::setMaxConnections( int n )
-{
-  if ( !this->isServer() )
-  {
-    PRINTF(1)("Cannot set maxConnections because I am no server.\n");
-  }
-  if ( this->networkSockets.size() > 1 )
-  {
-    PRINTF(1)("Cannot set maxConnections because there are already %d connections.\n", this->networkSockets.size());
-    return;
-  }
-
-  if ( n > MAX_CONNECTIONS )
-  {
-    PRINTF(1)("Cannot set maxConnectiosn to %d because of hardcoded limit %d\n", n, MAX_CONNECTIONS);
-    return;
-  }
-
-  this->maxConnections = n;
-  this->networkGameManager->setUniqueID( n+2 );
-}
-
-
+  for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ )
+  {
+    if ( it->second.socket && !it->second.socket->isOk() )
+    {
+      PRINTF(0)("Client is gone: %d\n", it->second.userId);
+
+      it->second.socket->disconnectServer();
+      delete it->second.socket;
+      it->second.socket = NULL;
+
+      if ( it->second.handshake )
+        delete it->second.handshake;
+      it->second.handshake = NULL;
+
+
+      NetworkGameManager::getInstance()->signalLeftPlayer(it->second.userId);
+
+      freeSocketSlots.push_back( it->second.userId );
+
+    }
+  }
+
+
+}
 
 void NetworkStream::debug()
@@ -485,7 +430,199 @@
 }
 
-
-
-
-
-
+/**
+ * check if handshakes completed
+ */
+void NetworkStream::handleHandshakes( )
+{
+  for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ )
+  {
+    if ( it->second.handshake )
+    {
+      if ( it->second.handshake->completed() )
+      {
+        if ( it->second.handshake->ok() )
+        {
+          if ( type != NET_SERVER )
+          {
+            SharedNetworkData::getInstance()->setHostID( it->second.handshake->getHostId() );
+            myHostId = SharedNetworkData::getInstance()->getHostID();
+
+            this->networkGameManager = NetworkGameManager::getInstance();
+            this->networkGameManager->setUniqueID( it->second.handshake->getNetworkGameManagerId() );
+          }
+
+          PRINT(0)("handshake finished id=%d\n", it->second.handshake->getNetworkGameManagerId());
+
+
+          delete it->second.handshake;
+          it->second.handshake = NULL;
+        }
+        else
+        {
+          PRINT(1)("handshake failed!\n");
+          it->second.socket->disconnectServer();
+        }
+      }
+    }
+  }
+}
+
+/**
+ * handle upstream network traffic
+ */
+void NetworkStream::handleUpstream( )
+{
+  byte buf[UDP_PACKET_SIZE];
+  int offset;
+  int n;
+  
+  for ( PeerList::iterator peer = peers.begin(); peer != peers.end(); peer++ )
+  {
+    offset = INTSIZE; //make already space for length 
+    
+    if ( peer->second.socket )
+      continue;
+    
+    n = Converter::intToByteArray( currentState, buf + offset, UDP_PACKET_SIZE - offset );
+    assert( n == INTSIZE );
+    offset += n;
+    
+    n = Converter::intToByteArray( peer->second.lastAckedState, buf + offset, UDP_PACKET_SIZE - offset );
+    assert( n == INTSIZE );
+    offset += n;
+    
+    n = Converter::intToByteArray( peer->second.lastRecvedState, buf + offset, UDP_PACKET_SIZE - offset );
+    assert( n == INTSIZE );
+    offset += n;
+    
+    for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ )
+    {
+      Synchronizeable & sync = **it;
+      
+      assert( offset + INTSIZE <= UDP_PACKET_SIZE );
+      
+      n = Converter::intToByteArray( sync.getUniqueID(), buf + offset, UDP_PACKET_SIZE - offset );
+      assert( n == INTSIZE );
+      offset += n;
+      
+      offset += sync.getStateDiff( peer->second.userId, buf + offset, UDP_PACKET_SIZE-offset, currentState, peer->second.lastAckedState, 0 );
+    }
+    
+    assert( Converter::intToByteArray( offset, buf, INTSIZE ) == INTSIZE );
+    
+    assert( peer->second.socket->writePacket( buf, offset ) );
+  }
+}
+
+/**
+ * handle downstream network traffic
+ */
+void NetworkStream::handleDownstream( )
+{
+  byte buf[UDP_PACKET_SIZE];
+  int offset = 0;
+  
+  int length = 0;
+  int packetLength = 0;
+  int uniqueId = 0;
+  int state = 0;
+  int ackedState = 0;
+  int fromState = 0;
+  
+  for ( PeerList::iterator peer = peers.begin(); peer != peers.end(); peer++ )
+  {
+    packetLength = peer->second.socket->readPacket( buf, UDP_PACKET_SIZE );
+    
+    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 );
+    
+    //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;
+    }
+    
+    offset = 4*INTSIZE;
+    
+    while ( offset < length )
+    {
+      assert( Converter::byteArrayToInt( buf + offset, &uniqueId ) == INTSIZE );
+      offset += INTSIZE;
+      
+      Synchronizeable * sync = NULL;
+      
+      for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ )
+      {
+        if ( (*it)->getUniqueID() == uniqueId )
+        {
+          sync = *it;
+          break;
+        }
+      }
+      
+      if ( sync == NULL )
+      {
+        //TODO dont accept new object from all peers (probably only servers)
+        int leafClassId;
+        if ( INTSIZE > length - offset )
+          break;
+        
+        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)
+        {
+          PRINTF(1)("Can not create Class with ID %x!", (int)leafClassId);
+          break;
+        }
+        else
+          b = Factory::fabricate( (ClassID)leafClassId );
+
+        if ( !b )
+        {
+          PRINTF(1)("Could not fabricate Object with classID %x\n", leafClassId);
+          break;
+        }
+        
+        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!", (int)leafClassId);
+          delete b;
+          break;
+        }
+      }
+      
+      offset += sync->setStateDiff( peer->second.userId, buf+offset, length-offset, state, fromState );
+    }
+    
+    if ( offset != length )
+    {
+      peer->second.socket->disconnectServer();
+    }
+    
+    peer->second.lastAckedState = ackedState;
+  }
+}
+
+
+
+
+
+
Index: branches/network/src/lib/network/network_stream.h
===================================================================
--- branches/network/src/lib/network/network_stream.h	(revision 7559)
+++ branches/network/src/lib/network/network_stream.h	(revision 7565)
@@ -9,4 +9,5 @@
 #include <vector>
 #include <list>
+#include <map>
 
 #include "data_stream.h"
@@ -22,7 +23,19 @@
 class NetworkGameManager;
 
+class PeerInfo
+{
+  public:
+    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;
+};
+
 typedef std::list<Synchronizeable*>  SynchronizeableList;
-typedef std::vector<NetworkSocket*>  NetworkSocketVector;
-typedef std::vector<Handshake*>      HandshakeVector;
+typedef std::map<int,PeerInfo>       PeerList;
 
 
@@ -48,5 +61,4 @@
 
     inline int getMaxConnections(){ return maxConnections; }
-    void setMaxConnections( int n );
 
     virtual void processData();
@@ -56,11 +68,16 @@
     int getSyncCount();
 
-    inline bool isUserIdActive( int userID ) { if (userID>=networkSockets.size()) return false; else return networkSockets[userID]!=NULL; }
+    inline bool isUserIdActive( int userID ) { return (peers.find(userID) != peers.end()); }
 
     void debug();
+    
+    inline PeerInfo & getPeerInfo( int userId ) { return peers[userId]; }
 
 
   private:
     void updateConnectionList();
+    void handleHandshakes();
+    void handleUpstream();
+    void handleDownstream(); 
 
 
@@ -69,6 +86,5 @@
     ConnectionMonitor*         connectionMonitor;
     SynchronizeableList        synchronizeables;
-    NetworkSocketVector        networkSockets;
-    HandshakeVector            handshakes;
+    PeerList                   peers;
     ServerSocket*              serverSocket;
     int                        type;
@@ -79,4 +95,6 @@
     int                        myHostId;
     int                        maxConnections;
+    
+    int                        currentState;
 
     NetworkGameManager*        networkGameManager;
Index: branches/network/src/lib/network/server_socket.h
===================================================================
--- branches/network/src/lib/network/server_socket.h	(revision 7559)
+++ branches/network/src/lib/network/server_socket.h	(revision 7565)
@@ -27,4 +27,5 @@
     virtual NetworkSocket* getNewSocket( void ) = 0;
     virtual void close() = 0;
+    virtual void update() = 0;
     virtual bool isOk() { return this->bOk; };
 
Index: branches/network/src/lib/network/synchronizeable.cc
===================================================================
--- branches/network/src/lib/network/synchronizeable.cc	(revision 7559)
+++ branches/network/src/lib/network/synchronizeable.cc	(revision 7565)
@@ -41,4 +41,5 @@
   this->networkStream = NULL;
   this->bSynchronize = false;
+  this->mLeafClassId = getLeafClassID();
 
   if( State::isOnline())
@@ -50,4 +51,9 @@
   }
 
+  /* make sure loadClassId is first synced var because this is read by networkStream */
+  assert( syncVarList.size() == 0 );
+  this->registerVar( new SynchronizeableInt( &this->mLeafClassId, &this->mLeafClassId, "leafClassId" ) );
+  
+  this->registerVar( new SynchronizeableInt( &this->owner, &this->owner, "owner" ) );
   this->registerVar( new SynchronizeableString( &this->objectName, &this->objectName, "objectName" ) );
 }
@@ -175,4 +181,16 @@
       i += n;
     }
+    else if ( ! (
+                 this->isServer() && (*it)->checkPremission( PERMISSION_SERVER ) ||
+                 this->owner == this->hostID && (*it)->checkPremission( PERMISSION_OWNER ) ||
+                 (*it)->checkPremission( PERMISSION_ALL ) 
+                ) )
+    {
+      for ( int j = 0; j < (*it)->getSize(); it++ )
+      {
+        stateTo->data[i] = 0;
+        i++;
+      }
+    }
     else
     {
@@ -212,4 +230,5 @@
  * @param fromStateId id of the base state id
  * @return true on success
+ * @todo check for permissions
  */
 bool Synchronizeable::setStateDiff( int userId, byte* data, int length, int stateId, int fromStateId )
@@ -264,4 +283,6 @@
     stateFrom = (*it);
 
+  std::list<int> changes;
+  
   //apply diff
   for ( int i = 0; i<length; i++ )
@@ -271,4 +292,7 @@
     else
       stateTo->data[i] = data[i];
+    
+    if ( data[i] != 0 )
+      changes.push_back(i);
   }
 
@@ -282,4 +306,6 @@
     i += (*it)->readFromBuf( stateTo->data + i, stateTo->dataLength - i );
   }
+  
+  this->varChangeHandler( changes );
 
   assert( i == length -1 );
Index: branches/network/src/lib/network/synchronizeable.h
===================================================================
--- branches/network/src/lib/network/synchronizeable.h	(revision 7559)
+++ branches/network/src/lib/network/synchronizeable.h	(revision 7565)
@@ -41,6 +41,7 @@
 
 enum {
-  PERMISSION_OWNER = 1,
-  PERMISSION_ALL   = 2
+  PERMISSION_SERVER = 1,
+  PERMISSION_OWNER  = 2,
+  PERMISSION_ALL    = 4
 };
 
@@ -89,4 +90,5 @@
   private:
     int               uniqueID;       //!< unique id assigned to synchronizeable
+    int               mLeafClassId;   //!< store leafClassId to send via states
     int               owner;          //!< hostId of owner ( 0 if none / server )
     int               hostID;         //!< my own host id
Index: branches/network/src/lib/network/synchronizeable_var/synchronizeable_quaternion.cc
===================================================================
--- branches/network/src/lib/network/synchronizeable_var/synchronizeable_quaternion.cc	(revision 7559)
+++ branches/network/src/lib/network/synchronizeable_var/synchronizeable_quaternion.cc	(revision 7565)
@@ -87,9 +87,9 @@
   n += res;
 
-  res += Converter::byteArrayToFloat( buf + n, &x );
+  res += Converter::byteArrayToFloat( buf + n, &y );
   assert( res > 0 );
   n += res;
 
-  res += Converter::byteArrayToFloat( buf + n, &x );
+  res += Converter::byteArrayToFloat( buf + n, &z );
   assert( res > 0 );
   n += res;
Index: branches/network/src/lib/network/synchronizeable_var/synchronizeable_vector.cc
===================================================================
--- branches/network/src/lib/network/synchronizeable_var/synchronizeable_vector.cc	(revision 7559)
+++ branches/network/src/lib/network/synchronizeable_var/synchronizeable_vector.cc	(revision 7565)
@@ -84,9 +84,9 @@
   n += res;
 
-  res += Converter::byteArrayToFloat( buf + n, &x );
+  res += Converter::byteArrayToFloat( buf + n, &y );
   assert( res > 0 );
   n += res;
 
-  res += Converter::byteArrayToFloat( buf + n, &x );
+  res += Converter::byteArrayToFloat( buf + n, &z );
   assert( res > 0 );
   n += res;
Index: branches/network/src/lib/network/tcp_server_socket.h
===================================================================
--- branches/network/src/lib/network/tcp_server_socket.h	(revision 7559)
+++ branches/network/src/lib/network/tcp_server_socket.h	(revision 7565)
@@ -27,4 +27,5 @@
     virtual NetworkSocket* getNewSocket( void );
     virtual void close();
+    virtual void update() {};
     
   private:
Index: branches/network/src/lib/network/udp_server_socket.cc
===================================================================
--- branches/network/src/lib/network/udp_server_socket.cc	(revision 7559)
+++ branches/network/src/lib/network/udp_server_socket.cc	(revision 7565)
@@ -72,9 +72,163 @@
 
 /**
- * get newly connected socket. note: this function will also recieve
- * packets and create new sockets
+ * get newly connected socket. note
  * @return new socket or NULL if no new socket exists
  */
 NetworkSocket * UdpServerSocket::getNewSocket( void )
+{
+  NetworkSocket * result = NULL;
+  
+  if ( newSocketList.size() > 0 )
+  {
+    result = newSocketList.front();
+  
+    newSocketList.pop_front();
+  }
+  
+  return result;
+}
+
+/**
+ * stop listening on server
+ */
+void UdpServerSocket::close( )
+{
+  
+  for ( int i = 0; i < packetBuffer.size(); i++ )
+    removeUserPackets( i );
+  
+  packetBuffer.clear();
+  userList.clear();
+  
+  SDLNet_UDP_Close( socket );
+  socket = NULL;
+}
+
+/**
+ * clean up users buffer
+ * @param userId users userid
+ */
+void UdpServerSocket::removeUserPackets( int userId )
+{
+  if ( userId >= packetBuffer.size() )
+    return;
+  
+  for ( NetworkPacketList::iterator it = packetBuffer[userId].begin(); it!=packetBuffer[userId].end(); it++ )
+  {
+    if ( it->data )
+    {
+      free( it->data );
+      it->data = NULL;
+    }
+  }
+  
+  packetBuffer[userId].clear();
+}
+
+/**
+ * get next packet for user
+ * @param userId user id
+ * @return recieved packet or packet with length 0 if no packet available
+ */
+NetworkPacket UdpServerSocket::getPacket( int userId )
+{
+  NetworkPacket res;
+  res.data = NULL;
+  res.length = 0;
+  
+  if ( packetBuffer.size() > userId && packetBuffer[userId].size() > 0 )
+  {
+    res.data = packetBuffer[userId].front().data;
+    res.length = packetBuffer[userId].front().length;
+    packetBuffer[userId].pop_front();
+  }
+  
+  return res;
+}
+
+/**
+ * get number of packets recieved for user
+ * @param userId user id
+ * @return number of packets in buffer
+ */
+int UdpServerSocket::getPacketCount( int userId )
+{
+  if ( userId >= packetBuffer.size() )
+    return -1;
+  
+  return packetBuffer[userId].size();
+}
+
+/**
+ * will set user state 
+ * @param userId users id
+ * @param ip users host / port
+ */
+void UdpServerSocket::initUser( int userId, IPaddress ip )
+{
+  int channel = SDLNet_UDP_Bind( socket, userId, &ip );
+  if( channel != userId ) 
+  {
+    PRINTF(1)("SDLNet_UDP_Bind: %s\n", SDLNet_GetError());
+    assert(false);
+    return;
+  }
+  
+  if ( userId < packetBuffer.size() )
+    removeUserPackets( userId );
+  
+  if ( packetBuffer.size() <= userId )
+    packetBuffer.resize( userId + 1 );
+  
+  if ( userList.size() <= userId )
+    userList.resize( userId + 1 );
+  
+  userList[ userId ] = ip;
+}
+
+/**
+ * remove user from list
+ * @param userId user id
+ */
+void UdpServerSocket::removeUser( int userId )
+{
+  removeUserPackets( userId );
+  
+  if ( userId >= userList.size() )
+    return;
+  
+  userList[userId].host = 0;
+  userList[userId].port = 0;
+  
+  SDLNet_UDP_Unbind( socket, userId );
+}
+
+/**
+ * send one packet to client associated to userId
+ * @param networkPacket packet to send
+ * @param userId users id
+ * @return true on success
+ */
+bool UdpServerSocket::sendPacket( NetworkPacket networkPacket , int userId )
+{
+  assert( networkPacket.length <= UDP_PACKET_SIZE );
+  
+  memcpy( packet->data, networkPacket.data, networkPacket.length );
+  packet->len = networkPacket.length;
+  packet->channel = -1;
+  
+  if ( SDLNet_UDP_Send( socket, userId, packet ) == 0 )
+  {
+    PRINTF(1)("SDLNet_UDP_Send: %s\n", SDLNet_GetError());
+    return false;
+  }
+  
+  return true;
+}
+
+/**
+ * do periodically things
+ */
+void UdpServerSocket::update( )
 {
   int res;
@@ -84,4 +238,5 @@
   {
     int userId;
+    bool isNewConnection = false;
     
     for ( userId =0; userId < userList.size(); userId++ )
@@ -92,4 +247,5 @@
     {
       newConn++;
+      isNewConnection = true;
       
       if ( newConn > MAX_NEW_CONNECTIONS )
@@ -118,5 +274,10 @@
     }
     else
+    {
+      if ( isNewConnection )
+        continue;
+      
       networkPacket.data = NULL;
+    }
     memcpy( networkPacket.data, packet->data, packet->len );
     packetBuffer[userId].push_back( networkPacket );
@@ -124,152 +285,3 @@
   
   assert( res == 0 );
-  
-  NetworkSocket * result = NULL;
-  
-  if ( newSocketList.size() > 0 )
-  {
-    result = newSocketList.front();
-  
-    newSocketList.pop_front();
-  }
-  
-  return result;
-}
-
-/**
- * stop listening on server
- */
-void UdpServerSocket::close( )
-{
-  
-  for ( int i = 0; i < packetBuffer.size(); i++ )
-    removeUserPackets( i );
-  
-  packetBuffer.clear();
-  userList.clear();
-  
-  SDLNet_UDP_Close( socket );
-  socket = NULL;
-}
-
-/**
- * clean up users buffer
- * @param userId users userid
- */
-void UdpServerSocket::removeUserPackets( int userId )
-{
-  if ( userId >= packetBuffer.size() )
-    return;
-  
-  for ( NetworkPacketList::iterator it = packetBuffer[userId].begin(); it!=packetBuffer[userId].end(); it++ )
-  {
-    if ( it->data )
-    {
-      free( it->data );
-      it->data = NULL;
-    }
-  }
-  
-  packetBuffer[userId].clear();
-}
-
-/**
- * get next packet for user
- * @param userId user id
- * @return recieved packet or packet with length 0 if no packet available
- */
-NetworkPacket UdpServerSocket::getPacket( int userId )
-{
-  NetworkPacket res;
-  res.data = NULL;
-  res.length = 0;
-  
-  if ( packetBuffer.size() > userId && packetBuffer[userId].size() > 0 )
-  {
-    res.data = packetBuffer[userId].front().data;
-    res.length = packetBuffer[userId].front().length;
-    packetBuffer[userId].pop_front();
-  }
-  
-  return res;
-}
-
-/**
- * get number of packets recieved for user
- * @param userId user id
- * @return number of packets in buffer
- */
-int UdpServerSocket::getPacketCount( int userId )
-{
-  if ( userId >= packetBuffer.size() )
-    return -1;
-  
-  return packetBuffer[userId].size();
-}
-
-/**
- * will set user state 
- * @param userId users id
- * @param ip users host / port
- */
-void UdpServerSocket::initUser( int userId, IPaddress ip )
-{
-  int channel = SDLNet_UDP_Bind( socket, userId, &ip );
-  if( channel != userId ) 
-  {
-    PRINTF(1)("SDLNet_UDP_Bind: %s\n", SDLNet_GetError());
-    assert(false);
-    return;
-  }
-  
-  if ( userId < packetBuffer.size() )
-    removeUserPackets( userId );
-  
-  if ( packetBuffer.size() <= userId )
-    packetBuffer.resize( userId + 1 );
-  
-  if ( userList.size() <= userId )
-    userList.resize( userId + 1 );
-  
-  userList[ userId ] = ip;
-}
-
-/**
- * remove user from list
- * @param userId user id
- */
-void UdpServerSocket::removeUser( int userId )
-{
-  removeUserPackets( userId );
-  
-  if ( userId >= userList.size() )
-    return;
-  
-  userList[userId].host = 0;
-  userList[userId].port = 0;
-  
-  SDLNet_UDP_Unbind( socket, userId );
-}
-
-/**
- * send one packet to client associated to userId
- * @param networkPacket packet to send
- * @param userId users id
- * @return true on success
- */
-bool UdpServerSocket::sendPacket( NetworkPacket networkPacket , int userId )
-{
-  assert( networkPacket.length <= UDP_PACKET_SIZE );
-  
-  memcpy( packet->data, networkPacket.data, networkPacket.length );
-  packet->len = networkPacket.length;
-  packet->channel = -1;
-  
-  if ( SDLNet_UDP_Send( socket, userId, packet ) == 0 )
-  {
-    PRINTF(1)("SDLNet_UDP_Send: %s\n", SDLNet_GetError());
-    return false;
-  }
-  
-  return true;
-}
+}
Index: branches/network/src/lib/network/udp_server_socket.h
===================================================================
--- branches/network/src/lib/network/udp_server_socket.h	(revision 7559)
+++ branches/network/src/lib/network/udp_server_socket.h	(revision 7565)
@@ -50,4 +50,6 @@
     virtual void close();
     
+    virtual void update();
+    
     void removeUserPackets( int userId );
     void removeUser( int userId );
Index: branches/network/src/lib/network/udp_socket.cc
===================================================================
--- branches/network/src/lib/network/udp_socket.cc	(revision 7559)
+++ branches/network/src/lib/network/udp_socket.cc	(revision 7565)
@@ -121,4 +121,6 @@
     return;
   }
+  
+  writePacket( NULL, 0 );
 }
 
@@ -130,4 +132,5 @@
   SDLNet_UDP_Unbind( socket, -1 );
   SDLNet_UDP_Close( socket );
+  bOk = false;
   socket = NULL;
 }
@@ -180,4 +183,6 @@
 int UdpSocket::readPacket( byte * data, int maxLength )
 {
+  assert( maxLength <= UDP_PACKET_SIZE );
+  
   if ( serverSocket )
   {
Index: branches/network/src/subprojects/network/network_unit_test.cc
===================================================================
--- branches/network/src/subprojects/network/network_unit_test.cc	(revision 7559)
+++ branches/network/src/subprojects/network/network_unit_test.cc	(revision 7565)
@@ -42,8 +42,10 @@
   NetworkSocket* client1 = new UdpSocket("localhost", 9999);
 
-  client1->writePacket( (byte*)"test", 5 );
   NetworkSocket* server1 = NULL;
   while ( server1 == NULL )
+  {
+    server.update();
     server1 = server.getNewSocket();
+  }
   
   assert( server1->isOk() );
@@ -51,8 +53,10 @@
   NetworkSocket* client2 = new UdpSocket("localhost", 9999);
 
-  client2->writePacket( (byte*)"test", 5 );
   NetworkSocket* server2 = NULL;
   while ( server2 == NULL )
+  {
+    server.update();
     server2 = server.getNewSocket();
+  }
 
   char buf[1024];
@@ -77,5 +81,53 @@
   n = server2->writePacket((byte*)str4, strlen(str4)+1);
   printf("%d bytes send from server2\n", n);
-  SDL_Delay(1000);
+  SDL_Delay(10);
+  
+  server.update();
+
+  printf("read from server1\n");
+  n = server1->readPacket((byte*)buf, 1024);
+  printf("read %d bytes\n", n);
+  if (n<0)
+    return -1;
+
+  printf("data: '%s'\n", buf);
+
+  printf("read from server2\n");
+  n = server2->readPacket((byte*)buf, 1024);
+  printf("read %d bytes\n", n);
+  if (n<0)
+    return -1;
+
+  printf("data: '%s'\n", buf);
+
+  printf("read from client1\n");
+  n = client1->readPacket((byte*)buf, 1024);
+  printf("read %d bytes\n", n);
+  if (n<0)
+    return -1;
+
+  printf("data: '%s'\n", buf);
+
+  printf("read from client2\n");
+  n = client2->readPacket((byte*)buf, 1024);
+  printf("read %d bytes\n", n);
+  if (n<0)
+    return -1;
+
+  printf("data: '%s'\n", buf);
+  
+  
+  //22222222222222222222222222222222222222222
+  n = client1->writePacket((byte*)str1, strlen(str1)+1);
+  printf("%d bytes send from client1\n", n);
+  n = server1->writePacket((byte*)str2, strlen(str2)+1);
+  printf("%d bytes send from server1\n", n);
+  n = client2->writePacket((byte*)str3, strlen(str3)+1);
+  printf("%d bytes send from client2\n", n);
+  n = server2->writePacket((byte*)str4, strlen(str4)+1);
+  printf("%d bytes send from server2\n", n);
+  SDL_Delay(10);
+  
+  server.update();
 
   printf("read from server1\n");
