Skip to content

Commit 544de80

Browse files
committed
Merge branch 'sam/feature/interpolation-for-network-transform' of github.com:Unity-Technologies/com.unity.multiplayer.mlapi into sam/feature/interpolation-for-network-transform
* 'sam/feature/interpolation-for-network-transform' of github.com:Unity-Technologies/com.unity.multiplayer.mlapi: fix: networkscenemanager not releasing buffers from pool (#1132) test: fixed-length strings in netvars (#1119) fix: snapshot system. last fixes for release (#1129) refactor!: Unified Shutdown (#1108) chore: Fill out unity project for integration test project (#1128) feat: make ServerRpc ownership check an error log instead of warning log (#1126)
2 parents ae56748 + dcf36fc commit 544de80

40 files changed

+1391
-236
lines changed

com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
7878
return new ILPostProcessResult(new InMemoryAssembly(pe.ToArray(), pdb.ToArray()), m_Diagnostics);
7979
}
8080

81-
private MethodReference m_Debug_LogWarning_MethodRef;
81+
private MethodReference m_Debug_LogError_MethodRef;
8282
private TypeReference m_NetworkManager_TypeRef;
8383
private MethodReference m_NetworkManager_getLocalClientId_MethodRef;
8484
private MethodReference m_NetworkManager_getIsListening_MethodRef;
@@ -150,7 +150,7 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
150150
private MethodReference m_NetworkSerializer_SerializeRayArray_MethodRef;
151151
private MethodReference m_NetworkSerializer_SerializeRay2DArray_MethodRef;
152152

153-
private const string k_Debug_LogWarning = nameof(Debug.LogWarning);
153+
private const string k_Debug_LogError = nameof(Debug.LogError);
154154
private const string k_NetworkManager_LocalClientId = nameof(NetworkManager.LocalClientId);
155155
private const string k_NetworkManager_IsListening = nameof(NetworkManager.IsListening);
156156
private const string k_NetworkManager_IsHost = nameof(NetworkManager.IsHost);
@@ -182,10 +182,10 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
182182
{
183183
switch (methodInfo.Name)
184184
{
185-
case k_Debug_LogWarning:
185+
case k_Debug_LogError:
186186
if (methodInfo.GetParameters().Length == 1)
187187
{
188-
m_Debug_LogWarning_MethodRef = moduleDefinition.ImportReference(methodInfo);
188+
m_Debug_LogError_MethodRef = moduleDefinition.ImportReference(methodInfo);
189189
}
190190

191191
break;
@@ -830,9 +830,9 @@ private void InjectWriteAndCallBlocks(MethodDefinition methodDefinition, CustomA
830830
instructions.Add(processor.Create(OpCodes.Ceq));
831831
instructions.Add(processor.Create(OpCodes.Brfalse, logNextInstr));
832832

833-
// Debug.LogWarning(...);
833+
// Debug.LogError(...);
834834
instructions.Add(processor.Create(OpCodes.Ldstr, "Only the owner can invoke a ServerRpc that requires ownership!"));
835-
instructions.Add(processor.Create(OpCodes.Call, m_Debug_LogWarning_MethodRef));
835+
instructions.Add(processor.Create(OpCodes.Call, m_Debug_LogError_MethodRef));
836836

837837
instructions.Add(logNextInstr);
838838

@@ -1827,9 +1827,9 @@ private MethodDefinition GenerateStaticHandler(MethodDefinition methodDefinition
18271827
processor.Emit(OpCodes.Ceq);
18281828
processor.Emit(OpCodes.Brfalse, logNextInstr);
18291829

1830-
// Debug.LogWarning(...);
1830+
// Debug.LogError(...);
18311831
processor.Emit(OpCodes.Ldstr, "Only the owner can invoke a ServerRpc that requires ownership!");
1832-
processor.Emit(OpCodes.Call, m_Debug_LogWarning_MethodRef);
1832+
processor.Emit(OpCodes.Call, m_Debug_LogError_MethodRef);
18331833

18341834
processor.Append(logNextInstr);
18351835

com.unity.netcode.gameobjects/Editor/NetworkManagerEditor.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -352,18 +352,7 @@ public override void OnInspectorGUI()
352352

353353
if (GUILayout.Button(new GUIContent("Stop " + instanceType, "Stops the " + instanceType + " instance.")))
354354
{
355-
if (m_NetworkManager.IsHost)
356-
{
357-
m_NetworkManager.StopHost();
358-
}
359-
else if (m_NetworkManager.IsServer)
360-
{
361-
m_NetworkManager.StopServer();
362-
}
363-
else if (m_NetworkManager.IsClient)
364-
{
365-
m_NetworkManager.StopClient();
366-
}
355+
m_NetworkManager.Shutdown();
367356
}
368357
}
369358
}

com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs

Lines changed: 61 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -726,94 +726,6 @@ public SocketTasks StartClient()
726726
return socketTasks;
727727
}
728728

729-
/// <summary>
730-
/// Stops the running server
731-
/// </summary>
732-
public void StopServer()
733-
{
734-
if (NetworkLog.CurrentLogLevel <= LogLevel.Developer)
735-
{
736-
NetworkLog.LogInfo(nameof(StopServer));
737-
}
738-
739-
var disconnectedIds = new HashSet<ulong>();
740-
741-
//Don't know if I have to disconnect the clients. I'm assuming the NetworkTransport does all the cleaning on shtudown. But this way the clients get a disconnect message from server (so long it does't get lost)
742-
743-
// make sure all messages are flushed before transport disconnect clients
744-
if (MessageQueueContainer != null)
745-
{
746-
MessageQueueContainer.ProcessAndFlushMessageQueue(
747-
queueType: MessageQueueContainer.MessageQueueProcessingTypes.Send,
748-
NetworkUpdateStage.PostLateUpdate); // flushing messages in case transport's disconnect
749-
}
750-
751-
foreach (KeyValuePair<ulong, NetworkClient> pair in ConnectedClients)
752-
{
753-
if (!disconnectedIds.Contains(pair.Key))
754-
{
755-
disconnectedIds.Add(pair.Key);
756-
757-
if (pair.Key == NetworkConfig.NetworkTransport.ServerClientId)
758-
{
759-
continue;
760-
}
761-
762-
NetworkConfig.NetworkTransport.DisconnectRemoteClient(pair.Key);
763-
}
764-
}
765-
766-
foreach (KeyValuePair<ulong, PendingClient> pair in PendingClients)
767-
{
768-
if (!disconnectedIds.Contains(pair.Key))
769-
{
770-
disconnectedIds.Add(pair.Key);
771-
if (pair.Key == NetworkConfig.NetworkTransport.ServerClientId)
772-
{
773-
continue;
774-
}
775-
776-
NetworkConfig.NetworkTransport.DisconnectRemoteClient(pair.Key);
777-
}
778-
}
779-
780-
IsServer = false;
781-
Shutdown();
782-
}
783-
784-
/// <summary>
785-
/// Stops the running host
786-
/// </summary>
787-
public void StopHost()
788-
{
789-
if (NetworkLog.CurrentLogLevel <= LogLevel.Developer)
790-
{
791-
NetworkLog.LogInfo(nameof(StopHost));
792-
}
793-
794-
IsServer = false;
795-
IsClient = false;
796-
StopServer();
797-
798-
//We don't stop client since we dont actually have a transport connection to our own host. We just handle host messages directly in the netcode
799-
}
800-
801-
/// <summary>
802-
/// Stops the running client
803-
/// </summary>
804-
public void StopClient()
805-
{
806-
if (NetworkLog.CurrentLogLevel <= LogLevel.Developer)
807-
{
808-
NetworkLog.LogInfo(nameof(StopClient));
809-
}
810-
811-
IsClient = false;
812-
NetworkConfig.NetworkTransport.DisconnectLocalClient();
813-
IsConnectedClient = false;
814-
Shutdown();
815-
}
816-
817729
/// <summary>
818730
/// Starts a Host
819731
/// </summary>
@@ -942,13 +854,71 @@ private void OnDestroy()
942854
}
943855
}
944856

857+
/// <summary>
858+
/// Globally shuts down the library.
859+
/// Disconnects clients if connected and stops server if running.
860+
/// </summary>
945861
public void Shutdown()
946862
{
947863
if (NetworkLog.CurrentLogLevel <= LogLevel.Developer)
948864
{
949865
NetworkLog.LogInfo(nameof(Shutdown));
950866
}
951867

868+
if (IsServer)
869+
{
870+
// make sure all messages are flushed before transport disconnect clients
871+
if (MessageQueueContainer != null)
872+
{
873+
MessageQueueContainer.ProcessAndFlushMessageQueue(
874+
queueType: MessageQueueContainer.MessageQueueProcessingTypes.Send,
875+
NetworkUpdateStage.PostLateUpdate); // flushing messages in case transport's disconnect
876+
}
877+
878+
var disconnectedIds = new HashSet<ulong>();
879+
880+
//Don't know if I have to disconnect the clients. I'm assuming the NetworkTransport does all the cleaning on shutdown. But this way the clients get a disconnect message from server (so long it does't get lost)
881+
882+
foreach (KeyValuePair<ulong, NetworkClient> pair in ConnectedClients)
883+
{
884+
if (!disconnectedIds.Contains(pair.Key))
885+
{
886+
disconnectedIds.Add(pair.Key);
887+
888+
if (pair.Key == NetworkConfig.NetworkTransport.ServerClientId)
889+
{
890+
continue;
891+
}
892+
893+
NetworkConfig.NetworkTransport.DisconnectRemoteClient(pair.Key);
894+
}
895+
}
896+
897+
foreach (KeyValuePair<ulong, PendingClient> pair in PendingClients)
898+
{
899+
if (!disconnectedIds.Contains(pair.Key))
900+
{
901+
disconnectedIds.Add(pair.Key);
902+
if (pair.Key == NetworkConfig.NetworkTransport.ServerClientId)
903+
{
904+
continue;
905+
}
906+
907+
NetworkConfig.NetworkTransport.DisconnectRemoteClient(pair.Key);
908+
}
909+
}
910+
}
911+
912+
if (IsClient)
913+
{
914+
// Client only, send disconnect to server
915+
NetworkConfig.NetworkTransport.DisconnectLocalClient();
916+
}
917+
918+
IsConnectedClient = false;
919+
IsServer = false;
920+
IsClient = false;
921+
952922
// Unregister INetworkUpdateSystem before shutting down the MessageQueueContainer
953923
this.UnregisterAllNetworkUpdates();
954924

@@ -971,8 +941,6 @@ public void Shutdown()
971941
NetworkTickSystem = null;
972942
}
973943

974-
IsServer = false;
975-
IsClient = false;
976944
NetworkConfig.NetworkTransport.OnTransportEvent -= HandleRawTransportPoll;
977945

978946
if (SpawnManager != null)
@@ -985,6 +953,8 @@ public void Shutdown()
985953

986954
if (SceneManager != null)
987955
{
956+
// Let the NetworkSceneManager clean up its two SceneEvenData instances
957+
SceneManager.Dispose();
988958
SceneManager = null;
989959
}
990960

@@ -1203,8 +1173,7 @@ private void HandleRawTransportPoll(NetworkEvent networkEvent, ulong clientId, N
12031173
}
12041174
else
12051175
{
1206-
IsConnectedClient = false;
1207-
StopClient();
1176+
Shutdown();
12081177
}
12091178

12101179
OnClientDisconnectCallback?.Invoke(clientId);

com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -394,17 +394,15 @@ private void OnDestroy()
394394

395395
private SnapshotDespawnCommand GetDespawnCommand()
396396
{
397-
SnapshotDespawnCommand command;
397+
var command = new SnapshotDespawnCommand();
398398
command.NetworkObjectId = NetworkObjectId;
399-
command.TickWritten = default; // value will be set internally by SnapshotSystem
400-
command.TargetClientIds = default;
401399

402400
return command;
403401
}
404402

405403
private SnapshotSpawnCommand GetSpawnCommand()
406404
{
407-
SnapshotSpawnCommand command;
405+
var command = new SnapshotSpawnCommand();
408406
command.NetworkObjectId = NetworkObjectId;
409407
command.OwnerClientId = OwnerClientId;
410408
command.IsPlayerObject = IsPlayerObject;
@@ -426,8 +424,6 @@ private SnapshotSpawnCommand GetSpawnCommand()
426424
command.ObjectPosition = transform.position;
427425
command.ObjectRotation = transform.rotation;
428426
command.ObjectScale = transform.localScale;
429-
command.TickWritten = default; // value will be set internally by SnapshotSystem
430-
command.TargetClientIds = default;
431427

432428
return command;
433429
}

0 commit comments

Comments
 (0)