NetworkServerTest: [Command] test simple version

This commit is contained in:
vis2k 2021-06-16 12:11:27 +08:00
parent e49a5392b3
commit 453e94a35d

View File

@ -411,6 +411,53 @@ public void ReadyMessageSetsClientReady()
Assert.That(connectionToClient.isReady, Is.True);
}
// simply send a [Command] from client to server
[Test]
public void SendCommand()
{
// listen & connect
NetworkServer.Listen(1);
ConnectClientBlocking();
// set authenticated (required for message)
NetworkConnectionToClient connectionToClient = NetworkServer.connections.Values.First();
connectionToClient.isAuthenticated = true;
// add an identity with two networkbehaviour components
CreateNetworked(out GameObject _, out NetworkIdentity identity, out CommandTestNetworkBehaviour comp);
identity.netId = 42;
// for authority check
identity.connectionToClient = connectionToClient;
connectionToClient.identity = identity;
// identity needs to be in spawned dict, otherwise command handler
// won't find it
NetworkIdentity.spawned[identity.netId] = identity;
// register the command delegate, otherwise it's not found
int registeredHash = RemoteCallHelper.RegisterDelegate(
typeof(CommandTestNetworkBehaviour),
nameof(CommandTestNetworkBehaviour.CommandGenerated),
MirrorInvokeType.Command,
CommandTestNetworkBehaviour.CommandGenerated,
true);
// send message to server
CommandMessage message = new CommandMessage
{
componentIndex = 0,
functionHash = RemoteCallHelper.GetMethodHash(typeof(CommandTestNetworkBehaviour), nameof(CommandTestNetworkBehaviour.CommandGenerated)),
netId = identity.netId,
payload = new ArraySegment<byte>(new byte[0])
};
NetworkClient.Send(message);
ProcessMessages();
Assert.That(comp.called, Is.EqualTo(1));
// clean up
RemoteCallHelper.RemoveDelegate(registeredHash);
}
// this runs a command all the way:
// byte[]->transport->server->identity->component
[Test]