@@ -269,7 +269,11 @@ private void Update()
269269 }
270270 else
271271 {
272- using ( MemoryStream writeStream = new MemoryStream ( ) )
272+ int sizeOfStream = 32 ;
273+ if ( NetworkConfig . ConnectionApproval )
274+ sizeOfStream += 2 + NetworkConfig . ConnectionData . Length ;
275+
276+ using ( MemoryStream writeStream = new MemoryStream ( sizeOfStream ) )
273277 {
274278 using ( BinaryWriter writer = new BinaryWriter ( writeStream ) )
275279 {
@@ -280,7 +284,7 @@ private void Update()
280284 writer . Write ( NetworkConfig . ConnectionData ) ;
281285 }
282286 }
283- Send ( clientId , "MLAPI_CONNECTION_REQUEST" , "MLAPI_RELIABLE_FRAGMENTED" , writeStream . ToArray ( ) ) ;
287+ Send ( clientId , "MLAPI_CONNECTION_REQUEST" , "MLAPI_RELIABLE_FRAGMENTED" , writeStream . GetBuffer ( ) ) ;
284288 }
285289 }
286290 break ;
@@ -342,7 +346,6 @@ private void HandleIncomingData(int connectonId, byte[] data)
342346 if ( targeted )
343347 {
344348 List < int > handlerIds = MessageManager . targetedMessages [ messageType ] [ targetNetworkId ] ;
345- Debug . Log ( handlerIds . Count ) ;
346349 for ( int i = 0 ; i < handlerIds . Count ; i ++ )
347350 {
348351 MessageManager . messageCallbacks [ messageType ] [ handlerIds [ i ] ] ( clientId , incommingData ) ;
@@ -720,24 +723,26 @@ private void OnClientDisconnect(int clientId)
720723 {
721724 if ( NetworkConfig . HandleObjectSpawning )
722725 {
723- Destroy ( connectedClients [ clientId ] . PlayerObject ) ;
726+ if ( connectedClients [ clientId ] . PlayerObject != null )
727+ Destroy ( connectedClients [ clientId ] . PlayerObject ) ;
724728 for ( int i = 0 ; i < connectedClients [ clientId ] . OwnedObjects . Count ; i ++ )
725729 {
726- Destroy ( connectedClients [ clientId ] . OwnedObjects [ i ] . gameObject ) ;
730+ if ( connectedClients [ clientId ] . OwnedObjects [ i ] != null )
731+ Destroy ( connectedClients [ clientId ] . OwnedObjects [ i ] . gameObject ) ;
727732 }
728733 }
729734 connectedClients . Remove ( clientId ) ;
730735 }
731736
732737 if ( isServer )
733738 {
734- using ( MemoryStream stream = new MemoryStream ( ) )
739+ using ( MemoryStream stream = new MemoryStream ( 4 ) )
735740 {
736741 using ( BinaryWriter writer = new BinaryWriter ( stream ) )
737742 {
738743 writer . Write ( clientId ) ;
739744 }
740- Send ( "MLAPI_CLIENT_DISCONNECT" , "MLAPI_RELIABLE_FRAGMENTED" , stream . ToArray ( ) , clientId ) ;
745+ Send ( "MLAPI_CLIENT_DISCONNECT" , "MLAPI_RELIABLE_FRAGMENTED" , stream . GetBuffer ( ) , clientId ) ;
741746 }
742747 }
743748 }
@@ -761,7 +766,24 @@ private void HandleApproval(int clientId, bool approved)
761766 GameObject go = SpawnManager . SpawnPlayerObject ( clientId , networkId ) ;
762767 connectedClients [ clientId ] . PlayerObject = go ;
763768 }
764- using ( MemoryStream writeStream = new MemoryStream ( ) )
769+
770+
771+ int sizeOfStream = 4 + 4 + ( ( connectedClients . Count - 1 ) * 4 ) ;
772+ int amountOfObjectsToSend = 0 ;
773+ foreach ( KeyValuePair < uint , NetworkedObject > pair in SpawnManager . spawnedObjects )
774+ {
775+ if ( pair . Value . ServerOnly )
776+ continue ;
777+ else
778+ amountOfObjectsToSend ++ ;
779+ }
780+ if ( NetworkConfig . HandleObjectSpawning )
781+ {
782+ sizeOfStream += 4 ;
783+ sizeOfStream += 13 * amountOfObjectsToSend ;
784+ }
785+
786+ using ( MemoryStream writeStream = new MemoryStream ( sizeOfStream ) )
765787 {
766788 using ( BinaryWriter writer = new BinaryWriter ( writeStream ) )
767789 {
@@ -776,23 +798,10 @@ private void HandleApproval(int clientId, bool approved)
776798 }
777799 if ( NetworkConfig . HandleObjectSpawning )
778800 {
779- int amountOfObjectsToSend = 0 ;
780- foreach ( KeyValuePair < uint , NetworkedObject > pair in SpawnManager . spawnedObjects )
781- {
782- if ( pair . Value . ServerOnly )
783- continue ;
784- else
785- amountOfObjectsToSend ++ ;
786- }
787801 writer . Write ( amountOfObjectsToSend ) ;
788802
789803 foreach ( KeyValuePair < uint , NetworkedObject > pair in SpawnManager . spawnedObjects )
790804 {
791- if ( pair . Value . ServerOnly )
792- continue ;
793- else
794- amountOfObjectsToSend ++ ;
795-
796805 if ( pair . Value . ServerOnly )
797806 continue ;
798807 writer . Write ( pair . Value . isPlayerObject ) ;
@@ -802,11 +811,17 @@ private void HandleApproval(int clientId, bool approved)
802811 }
803812 }
804813 }
805- Send ( clientId , "MLAPI_CONNECTION_APPROVED" , "MLAPI_RELIABLE_FRAGMENTED" , writeStream . ToArray ( ) ) ;
814+ Send ( clientId , "MLAPI_CONNECTION_APPROVED" , "MLAPI_RELIABLE_FRAGMENTED" , writeStream . GetBuffer ( ) ) ;
806815 }
807816
808817 //Inform old clients of the new player
809- using ( MemoryStream stream = new MemoryStream ( ) )
818+
819+ if ( NetworkConfig . HandleObjectSpawning )
820+ sizeOfStream = 13 ;
821+ else
822+ sizeOfStream = 4 ;
823+
824+ using ( MemoryStream stream = new MemoryStream ( sizeOfStream ) )
810825 {
811826 using ( BinaryWriter writer = new BinaryWriter ( stream ) )
812827 {
@@ -822,7 +837,7 @@ private void HandleApproval(int clientId, bool approved)
822837 writer . Write ( clientId ) ;
823838 }
824839 }
825- Send ( "MLAPI_ADD_OBJECT" , "MLAPI_RELIABLE_FRAGMENTED" , stream . ToArray ( ) , clientId ) ;
840+ Send ( "MLAPI_ADD_OBJECT" , "MLAPI_RELIABLE_FRAGMENTED" , stream . GetBuffer ( ) , clientId ) ;
826841 }
827842 }
828843 else
0 commit comments