public void ReadyMessageSetsClientReadyTest() { // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // add connection ULocalConnectionToClient connection = new ULocalConnectionToClient(); connection.connectionToServer = new ULocalConnectionToServer(); 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(); MessagePacker.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); // shutdown NetworkServer.Shutdown(); }
public void DisconnectAllTest() { // message handlers NetworkServer.RegisterHandler <ConnectMessage>((conn, msg) => { }, false); NetworkServer.RegisterHandler <DisconnectMessage>((conn, msg) => { }, false); NetworkServer.RegisterHandler <ErrorMessage>((conn, msg) => { }, false); // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // set local connection ULocalConnectionToClient localConnection = new ULocalConnectionToClient(); NetworkServer.SetLocalConnection(localConnection); Assert.That(NetworkServer.localConnection, Is.EqualTo(localConnection)); // add connection NetworkConnectionToClient conn42 = new NetworkConnectionToClient(42); 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); // shutdown NetworkServer.Shutdown(); }
public void SendToAllTest() { // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // add connection ULocalConnectionToClient connection = new ULocalConnectionToClient(); connection.connectionToServer = new ULocalConnectionToServer(); // set a client handler int called = 0; connection.connectionToServer.SetHandlers(new Dictionary <int, NetworkMessageDelegate>() { { MessagePacker.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 ULocalConnectionToServer(); connectionToClient = new ULocalConnectionToClient(); connectionToClient.connectionToServer = connectionToServer; connectionToServer.connectionToClient = connectionToClient; }
public void ShowForConnection() { // message handlers NetworkServer.RegisterHandler <ConnectMessage>((conn, msg) => { }, false); NetworkServer.RegisterHandler <DisconnectMessage>((conn, msg) => { }, false); NetworkServer.RegisterHandler <ErrorMessage>((conn, msg) => { }, false); // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // add connection ULocalConnectionToClient connection = new ULocalConnectionToClient(); // required for ShowForConnection connection.isReady = true; connection.connectionToServer = new ULocalConnectionToServer(); // set a client handler int called = 0; connection.connectionToServer.SetHandlers(new Dictionary <int, NetworkMessageDelegate>() { { MessagePacker.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 SetClientReadyAndNotReadyTest() { ULocalConnectionToClient connection = new ULocalConnectionToClient(); connection.connectionToServer = new ULocalConnectionToServer(); 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 SetLocalConnectionTest() { // listen NetworkServer.Listen(1); // set local connection ULocalConnectionToClient localConnection = new ULocalConnectionToClient(); NetworkServer.SetLocalConnection(localConnection); Assert.That(NetworkServer.localConnection, Is.EqualTo(localConnection)); // shutdown NetworkServer.Shutdown(); }
public void SendToClientOfPlayer() { // message handlers NetworkServer.RegisterHandler <ConnectMessage>((conn, msg) => { }, false); NetworkServer.RegisterHandler <DisconnectMessage>((conn, msg) => { }, false); NetworkServer.RegisterHandler <ErrorMessage>((conn, msg) => { }, false); // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // add connection ULocalConnectionToClient connection = new ULocalConnectionToClient(); connection.connectionToServer = new ULocalConnectionToServer(); // set a client handler int called = 0; connection.connectionToServer.SetHandlers(new Dictionary <int, NetworkMessageDelegate>() { { MessagePacker.GetId <TestMessage>(), ((conn, reader, channelId) => ++ called) } }); NetworkServer.AddConnection(connection); // create a message TestMessage message = new TestMessage { 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 NetworkServer.SendToClientOfPlayer(identity, 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 void NoConnectionsTest_WithHostOnly() { ULocalConnectionToServer connectionToServer = new ULocalConnectionToServer(); ULocalConnectionToClient connectionToClient = new ULocalConnectionToClient(); connectionToServer.connectionToClient = connectionToClient; connectionToClient.connectionToServer = connectionToServer; NetworkServer.SetLocalConnection(connectionToClient); NetworkServer.connections.Add(0, connectionToClient); Assert.That(NetworkServer.NoConnections(), Is.True); Assert.That(NetworkServer.connections.Count, Is.EqualTo(1)); NetworkServer.connections.Clear(); NetworkServer.RemoveLocalConnection(); }
public void RemoveLocalConnectionTest() { // listen NetworkServer.Listen(1); // set local connection ULocalConnectionToClient localConnection = new ULocalConnectionToClient(); NetworkServer.SetLocalConnection(localConnection); Assert.That(NetworkServer.localConnection, Is.EqualTo(localConnection)); // local connection needs a server connection because // RemoveLocalConnection calls localConnection.Disconnect localConnection.connectionToServer = new ULocalConnectionToServer(); // remove local connection NetworkServer.RemoveLocalConnection(); Assert.That(NetworkServer.localConnection, Is.Null); }
public void SendToAllTest() { // message handlers NetworkServer.RegisterHandler <ConnectMessage>((conn, msg) => {}, false); NetworkServer.RegisterHandler <DisconnectMessage>((conn, msg) => {}, false); NetworkServer.RegisterHandler <ErrorMessage>((conn, msg) => {}, false); // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // add connection ULocalConnectionToClient connection = new ULocalConnectionToClient(); connection.connectionToServer = new ULocalConnectionToServer(); // set a client handler int called = 0; connection.connectionToServer.SetHandlers(new Dictionary <int, NetworkMessageDelegate>() { { MessagePacker.GetId <TestMessage>(), (msg => ++ called) } }); NetworkServer.AddConnection(connection); // create a message TestMessage message = new TestMessage { IntValue = 1, DoubleValue = 2, StringValue = "3" }; // send it to all bool result = NetworkServer.SendToAll(message); Assert.That(result, Is.True); // 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(); }
public void SetLocalConnectionTest() { // listen NetworkServer.Listen(1); // set local connection ULocalConnectionToClient localConnection = new ULocalConnectionToClient(); NetworkServer.SetLocalConnection(localConnection); Assert.That(NetworkServer.localConnection, Is.EqualTo(localConnection)); // try to overwrite it, which should not work // (it will show an error message, which is expected) LogAssert.ignoreFailingMessages = true; ULocalConnectionToClient overwrite = new ULocalConnectionToClient(); NetworkServer.SetLocalConnection(overwrite); Assert.That(NetworkServer.localConnection, Is.EqualTo(localConnection)); LogAssert.ignoreFailingMessages = false; }
public void HideForConnection() { // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // add connection ULocalConnectionToClient connection = new ULocalConnectionToClient(); // required for ShowForConnection connection.isReady = true; connection.connectionToServer = new ULocalConnectionToServer(); // set a client handler int called = 0; connection.connectionToServer.SetHandlers(new Dictionary <int, NetworkMessageDelegate>() { { MessagePacker.GetId <ObjectHideMessage>(), ((conn, reader, channelId) => ++ called) } }); NetworkServer.AddConnection(connection); // create a gameobject and networkidentity NetworkIdentity identity = new GameObject().AddComponent <NetworkIdentity>(); identity.connectionToClient = connection; // call HideForConnection NetworkServer.HideForConnection(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)); // 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 SetAllClientsNotReadyTest() { // add first ready client ULocalConnectionToClient first = new ULocalConnectionToClient(); first.connectionToServer = new ULocalConnectionToServer(); first.isReady = true; NetworkServer.connections[42] = first; // add second ready client ULocalConnectionToClient second = new ULocalConnectionToClient(); second.connectionToServer = new ULocalConnectionToServer(); 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 SetClientOwner() { // create a networkidentity GameObject gameObject = new GameObject(); NetworkIdentity identity = gameObject.AddComponent <NetworkIdentity>(); // SetClientOwner ULocalConnectionToClient original = new ULocalConnectionToClient(); identity.SetClientOwner(original); Assert.That(identity.connectionToClient, Is.EqualTo(original)); // setting it when it's already set shouldn't overwrite the original ULocalConnectionToClient overwrite = new ULocalConnectionToClient(); LogAssert.ignoreFailingMessages = true; // will log a warning identity.SetClientOwner(overwrite); Assert.That(identity.connectionToClient, Is.EqualTo(original)); LogAssert.ignoreFailingMessages = false; // clean up GameObject.DestroyImmediate(gameObject); }
public void DisconnectAllTest() { // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // set local connection ULocalConnectionToClient localConnection = new ULocalConnectionToClient(); 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 ULocalConnectionToClient connection = new ULocalConnectionToClient(); connection.connectionToServer = new ULocalConnectionToServer(); 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 NetworkBehaviour.RegisterCommandDelegate(typeof(CommandTestNetworkBehaviour), nameof(CommandTestNetworkBehaviour.CommandGenerated), CommandTestNetworkBehaviour.CommandGenerated); // 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 = NetworkBehaviour.GetMethodHash(typeof(CommandTestNetworkBehaviour), nameof(CommandTestNetworkBehaviour.CommandGenerated)), netId = identity.netId, payload = new ArraySegment <byte>(new byte[0]) }; NetworkWriter writer = new NetworkWriter(); MessagePacker.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(); MessagePacker.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 ULocalConnectionToClient(); 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 MessagePacker.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 NetworkBehaviour.ClearDelegates(); NetworkIdentity.spawned.Clear(); NetworkBehaviour.ClearDelegates(); 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); }