Index: branches/network/src/lib/network/network_game_manager.cc
===================================================================
--- branches/network/src/lib/network/network_game_manager.cc	(revision 6230)
+++ branches/network/src/lib/network/network_game_manager.cc	(revision 6231)
@@ -229,21 +229,8 @@
 void NetworkGameManager::requestCreateEntity(ClassID classID)
 {
-  byte* bp;
-  byte b = REQUEST_CREATE;
-  bp = Converter::intToByteArray( classID );
-
-  if ( !writeToClientBuffer( allOutBuffer, &b, 1 ) )
-  {
-    PRINTF(1)("Could not write to clientBuffer\n");
-    return;
-  }
-
-  if ( !writeToClientBuffer( allOutBuffer, bp, INTSIZE ) )
-  {
-    PRINTF(1)("Could not write to clientBuffer\n");
-    delete bp;
-    return;
-  }
-  delete bp;
+  if ( !writeToClientBuffer( allOutBuffer, (byte)REQUEST_CREATE ) )
+    return;
+  if ( !writeToClientBuffer( allOutBuffer, (int)classID ) )
+    return;
 }
 
@@ -254,21 +241,8 @@
 void NetworkGameManager::requestRemoveEntity(int uniqueID)
 {
-  byte* bp;
-  byte b = REQUEST_REMOVE;
-  bp = Converter::intToByteArray( uniqueID );
-
-  if ( !writeToClientBuffer( allOutBuffer, &b, 1 ) )
-  {
-    PRINTF(1)("Could not write to clientBuffer\n");
-    return;
-  }
-
-  if ( !writeToClientBuffer( allOutBuffer, bp, INTSIZE ) )
-  {
-    PRINTF(1)("Could not write to clientBuffer\n");
-    delete bp;
-    return;
-  }
-  delete bp;
+  if ( !writeToClientBuffer( allOutBuffer, (byte)REQUEST_REMOVE ) )
+    return;
+  if ( !writeToClientBuffer( allOutBuffer, uniqueID ) )
+    return;
 }
 
@@ -280,39 +254,12 @@
 void NetworkGameManager::executeCreateEntity(ClassID classID, int uniqueID, int owner)
 {
-  byte* bp;
-  byte b = CREATE_ENTITY;
-
-  if ( !writeToClientBuffer( allOutBuffer, &b, 1 ) )
-  {
-    PRINTF(1)("Could not write to clientBuffer\n");
-    return;
-  }
-
-  bp = Converter::intToByteArray( uniqueID );
-  if ( !writeToClientBuffer( allOutBuffer, bp, INTSIZE ) )
-  {
-    PRINTF(1)("Could not write to clientBuffer\n");
-    delete bp;
-    return;
-  }
-  delete bp;
-
-  bp = Converter::intToByteArray( uniqueID );
-  if ( !writeToClientBuffer( allOutBuffer, bp, INTSIZE ) )
-  {
-    PRINTF(1)("Could not write to clientBuffer\n");
-    delete bp;
-    return;
-  }
-  delete bp;
-
-  bp = Converter::intToByteArray( owner );
-  if ( !writeToClientBuffer( allOutBuffer, bp, INTSIZE ) )
-  {
-    PRINTF(1)("Could not write to clientBuffer\n");
-    delete bp;
-    return;
-  }
-  delete bp;
+  if ( !writeToClientBuffer( allOutBuffer, (byte)CREATE_ENTITY ) )
+    return;
+  if ( !writeToClientBuffer( allOutBuffer, (int)classID ) )
+    return;
+  if ( !writeToClientBuffer( allOutBuffer, uniqueID ) )
+    return;
+  if ( !writeToClientBuffer( allOutBuffer, owner ) )
+    return;
 
   doCreateEntity( classID, uniqueID, owner );
@@ -326,21 +273,8 @@
 void NetworkGameManager::executeRemoveEntity(int uniqueID)
 {
-  byte* bp;
-  byte b = REMOVE_ENTITY;
-
-  if ( !writeToClientBuffer( allOutBuffer, &b, 1 ) )
-  {
-    PRINTF(1)("Could not write to clientBuffer\n");
-    return;
-  }
-
-  bp = Converter::intToByteArray( uniqueID );
-  if ( !writeToClientBuffer( allOutBuffer, bp, INTSIZE ) )
-  {
-    PRINTF(1)("Could not write to clientBuffer\n");
-    delete bp;
-    return;
-  }
-  delete bp;
+  if ( !writeToClientBuffer( allOutBuffer, (byte)REMOVE_ENTITY ) )
+    return;
+  if ( !writeToClientBuffer( allOutBuffer, uniqueID ) )
+    return;
 
   doRemoveEntity(uniqueID);
@@ -373,50 +307,20 @@
   e = this->networkStream->getSyncEnd();
 
-  byte b = CREATE_ENTITY_LIST;
-  byte* ib;
-
-  if ( !writeToClientBuffer( outBuffer[userID], &b, 1 ) )
-  {
-    PRINTF(1)("Could not write to clientBuffer\n");
-    return;
-  }
-
-  ib = Converter::intToByteArray( networkStream->getSyncCount() );
-  if ( !writeToClientBuffer( outBuffer[userID], ib, INTSIZE ) )
-  {
-    PRINTF(1)("Could not write to clientBuffer\n");
-    delete ib;
-    return;
-  }
-  delete ib;
+  if ( !writeToClientBuffer( outBuffer[userID], (byte)CREATE_ENTITY_LIST ) )
+    return;
+
+  if ( !writeToClientBuffer( outBuffer[userID], networkStream->getSyncCount() ) )
+    return;
 
   while ( it != e )
   {
-    ib = Converter::intToByteArray( (*it)->getClassID() );
-    if ( !writeToClientBuffer( outBuffer[userID], ib, INTSIZE ) )
-    {
-      PRINTF(1)("Could not write to clientBuffer\n");
-      delete ib;
+    if ( !writeToClientBuffer( outBuffer[userID], (*it)->getClassID() ) )
       return;
-    }
-    delete ib;
-
-    ib = Converter::intToByteArray( (*it)->getUniqueID() );
-    if ( !writeToClientBuffer( outBuffer[userID], ib, INTSIZE ) )
-    {
-      PRINTF(1)("Could not write to clientBuffer\n");
-      delete ib;
+
+    if ( !writeToClientBuffer( outBuffer[userID], (*it)->getUniqueID() ) )
       return;
-    }
-    delete ib;
-
-    ib = Converter::intToByteArray( (*it)->getOwner() );
-    if ( !writeToClientBuffer( outBuffer[userID], ib, INTSIZE ) )
-    {
-      PRINTF(1)("Could not write to clientBuffer\n");
-      delete ib;
+
+    if ( !writeToClientBuffer( outBuffer[userID], (*it)->getOwner() ) )
       return;
-    }
-    delete ib;
 
     it++;
@@ -574,23 +478,11 @@
     return;
 
-  byte* bp;
-  byte b = YOU_ARE_ENTITY;
-
   if ( userID != 0 )
   {
-    if ( !writeToClientBuffer( allOutBuffer, &b, 1 ) )
-    {
-      PRINTF(1)("Could not write to clientBuffer\n");
+    if ( !writeToClientBuffer( outBuffer[userID], (byte)YOU_ARE_ENTITY ) )
       return;
-    }
-
-    bp = Converter::intToByteArray( uniqueID );
-    if ( !writeToClientBuffer( allOutBuffer, bp, INTSIZE ) )
-    {
-      PRINTF(1)("Could not write to clientBuffer\n");
-      delete bp;
+
+    if ( !writeToClientBuffer( outBuffer[userID], uniqueID ) )
       return;
-    }
-    delete bp;
   }
   else
@@ -607,6 +499,6 @@
     return false;
   }
-  int classID = Converter::byteArrayToInt( data );
-  i += INTSIZE;
+  int classID;
+  i += Converter::byteArrayToInt( data, &classID );
 
   createEntity( (ClassID)classID );
@@ -622,6 +514,6 @@
     return false;
   }
-  int uniqueID = Converter::byteArrayToInt( data );
-  i += INTSIZE;
+  int uniqueID;
+  i += Converter::byteArrayToInt( data, &uniqueID );
 
   removeEntity( uniqueID );
@@ -637,6 +529,6 @@
     return false;
   }
-  int classID = Converter::byteArrayToInt( data );
-  i += INTSIZE;
+  int classID;
+  i += Converter::byteArrayToInt( data, &classID );
 
   if ( INTSIZE > length-i )
@@ -645,6 +537,6 @@
     return false;
   }
-  int uniqueID = Converter::byteArrayToInt( data );
-  i += INTSIZE;
+  int uniqueID;
+  i += Converter::byteArrayToInt( data, &uniqueID );
 
   if ( INTSIZE > length-i )
@@ -653,6 +545,6 @@
     return false;
   }
-  int owner = Converter::byteArrayToInt( data );
-  i += INTSIZE;
+  int owner;
+  i += Converter::byteArrayToInt( data, &owner );
 
   doCreateEntity( (ClassID)classID, uniqueID, owner );
@@ -668,6 +560,6 @@
     return false;
   }
-  int uniqueID = Converter::byteArrayToInt( data );
-  i += INTSIZE;
+  int uniqueID;
+  i += Converter::byteArrayToInt( data, &uniqueID );
 
   doRemoveEntity( uniqueID );
@@ -683,6 +575,6 @@
     return false;
   }
-  int n = Converter::byteArrayToInt( data );
-  i += INTSIZE;
+  int n;
+  i += Converter::byteArrayToInt( data, &n );
 
   int classID, uniqueID, owner;
@@ -696,6 +588,5 @@
       return false;
     }
-    classID = Converter::byteArrayToInt( data );
-    i += INTSIZE;
+    i += Converter::byteArrayToInt( data, &classID );
 
     if ( INTSIZE > length-i )
@@ -704,6 +595,5 @@
       return false;
     }
-    uniqueID = Converter::byteArrayToInt( data );
-    i += INTSIZE;
+    i += Converter::byteArrayToInt( data, &uniqueID );
 
     if ( INTSIZE > length-i )
@@ -712,6 +602,5 @@
       return false;
     }
-    owner = Converter::byteArrayToInt( data );
-    i += INTSIZE;
+    i += Converter::byteArrayToInt( data, &owner );
 
     doCreateEntity( (ClassID)classID, uniqueID, owner );
@@ -728,6 +617,6 @@
     return false;
   }
-  int n = Converter::byteArrayToInt( data );
-  i += INTSIZE;
+  int n;
+  i += Converter::byteArrayToInt( data, &n );
 
   int uniqueID;
@@ -741,6 +630,5 @@
       return false;
     }
-    uniqueID = Converter::byteArrayToInt( data );
-    i += INTSIZE;
+    i += Converter::byteArrayToInt( data, &uniqueID );
 
     doRemoveEntity( uniqueID );
@@ -757,6 +645,7 @@
     return false;
   }
-  int uniqueID = Converter::byteArrayToInt( data );
-  i += INTSIZE;
+
+  int uniqueID;
+  i += Converter::byteArrayToInt( data, &uniqueID );
 
   doYouAre( uniqueID );
@@ -772,6 +661,6 @@
     return false;
   }
-  int uniqueID = Converter::byteArrayToInt( data );
-  i += INTSIZE;
+  int uniqueID;
+  i += Converter::byteArrayToInt( data, &uniqueID );
 
   doRequestSync( uniqueID, sender );
@@ -780,2 +669,29 @@
 }
 
+bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, byte b )
+{
+  if ( cb.maxLength-cb.length < 1 )
+  {
+    PRINTF(1)("Cannot write to clientBuffer! Not enough space for 1 byte\n");
+    return false;
+  }
+
+  cb.buffer[cb.length++] = b;
+
+  return true;
+}
+
+bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, int i )
+{
+  int n = Converter::intToByteArray( i, cb.buffer, cb.maxLength-cb.length );
+  cb.length += n;
+
+  if ( n <= 0 )
+  {
+    PRINTF(1)("Cannot write to clientBuffer! Not enough space for 1 int\n");
+    return false;
+  }
+
+  return true;
+}
+
