1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Runtime . CompilerServices ;
4
- using Unity . Collections ;
5
4
using Unity . Mathematics ;
6
5
using UnityEngine ;
7
6
@@ -1330,9 +1329,6 @@ internal NetworkTransformState LocalAuthoritativeNetworkState
1330
1329
private Quaternion m_CurrentRotation ;
1331
1330
private Vector3 m_TargetRotation ;
1332
1331
1333
- // Used to for each instance to uniquely identify the named message
1334
- private string m_MessageName ;
1335
-
1336
1332
1337
1333
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
1338
1334
internal void UpdatePositionInterpolator ( Vector3 position , double time , bool resetInterpolator = false )
@@ -2753,21 +2749,12 @@ public override void OnNetworkSpawn()
2753
2749
// Started using this again to avoid the getter processing cost of NetworkBehaviour.NetworkManager
2754
2750
m_CachedNetworkManager = NetworkManager ;
2755
2751
2756
- // Register a custom named message specifically for this instance
2757
- m_MessageName = $ "NTU_{ NetworkObjectId } _{ NetworkBehaviourId } ";
2758
- m_CachedNetworkManager . CustomMessagingManager . RegisterNamedMessageHandler ( m_MessageName , TransformStateUpdate ) ;
2759
2752
Initialize ( ) ;
2760
2753
}
2761
2754
2762
2755
/// <inheritdoc/>
2763
2756
public override void OnNetworkDespawn ( )
2764
2757
{
2765
- // During destroy, use NetworkBehaviour.NetworkManager as opposed to m_CachedNetworkManager
2766
- if ( ! NetworkManager . ShutdownInProgress && NetworkManager . CustomMessagingManager != null && ! string . IsNullOrEmpty ( m_MessageName ) )
2767
- {
2768
- NetworkManager . CustomMessagingManager . UnregisterNamedMessageHandler ( m_MessageName ) ;
2769
- }
2770
-
2771
2758
DeregisterForTickUpdate ( this ) ;
2772
2759
2773
2760
CanCommitToTransform = false ;
@@ -3156,79 +3143,23 @@ public bool IsServerAuthoritative()
3156
3143
}
3157
3144
3158
3145
/// <summary>
3159
- /// Receives the <see cref="NetworkTransformState "/> named message updates
3146
+ /// Invoked by <see cref="NetworkTransformMessage "/> to update the transform state
3160
3147
/// </summary>
3161
- /// <param name="senderId">authority of the transform</param>
3162
- /// <param name="messagePayload">serialzied <see cref="NetworkTransformState"/></param>
3163
- private void TransformStateUpdate ( ulong senderId , FastBufferReader messagePayload )
3148
+ /// <param name="networkTransformState"></param>
3149
+ internal void TransformStateUpdate ( ref NetworkTransformState networkTransformState )
3164
3150
{
3165
- var ownerAuthoritativeServerSide = ! OnIsServerAuthoritative ( ) && IsServer ;
3166
- if ( ownerAuthoritativeServerSide && OwnerClientId == NetworkManager . ServerClientId )
3167
- {
3168
- // Ownership must have changed, ignore any additional pending messages that might have
3169
- // come from a previous owner client.
3170
- return ;
3171
- }
3172
-
3173
3151
// Store the previous/old state
3174
3152
m_OldState = m_LocalAuthoritativeNetworkState ;
3175
3153
3176
- // Save the current payload stream position
3177
- var currentPosition = messagePayload . Position ;
3178
-
3179
- // Deserialize the message (and determine network delivery)
3180
- messagePayload . ReadNetworkSerializableInPlace ( ref m_LocalAuthoritativeNetworkState ) ;
3181
-
3182
- // Rewind back prior to serialization
3183
- messagePayload . Seek ( currentPosition ) ;
3154
+ // Assign the new incoming state
3155
+ m_LocalAuthoritativeNetworkState = networkTransformState ;
3184
3156
3185
- // Get the network delivery method used to send this state update
3186
- var networkDelivery = m_LocalAuthoritativeNetworkState . ReliableSequenced ? NetworkDelivery . ReliableSequenced : NetworkDelivery . UnreliableSequenced ;
3187
-
3188
- // Forward owner authoritative messages before doing anything else
3189
- if ( ownerAuthoritativeServerSide )
3190
- {
3191
- // Forward the state update if there are any remote clients to foward it to
3192
- if ( m_CachedNetworkManager . ConnectionManager . ConnectedClientsList . Count > ( IsHost ? 2 : 1 ) )
3193
- {
3194
- ForwardStateUpdateMessage ( messagePayload , networkDelivery ) ;
3195
- }
3196
- }
3197
-
3198
- // Apply the message
3157
+ // Apply the state update
3199
3158
OnNetworkStateChanged ( m_OldState , m_LocalAuthoritativeNetworkState ) ;
3200
3159
}
3201
3160
3202
3161
/// <summary>
3203
- /// Forwards owner authoritative state updates when received by the server
3204
- /// </summary>
3205
- /// <param name="messagePayload">the owner state message payload</param>
3206
- private unsafe void ForwardStateUpdateMessage ( FastBufferReader messagePayload , NetworkDelivery networkDelivery )
3207
- {
3208
- var serverAuthoritative = OnIsServerAuthoritative ( ) ;
3209
- var currentPosition = messagePayload . Position ;
3210
- var messageSize = messagePayload . Length - currentPosition ;
3211
- var writer = new FastBufferWriter ( messageSize , Allocator . Temp ) ;
3212
- using ( writer )
3213
- {
3214
- writer . WriteBytesSafe ( messagePayload . GetUnsafePtr ( ) , messageSize , currentPosition ) ;
3215
-
3216
- var clientCount = m_CachedNetworkManager . ConnectionManager . ConnectedClientsList . Count ;
3217
- for ( int i = 0 ; i < clientCount ; i ++ )
3218
- {
3219
- var clientId = m_CachedNetworkManager . ConnectionManager . ConnectedClientsList [ i ] . ClientId ;
3220
- if ( NetworkManager . ServerClientId == clientId || ( ! serverAuthoritative && clientId == OwnerClientId ) )
3221
- {
3222
- continue ;
3223
- }
3224
- m_CachedNetworkManager . CustomMessagingManager . SendNamedMessage ( m_MessageName , clientId , writer , networkDelivery ) ;
3225
- }
3226
- }
3227
- messagePayload . Seek ( currentPosition ) ;
3228
- }
3229
-
3230
- /// <summary>
3231
- /// Sends <see cref="NetworkTransformState"/> named message updates by the authority of the transform
3162
+ /// Invoked by the authoritative instance to sends a <see cref="NetworkTransformMessage"/> containing the <see cref="NetworkTransformState"/>
3232
3163
/// </summary>
3233
3164
private void UpdateTransformState ( )
3234
3165
{
@@ -3248,7 +3179,12 @@ private void UpdateTransformState()
3248
3179
}
3249
3180
var customMessageManager = m_CachedNetworkManager . CustomMessagingManager ;
3250
3181
3251
- var writer = new FastBufferWriter ( 128 , Allocator . Temp ) ;
3182
+ var networkTransformMessage = new NetworkTransformMessage ( )
3183
+ {
3184
+ NetworkObjectId = NetworkObjectId ,
3185
+ NetworkBehaviourId = NetworkBehaviourId ,
3186
+ State = m_LocalAuthoritativeNetworkState
3187
+ } ;
3252
3188
3253
3189
// Determine what network delivery method to use:
3254
3190
// When to send reliable packets:
@@ -3259,32 +3195,32 @@ private void UpdateTransformState()
3259
3195
| m_LocalAuthoritativeNetworkState . UnreliableFrameSync | m_LocalAuthoritativeNetworkState . SynchronizeBaseHalfFloat
3260
3196
? NetworkDelivery . ReliableSequenced : NetworkDelivery . UnreliableSequenced ;
3261
3197
3262
- using ( writer )
3198
+ // Server-host always sends updates to all clients (but itself)
3199
+ if ( IsServer )
3263
3200
{
3264
- writer . WriteNetworkSerializable ( m_LocalAuthoritativeNetworkState ) ;
3265
- // Server-host always sends updates to all clients (but itself)
3266
- if ( IsServer )
3201
+ var clientCount = m_CachedNetworkManager . ConnectionManager . ConnectedClientsList . Count ;
3202
+ for ( int i = 0 ; i < clientCount ; i ++ )
3267
3203
{
3268
- var clientCount = m_CachedNetworkManager . ConnectionManager . ConnectedClientsList . Count ;
3269
- for ( int i = 0 ; i < clientCount ; i ++ )
3204
+ var clientId = m_CachedNetworkManager . ConnectionManager . ConnectedClientsList [ i ] . ClientId ;
3205
+ if ( NetworkManager . ServerClientId == clientId )
3270
3206
{
3271
- var clientId = m_CachedNetworkManager . ConnectionManager . ConnectedClientsList [ i ] . ClientId ;
3272
- if ( NetworkManager . ServerClientId == clientId )
3273
- {
3274
- continue ;
3275
- }
3276
- customMessageManager . SendNamedMessage ( m_MessageName , clientId , writer , networkDelivery ) ;
3207
+ continue ;
3277
3208
}
3209
+ if ( ! NetworkObject . Observers . Contains ( clientId ) )
3210
+ {
3211
+ continue ;
3212
+ }
3213
+ NetworkManager . MessageManager . SendMessage ( ref networkTransformMessage , networkDelivery , clientId ) ;
3278
3214
}
3279
- else
3280
- {
3281
- // Clients (owner authoritative) send messages to the server-host
3282
- customMessageManager . SendNamedMessage ( m_MessageName , NetworkManager . ServerClientId , writer , networkDelivery ) ;
3283
- }
3215
+ }
3216
+ else
3217
+ {
3218
+ // Clients (owner authoritative) send messages to the server-host
3219
+ NetworkManager . MessageManager . SendMessage ( ref networkTransformMessage , networkDelivery , NetworkManager . ServerClientId ) ;
3284
3220
}
3285
3221
}
3286
3222
3287
-
3223
+ #region Network Tick Registration and Handling
3288
3224
private static Dictionary < NetworkManager , NetworkTransformTickRegistration > s_NetworkTickRegistration = new Dictionary < NetworkManager , NetworkTransformTickRegistration > ( ) ;
3289
3225
3290
3226
private static void RemoveTickUpdate ( NetworkManager networkManager )
@@ -3394,6 +3330,8 @@ private static void DeregisterForTickUpdate(NetworkTransform networkTransform)
3394
3330
}
3395
3331
}
3396
3332
}
3333
+
3334
+ #endregion
3397
3335
}
3398
3336
3399
3337
internal interface INetworkTransformLogStateEntry
0 commit comments