// helper function to create local connection pair protected void CreateLocalConnectionPair(out LocalConnectionToClient connectionToClient, out LocalConnectionToServer connectionToServer) { connectionToClient = new LocalConnectionToClient(); connectionToServer = new LocalConnectionToServer(); connectionToClient.connectionToServer = connectionToServer; connectionToServer.connectionToClient = connectionToClient; }
public void ReadyMessageSetsClientReadyTest() { // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // add connection LocalConnectionToClient connection = new LocalConnectionToClient(); connection.connectionToServer = new LocalConnectionToServer(); NetworkServer.AddConnection(connection); // set as authenticated, otherwise readymessage is rejected connection.isAuthenticated = true; // serialize a ready message into an arraysegment ReadyMessage message = new ReadyMessage(); NetworkWriter writer = new NetworkWriter(); MessagePacking.Pack(message, writer); ArraySegment <byte> segment = writer.ToArraySegment(); // call transport.OnDataReceived with the message // -> calls NetworkServer.OnClientReadyMessage // -> calls SetClientReady(conn) Transport.activeTransport.OnServerDataReceived.Invoke(0, segment, 0); // ready? Assert.That(connection.isReady, Is.True); }
public void SendToAllTest() { // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // add connection LocalConnectionToClient connection = new LocalConnectionToClient(); connection.connectionToServer = new LocalConnectionToServer(); // set a client handler int called = 0; connection.connectionToServer.SetHandlers(new Dictionary <ushort, NetworkMessageDelegate>() { { MessagePacking.GetId <TestMessage1>(), ((conn, reader, channelId) => ++ called) } }); NetworkServer.AddConnection(connection); // create a message TestMessage1 message = new TestMessage1 { IntValue = 1, DoubleValue = 2, StringValue = "3" }; // send it to all NetworkServer.SendToAll(message); // update local connection once so that the incoming queue is processed connection.connectionToServer.Update(); // was it send to and handled by the connection? Assert.That(called, Is.EqualTo(1)); }
public void SetUpConnections() { connectionToServer = new LocalConnectionToServer(); connectionToClient = new LocalConnectionToClient(); connectionToClient.connectionToServer = connectionToServer; connectionToServer.connectionToClient = connectionToClient; }
public void SetLocalConnection() { // listen NetworkServer.Listen(1); // set local connection LocalConnectionToClient localConnection = new LocalConnectionToClient(); NetworkServer.SetLocalConnection(localConnection); Assert.That(NetworkServer.localConnection, Is.EqualTo(localConnection)); }
public void SetUp() { connectionToServer = new LocalConnectionToServer(); connectionToClient = new LocalConnectionToClient(); connectionToClient.connectionToServer = connectionToServer; connectionToServer.connectionToClient = connectionToClient; // set up server/client connections so message handling works NetworkClient.connection = connectionToServer; NetworkServer.connections[connectionToClient.connectionId] = connectionToClient; }
public void SetClientReadyAndNotReadyTest() { LocalConnectionToClient connection = new LocalConnectionToClient(); connection.connectionToServer = new LocalConnectionToServer(); Assert.That(connection.isReady, Is.False); NetworkServer.SetClientReady(connection); Assert.That(connection.isReady, Is.True); NetworkServer.SetClientNotReady(connection); Assert.That(connection.isReady, Is.False); }
public void DisconnectAllTest_LocalConnection() { // listen NetworkServer.Listen(1); // set local connection LocalConnectionToClient localConnection = new LocalConnectionToClient(); NetworkServer.SetLocalConnection(localConnection); // disconnect all connections should remove local connection NetworkServer.DisconnectAll(); Assert.That(NetworkServer.localConnection, Is.Null); }
public IEnumerator SetClientReadyAndNotReadyCheckObserversOfDontDestroyOnLoadNetworkIdentityTest() { // add connection LocalConnectionToClient connection = new LocalConnectionToClient(); connection.connectionToServer = new LocalConnectionToServer(); NetworkServer.AddConnection(connection); Assert.That(connection.isReady, Is.False); connection.isAuthenticated = true; // set client ready NetworkServer.SetClientReady(connection); Assert.That(connection.isReady, Is.True); // spawn network identity on the server // note: we need runtime test instead of edit mode test // because otherwise DontDestroyOnLoad won't work GameObject gameObject = new GameObject(); NetworkIdentity networkIdentity = gameObject.AddComponent <NetworkIdentity>(); NetworkServer.Spawn(gameObject); UnityEngine.Object.DontDestroyOnLoad(gameObject); // allow 1 frame to spawn object yield return(null); // connection should now automatically be observing the spawned identity Assert.That(connection.observing.Contains(networkIdentity), Is.True); Assert.That(networkIdentity.observers.ContainsKey(connection.connectionId), Is.True); Assert.That(networkIdentity.observers[connection.connectionId], Is.EqualTo(connection)); // setting client to not ready NetworkServer.SetClientNotReady(connection); Assert.That(connection.isReady, Is.False); // client that is not ready should still be observing network identity, since it's "DontDestroyOnLoad" Assert.That(connection.observing.Contains(networkIdentity), Is.True); Assert.That(networkIdentity.observers.ContainsKey(connection.connectionId), Is.True); Assert.That(networkIdentity.observers[connection.connectionId], Is.EqualTo(connection)); // clean up NetworkIdentity.spawned.Clear(); NetworkServer.Shutdown(); // destroy the test gameobject AFTER server was stopped. // otherwise isServer is true in OnDestroy, which means it would try // to call Destroy(go). but we need to use DestroyImmediate in // Editor GameObject.DestroyImmediate(gameObject); }
public void ShowForConnection() { // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // setup connections LocalConnectionToClient connectionToClient = new LocalConnectionToClient(); LocalConnectionToServer connectionToServer = new LocalConnectionToServer(); connectionToClient.connectionToServer = new LocalConnectionToServer(); // setup NetworkServer/Client connections so messages are handled NetworkClient.connection = connectionToServer; NetworkServer.connections[connectionToClient.connectionId] = connectionToClient; // required for ShowForConnection connectionToClient.isReady = true; connectionToClient.connectionToServer = new LocalConnectionToServer(); // set a client handler int called = 0; void Handler(SpawnMessage _) => ++ called; NetworkClient.RegisterHandler <SpawnMessage>(Handler, false); NetworkServer.AddConnection(connectionToClient); // create a gameobject and networkidentity and some unique values CreateNetworked(out GameObject _, out NetworkIdentity identity); identity.connectionToClient = connectionToClient; // call ShowForConnection NetworkServer.ShowForConnection(identity, connectionToClient); // update local connection once so that the incoming queue is processed connectionToClient.connectionToServer.Update(); // was it sent to and handled by the connection? Assert.That(called, Is.EqualTo(1)); // it shouldn't send it if connection isn't ready, so try that too connectionToClient.isReady = false; NetworkServer.ShowForConnection(identity, connectionToClient); connectionToClient.connectionToServer.Update(); // not 2 but 1 like before? Assert.That(called, Is.EqualTo(1)); }
public void ShowForConnection() { // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // add connection LocalConnectionToClient connection = new LocalConnectionToClient(); // required for ShowForConnection connection.isReady = true; connection.connectionToServer = new LocalConnectionToServer(); // set a client handler int called = 0; connection.connectionToServer.SetHandlers(new Dictionary <ushort, NetworkMessageDelegate>() { { MessagePacking.GetId <SpawnMessage>(), ((conn, reader, channelId) => ++ called) } }); NetworkServer.AddConnection(connection); // create a gameobject and networkidentity and some unique values NetworkIdentity identity = new GameObject().AddComponent <NetworkIdentity>(); identity.connectionToClient = connection; // call ShowForConnection NetworkServer.ShowForConnection(identity, connection); // update local connection once so that the incoming queue is processed connection.connectionToServer.Update(); // was it sent to and handled by the connection? Assert.That(called, Is.EqualTo(1)); // it shouldn't send it if connection isn't ready, so try that too connection.isReady = false; NetworkServer.ShowForConnection(identity, connection); connection.connectionToServer.Update(); // not 2 but 1 like before? Assert.That(called, Is.EqualTo(1)); // clean up NetworkServer.Shutdown(); // destroy GO after shutdown, otherwise isServer is true in OnDestroy and it tries to call // GameObject.Destroy (but we need DestroyImmediate in Editor) GameObject.DestroyImmediate(identity.gameObject); }
public void NoExternalConnectionsTest_WithHostOnly() { LocalConnectionToServer connectionToServer = new LocalConnectionToServer(); LocalConnectionToClient connectionToClient = new LocalConnectionToClient(); connectionToServer.connectionToClient = connectionToClient; connectionToClient.connectionToServer = connectionToServer; NetworkServer.SetLocalConnection(connectionToClient); NetworkServer.connections.Add(0, connectionToClient); Assert.That(NetworkServer.NoExternalConnections(), Is.True); Assert.That(NetworkServer.connections.Count, Is.EqualTo(1)); NetworkServer.connections.Clear(); NetworkServer.RemoveLocalConnection(); }
public void SetLocalConnection_PreventsOverwrite() { // listen NetworkServer.Listen(1); // set local connection LocalConnectionToClient localConnection = new LocalConnectionToClient(); NetworkServer.SetLocalConnection(localConnection); // try to overwrite it, which should not work // (it will show an error message, which is expected) LogAssert.ignoreFailingMessages = true; NetworkServer.SetLocalConnection(new LocalConnectionToClient()); Assert.That(NetworkServer.localConnection, Is.EqualTo(localConnection)); LogAssert.ignoreFailingMessages = false; }
public void SendToClientOfPlayer() { // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // setup connections LocalConnectionToClient connectionToClient = new LocalConnectionToClient(); LocalConnectionToServer connectionToServer = new LocalConnectionToServer(); connectionToClient.connectionToServer = new LocalConnectionToServer(); // setup NetworkServer/Client connections so messages are handled NetworkClient.connection = connectionToServer; NetworkServer.connections[connectionToClient.connectionId] = connectionToClient; // set a client handler int called = 0; void Handler(TestMessage1 _) => ++ called; NetworkClient.RegisterHandler <TestMessage1>(Handler, false); NetworkServer.AddConnection(connectionToClient); // create a message TestMessage1 message = new TestMessage1 { IntValue = 1, DoubleValue = 2, StringValue = "3" }; // create a gameobject and networkidentity CreateNetworked(out GameObject _, out NetworkIdentity identity); identity.connectionToClient = connectionToClient; // send it to that player identity.connectionToClient.Send(message); // update local connection once so that the incoming queue is processed connectionToClient.connectionToServer.Update(); // was it send to and handled by the connection? Assert.That(called, Is.EqualTo(1)); // clean up NetworkServer.Shutdown(); }
public void NoConnectionsTest_WithHostAndConnection() { LocalConnectionToServer connectionToServer = new LocalConnectionToServer(); LocalConnectionToClient connectionToClient = new LocalConnectionToClient(); connectionToServer.connectionToClient = connectionToClient; connectionToClient.connectionToServer = connectionToServer; NetworkServer.SetLocalConnection(connectionToClient); NetworkServer.connections.Add(0, connectionToClient); NetworkServer.connections.Add(1, null); Assert.That(NetworkServer.NoConnections(), Is.False); Assert.That(NetworkServer.connections.Count, Is.EqualTo(2)); NetworkServer.connections.Clear(); NetworkServer.RemoveLocalConnection(); }
public void SendToClientOfPlayer() { // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // add connection LocalConnectionToClient connection = new LocalConnectionToClient(); connection.connectionToServer = new LocalConnectionToServer(); // set a client handler int called = 0; connection.connectionToServer.SetHandlers(new Dictionary <ushort, NetworkMessageDelegate>() { { MessagePacking.GetId <TestMessage1>(), ((conn, reader, channelId) => ++ called) } }); NetworkServer.AddConnection(connection); // create a message TestMessage1 message = new TestMessage1 { IntValue = 1, DoubleValue = 2, StringValue = "3" }; // create a gameobject and networkidentity NetworkIdentity identity = new GameObject().AddComponent <NetworkIdentity>(); identity.connectionToClient = connection; // send it to that player identity.connectionToClient.Send(message); // update local connection once so that the incoming queue is processed connection.connectionToServer.Update(); // was it send to and handled by the connection? Assert.That(called, Is.EqualTo(1)); // clean up NetworkServer.Shutdown(); // destroy GO after shutdown, otherwise isServer is true in OnDestroy and it tries to call // GameObject.Destroy (but we need DestroyImmediate in Editor) GameObject.DestroyImmediate(identity.gameObject); }
public IEnumerator SetClientReadyAndNotReadyCheckObserversOfNetworkIdentityTest() { // add connection LocalConnectionToClient connection = new LocalConnectionToClient(); connection.connectionToServer = new LocalConnectionToServer(); NetworkServer.AddConnection(connection); Assert.That(connection.isReady, Is.False); connection.isAuthenticated = true; // set client ready NetworkServer.SetClientReady(connection); Assert.That(connection.isReady, Is.True); // spawn network identity on the server GameObject gameObject = new GameObject(); NetworkIdentity networkIdentity = gameObject.AddComponent <NetworkIdentity>(); NetworkServer.Spawn(gameObject); // allow 1 frame to spawn object yield return(null); // connection should now automatically be observing the spawned identity Assert.That(connection.observing.Contains(networkIdentity), Is.True); Assert.That(networkIdentity.observers.ContainsKey(connection.connectionId), Is.True); Assert.That(networkIdentity.observers[connection.connectionId], Is.EqualTo(connection)); // setting client to not ready // note: we need runtime test instead of edit mode test // because otherwise gameobject.scene.buildIndex is not valid, // and buildIndex is needed to check if observer should be removed NetworkServer.SetClientNotReady(connection); Assert.That(connection.isReady, Is.False); // client that is not ready should not observe the spawned identity Assert.That(connection.observing.Contains(networkIdentity), Is.False); Assert.That(networkIdentity.observers.ContainsKey(connection.connectionId), Is.False); // clean up NetworkServer.Destroy(gameObject); NetworkIdentity.spawned.Clear(); NetworkServer.Shutdown(); }
public void RemoveLocalConnectionTest() { // listen NetworkServer.Listen(1); // set local connection LocalConnectionToClient localConnection = new LocalConnectionToClient(); NetworkServer.SetLocalConnection(localConnection); Assert.That(NetworkServer.localConnection, Is.EqualTo(localConnection)); // local connection needs a server connection because // RemoveLocalConnection calls localConnection.Disconnect localConnection.connectionToServer = new LocalConnectionToServer(); // remove local connection NetworkServer.RemoveLocalConnection(); Assert.That(NetworkServer.localConnection, Is.Null); }
public void SetClientOwner() { CreateNetworked(out GameObject _, out NetworkIdentity identity); // SetClientOwner LocalConnectionToClient original = new LocalConnectionToClient(); identity.SetClientOwner(original); Assert.That(identity.connectionToClient, Is.EqualTo(original)); // setting it when it's already set shouldn't overwrite the original LocalConnectionToClient overwrite = new LocalConnectionToClient(); // will log a warning LogAssert.ignoreFailingMessages = true; identity.SetClientOwner(overwrite); Assert.That(identity.connectionToClient, Is.EqualTo(original)); LogAssert.ignoreFailingMessages = false; }
public void SetAllClientsNotReadyTest() { // add first ready client LocalConnectionToClient first = new LocalConnectionToClient(); first.connectionToServer = new LocalConnectionToServer(); first.isReady = true; NetworkServer.connections[42] = first; // add second ready client LocalConnectionToClient second = new LocalConnectionToClient(); second.connectionToServer = new LocalConnectionToServer(); second.isReady = true; NetworkServer.connections[43] = second; // set all not ready NetworkServer.SetAllClientsNotReady(); Assert.That(first.isReady, Is.False); Assert.That(second.isReady, Is.False); }
public void DisconnectAllTest() { // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // set local connection LocalConnectionToClient localConnection = new LocalConnectionToClient(); NetworkServer.SetLocalConnection(localConnection); Assert.That(NetworkServer.localConnection, Is.EqualTo(localConnection)); // add connection NetworkConnectionToClient conn42 = new NetworkConnectionToClient(42, false, 0); NetworkServer.AddConnection(conn42); Assert.That(NetworkServer.connections.Count, Is.EqualTo(1)); // disconnect all connections and local connection NetworkServer.DisconnectAll(); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); Assert.That(NetworkServer.localConnection, Is.Null); }
public void CommandMessageCallsCommandTest() { // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // add connection LocalConnectionToClient connection = new LocalConnectionToClient(); connection.connectionToServer = new LocalConnectionToServer(); NetworkServer.AddConnection(connection); // set as authenticated, otherwise removeplayer is rejected connection.isAuthenticated = true; // add an identity with two networkbehaviour components GameObject go = new GameObject(); NetworkIdentity identity = go.AddComponent <NetworkIdentity>(); identity.netId = 42; // for authority check identity.connectionToClient = connection; CommandTestNetworkBehaviour comp0 = go.AddComponent <CommandTestNetworkBehaviour>(); Assert.That(comp0.called, Is.EqualTo(0)); CommandTestNetworkBehaviour comp1 = go.AddComponent <CommandTestNetworkBehaviour>(); Assert.That(comp1.called, Is.EqualTo(0)); connection.identity = identity; // register the command delegate, otherwise it's not found int registeredHash = RemoteCallHelper.RegisterDelegate(typeof(CommandTestNetworkBehaviour), nameof(CommandTestNetworkBehaviour.CommandGenerated), MirrorInvokeType.Command, CommandTestNetworkBehaviour.CommandGenerated, true); // identity needs to be in spawned dict, otherwise command handler // won't find it NetworkIdentity.spawned[identity.netId] = identity; // serialize a removeplayer message into an arraysegment CommandMessage message = new CommandMessage { componentIndex = 0, functionHash = RemoteCallHelper.GetMethodHash(typeof(CommandTestNetworkBehaviour), nameof(CommandTestNetworkBehaviour.CommandGenerated)), netId = identity.netId, payload = new ArraySegment <byte>(new byte[0]) }; NetworkWriter writer = new NetworkWriter(); MessagePacking.Pack(message, writer); ArraySegment <byte> segment = writer.ToArraySegment(); // call transport.OnDataReceived with the message // -> calls NetworkServer.OnRemovePlayerMessage // -> destroys conn.identity and sets it to null Transport.activeTransport.OnServerDataReceived.Invoke(0, segment, 0); // was the command called in the first component, not in the second one? Assert.That(comp0.called, Is.EqualTo(1)); Assert.That(comp1.called, Is.EqualTo(0)); // send another command for the second component comp0.called = 0; message.componentIndex = 1; writer = new NetworkWriter(); MessagePacking.Pack(message, writer); segment = writer.ToArraySegment(); Transport.activeTransport.OnServerDataReceived.Invoke(0, segment, 0); // was the command called in the second component, not in the first one? Assert.That(comp0.called, Is.EqualTo(0)); Assert.That(comp1.called, Is.EqualTo(1)); // sending a command without authority should fail // (= if connectionToClient is not what we received the data on) // set wrong authority identity.connectionToClient = new LocalConnectionToClient(); comp0.called = 0; comp1.called = 0; Transport.activeTransport.OnServerDataReceived.Invoke(0, segment, 0); Assert.That(comp0.called, Is.EqualTo(0)); Assert.That(comp1.called, Is.EqualTo(0)); // restore authority identity.connectionToClient = connection; // sending a component with wrong netId should fail // wrong netid message.netId += 1; writer = new NetworkWriter(); // need to serialize the message again with wrong netid MessagePacking.Pack(message, writer); ArraySegment <byte> segmentWrongNetId = writer.ToArraySegment(); comp0.called = 0; comp1.called = 0; Transport.activeTransport.OnServerDataReceived.Invoke(0, segmentWrongNetId, 0); Assert.That(comp0.called, Is.EqualTo(0)); Assert.That(comp1.called, Is.EqualTo(0)); // clean up NetworkIdentity.spawned.Clear(); RemoteCallHelper.RemoveDelegate(registeredHash); NetworkServer.Shutdown(); // destroy the test gameobject AFTER server was stopped. // otherwise isServer is true in OnDestroy, which means it would try // to call Destroy(go). but we need to use DestroyImmediate in // Editor GameObject.DestroyImmediate(go); }