Index: branches/network/src/lib/network/udp_socket.cc
===================================================================
--- branches/network/src/lib/network/udp_socket.cc	(revision 8710)
+++ branches/network/src/lib/network/udp_socket.cc	(revision 8723)
@@ -25,4 +25,5 @@
   this->socket = NULL;
   this->packet = NULL;
+  this->randomByte = 0;
 }
 
@@ -70,9 +71,10 @@
  * @param userId userid used by serverSocket
  */
-UdpSocket::UdpSocket( UdpServerSocket * serverSocket, IPaddress ip, int userId )
+UdpSocket::UdpSocket( UdpServerSocket * serverSocket, IPaddress ip, int userId, byte randomByte )
 {
   this->init();
   this->serverSocket = serverSocket;
   this->userId = userId;
+  this->randomByte = randomByte;
 }
 
@@ -104,4 +106,6 @@
 
   IPaddress ip;
+  
+  this->randomByte = generateNewRandomByte();
 
   PRINTF(0)("connect to server %s on port %d\n", host.c_str(), port);
@@ -137,5 +141,6 @@
 {
   PRINTF(0)("disconnect\n");
-  writePacket( NULL, 0 );
+  byte cmd = this->randomByte | UDPCMD_DISCONNECT;
+  writeRawPacket( &cmd, 1 );
   SDLNet_UDP_Unbind( socket, -1 );
   SDLNet_UDP_Close( socket );
@@ -152,4 +157,91 @@
 bool UdpSocket::writePacket( byte * data, int length )
 {
+  byte * buf = new byte[length+1];
+  if ( length > 0 )
+    memcpy( buf+1, data, length );
+  buf[0] = this->randomByte;
+  return writeRawPacket( buf, length+1 );
+}
+
+/**
+ * recieve one packet from another host
+ * @param data pointer to buffer to copy data into
+ * @param maxLength maximal length of buffer
+ * @return less than 0 on error, number bytes read else
+ */
+int UdpSocket::readPacket( byte * data, int maxLength )
+{
+  assert( maxLength <= UDP_PACKET_SIZE );
+
+  if ( serverSocket )
+  {
+    NetworkPacket networkPacket = serverSocket->getPacket( this->userId );
+
+    byte udpCmd = 0;
+    
+    if ( networkPacket.length > 0 )
+    {
+      assert( maxLength > networkPacket.length );
+
+      memcpy( data, networkPacket.data+1, networkPacket.length-1 );
+      udpCmd = networkPacket.data[0];
+    }
+    else
+      return 0;
+      
+    if ( !checkRandomByte( networkPacket.data[0] ) )
+      return 0;
+
+    if ( networkPacket.data )
+    {
+      free( networkPacket.data );
+      networkPacket.data = NULL;
+    }
+    
+    if ( !checkUdpCmd( udpCmd ) )
+      return 0;
+
+    return networkPacket.length-1;
+  }
+  else
+  {
+    int numrecv = SDLNet_UDP_Recv( socket, packet);
+    
+    byte udpCmd = 0;
+
+    if ( numrecv > 0)
+    {
+      assert( packet->len <= maxLength );
+
+      if ( packet->len > 0 )
+        memcpy( data, packet->data+1, packet->len-1 );
+      else
+        return 0;
+            
+      if ( !checkRandomByte( packet->data[0] ) )
+        return 0;
+              
+      if ( !checkUdpCmd( udpCmd ) )
+        return 0;
+
+      return packet->len-1;
+    }
+    else if ( numrecv < 0 )
+    {
+      PRINTF(1)("SDLNet_UDP_Recv: %s\n", SDLNet_GetError());
+      bOk = false;
+      return -1;
+    }
+    else
+    {
+      return 0;
+    }
+  }
+
+  return 0;
+}
+
+bool UdpSocket::writeRawPacket( byte * data, int length )
+{
   if ( serverSocket )
   {
@@ -183,70 +275,48 @@
 }
 
-/**
- * recieve one packet from another host
- * @param data pointer to buffer to copy data into
- * @param maxLength maximal length of buffer
- * @return less than 0 on error, number bytes read else
- */
-int UdpSocket::readPacket( byte * data, int maxLength )
-{
-  assert( maxLength <= UDP_PACKET_SIZE );
-
-  if ( serverSocket )
-  {
-    NetworkPacket networkPacket = serverSocket->getPacket( this->userId );
-
-    if ( networkPacket.length == -1 )
-    {
-      this->disconnectServer();
-      return 0;
-    }
-
-    if ( networkPacket.length > 0 )
-    {
-      assert( maxLength > networkPacket.length );
-
-      memcpy( data, networkPacket.data, networkPacket.length );
-    }
-
-    if ( networkPacket.data )
-    {
-      free( networkPacket.data );
-      networkPacket.data = NULL;
-    }
-
-    return networkPacket.length;
+bool UdpSocket::checkUdpCmd( byte udpCmd )
+{
+  if ( udpCmd & UDPCMD_DISCONNECT )
+  {
+    this->disconnectServer();
+    PRINTF(0)("received disconnect byte\n");
+    return false;
+  }
+  
+  if ( !this->serverSocket && ( udpCmd & UDPCMD_INVALIDRNDBYTE ) )
+  {
+    PRINTF(0)("received invlid random number byte\n");
+    byte cmd = this->randomByte | UDPCMD_DISCONNECT;
+    writeRawPacket( &cmd, 1 );
+    this->randomByte = generateNewRandomByte();
+    return false;
+  }
+  
+  return true;
+}
+
+byte UdpSocket::generateNewRandomByte( )
+{
+  srand( SDL_GetTicks() );
+  byte res = ( rand() & 0xFC );
+  
+  PRINTF(0)("generated random byte: %x\n", res);
+  
+  return res;
+}
+
+bool UdpSocket::checkRandomByte( byte rndByte )
+{
+  if ( ( rndByte & 0xFC ) == this->randomByte )
+  {
+    return true;
   }
   else
   {
-    int numrecv = SDLNet_UDP_Recv( socket, packet);
-    if ( numrecv > 0)
-    {
-      assert( packet->len <= maxLength );
-
-      if ( packet->len == 0 )
-      {
-        this->disconnectServer();
-        return 0;
-      }
-
-      memcpy( data, packet->data, packet->len );
-      return packet->len;
-    }
-    else if ( numrecv < 0 )
-    {
-      PRINTF(1)("SDLNet_UDP_Recv: %s\n", SDLNet_GetError());
-      bOk = false;
-      return -1;
-    }
-    else
-    {
-      return 0;
-    }
-  }
-
-  return 0;
-}
-
-
-
+    PRINTF(2)("wrong random byte: %x\n", ( rndByte & 0xFC ));
+    return false;
+  }
+}
+
+
+
