public void SendsData()
        {
            Message message = new Message
            {
                collection = new int[]
                {
                    3, 4, 5
                }
            };

            byte[] data = MessagePacker.Pack(message);

            Message unpacked = MessagePacker.Unpack <Message>(data);

            int[] unpackedCollection = unpacked.collection;

            Assert.IsNotNull(unpackedCollection);
            Assert.IsNotEmpty(unpackedCollection);
            Assert.That(unpackedCollection[0], Is.EqualTo(3));
            Assert.That(unpackedCollection[1], Is.EqualTo(4));
            Assert.That(unpackedCollection[2], Is.EqualTo(5));
        }
示例#2
0
        public void TestUnpackIdMismatch()
        {
            // Unpack<T> has a id != msgType case that throws a FormatException.
            // let's try to trigger it.

            var message = new SceneMessage
            {
                scenePath      = "Hello world",
                sceneOperation = SceneOperation.LoadAdditive
            };

            byte[] data = MessagePacker.Pack(message);

            // overwrite the id
            data[0] = 0x01;
            data[1] = 0x02;

            Assert.Throws <FormatException>(delegate
            {
                _ = MessagePacker.Unpack <SceneMessage>(data);
            });
        }
        public void SendsData()
        {
            Message message = new Message
            {
                collection = new Vector3[]
                {
                    new Vector3(1, 2, 3), new Vector3(4, 5, 6), new Vector3(7, 8, 9)
                }
            };

            byte[] data = MessagePacker.Pack(message);

            Message unpacked = MessagePacker.Unpack <Message>(data);

            Vector3[] unpackedCollection = unpacked.collection;

            Assert.IsNotNull(unpackedCollection);
            Assert.IsNotEmpty(unpackedCollection);
            Assert.That(unpackedCollection[0], Is.EqualTo(new Vector3(1, 2, 3)));
            Assert.That(unpackedCollection[1], Is.EqualTo(new Vector3(4, 5, 6)));
            Assert.That(unpackedCollection[2], Is.EqualTo(new Vector3(7, 8, 9)));
        }
        public void SendsData()
        {
            Message message = new Message
            {
                collection = new string[]
                {
                    "Some", "String", "Value"
                }
            };

            byte[] data = MessagePacker.Pack(message);

            Message unpacked = MessagePacker.Unpack <Message>(data);

            string[] unpackedCollection = unpacked.collection;

            Assert.IsNotNull(unpackedCollection);
            Assert.IsNotEmpty(unpackedCollection);
            Assert.That(unpackedCollection[0], Is.EqualTo("Some"));
            Assert.That(unpackedCollection[1], Is.EqualTo("String"));
            Assert.That(unpackedCollection[2], Is.EqualTo("Value"));
        }
示例#5
0
        public void RpcMessageTest()
        {
            // try setting value with constructor
            var message = new RpcMessage
            {
                netId          = 42,
                componentIndex = 4,
                functionIndex  = 3,
                payload        = new ArraySegment <byte>(new byte[] { 0x01, 0x02 })
            };
            var arr   = MessagePacker.Pack(message);
            var fresh = MessagePacker.Unpack <RpcMessage>(arr);

            Assert.That(fresh.netId, Is.EqualTo(message.netId));
            Assert.That(fresh.componentIndex, Is.EqualTo(message.componentIndex));
            Assert.That(fresh.functionIndex, Is.EqualTo(message.functionIndex));
            Assert.That(fresh.payload.Count, Is.EqualTo(message.payload.Count));
            for (var i = 0; i < fresh.payload.Count; ++i)
            {
                Assert.That(fresh.payload.Array[fresh.payload.Offset + i],
                            Is.EqualTo(message.payload.Array[message.payload.Offset + i]));
            }
        }
示例#6
0
        public void OnDataReceivedInvalidConnectionIdTest()
        {
            // add one custom message handler
            bool wasReceived = false;
            NetworkConnection connectionReceived = null;
            TestMessage1      messageReceived    = new TestMessage1();

            NetworkServer.RegisterHandler <TestMessage1>((conn, msg) =>
            {
                wasReceived        = true;
                connectionReceived = conn;
                messageReceived    = msg;
            }, false);

            // listen
            NetworkServer.Listen(1);
            Assert.That(NetworkServer.connections.Count, Is.EqualTo(0));

            // serialize a test message into an arraysegment
            TestMessage1 testMessage = new TestMessage1 {
                IntValue = 13, DoubleValue = 14, StringValue = "15"
            };
            NetworkWriter writer = new NetworkWriter();

            MessagePacker.Pack(testMessage, writer);
            ArraySegment <byte> segment = writer.ToArraySegment();

            // call transport.OnDataReceived with an invalid connectionId
            // an error log is expected.
            LogAssert.ignoreFailingMessages = true;
            Transport.activeTransport.OnServerDataReceived.Invoke(42, segment, 0);
            LogAssert.ignoreFailingMessages = false;

            // message handler should never be called
            Assert.That(wasReceived, Is.False);
            Assert.That(connectionReceived, Is.Null);
        }
示例#7
0
        public void SyncEventMessageTest()
        {
            // try setting value with constructor
            SyncEventMessage message = new SyncEventMessage
            {
                netId          = 42,
                componentIndex = 4,
                functionHash   = 0xABCDEF,
                payload        = new ArraySegment <byte>(new byte[] { 0x01, 0x02 })
            };

            byte[]           arr   = MessagePacker.Pack(message);
            SyncEventMessage fresh = MessagePacker.Unpack <SyncEventMessage>(arr);

            Assert.That(fresh.netId, Is.EqualTo(message.netId));
            Assert.That(fresh.componentIndex, Is.EqualTo(message.componentIndex));
            Assert.That(fresh.functionHash, Is.EqualTo(message.functionHash));
            Assert.That(fresh.payload.Count, Is.EqualTo(message.payload.Count));
            for (int i = 0; i < fresh.payload.Count; ++i)
            {
                Assert.That(fresh.payload.Array[fresh.payload.Offset + i],
                            Is.EqualTo(message.payload.Array[message.payload.Offset + i]));
            }
        }
        public void TestUnpackMessageNonGeneric()
        {
            // try a regular message
            SceneMessage message = new SceneMessage()
            {
                sceneName      = "Hello world",
                sceneOperation = SceneOperation.LoadAdditive
            };

            byte[]        data   = MessagePacker.Pack(message);
            NetworkReader reader = new NetworkReader(data);

            bool result = MessagePacker.UnpackMessage(reader, out int msgType);

            Assert.That(result, Is.EqualTo(true));
            Assert.That(msgType, Is.EqualTo(BitConverter.ToUInt16(data, 0)));

            // try an invalid message
            NetworkReader reader2 = new NetworkReader(new byte[0]);
            bool          result2 = MessagePacker.UnpackMessage(reader2, out int msgType2);

            Assert.That(result2, Is.EqualTo(false));
            Assert.That(msgType2, Is.EqualTo(0));
        }
示例#9
0
        public void RegisterUnregisterClearHandlerTest()
        {
            // message handlers that are needed for the test
            NetworkServer.RegisterHandler <ConnectMessage>((conn, msg) => { }, false);
            NetworkServer.RegisterHandler <DisconnectMessage>((conn, msg) => { }, false);
            NetworkServer.RegisterHandler <ErrorMessage>((conn, msg) => { }, false);


            // RegisterHandler(conn, msg) variant
            int variant1Called = 0;

            NetworkServer.RegisterHandler <TestMessage>((conn, msg) => { ++variant1Called; }, false);

            // RegisterHandler(msg) variant
            int variant2Called = 0;

            NetworkServer.RegisterHandler <WovenTestMessage>(msg => { ++variant2Called; }, false);

            // listen
            NetworkServer.Listen(1);
            Assert.That(NetworkServer.connections.Count, Is.EqualTo(0));

            // add a connection
            NetworkConnectionToClient connection = new NetworkConnectionToClient(42);

            NetworkServer.AddConnection(connection);
            Assert.That(NetworkServer.connections.Count, Is.EqualTo(1));

            // serialize first message, send it to server, check if it was handled
            NetworkWriter writer = new NetworkWriter();

            MessagePacker.Pack(new TestMessage(), writer);
            Transport.activeTransport.OnServerDataReceived.Invoke(42, writer.ToArraySegment(), 0);
            Assert.That(variant1Called, Is.EqualTo(1));

            // serialize second message, send it to server, check if it was handled
            writer = new NetworkWriter();
            MessagePacker.Pack(new WovenTestMessage(), writer);
            Transport.activeTransport.OnServerDataReceived.Invoke(42, writer.ToArraySegment(), 0);
            Assert.That(variant2Called, Is.EqualTo(1));

            // unregister first handler, send, should fail
            NetworkServer.UnregisterHandler <TestMessage>();
            writer = new NetworkWriter();
            MessagePacker.Pack(new TestMessage(), writer);
            // log error messages are expected
            LogAssert.ignoreFailingMessages = true;
            Transport.activeTransport.OnServerDataReceived.Invoke(42, writer.ToArraySegment(), 0);
            LogAssert.ignoreFailingMessages = false;
            // still 1, not 2
            Assert.That(variant1Called, Is.EqualTo(1));

            // unregister second handler via ClearHandlers to test that one too. send, should fail
            NetworkServer.ClearHandlers();
            // (only add this one to avoid disconnect error)
            NetworkServer.RegisterHandler <DisconnectMessage>((conn, msg) => { }, false);
            writer = new NetworkWriter();
            MessagePacker.Pack(new TestMessage(), writer);
            // log error messages are expected
            LogAssert.ignoreFailingMessages = true;
            Transport.activeTransport.OnServerDataReceived.Invoke(42, writer.ToArraySegment(), 0);
            LogAssert.ignoreFailingMessages = false;
            // still 1, not 2
            Assert.That(variant2Called, Is.EqualTo(1));

            // clean up
            NetworkServer.Shutdown();
        }
示例#10
0
        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);
        }
示例#11
0
 void Send(long sid, MsgBase msg)
 {
     byte[] data = MessagePacker.Pack(msg);
     server.Send(sid, data);
 }