Ejemplo n.º 1
0
        public void SendsData()
        {
            Message message = new Message
            {
                collection = new FloatStringStruct[]
                {
                    new FloatStringStruct {
                        value = 3, anotherValue = "Some"
                    }, new FloatStringStruct {
                        value = 4, anotherValue = "String"
                    }, new FloatStringStruct {
                        value = 5, anotherValue = "Values"
                    }
                }
            };

            byte[] data = MessagePackerTest.PackToByteArray(message);

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

            FloatStringStruct[] unpackedCollection = unpacked.collection;

            Assert.IsNotNull(unpackedCollection);
            Assert.IsNotEmpty(unpackedCollection);
            Assert.That(unpackedCollection[0], Is.EqualTo(new FloatStringStruct {
                value = 3, anotherValue = "Some"
            }));
            Assert.That(unpackedCollection[1], Is.EqualTo(new FloatStringStruct {
                value = 4, anotherValue = "String"
            }));
            Assert.That(unpackedCollection[2], Is.EqualTo(new FloatStringStruct {
                value = 5, anotherValue = "Values"
            }));
        }
Ejemplo n.º 2
0
        public void Roundtrip()
        {
            byte[]      arr = MessagePacker.Pack(new TestMessage(1, "2", 3.3));
            TestMessage t   = MessagePacker.Unpack <TestMessage>(arr);

            Assert.AreEqual(1, t.IntValue);
        }
Ejemplo n.º 3
0
        public void ServerRpcMessageTest()
        {
            // try setting value with constructor
            var message = new ServerRpcMessage
            {
                netId          = 42,
                componentIndex = 4,
                functionHash   = 0xABCDEF,
                payload        = new ArraySegment <byte>(new byte[] { 0x01, 0x02 })
            };

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

            // deserialize the same data - do we get the same result?
            ServerRpcMessage fresh = MessagePacker.Unpack <ServerRpcMessage>(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, Has.Count.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]));
            }
        }
Ejemplo n.º 4
0
        public void StructWithMethods()
        {
            byte[]      arr = MessagePacker.Pack(new TestMessage(1, "2", 3.3));
            TestMessage t   = MessagePacker.Unpack <TestMessage>(arr);

            Assert.AreEqual(1, t.IntValue);
        }
Ejemplo n.º 5
0
        public void SendsData()
        {
            Message message = new Message
            {
                collection = new ClassWithNoConstructor[]
                {
                    new ClassWithNoConstructor {
                        a = 3
                    }, new ClassWithNoConstructor {
                        a = 4
                    }, new ClassWithNoConstructor {
                        a = 5
                    }
                }
            };

            byte[] data = MessagePackerTest.PackToByteArray(message);

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

            ClassWithNoConstructor[] unpackedCollection = unpacked.collection;

            Assert.IsNotNull(unpackedCollection);
            Assert.IsNotEmpty(unpackedCollection);
            Assert.That(unpackedCollection[0].a, Is.EqualTo(new ClassWithNoConstructor {
                a = 3
            }.a));
            Assert.That(unpackedCollection[1].a, Is.EqualTo(new ClassWithNoConstructor {
                a = 4
            }.a));
            Assert.That(unpackedCollection[2].a, Is.EqualTo(new ClassWithNoConstructor {
                a = 5
            }.a));
        }
Ejemplo n.º 6
0
        public void SpawnMessageTest(ulong testSceneId)
        {
            // try setting value with constructor
            var message = new SpawnMessage
            {
                netId         = 42,
                isLocalPlayer = true,
                isOwner       = true,
                sceneId       = testSceneId,
                prefabHash    = Guid.NewGuid().GetHashCode(),
                position      = UnityEngine.Vector3.one,
                rotation      = UnityEngine.Quaternion.identity,
                scale         = UnityEngine.Vector3.one,
                payload       = new ArraySegment <byte>(new byte[] { 0x01, 0x02 })
            };

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

            Assert.That(fresh.netId, Is.EqualTo(message.netId));
            Assert.That(fresh.isLocalPlayer, Is.EqualTo(message.isLocalPlayer));
            Assert.That(fresh.isOwner, Is.EqualTo(message.isOwner));
            Assert.That(fresh.sceneId, Is.EqualTo(message.sceneId));
            Assert.That(fresh.prefabHash, Is.EqualTo(message.prefabHash));
            Assert.That(fresh.position, Is.EqualTo(message.position));
            Assert.That(fresh.rotation, Is.EqualTo(message.rotation));
            Assert.That(fresh.scale, Is.EqualTo(message.scale));
            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 TestUnpackIdMismatch()
        {
            // Unpack<T> has a id != msgType case that throws a FormatException.
            // let's try to trigger it.

            SceneMessage message = new SceneMessage()
            {
                sceneName      = "Hello world",
                sceneOperation = SceneOperation.LoadAdditive
            };

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

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

            try
            {
                SceneMessage unpacked = MessagePacker.Unpack <SceneMessage>(data);
                // BAD: IF WE GET HERE THEN NO EXCEPTION WAS THROWN
                Assert.Fail();
            }
            catch (FormatException)
            {
                // GOOD
            }
        }
Ejemplo n.º 8
0
        private void TestSerializeDeserialize <T>(T message)
        {
            // serialize
            byte[] arr   = MessagePacker.Pack(message);
            T      fresh = MessagePacker.Unpack <T>(arr);

            Assert.That(fresh, Is.EqualTo(message));
        }
Ejemplo n.º 9
0
        public void UnpackInvalidMessage()
        {
            // try an invalid message
            NetworkReader reader2 = new NetworkReader(new byte[0]);
            bool          result2 = MessagePacker.Unpack(reader2, out int msgType2);

            Assert.That(result2, Is.EqualTo(false));
            Assert.That(msgType2, Is.EqualTo(0));
        }
Ejemplo n.º 10
0
        public void TestCustomRW()
        {
            var quest = new MockQuest(100);

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

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

            Assert.That(unpacked.Id, Is.EqualTo(100));
        }
Ejemplo n.º 11
0
        public void TestDataIntArray()
        {
            int[] array = new[] { 3, 4, 5 };

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

            int[] unpacked = MessagePacker.Unpack <int[]>(data);

            Assert.That(unpacked, Is.EquivalentTo(new [] { 3, 4, 5 }));
        }
Ejemplo n.º 12
0
        public void TestNullByterray()
        {
            byte[] array = null;

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

            byte[] unpacked = MessagePacker.Unpack <byte[]>(data);

            Assert.IsNull(unpacked);
        }
Ejemplo n.º 13
0
        public void TestNullIntArray()
        {
            int[] array = null;

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

            int[] unpacked = MessagePacker.Unpack <int[]>(data);

            Assert.That(unpacked, Is.Null);
        }
Ejemplo n.º 14
0
        public void RemovePlayerMessageTest()
        {
            // try setting value with constructor
            RemovePlayerMessage message = new RemovePlayerMessage();

            byte[] arr = MessagePacker.Pack(message);
            Assert.DoesNotThrow(() => {
                RemovePlayerMessage fresh = MessagePacker.Unpack <RemovePlayerMessage>(arr);
            });
        }
Ejemplo n.º 15
0
        public void ObjectSpawnStartedMessageTest()
        {
            // try setting value with constructor
            ObjectSpawnStartedMessage message = new ObjectSpawnStartedMessage();

            byte[] arr = MessagePacker.Pack(message);
            Assert.DoesNotThrow(() => {
                ObjectSpawnStartedMessage fresh = MessagePacker.Unpack <ObjectSpawnStartedMessage>(arr);
            });
        }
Ejemplo n.º 16
0
        public void NotReadyMessageTest()
        {
            // try setting value with constructor
            NotReadyMessage message = new NotReadyMessage();

            byte[] arr = MessagePacker.Pack(message);
            Assert.DoesNotThrow(() => {
                NotReadyMessage fresh = MessagePacker.Unpack <NotReadyMessage>(arr);
            });
        }
Ejemplo n.º 17
0
        public void TestEmptyIntArray()
        {
            var array = new int[] { };

            var data = MessagePacker.Pack(array);

            var unpacked = MessagePacker.Unpack <int[]>(data);

            Assert.That(unpacked, Is.EquivalentTo(new int[] { }));
        }
Ejemplo n.º 18
0
        public void NetworkPingMessageTest()
        {
            // try setting value with constructor
            NetworkPingMessage message = new NetworkPingMessage(DateTime.Now.ToOADate());

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

            Assert.That(fresh.clientTime, Is.EqualTo(message.clientTime));
        }
Ejemplo n.º 19
0
        public void ErrorMessageTest()
        {
            // try setting value with constructor
            ErrorMessage message = new ErrorMessage(42);

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

            Assert.That(fresh.value, Is.EqualTo(message.value));
        }
Ejemplo n.º 20
0
        public void StructWithEmptyMethods()
        {
            byte[] arr = MessagePacker.Pack(new StructWithEmptyMethodMessage {
                IntValue = 1, StringValue = "2", DoubleValue = 3.3
            });
            StructWithEmptyMethodMessage t = MessagePacker.Unpack <StructWithEmptyMethodMessage>(arr);

            Assert.AreEqual(1, t.IntValue);
            Assert.AreEqual("2", t.StringValue);
            Assert.AreEqual(3.3, t.DoubleValue);
        }
Ejemplo n.º 21
0
        public void WovenSerializationBodyRoundtrip()
        {
            byte[] arr = MessagePacker.Pack(new WovenTestMessage {
                IntValue = 1, StringValue = "2", DoubleValue = 3.3
            });
            WovenTestMessage t = MessagePacker.Unpack <WovenTestMessage>(arr);

            Assert.AreEqual(1, t.IntValue);
            Assert.AreEqual("2", t.StringValue);
            Assert.AreEqual(3.3, t.DoubleValue);
        }
Ejemplo n.º 22
0
        public void UnpackWrongMessage()
        {
            ConnectMessage message = new ConnectMessage();

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

            Assert.Throws <FormatException>(() =>
            {
                DisconnectMessage unpacked = MessagePacker.Unpack <DisconnectMessage>(data);
            });
        }
        public void TestEmptyIntArray()
        {
            var array = new ArraySegment <int>(new int[0]);

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

            ArraySegment <int> unpacked = MessagePacker.Unpack <ArraySegment <int> >(data);

            Assert.IsNotNull(unpacked.Array);
            Assert.That(unpacked.Count, Is.EqualTo(0));
        }
        public void TestNullIntArray()
        {
            ArraySegment <int> array = default;

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

            ArraySegment <int> unpacked = MessagePacker.Unpack <ArraySegment <int> >(data);

            Assert.That(unpacked.Offset, Is.EqualTo(0));
            Assert.That(unpacked.Count, Is.EqualTo(0));
        }
Ejemplo n.º 25
0
        public void ObjectSpawnFinishedMessage()
        {
            // try setting value with constructor
            ObjectSpawnFinishedMessage message = new ObjectSpawnFinishedMessage();

            byte[] arr = MessagePackerTest.PackToByteArray(message);
            Assert.DoesNotThrow(() =>
            {
                ObjectSpawnFinishedMessage fresh = MessagePacker.Unpack <ObjectSpawnFinishedMessage>(arr);
            });
        }
Ejemplo n.º 26
0
        public void DisconnectMessageTest()
        {
            // try setting value with constructor
            DisconnectMessage message = new DisconnectMessage();

            byte[] arr = MessagePacker.Pack(message);
            Assert.DoesNotThrow(() =>
            {
                MessagePacker.Unpack <DisconnectMessage>(arr);
            });
        }
Ejemplo n.º 27
0
        public void ConnectMessage()
        {
            // try setting value with constructor
            ConnectMessage message = new ConnectMessage();

            byte[] arr = MessagePackerTest.PackToByteArray(message);
            Assert.DoesNotThrow(() =>
            {
                MessagePacker.Unpack <ConnectMessage>(arr);
            });
        }
Ejemplo n.º 28
0
        public void ReadyMessage()
        {
            // try setting value with constructor
            ReadyMessage message = new ReadyMessage();

            byte[] arr = MessagePackerTest.PackToByteArray(message);
            Assert.DoesNotThrow(() =>
            {
                ReadyMessage fresh = MessagePacker.Unpack <ReadyMessage>(arr);
            });
        }
Ejemplo n.º 29
0
        public void UnpackWrongMessage()
        {
            var message = new ReadyMessage();

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

            Assert.Throws <FormatException>(() =>
            {
                _ = MessagePacker.Unpack <AddPlayerMessage>(data);
            });
        }
Ejemplo n.º 30
0
        public void UnpackWrongMessage()
        {
            var message = new SceneReadyMessage();

            var data = MessagePacker.Pack(message);

            Assert.Throws <FormatException>(() =>
            {
                _ = MessagePacker.Unpack <AddCharacterMessage>(data);
            });
        }