Skip to content

Commit

Permalink
refactor: removing connection to server (MirageNet#703)
Browse files Browse the repository at this point in the history
* refactor: removing connection to server

this property is only used to send message to the server, there are alternative ways to send message to the server

BREAKING CHANGE: Removed ConnectionToServer property

* removing uses of ConnectionToServer

* removing use in test

* removing ClientRpc player target

* creating null

* changing tests to expect null

* fixing docs

* using client player for target rpc

Co-authored-by: Paul Pacheco <paulpach@gmail.com>

* fixing tests for rpc target

Co-authored-by: Paul Pacheco <paulpach@gmail.com>
  • Loading branch information
James-Frowen and paulpach authored Mar 24, 2021
1 parent d23173e commit ff95634
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 32 deletions.
1 change: 0 additions & 1 deletion Assets/Mirage/Runtime/ClientObjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,6 @@ void CheckForLocalPlayer(NetworkIdentity identity)
if (identity && identity == LocalPlayer)
{
// Set isLocalPlayer to true on this NetworkIdentity and trigger OnStartLocalPlayer in all scripts on the same GO
identity.ConnectionToServer = Client.Player;
identity.StartLocalPlayer();

if (logger.LogEnabled()) logger.Log("ClientScene.OnOwnerMessage - player=" + identity.name);
Expand Down
7 changes: 1 addition & 6 deletions Assets/Mirage/Runtime/NetworkBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,6 @@ public abstract class NetworkBehaviour : MonoBehaviour
/// </summary>
public ClientObjectManager ClientObjectManager => NetIdentity.ClientObjectManager;

/// <summary>
/// The <see cref="NetworkPlayer">NetworkConnection</see> associated with this <see cref="NetworkIdentity">NetworkIdentity.</see> This is only valid for player objects on the client.
/// </summary>
public INetworkPlayer ConnectionToServer => NetIdentity.ConnectionToServer;

/// <summary>
/// The <see cref="NetworkPlayer">NetworkConnection</see> associated with this <see cref="NetworkIdentity">NetworkIdentity.</see> This is only valid for player objects on the server.
/// </summary>
Expand Down Expand Up @@ -263,7 +258,7 @@ private void ValidateServerRpc(Type invokeClass, string cmdName, bool requireAut

if (Client.Player == null)
{
throw new InvalidOperationException("Send ServerRpc attempted with no client running [client=" + ConnectionToServer + "].");
throw new InvalidOperationException("Send ServerRpc attempted with no client connection.");
}
}

Expand Down
6 changes: 0 additions & 6 deletions Assets/Mirage/Runtime/NetworkIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,6 @@ public sealed class NetworkIdentity : MonoBehaviour
/// </summary>
public ServerObjectManager ServerObjectManager;

/// <summary>
/// The NetworkConnection associated with this NetworkIdentity. This is only valid for player objects on a local client.
/// </summary>
public INetworkPlayer ConnectionToServer { get; internal set; }

/// <summary>
/// The NetworkClient associated with this NetworkIdentity.
/// </summary>
Expand Down Expand Up @@ -1161,7 +1156,6 @@ internal void Reset()
Client = null;
ServerObjectManager = null;
ClientObjectManager = null;
ConnectionToServer = null;
ConnectionToClient = null;
networkBehavioursCache = null;

Expand Down
6 changes: 4 additions & 2 deletions Assets/Mirage/Weaver/Processors/ClientRpcProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ MethodDefinition GenerateSkeleton(MethodDefinition md, MethodDefinition userCode

if (hasNetworkConnection)
{
//client.connection
// this is called in the skeleton (the client)
// the client should just get the connection to the server and pass that in
worker.Append(worker.Create(OpCodes.Ldarg_0));
worker.Append(worker.Create(OpCodes.Call, (NetworkBehaviour nb) => nb.ConnectionToServer));
worker.Append(worker.Create(OpCodes.Call, (NetworkBehaviour nb) => nb.Client));
worker.Append(worker.Create(OpCodes.Call, (NetworkClient nb) => nb.Player));
}

if (!ReadArguments(md, worker, hasNetworkConnection))
Expand Down
2 changes: 0 additions & 2 deletions Assets/Tests/Editor/NetworkIdentityCallbackTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -704,14 +704,12 @@ public void Reset()
// creates .observers and generates a netId
identity.StartServer();
identity.ConnectionToClient = new NetworkPlayer(tconn42);
identity.ConnectionToServer = new NetworkPlayer(tconn43);
identity.observers.Add(new NetworkPlayer(tconn42));

// mark for reset and reset
identity.Reset();
Assert.That(identity.NetId, Is.EqualTo(0));
Assert.That(identity.ConnectionToClient, Is.Null);
Assert.That(identity.ConnectionToServer, Is.Null);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public IEnumerator ClientConnRpc() => UniTask.ToCoroutine(async () =>
// process spawn message from server
await AsyncUtil.WaitUntilWithTimeout(() => clientComponent.targetRpcArg1 != 0);
Assert.That(clientComponent.targetRpcPlayer, Is.SameAs(connectionToServer));
Assert.That(clientComponent.targetRpcPlayer, Is.EqualTo(connectionToServer));
Assert.That(clientComponent.targetRpcArg1, Is.EqualTo(1));
Assert.That(clientComponent.targetRpcArg2, Is.EqualTo("hello"));
});
Expand Down
2 changes: 1 addition & 1 deletion Assets/Tests/Runtime/Host/HostComponentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public IEnumerator ClientConnRpc() => UniTask.ToCoroutine(async () =>
// process spawn message from server
await AsyncUtil.WaitUntilWithTimeout(() => component.targetRpcArg1 != 0);
Assert.That(component.targetRpcPlayer, Is.SameAs(manager.Client.Player));
Assert.That(component.targetRpcPlayer, Is.EqualTo(manager.Client.Player));
Assert.That(component.targetRpcArg1, Is.EqualTo(1));
Assert.That(component.targetRpcArg2, Is.EqualTo("hello"));
});
Expand Down
7 changes: 0 additions & 7 deletions Assets/Tests/Runtime/Host/NetworkBehaviourTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,6 @@ public void TimeTest()
Assert.That(behaviour1.NetworkTime, Is.EqualTo(client.Time));
}

[Test]
public void HasIdentitysConnectionToServer()
{
(identity.ConnectionToServer, _) = PipedConnections();
Assert.That(component.ConnectionToServer, Is.EqualTo(identity.ConnectionToServer));
}

[Test]
public void HasIdentitysConnectionToClient()
{
Expand Down
12 changes: 6 additions & 6 deletions doc/Articles/Guides/MirrorMigration.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,23 +182,23 @@ These fields/properties have been renamed:
| Mirror | Mirage |
|:-------------------------------------:|:--------------------------------------------------------------------------------------:|
| `ClientScene.localPlayer` | [ClientObjectManager.LocalPlayer](xref:Mirage.ClientObjectManager.LocalPlayer) |
| `ClientScene.ready` | [NetworkClient.Connection.IsReady](xref:Mirage.NetworkPlayer.IsReady) |
| `ClientScene.ready` | [NetworkClient.Connection.IsReady](xref:Mirage.NetworkPlayer.IsReady) |
| `NetworkIdentity.assetId` | [NetworkIdentity.AssetId](xref:Mirage.NetworkIdentity.AssetId) |
| `NetworkIdentity.netId` | [NetworkIdentity.NetId](xref:Mirage.NetworkIdentity.NetId) |
| `NetworkIdentity.connectionToClient` | [NetworkIdentity.ConnectionToClient](xref:Mirage.NetworkIdentity.ConnectionToClient) |
| `NetworkBehaviour.isServer` | [NetworkBehaviour.IsServer](xref:Mirage.NetworkBehaviour.IsServer) |
| `NetworkBehaviour.connectionToClient` | [NetworkBehaviour.ConnectionToClient](xref:Mirage.NetworkBehaviour.ConnectionToClient) |
| `NetworkBehaviour.connectionToServer` | [NetworkBehaviour.ConnectionToServer](xref:Mirage.NetworkBehaviour.ConnectionToServer) |
| `NetworkBehaviour.connectionToServer` | Removed, use [Client.Player](xref:Mirage.NetworkClient.Player) instead |
| `NetworkBehaviour.hasAuthority` | [NetworkBehaviour.HasAuthority](xref:Mirage.NetworkBehaviour.HasAuthority) |
| `NetworkBehaviour.netIdentity` | [NetworkBehaviour.NetIdentity](xref:Mirage.NetworkBehaviour.NetIdentity) |
| `NetworkBehaviour.netId` | [NetworkBehaviour.NetId](xref:Mirage.NetworkBehaviour.NetId) |
| `NetworkBehaviour.isClientOnly` | [NetworkBehaviour.IsClientOnly](xref:Mirage.NetworkBehaviour.IsClientOnly) |
| `NetworkBehaviour.islocalPlayer` | [NetworkBehaviour.IsLocalPlayer](xref:Mirage.NetworkBehaviour.IsLocalPlayer) |
| `NetworkConnection.isReady` | [NetworkPlayer.IsReady](xref:Mirage.NetworkPlayer.IsReady) |
| `NetworkConnection.identity` | [NetworkPlayer.Identity](xref:Mirage.NetworkPlayer.Identity) |
| `NetworkConnection.isReady` | [NetworkPlayer.IsReady](xref:Mirage.NetworkPlayer.IsReady) |
| `NetworkConnection.identity` | [NetworkPlayer.Identity](xref:Mirage.NetworkPlayer.Identity) |
| `NetworkServer.active` | [NetworkServer.Active](xref:Mirage.NetworkServer.Active) |
| `NetworkServer.localConnection` | [NetworkServer.LocalPlayer](xref:Mirage.NetworkServer.LocalPlayer) |
| `NetworkClient.connection` | [NetworkClient.Player](xref:Mirage.NetworkClient.Player) |
| `NetworkServer.localConnection` | [NetworkServer.LocalPlayer](xref:Mirage.NetworkServer.LocalPlayer) |
| `NetworkClient.connection` | [NetworkClient.Player](xref:Mirage.NetworkClient.Player) |
| `NetworkTime.time` | [NetworkTime.Time](xref:Mirage.NetworkTime.Time) |

## Object Management
Expand Down

0 comments on commit ff95634

Please sign in to comment.