Example #1
0
        public override void WriteValueNotExisted(HessianWriter writer, HessianContext context, object value)
        {
            Type type = value.GetType();

            if (!type.IsArray)
            {
                throw Exceptions.UnExpectedTypeException(type);
            }

            var elementType = type.GetElementType();

            if (elementType == typeof(byte) || elementType == typeof(sbyte))
            {
                GetConverter <BinaryConverter>().WriteValueNotNull(writer, context, value);
                return;
            }

            var array = (Array)value;
            HessianConverter itemConverter = null;

            if (elementType == typeof(object))
            {
                //untyped array
                if (array.Length <= Constants.LIST_DIRECT_MAX)
                {
                    writer.Write((byte)(Constants.BC_LIST_DIRECT_UNTYPED + array.Length));
                }
                else
                {
                    writer.Write(Constants.BC_LIST_FIXED_UNTYPED);
                    IntConverter.WriteInt(writer, context, array.Length);
                }
                itemConverter = AutoConverter;
            }
            else
            {
                if (array.Length <= Constants.LIST_DIRECT_MAX)
                {
                    writer.Write((byte)(Constants.BC_LIST_DIRECT + array.Length));
                    TypeConverter.WriteType(writer, context, type);
                }
                else
                {
                    writer.Write(Constants.BC_LIST_FIXED);
                    TypeConverter.WriteType(writer, context, type);
                    IntConverter.WriteInt(writer, context, array.Length);
                }
                itemConverter = AutoConverter.GetConverter(elementType);
            }

            foreach (var item in array)
            {
                itemConverter.WriteValue(writer, context, item);
            }
        }
 public override object ReadValue(HessianReader reader, HessianContext context, Type objectType, byte initialOctet)
 {
     if (Constants.BC_REF == initialOctet)
     {
         var index = (int)IntConverter.ReadValue(reader, context, typeof(int));
         return(context.ValueRefs.GetItem(index));
     }
     else
     {
         return(ReadValueNotExisted(reader, context, objectType, initialOctet));
     }
 }
 public override void WriteValueNotNull(HessianWriter writer, HessianContext context, object value)
 {
     (var index, var isNew) = context.ValueRefs.AddItem(value);
     if (isNew)
     {
         WriteValueNotExisted(writer, context, value);
     }
     else
     {
         writer.Write(Constants.BC_REF);
         IntConverter.WriteInt(writer, context, index);
     }
 }
Example #4
0
        public override object ReadValueNotExisted(HessianReader reader, HessianContext context, Type objectType, byte initialOctet)
        {
            int  len         = 0;
            Type elementType = null;

            if (0x70 <= initialOctet && initialOctet <= 0x77)
            {
                elementType = ((Type)TypeConverter.ReadValue(reader, context, null)).GetElementType();
                len         = initialOctet - 0x70;
            }
            else if (0x78 <= initialOctet && initialOctet <= 0x7f)
            {
                elementType = typeof(object);
                len         = initialOctet - 0x78;
            }
            else if (Constants.BC_LIST_FIXED_UNTYPED == initialOctet)
            {
                elementType = typeof(object);
                len         = (int)IntConverter.ReadValue(reader, context, typeof(int));
            }
            else if (Constants.BC_LIST_FIXED == initialOctet)
            {
                elementType = ((Type)TypeConverter.ReadValue(reader, context, null)).GetElementType();
                len         = (int)IntConverter.ReadValue(reader, context, typeof(int));
            }

            var array = Array.CreateInstance(elementType, len);

            context.ValueRefs.AddItem(array);

            HessianConverter itemConverter = null;

            if (elementType == typeof(object))
            {
                itemConverter = AutoConverter;
            }
            else
            {
                itemConverter = AutoConverter.GetConverter(elementType);
            }

            for (int i = 0; i < len; i++)
            {
                var item = itemConverter.ReadValue(reader, context, elementType);
                array.SetValue(item, i);
            }
            return(array);
        }
Example #5
0
        public override object ReadValueNotExisted(HessianReader reader, HessianContext context, Type objectType, byte initialOctet)
        {
            if (ClassDefinitionConverter.CanRead(initialOctet))
            {
                ClassDefinitionConverter.ReadValue(reader, context, typeof(ClassDefinition), initialOctet);
                initialOctet = reader.ReadByte();
            }

            int definitionIndex = 0;

            if (0x60 <= initialOctet && initialOctet <= 0x6f)
            {
                definitionIndex = initialOctet - Constants.BC_OBJECT_DIRECT;
            }
            else if (Constants.BC_OBJECT == initialOctet)
            {
                definitionIndex = (int)IntConverter.ReadValue(reader, context, typeof(int));
            }
            var definition = context.ClassRefs.GetItem(definitionIndex);

            object value = null;

            if (definition.Type.IsEnum)
            {
                definition.VisitFields(field =>
                {
                    var fieldValue = AutoConverter.ReadValue(reader, context, field.Type);
                    if (field.Name == "name")
                    {
                        value = Enum.Parse(definition.Type, (string)fieldValue);
                        context.ValueRefs.AddItem(value);
                    }
                });
            }
            else
            {
                value = Activator.CreateInstance(definition.Type);
                context.ValueRefs.AddItem(value);
                definition.VisitFields(field =>
                {
                    var fieldValue = AutoConverter.ReadValue(reader, context, field.Type);
                    field.Setter(value, fieldValue);
                });
            }
            return(value);
        }
Example #6
0
        public void WriteType(HessianWriter writer, HessianContext context, Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            (int index, bool isNewItem) = context.TypeRefs.AddItem(type);

            if (isNewItem)
            {
                StringConverter.WriteValueNotNull(writer, context, GetTypeName(type));
            }
            else
            {
                IntConverter.WriteInt(writer, context, index);
            }
        }
Example #7
0
        public override object ReadValue(HessianReader reader, HessianContext context, Type objectType, byte initialOctet)
        {
            if (Constants.BC_REF == initialOctet)
            {
                var index = (int)IntConverter.ReadValue(reader, context, typeof(int));
                return(context.ValueRefs.GetItem(index));
            }

            foreach (var converter in ConverterCache.Values)
            {
                if (converter != this && !(converter is ClassDefinitionConverter) && converter.CanRead(initialOctet))
                {
                    return(converter.ReadValue(reader, context, objectType, initialOctet));
                }
            }

            throw Exceptions.UnExpectedInitialOctet(this, initialOctet);
        }
Example #8
0
 public override object ReadValue(HessianReader reader, HessianContext context, Type objectType, byte initialOctet)
 {
     if (StringConverter.CanRead(initialOctet))
     {
         var typeName = (string)StringConverter.ReadValue(reader, context, typeof(string), initialOctet);
         var type     = GetType(typeName);
         context.TypeRefs.AddItem(type);
         return(type);
     }
     else if (IntConverter.CanRead(initialOctet))
     {
         var typeIndex = (int)IntConverter.ReadValue(reader, context, typeof(int), initialOctet);
         return(context.TypeRefs.GetItem(typeIndex));
     }
     else
     {
         throw Exceptions.UnExpectedInitialOctet(this, initialOctet);
     }
 }
Example #9
0
        public void WriteClassDefinition(HessianWriter writer, HessianContext context, ClassDefinition definition, out int definitionIndex)
        {
            if (definition == null)
            {
                throw new ArgumentNullException(nameof(definition));
            }

            (var index, var isNewItem) = context.ClassRefs.AddItem(definition);
            if (isNewItem)
            {
                writer.Write(Constants.BC_OBJECT_DEF);
                TypeConverter.WriteType(writer, context, definition.Type);
                IntConverter.WriteInt(writer, context, definition.FieldNames.Count);
                foreach (var fieldName in definition.FieldNames)
                {
                    StringConverter.WriteValueNotNull(writer, context, fieldName);
                }
            }
            definitionIndex = index;
        }
Example #10
0
        public override object ReadValue(HessianReader reader, HessianContext context, Type objectType, byte initialOctet)
        {
            if (Constants.BC_OBJECT_DEF != initialOctet)
            {
                throw Exceptions.UnExpectedInitialOctet(this, initialOctet);
            }

            var type       = (Type)TypeConverter.ReadValue(reader, context, null);
            var len        = (int)IntConverter.ReadValue(reader, context, typeof(int));
            var fieldNames = new List <string>(len);

            for (int i = 0; i < len; i++)
            {
                var fieldName = (string)StringConverter.ReadValue(reader, context, typeof(string));
                fieldNames.Add(fieldName);
            }
            var definition = ClassDefinition.FromHessianDefinition(type, fieldNames);

            context.ClassRefs.AddItem(definition);
            return(definition);
        }
Example #11
0
 public override bool CanRead(byte initialOctet)
 {
     return(StringConverter.CanRead(initialOctet) || IntConverter.CanRead(initialOctet));
 }