Exemplo n.º 1
0
        public void CreateTest()
        {
            Type        t        = typeof(int);
            SuperStream ms       = new SuperStream(Endianess.Little);
            var         expected = new StreamDataTestData
            {
                A = -15,
                B = "Test one!",
                C = "Test two!",
                D = new List <int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }),
                E = ByteEnum.Hello,
                F = LongEnum.Max
            };

            ms.WriteInt32(expected.A);
            ms.WriteString(expected.B);
            ms.WriteCString(expected.C);
            ms.WriteUInt32((uint)expected.D.Count);
            foreach (var i in expected.D)
            {
                ms.WriteInt32(i);
            }

            ms.WriteByte((byte)ByteEnum.Hello);
            ms.WriteInt64((Int64)LongEnum.Max);

            ms.Position = 0;

            StreamDataTestData actual;

            actual = StreamData.Create <StreamDataTestData>(ms);
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 2
0
        public void SerializeTest()
        {
            SuperStream expectedStream = new SuperStream(Endianess.Little);
            var         expectedObject = new StreamDataTestData
            {
                A = -15,
                B = "Test one!",
                C = "Test two!",
                D = new List <int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }),
                E = ByteEnum.Hello,
                F = LongEnum.Max
            };

            expectedStream.WriteInt32(expectedObject.A);
            expectedStream.WriteString(expectedObject.B);
            expectedStream.WriteCString(expectedObject.C);
            expectedStream.WriteUInt32((uint)expectedObject.D.Count);
            foreach (var i in expectedObject.D)
            {
                expectedStream.WriteInt32(i);
            }
            expectedStream.WriteByte((byte)ByteEnum.Hello);
            expectedStream.WriteInt64((Int64)LongEnum.Max);

            var expected = ((MemoryStream)expectedStream.BaseStream).ToArray();

            var actualStream = new SuperStream(Endianess.Little);

            StreamData.Serialize(expectedObject, actualStream);

            var actual = ((MemoryStream)actualStream.BaseStream).ToArray();

            Assert.IsTrue(actual.SequenceEqual(expected));
        }
Exemplo n.º 3
0
        internal ulong WriteContentLength(SuperStream stream, ulong length)
        {
            var countType = this.Attributes.FirstOrDefault(a => a is StreamDataCollectionLengthAttribute) as StreamDataCollectionLengthAttribute;

            if (countType == null)
            {
                return(this.Entries);
            }
            switch (countType.Type)
            {
            case LengthType.Byte:
                stream.WriteByte((byte)length);
                break;

            case LengthType.UInt16:
                stream.WriteUInt16((ushort)length);
                break;

            default:
            case LengthType.UInt32:
                stream.WriteUInt32((uint)length);
                break;
            }
            return(length);
        }
        internal void WriteContentLength(SuperStream stream, ulong length)
        {
            switch (this.Type)
            {
            case LengthType.Byte:
                stream.WriteByte((byte)length);
                break;

            case LengthType.UInt16:
                stream.WriteUInt16((ushort)length);
                break;

            default:
            case LengthType.UInt32:
                stream.WriteUInt32((uint)length);
                break;
            }
        }
        private bool WriteValue(Type dataType, SuperStream stream, object value)
        {
            if (dataType == typeof(Byte))
            {
                stream.WriteByte((byte)value);
                return(true);
            }

            if (dataType == typeof(Int64))
            {
                stream.WriteInt64((Int64)value);
                return(true);
            }
            if (dataType == typeof(UInt64))
            {
                stream.WriteUInt64((UInt64)value);
                return(true);
            }

            if (dataType == typeof(Int32))
            {
                stream.WriteInt32((Int32)value);
                return(true);
            }
            if (dataType == typeof(UInt32))
            {
                stream.WriteUInt32((UInt32)value);
                return(true);
            }

            if (dataType == typeof(Int16))
            {
                stream.WriteInt16((Int16)value);
                return(true);
            }
            if (dataType == typeof(UInt16))
            {
                stream.WriteUInt16((UInt16)value);
                return(true);
            }

            if (dataType == typeof(Single))
            {
                stream.WriteSingle((Single)value);
                return(true);
            }

            if (dataType == typeof(Double))
            {
                stream.WriteDouble((Double)value);
                return(true);
            }

            if (dataType == typeof(bool))
            {
                // If bool is true, write 1. Otherwise, write 0.
                int val = ((bool)value) ? 1 : 0;
                stream.WriteInt32(val);
            }

            if (dataType.IsEnum)
            {
                dynamic storeValue = Convert.ChangeType(value, Enum.GetUnderlyingType(dataType));
                return(this.WriteValue(storeValue.GetType(), stream, storeValue));
            }

            // Test if type has StreamDataAttribute on properties.
            // This allows nesting of StreamData-aware task.DataTypes
            var props = StreamDataInfo.GetProperties(dataType);

            if (props.Length == 0)
            {
                return(false);
            }

            StreamData.Serialize(value, stream);
            // Need to add error condition here.
            return(true);
        }