Ejemplo n.º 1
0
        public object BufferToObject(byte[] buffer)
        {
            ulong id = 0;

            try
            {
                var readIdCount = Varint.BufferToNumber(buffer, 0, out id);
                if (id == 0)
                {
                    return(null);
                }

                var    describer = _GetDescriber((int)id);
                object instance;
                describer.ToObject(buffer, readIdCount, out instance);
                return(instance);
            }
            catch (DescriberException ex)
            {
                var describer = _GetDescriber((int)id);
                if (describer != null)
                {
                    throw new SystemException(string.Format("BufferToObject {0}:{1}", id, describer.Type.FullName), ex);
                }

                throw new SystemException(string.Format("BufferToObject {0}:unkown", id), ex);
            }
        }
Ejemplo n.º 2
0
        int ITypeDescriber.ToObject(byte[] buffer, int begin, out object instnace)
        {
            ulong value;
            var   readed = Varint.BufferToNumber(buffer, begin, out value);

            instnace = Enum.ToObject(_Type, value);
            return(readed);
        }
Ejemplo n.º 3
0
        public static int BufferToNumber(byte[] buffer, int offset, out int value)
        {
            ulong val;
            var   count = Varint.BufferToNumber(buffer, offset, out val);

            value = (int)val;

            return(count);
        }
Ejemplo n.º 4
0
        public void VarintTest()
        {
            var count  = Varint.GetByteCount(12671L);
            var buffer = new byte[count];

            Varint.NumberToBuffer(buffer, 0, 12671L);

            ulong value;

            Varint.BufferToNumber(buffer, 0, out value);

            NUnit.Framework.Assert.AreEqual(12671UL, value);
        }
Ejemplo n.º 5
0
        public void VarintTest()
        {
            int count = Varint.GetByteCount(12671L);

            byte[] buffer = new byte[count];
            Varint.NumberToBuffer(buffer, 0, 12671L);

            ulong value;

            Varint.BufferToNumber(buffer, 0, out value);

            Xunit.Assert.Equal(12671UL, value);
        }
Ejemplo n.º 6
0
        public void VarintToNumberTest()
        {
            ulong number;
            var   index = Varint.BufferToNumber(
                new byte[]
            {
                0x96,
                0x1
            },
                0,
                out number);

            NUnit.Framework.Assert.AreEqual(150UL, number);
            NUnit.Framework.Assert.AreEqual(2, index);
        }
Ejemplo n.º 7
0
        public void VarintToNumberTest()
        {
            ulong number;
            int   index = Varint.BufferToNumber(
                new byte[]
            {
                0x96,
                0x1
            },
                0,
                out number);

            Xunit.Assert.Equal(150UL, number);
            Xunit.Assert.Equal(2, index);
        }
Ejemplo n.º 8
0
        int ITypeDescriber.ToObject(byte[] buffer, int begin, out object instance)
        {
            try
            {
                var constructor = _Type.GetConstructors().OrderBy(c => c.GetParameters().Length).Select(c => c).FirstOrDefault();
                if (constructor != null)
                {
                    var argTypes = constructor.GetParameters().Select(info => info.ParameterType).ToArray();
                    var objArgs  = new object[argTypes.Length];

                    for (var i = 0; i < argTypes.Length; i++)
                    {
                        objArgs[i] = Activator.CreateInstance(argTypes[i]);
                    }

                    instance = Activator.CreateInstance(_Type, objArgs);
                }
                else
                {
                    instance = Activator.CreateInstance(_Type);
                }

                var offset = begin;

                ulong validLength;
                offset += Varint.BufferToNumber(buffer, offset, out validLength);

                for (var i = 0ul; i < validLength; i++)
                {
                    ulong index;
                    offset += Varint.BufferToNumber(buffer, offset, out index);

                    var    filed     = _Fields[index];
                    var    describer = _GetDescriber(filed);
                    object valueInstance;
                    offset += describer.ToObject(buffer, offset, out valueInstance);
                    filed.SetValue(instance, valueInstance);
                }

                return(offset - begin);
            }
            catch (Exception ex)
            {
                throw new DescriberException(typeof(ClassDescriber), _Type, _Id, "ToObject", ex);
            }
        }