Example #1
0
        public void TestOverwritingData()
        {
            NetworkWriter writer = new NetworkWriter();

            writer.Write(Matrix4x4.identity);
            writer.Write(1.23456789m);
            writer.Position += 10;
            writer.Write(Vector3.negativeInfinity);
            writer.Position = 46;
            // write right at the boundary before SetLength
            writer.Write(0xfeed_babe_c0ffee);
            // test that SetLength clears data beyond length
            writer.SetLength(50);
            // check that jumping leaves 0s between
            writer.Position = 100;
            writer.Write("no worries, m8");
            writer.Position = 64;
            writer.Write(true);
            // check that clipping off the end affect ToArray()'s length
            writer.SetLength(128);
            byte[] output   = writer.ToArray();
            byte[] expected = new byte[] {
                0, 0, 128, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 128, 63, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 63, 0, 0, 238,
                255, 192, 190, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 1, 14, 110, 111, 32, 119, 111, 114, 114, 105, 101,
                115, 44, 32, 109, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
            };
            Assert.That(output, Is.EqualTo(expected));
        }
Example #2
0
        public void TestSetLengthInitialization()
        {
            NetworkWriter writer = new NetworkWriter();

            writer.SetLength(10);
            Assert.That(writer.Position, Is.EqualTo(0), "Increasing length should not move position");
        }
Example #3
0
        public async UniTask <byte> ReceiveAsync(MemoryStream buffer)
        {
            // wait for a message
            await MessageCount.WaitAsync();

            buffer.SetLength(0);
            reader.buffer = writer.ToArraySegment();

            ArraySegment <byte> data = reader.ReadBytesAndSizeSegment();

            if (data.Count == 0)
            {
                throw new EndOfStreamException();
            }

            buffer.SetLength(0);
            buffer.Write(data.Array, data.Offset, data.Count);

            if (reader.Position == reader.Length)
            {
                // if we reached the end of the buffer, reset the buffer to recycle memory
                writer.SetLength(0);
                reader.Position = 0;
            }

            return(0);
        }
Example #4
0
        public void TestSetLengthZeroes()
        {
            writer.WriteString("I saw");
            writer.WriteInt64(0xA_FADED_DEAD_EEL);
            writer.WriteString("and ate it");
            int position = writer.Position;

            writer.SetLength(10);
            Assert.That(writer.Position, Is.EqualTo(10), "Decreasing length should move position");

            // lets grow it back and check there's zeroes now.
            writer.SetLength(position);
            byte[] data = writer.ToArray();
            for (int i = 10; i < data.Length; i++)
            {
                Assert.That(data[i], Is.EqualTo(0), $"index {i} should have value 0");
            }
        }
Example #5
0
        public void TestSetLengthZeroes()
        {
            NetworkWriter writer = new NetworkWriter();

            writer.Write("I saw");
            writer.Write(0xA_FADED_DEAD_EEL);
            writer.Write("and ate it");
            int position = writer.Position;

            writer.SetLength(10);
            // Setting length should set position too
            Assert.That(writer.Position, Is.EqualTo(10));
            // lets grow it back and check there's zeroes now.
            writer.SetLength(position);
            byte[] data = writer.ToArray();
            for (int i = position; i < data.Length; i++)
            {
                Assert.That(data[i], Is.EqualTo(0), $"index {i} should have value 0");
            }
        }
Example #6
0
        public void Benchmark()
        {
            // 10 million reads, Unity 2019.3, code coverage disabled
            //    4014ms ms
            NetworkWriter writer = new NetworkWriter();

            for (int i = 0; i < 10000000; ++i)
            {
                writer.SetLength(0);
                writer.WriteVector3(new Vector3(1, 2, 3));
            }
        }
        public void TestOverwritingData()
        {
            NetworkWriter writer = new NetworkWriter();

            writer.WriteMatrix4x4(Matrix4x4.identity);
            writer.WriteDecimal(1.23456789m);
            writer.Position += 10;
            writer.WriteVector3(Vector3.negativeInfinity);
            writer.Position = 46;
            // write right at the boundary before SetLength
            writer.WriteInt64(0xfeed_babe_c0ffee);
            // test that SetLength clears data beyond length
            writer.SetLength(50);
            // check that jumping leaves 0s between
            writer.Position = 100;
            writer.WriteString("no worries, m8");
            writer.Position = 64;
            writer.WriteBoolean(true);
            // check that clipping off the end affect ToArray()'s length
            writer.SetLength(128);
            byte[] output = writer.ToArray();
            //Debug.Log(BitConverter.ToString(output));
            byte[] expected =
            {
                0x00, 0x00, 0x80, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x80, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x80, 0x3F, 0x00, 0x00, 0xEE, 0xFF, 0xC0, 0xBE,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x0F, 0x00, 0x6E, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x72, 0x69,
                0x65, 0x73, 0x2C, 0x20, 0x6D, 0x38, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
            };
            Assert.That(output, Is.EqualTo(expected));
        }
Example #8
0
        public void TestSetLengthInitialization()
        {
            NetworkWriter writer = new NetworkWriter();

            writer.SetLength(10);
            // Setting length should leave position at 0
            Assert.That(writer.Position, Is.EqualTo(0));
            byte[] data = writer.ToArray();
            for (int i = 0; i < data.Length; i++)
            {
                Assert.That(data[i], Is.EqualTo(0), $"index {i} should have value 0");
            }
        }
        static void WriteQuaternion()
        {
            int count = 100000;

            // create big enough writer so we don't need to resize when testing
            NetworkWriter writer = new NetworkWriter();

            writer.SetLength(count * 16);
            writer.Position = 0;

            for (int i = 0; i < count; i++)
            {
                writer.WriteQuaternion(Quaternion.identity);
            }
        }