@@ -346,48 +346,52 @@ internal void NetworkedVarUpdate()
346346
347347 for ( int i = 0 ; i < NetworkingManager . Singleton . ConnectedClientsList . Count ; i ++ )
348348 {
349- //This iterates over every "channel group".
350- for ( int j = 0 ; j < channelMappedVarIndexes . Count ; j ++ )
349+ // Do this check here to prevent doing all the expensive dirty checks
350+ if ( ! IsServer || this . NetworkedObject . observers . Contains ( NetworkingManager . Singleton . ConnectedClientsList [ i ] . ClientId ) )
351351 {
352- using ( PooledBitStream stream = PooledBitStream . Get ( ) )
352+ //This iterates over every "channel group".
353+ for ( int j = 0 ; j < channelMappedVarIndexes . Count ; j ++ )
353354 {
354- using ( PooledBitWriter writer = PooledBitWriter . Get ( stream ) )
355+ using ( PooledBitStream stream = PooledBitStream . Get ( ) )
355356 {
356- writer . WriteUInt32Packed ( NetworkId ) ;
357- writer . WriteUInt16Packed ( NetworkedObject . GetOrderIndex ( this ) ) ;
358-
359- uint clientId = NetworkingManager . Singleton . ConnectedClientsList [ i ] . ClientId ;
360- bool writtenAny = false ;
361- for ( int k = 0 ; k < networkedVarFields . Count ; k ++ )
357+ using ( PooledBitWriter writer = PooledBitWriter . Get ( stream ) )
362358 {
363- if ( ! channelMappedVarIndexes [ j ] . Contains ( k ) )
359+ writer . WriteUInt32Packed ( NetworkId ) ;
360+ writer . WriteUInt16Packed ( NetworkedObject . GetOrderIndex ( this ) ) ;
361+
362+ uint clientId = NetworkingManager . Singleton . ConnectedClientsList [ i ] . ClientId ;
363+ bool writtenAny = false ;
364+ for ( int k = 0 ; k < networkedVarFields . Count ; k ++ )
364365 {
365- //This var does not belong to the currently iterating channel group.
366- writer . WriteBool ( false ) ;
367- continue ;
368- }
366+ if ( ! channelMappedVarIndexes [ j ] . Contains ( k ) )
367+ {
368+ //This var does not belong to the currently iterating channel group.
369+ writer . WriteBool ( false ) ;
370+ continue ;
371+ }
369372
370- bool isDirty = networkedVarFields [ k ] . IsDirty ( ) ; //cache this here. You never know what operations users will do in the dirty methods
371- writer . WriteBool ( isDirty ) ;
373+ bool isDirty = networkedVarFields [ k ] . IsDirty ( ) ; //cache this here. You never know what operations users will do in the dirty methods
374+ writer . WriteBool ( isDirty ) ;
372375
373- if ( isDirty && ( ! IsServer || networkedVarFields [ k ] . CanClientRead ( clientId ) ) )
374- {
375- writtenAny = true ;
376- networkedVarFields [ k ] . WriteDelta ( stream ) ;
377- if ( ! networkedVarIndexesToResetSet . Contains ( k ) )
376+ if ( isDirty && ( ! IsServer || networkedVarFields [ k ] . CanClientRead ( clientId ) ) )
378377 {
379- networkedVarIndexesToResetSet . Add ( k ) ;
380- networkedVarIndexesToReset . Add ( k ) ;
378+ writtenAny = true ;
379+ networkedVarFields [ k ] . WriteDelta ( stream ) ;
380+ if ( ! networkedVarIndexesToResetSet . Contains ( k ) )
381+ {
382+ networkedVarIndexesToResetSet . Add ( k ) ;
383+ networkedVarIndexesToReset . Add ( k ) ;
384+ }
381385 }
382386 }
383- }
384-
385- if ( writtenAny )
386- {
387- if ( IsServer )
388- InternalMessageHandler . Send ( clientId , MLAPIConstants . MLAPI_NETWORKED_VAR_DELTA , channelsForVarGroups [ j ] , stream , SecuritySendFlags . None ) ;
389- else
390- InternalMessageHandler . Send ( NetworkingManager . Singleton . ServerClientId , MLAPIConstants . MLAPI_NETWORKED_VAR_DELTA , channelsForVarGroups [ j ] , stream , SecuritySendFlags . None ) ;
387+
388+ if ( writtenAny )
389+ {
390+ if ( IsServer )
391+ InternalMessageHandler . Send ( clientId , MLAPIConstants . MLAPI_NETWORKED_VAR_DELTA , channelsForVarGroups [ j ] , stream , SecuritySendFlags . None , this . NetworkedObject ) ;
392+ else
393+ InternalMessageHandler . Send ( NetworkingManager . Singleton . ServerClientId , MLAPIConstants . MLAPI_NETWORKED_VAR_DELTA , channelsForVarGroups [ j ] , stream , SecuritySendFlags . None , null ) ;
394+ }
391395 }
392396 }
393397 }
@@ -875,7 +879,7 @@ internal void SendServerRPCPerformance(ulong hash, Stream messageStream, string
875879 }
876880 else
877881 {
878- InternalMessageHandler . Send ( NetworkingManager . Singleton . ServerClientId , MLAPIConstants . MLAPI_SERVER_RPC , string . IsNullOrEmpty ( channel ) ? "MLAPI_DEFAULT_MESSAGE" : channel , stream , security ) ;
882+ InternalMessageHandler . Send ( NetworkingManager . Singleton . ServerClientId , MLAPIConstants . MLAPI_SERVER_RPC , string . IsNullOrEmpty ( channel ) ? "MLAPI_DEFAULT_MESSAGE" : channel , stream , security , null ) ;
879883 }
880884 }
881885 }
@@ -932,7 +936,7 @@ internal RpcResponse<T> SendServerRPCPerformanceResponse<T>(ulong hash, Stream m
932936
933937 ResponseMessageManager . Add ( response . Id , response ) ;
934938
935- InternalMessageHandler . Send ( NetworkingManager . Singleton . ServerClientId , MLAPIConstants . MLAPI_SERVER_RPC_REQUEST , string . IsNullOrEmpty ( channel ) ? "MLAPI_DEFAULT_MESSAGE" : channel , stream , security ) ;
939+ InternalMessageHandler . Send ( NetworkingManager . Singleton . ServerClientId , MLAPIConstants . MLAPI_SERVER_RPC_REQUEST , string . IsNullOrEmpty ( channel ) ? "MLAPI_DEFAULT_MESSAGE" : channel , stream , security , null ) ;
936940
937941 return response ;
938942 }
@@ -963,29 +967,40 @@ internal void SendClientRPCPerformance(ulong hash, List<uint> clientIds, Stream
963967 {
964968 for ( int i = 0 ; i < NetworkingManager . Singleton . ConnectedClientsList . Count ; i ++ )
965969 {
970+ if ( ! this . NetworkedObject . observers . Contains ( NetworkingManager . Singleton . ConnectedClientsList [ i ] . ClientId ) )
971+ {
972+ continue ;
973+ }
974+
966975 if ( IsHost && NetworkingManager . Singleton . ConnectedClientsList [ i ] . ClientId == NetworkingManager . Singleton . LocalClientId )
967976 {
968977 messageStream . Position = 0 ;
969978 InvokeClientRPCLocal ( hash , NetworkingManager . Singleton . LocalClientId , messageStream ) ;
970979 }
971980 else
972981 {
973- InternalMessageHandler . Send ( NetworkingManager . Singleton . ConnectedClientsList [ i ] . ClientId , MLAPIConstants . MLAPI_CLIENT_RPC , string . IsNullOrEmpty ( channel ) ? "MLAPI_DEFAULT_MESSAGE" : channel , stream , security ) ;
982+ InternalMessageHandler . Send ( NetworkingManager . Singleton . ConnectedClientsList [ i ] . ClientId , MLAPIConstants . MLAPI_CLIENT_RPC , string . IsNullOrEmpty ( channel ) ? "MLAPI_DEFAULT_MESSAGE" : channel , stream , security , null ) ;
974983 }
975984 }
976985 }
977986 else
978987 {
979988 for ( int i = 0 ; i < clientIds . Count ; i ++ )
980989 {
990+ if ( ! this . NetworkedObject . observers . Contains ( clientIds [ i ] ) )
991+ {
992+ if ( LogHelper . CurrentLogLevel <= LogLevel . Normal ) LogHelper . LogWarning ( "Cannot send ClientRPC to client without visibility to the object" ) ;
993+ continue ;
994+ }
995+
981996 if ( IsHost && clientIds [ i ] == NetworkingManager . Singleton . LocalClientId )
982997 {
983998 messageStream . Position = 0 ;
984999 InvokeClientRPCLocal ( hash , NetworkingManager . Singleton . LocalClientId , messageStream ) ;
9851000 }
9861001 else
9871002 {
988- InternalMessageHandler . Send ( clientIds [ i ] , MLAPIConstants . MLAPI_CLIENT_RPC , string . IsNullOrEmpty ( channel ) ? "MLAPI_DEFAULT_MESSAGE" : channel , stream , security ) ;
1003+ InternalMessageHandler . Send ( clientIds [ i ] , MLAPIConstants . MLAPI_CLIENT_RPC , string . IsNullOrEmpty ( channel ) ? "MLAPI_DEFAULT_MESSAGE" : channel , stream , security , null ) ;
9891004 }
9901005 }
9911006 }
@@ -1015,16 +1030,17 @@ internal void SendClientRPCPerformance(ulong hash, Stream messageStream, uint cl
10151030
10161031 for ( int i = 0 ; i < NetworkingManager . Singleton . ConnectedClientsList . Count ; i ++ )
10171032 {
1018- if ( NetworkingManager . Singleton . ConnectedClientsList [ i ] . ClientId == clientIdToIgnore )
1033+ if ( NetworkingManager . Singleton . ConnectedClientsList [ i ] . ClientId == clientIdToIgnore || ! this . NetworkedObject . observers . Contains ( NetworkingManager . Singleton . ConnectedClientsList [ i ] . ClientId ) )
10191034 continue ;
1035+
10201036 if ( IsHost && NetworkingManager . Singleton . ConnectedClientsList [ i ] . ClientId == NetworkingManager . Singleton . LocalClientId )
10211037 {
10221038 messageStream . Position = 0 ;
10231039 InvokeClientRPCLocal ( hash , NetworkingManager . Singleton . LocalClientId , messageStream ) ;
10241040 }
10251041 else
10261042 {
1027- InternalMessageHandler . Send ( NetworkingManager . Singleton . ConnectedClientsList [ i ] . ClientId , MLAPIConstants . MLAPI_CLIENT_RPC , string . IsNullOrEmpty ( channel ) ? "MLAPI_DEFAULT_MESSAGE" : channel , stream , security ) ;
1043+ InternalMessageHandler . Send ( NetworkingManager . Singleton . ConnectedClientsList [ i ] . ClientId , MLAPIConstants . MLAPI_CLIENT_RPC , string . IsNullOrEmpty ( channel ) ? "MLAPI_DEFAULT_MESSAGE" : channel , stream , security , null ) ;
10281044 }
10291045 }
10301046 }
@@ -1039,6 +1055,12 @@ internal void SendClientRPCPerformance(ulong hash, uint clientId, Stream message
10391055 if ( LogHelper . CurrentLogLevel <= LogLevel . Normal ) LogHelper . LogWarning ( "Only clients and host can invoke ClientRPC" ) ;
10401056 return ;
10411057 }
1058+
1059+ if ( ! this . NetworkedObject . observers . Contains ( clientId ) )
1060+ {
1061+ if ( LogHelper . CurrentLogLevel <= LogLevel . Normal ) LogHelper . LogWarning ( "Cannot send ClientRPC to client without visibility to the object" ) ;
1062+ return ;
1063+ }
10421064
10431065 using ( PooledBitStream stream = PooledBitStream . Get ( ) )
10441066 {
@@ -1057,7 +1079,7 @@ internal void SendClientRPCPerformance(ulong hash, uint clientId, Stream message
10571079 }
10581080 else
10591081 {
1060- InternalMessageHandler . Send ( clientId , MLAPIConstants . MLAPI_CLIENT_RPC , string . IsNullOrEmpty ( channel ) ? "MLAPI_DEFAULT_MESSAGE" : channel , stream , security ) ;
1082+ InternalMessageHandler . Send ( clientId , MLAPIConstants . MLAPI_CLIENT_RPC , string . IsNullOrEmpty ( channel ) ? "MLAPI_DEFAULT_MESSAGE" : channel , stream , security , null ) ;
10611083 }
10621084 }
10631085 }
@@ -1072,6 +1094,12 @@ internal RpcResponse<T> SendClientRPCPerformanceResponse<T>(ulong hash, uint cli
10721094 return null ;
10731095 }
10741096
1097+ if ( ! this . NetworkedObject . observers . Contains ( clientId ) )
1098+ {
1099+ if ( LogHelper . CurrentLogLevel <= LogLevel . Normal ) LogHelper . LogWarning ( "Cannot send ClientRPC to client without visibility to the object" ) ;
1100+ return null ;
1101+ }
1102+
10751103 ulong responseId = ResponseMessageManager . GenerateMessageId ( ) ;
10761104
10771105 using ( PooledBitStream stream = PooledBitStream . Get ( ) )
@@ -1114,7 +1142,7 @@ internal RpcResponse<T> SendClientRPCPerformanceResponse<T>(ulong hash, uint cli
11141142
11151143 ResponseMessageManager . Add ( response . Id , response ) ;
11161144
1117- InternalMessageHandler . Send ( clientId , MLAPIConstants . MLAPI_CLIENT_RPC_REQUEST , string . IsNullOrEmpty ( channel ) ? "MLAPI_DEFAULT_MESSAGE" : channel , stream , security ) ;
1145+ InternalMessageHandler . Send ( clientId , MLAPIConstants . MLAPI_CLIENT_RPC_REQUEST , string . IsNullOrEmpty ( channel ) ? "MLAPI_DEFAULT_MESSAGE" : channel , stream , security , null ) ;
11181146
11191147 return response ;
11201148 }
0 commit comments