Beispiel #1
0
 public void Serialize(ReusableStream writer)
 {
     writer.Write(PublicKey.Length);
     writer.Write(PublicKey);
     writer.Write(Data.Length);
     writer.Write(Data);
 }
Beispiel #2
0
 public void Serialize(ReusableStream writer)
 {
     writer.Write(networkIDs.Count);
     for (int i = 0; i < networkIDs.Count; ++i)
     {
         writer.Write(networkIDs[i]);
     }
 }
Beispiel #3
0
 public void Serialize(ReusableStream writer)
 {
     writer.Write(playerRequested);
     writer.Write(playerToOwn);
     writer.Write(prefabName);
     writer.Write((int)spawnType);
     writer.Write(perminant);
 }
Beispiel #4
0
        public void Serialize(ReusableStream writer)
        {
            writer.Write(owner);

            writer.Write(networkedObjectData.Length);
            writer.Write(networkedObjectData);

            writer.Write(prefabName);
            writer.Write((int)spawnType);
        }
Beispiel #5
0
 public void Serialize(ReusableStream writer)
 {
     writer.Write(playerInfo.Count);
     foreach (var mapping in playerInfo)
     {
         writer.Write(mapping.Key);
         writer.Write(mapping.Value.PlayerID);
         writer.Write(mapping.Value.PlayerName);
         writer.Write(mapping.Value.PlayerPing);
     }
 }
Beispiel #6
0
 public void Serialize(ReusableStream writer)
 {
     writer.Write(sceneName);
     writer.Write(sceneMapping.Count);
     foreach (var mapping in sceneMapping)
     {
         writer.Write(mapping.Key);
         writer.Write(mapping.Value.Length);
         writer.Write(mapping.Value);
     }
 }
Beispiel #7
0
        public void CanGrow()
        {
            var s = new ReusableStream(8, true);

            Assert.AreEqual(8, s.Capacity);
            s.Write((long)3);

            // there shouldn't be any change yet
            Assert.AreEqual(8, s.Capacity);

            s.Write(4);
            Assert.IsTrue(s.Capacity >= 12);
        }
Beispiel #8
0
        public void CanGrowDisabled()
        {
            var s = new ReusableStream(8, false);

            Assert.AreEqual(8, s.Capacity);
            s.Write((long)3);

            // there shouldn't be any change
            Assert.AreEqual(8, s.Capacity);

            Assert.Throws <IndexOutOfRangeException>(() => s.Write(4));

            // there shouldn't be any change
            Assert.AreEqual(8, s.Capacity);
        }
Beispiel #9
0
        public void UninitializedStream()
        {
            var s = new ReusableStream();

            Assert.Throws <NullReferenceException>(() => s.Write(true));
            Assert.Throws <IndexOutOfRangeException>(() => s.ReadBoolean());
        }
Beispiel #10
0
        public void Resetting()
        {
            var s = new ReusableStream(16);

            Assert.AreEqual(0, s.Position);
            Assert.AreEqual(0, s.Length);

            var written = long.MaxValue;

            s.Write(written);

            Assert.AreEqual(8, s.Position);
            Assert.AreEqual(8, s.Length);

            s.ResetForReading();

            Assert.AreEqual(0, s.Position);
            Assert.AreEqual(8, s.Length);

            var read = s.ReadInt64();

            Assert.AreEqual(written, read);
            Assert.AreEqual(8, s.Position);
            Assert.AreEqual(8, s.Length);

            s.ResetForWriting();

            Assert.AreEqual(0, s.Position);
            Assert.AreEqual(0, s.Length);

            written = 1;
            s.Write(written);

            Assert.AreEqual(8, s.Position);
            Assert.AreEqual(8, s.Length);

            s.ResetForReading();

            Assert.AreEqual(0, s.Position);
            Assert.AreEqual(8, s.Length);

            read = s.ReadInt64();

            Assert.AreEqual(written, read);
            Assert.AreEqual(8, s.Position);
            Assert.AreEqual(8, s.Length);
        }
Beispiel #11
0
        public static byte[] Serialize <T>(ref T message) where T : IMessage
        {
            if (writeStream == null)
            {
                writeStream = new ReusableStream(new byte[64512], 0, 64512, true);
            }

            writeStream.ResetForReading();

            writeStream.Write(message.Channel);
            writeStream.Write(message.Part);
            writeStream.Write(message.TotalParts);
            writeStream.Write(message.Length);
            writeStream.Write(message.ID);
            writeStream.Write(message.IsAsync);

            if (message is ISerialized sMessage)
            {
                sMessage.Serialize(writeStream);
            }
            else
            {
                writeStream.Write(JsonUtility.ToJson(message));
            }

            return(writeStream.Data.SubArray(0, (int)writeStream.Position));
        }
Beispiel #12
0
        public static void Serialize <T>(ref T message, byte[] buffer, out int len) where T : IMessage
        {
            if (reusableStream == null)
            {
                reusableStream = new ReusableStream();
            }

            reusableStream.ReplaceData(buffer, 0, buffer.Length, false);

            reusableStream.Write(message.Channel);
            reusableStream.Write(message.Part);
            reusableStream.Write(message.TotalParts);
            reusableStream.Write(message.Length);
            reusableStream.Write(message.ID);
            reusableStream.Write(message.IsAsync);

            if (message is ISerialized sMessage)
            {
                sMessage.Serialize(reusableStream);
            }
            else
            {
                reusableStream.Write(JsonUtility.ToJson(message));
            }

            len = (int)reusableStream.Position;
        }
Beispiel #13
0
 public void Serialize(ReusableStream writer)
 {
     writer.Write(messageNum);
     writer.Write(force);
     writer.Write(networkData.Data.Count);
     foreach (KeyValuePair <ulong, byte[]> pair in networkData.Data)
     {
         writer.Write(pair.Key);
         writer.Write(pair.Value.Length);
         writer.Write(pair.Value);
     }
 }
Beispiel #14
0
        public unsafe void ReadPastEnd()
        {
            var s = new ReusableStream(16);

            s.Write((ulong)3);

            var buffer = new byte[16];

            fixed(byte *p = buffer)
            {
                var bufferPtr = p;

                Assert.AreEqual(-1, s.ReadByte());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadBoolean());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadInt8());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadUInt8());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadInt16());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadUInt16());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadInt32());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadUInt32());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadInt64());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadUInt64());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadSingle());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadDouble());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadDateTime());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadGuid());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadString(true));

                Assert.AreEqual(0, s.Read(buffer, 0, 1));
                Assert.AreEqual(0, s.Read(bufferPtr, 1));

                s.ResetForReading();
                Assert.AreEqual(8, s.Read(buffer, 0, 9));

                s.ResetForReading();
                Assert.AreEqual(8, s.Read(bufferPtr, 9));
            }
        }
Beispiel #15
0
 public void Serialize(ReusableStream writer)
 {
     writer.Write(SceneName);
 }
Beispiel #16
0
 public void Serialize(ReusableStream writer)
 {
     writer.Write(owner);
 }
Beispiel #17
0
 public void Serialize(ReusableStream writer)
 {
     writer.Write(Accepted);
     writer.Write(PlayerID);
     writer.Write(playerName);
 }
Beispiel #18
0
 public void Serialize(ReusableStream writer)
 {
     writer.Write(playerName);
 }
Beispiel #19
0
 public void Serialize(ReusableStream writer)
 {
     writer.Write(message.Length);
     writer.Write(message);
 }
Beispiel #20
0
 public void Serialize(ReusableStream writer)
 {
     writer.Write(MessageID);
     writer.Write(PartNumber);
 }
Beispiel #21
0
 public void Serialize(ReusableStream writer)
 {
     writer.Write((byte)step);
     writer.Write(Data.Length);
     writer.Write(Data);
 }
Beispiel #22
0
        public unsafe void ReadWriteBytes()
        {
            var s   = new ReusableStream(4000);
            var rng = new Random();

            var tests = new byte[][]
            {
                new byte[1],
                new byte[2],
                new byte[3],
                new byte[4],
                new byte[5],
                new byte[6],
                new byte[7],
                new byte[8],
                new byte[9],
                new byte[10],
                new byte[11],
                new byte[12],
                new byte[13],
                new byte[14],
                new byte[15],
                new byte[16],
                new byte[17],
                new byte[127],
                new byte[128],
                new byte[129],
                new byte[255],
                new byte[256],
                new byte[257],
                new byte[400],
                new byte[800],
                new byte[1200],
            };

            var expectedLength = 0;

            foreach (var test in tests)
            {
                rng.NextBytes(test);
                s.Write(test);
                expectedLength += test.Length;
                Assert.AreEqual(expectedLength, s.Length);
            }

            s.ResetForReading();

            foreach (var test in tests)
            {
                var read = new byte[test.Length];
                s.Read(read, 0, read.Length);
                AssertBytesAreEqual(test, read);
            }

            // do the same thing using the unsafe methods now

            s = new ReusableStream(4000);

            expectedLength = 0;

            foreach (var test in tests)
            {
                fixed(byte *ptr = test)
                {
                    s.Write(ptr, test.Length);
                }

                expectedLength += test.Length;
                Assert.AreEqual(expectedLength, s.Length);
            }

            s.ResetForReading();

            foreach (var test in tests)
            {
                var read = new byte[test.Length + 16];
                fixed(byte *ptr = read)
                {
                    const ulong GUARD = 0xAAAAAAAAAAAAAAAA;

                    var startGuard = (ulong *)ptr;
                    var data       = &ptr[8];
                    var endGuard   = (ulong *)&data[test.Length];

                    *startGuard = GUARD;
                    *endGuard   = GUARD;

                    s.Read(data, test.Length);

                    Assert.AreEqual(GUARD, *startGuard);
                    Assert.AreEqual(GUARD, *endGuard);
                }

                var actual = new byte[test.Length];
                Array.Copy(read, 8, actual, 0, test.Length);

                AssertBytesAreEqual(test, actual);
            }
        }
Beispiel #23
0
 public void Serialize(ReusableStream writer)
 {
     writer.Write(playerRequested);
     writer.Write(networkID);
 }
Beispiel #24
0
        public void ReadWritePrimitives()
        {
            var s   = new ReusableStream(100);
            var rng = new Random();

            var b   = (byte)rng.Next(255);
            var sb  = (sbyte)rng.Next(255);
            var sh  = (short)rng.Next(short.MinValue, short.MaxValue);
            var ush = (ushort)rng.Next(ushort.MaxValue);
            var i   = rng.Next(int.MinValue, int.MaxValue);
            var ui  = (uint)rng.Next(int.MinValue, int.MaxValue);
            var l   = (long)RandomULong(rng);
            var ul  = RandomULong(rng);
            var f   = (float)rng.NextDouble();
            var d   = rng.NextDouble();
            var c   = (char)rng.Next(char.MinValue, char.MaxValue);
            var t   = DateTime.UtcNow;
            var g   = Guid.NewGuid();

            var expectedLength = 0;

            s.Write(b);
            expectedLength += 1;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(sb);
            expectedLength += 1;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(sh);
            expectedLength += 2;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(ush);
            expectedLength += 2;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(i);
            expectedLength += 4;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(ui);
            expectedLength += 4;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(l);
            expectedLength += 8;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(ul);
            expectedLength += 8;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(f);
            expectedLength += 4;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(d);
            expectedLength += 8;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(c);
            expectedLength += 2;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(false);
            expectedLength += 1;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(true);
            expectedLength += 1;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(t);
            expectedLength += 8;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(g);
            expectedLength += 16;
            Assert.AreEqual(expectedLength, s.Length);

            s.ResetForReading();

            Assert.AreEqual(b, s.ReadUInt8());
            Assert.AreEqual(sb, s.ReadInt8());
            Assert.AreEqual(sh, s.ReadInt16());
            Assert.AreEqual(ush, s.ReadUInt16());
            Assert.AreEqual(i, s.ReadInt32());
            Assert.AreEqual(ui, s.ReadUInt32());
            Assert.AreEqual(l, s.ReadInt64());
            Assert.AreEqual(ul, s.ReadUInt64());
            Assert.AreEqual(f, s.ReadSingle());
            Assert.AreEqual(d, s.ReadDouble());
            Assert.AreEqual(c, s.ReadChar());
            Assert.AreEqual(false, s.ReadBoolean());
            Assert.AreEqual(true, s.ReadBoolean());
            Assert.AreEqual(t, s.ReadDateTime());
            Assert.AreEqual(g, s.ReadGuid());

            // verify that we read to the end
            Assert.AreEqual(s.Length, s.Position);

            s.ResetForReading();
            Assert.AreEqual((int)b, s.ReadByte());
        }
Beispiel #25
0
 public void Serialize(ReusableStream writer)
 {
     writer.Write(LastKnownFTT);
 }