Beispiel #1
0
        public static T Deserialize <T>(byte[] data, int len) where T : IMessage, new()
        {
            if (readStream == null)
            {
                readStream = new ReusableStream();
            }
            readStream.ReplaceData(data, 0, len, false);

            if (MessageTypes.TryGetValue(typeof(T), out ISerialized sBase))
            {
                return(sBase.Deserialize <T>(readStream));
            }
            else if (typeof(T).GetInterfaces().Contains(typeof(ISerialized)))
            {
                sBase = (ISerialized) new T();
                MessageTypes[typeof(T)] = sBase;
                return(sBase.Deserialize <T>(readStream));
            }

            ushort channel    = readStream.ReadUInt16();
            ushort part       = readStream.ReadUInt16();
            ushort totalParts = readStream.ReadUInt16();
            int    length     = readStream.ReadInt32();
            ushort id         = readStream.ReadUInt16();
            bool   isAsync    = readStream.ReadBoolean();

            T Mes;

            try
            {
                Mes            = JsonUtility.FromJson <T>(readStream.ReadString());
                Mes.Channel    = channel;
                Mes.Part       = part;
                Mes.TotalParts = totalParts;
                Mes.Length     = length;
                Mes.ID         = id;
                Mes.IsAsync    = isAsync;
            }
            catch
            {
                Mes = new T
                {
                    Channel    = channel,
                    Part       = part,
                    TotalParts = totalParts,
                    Length     = length,
                    ID         = id,
                    IsAsync    = isAsync
                };
            }

            return(Mes);
        }
Beispiel #2
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 #3
0
        public void ReplacingData()
        {
            var rng = new Random();
            var one = new byte[16];
            var two = new byte[32];

            rng.NextBytes(one);
            rng.NextBytes(two);

            var readable = one.Length - 2;
            var s        = new ReusableStream(one, 0, readable);

            Assert.AreEqual(readable, s.Length);
            Assert.AreEqual(readable, s.UnreadByteCount);
            Assert.AreEqual(0, s.Offset);
            Assert.AreEqual(0, s.Position);

            var readCount = 0;

            while (s.UnreadByteCount > 0)
            {
                Assert.AreEqual(one[readCount], s.ReadUInt8());
                readCount++;
            }

            Assert.AreEqual(readable, readCount);

            readable = two.Length - 2;
            s.ReplaceData(two, 2, readable);

            Assert.AreEqual(readable, s.Length);
            Assert.AreEqual(readable, s.UnreadByteCount);
            Assert.AreEqual(2, s.Offset);
            Assert.AreEqual(0, s.Position);

            readCount = 0;
            while (s.UnreadByteCount > 0)
            {
                Assert.AreEqual(two[2 + readCount], s.ReadUInt8());
                readCount++;
            }

            Assert.AreEqual(readable, readCount);
        }