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

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

            writer.Write(Constants.BC_MAP_UNTYPED);


            var kvType         = typeof(KeyValuePair <,>).MakeGenericType(type.GenericTypeArguments);
            var keyProperty    = kvType.GetProperty("Key");
            var valueProperty  = kvType.GetProperty("Value");
            var keyConverter   = AutoConverter.GetConverter(type.GenericTypeArguments[0]);
            var valueConverter = AutoConverter.GetConverter(type.GenericTypeArguments[1]);

            foreach (var entry in (IEnumerable)value)
            {
                keyConverter.WriteValue(writer, context, keyProperty.GetValue(entry));
                valueConverter.WriteValue(writer, context, valueProperty.GetValue(entry));
            }

            writer.Write(Constants.BC_END);
        }
Exemplo n.º 2
0
        public override void WriteValueNotNull(HessianWriter writer, HessianContext context, object value)
        {
            var    type = value.GetType();
            string s    = null;

            if (type == typeof(string))
            {
                s = (string)value;
            }
            else if (type == typeof(char))
            {
                s = value.ToString();
            }
            else
            {
                throw Exceptions.UnExpectedTypeException(type);
            }

            int length = s.Length;
            int index  = 0;

            while (index + Constants.BINARY_CHUNK_SIZE < length)
            {
                writer.Write(Constants.BC_STRING_CHUNK);
                writer.Write((short)Constants.STRING_CHUNK_SIZE);
                writer.Write(s.Substring(index, Constants.STRING_CHUNK_SIZE));
                index += Constants.BINARY_CHUNK_SIZE;
            }

            int leftSize = length - index;

            if (leftSize == 0)
            {
                if (index == 0)
                {
                    writer.Write(Constants.BC_STRING_DIRECT);
                }
            }
            else
            {
                if (leftSize <= Constants.STRING_DIRECT_MAX)
                {
                    writer.Write((byte)(Constants.BC_STRING_DIRECT + leftSize));
                }
                else if (leftSize <= Constants.STRING_SHORT_MAX)
                {
                    writer.Write((byte)(Constants.BC_STRING_SHORT + (leftSize >> 8)));
                    writer.Write((byte)leftSize);
                }
                else
                {
                    writer.Write(Constants.BC_STRING);
                    writer.Write((short)leftSize);
                }

                writer.Write(s.Substring(index, leftSize));
            }
        }
Exemplo n.º 3
0
        public override void WriteValueNotNull(HessianWriter writer, HessianContext context, object value)
        {
            var    type = value.GetType();
            double v    = 0;

            if (type == typeof(double))
            {
                v = (double)value;
            }
            else if (type == typeof(float))
            {
                v = Convert.ToDouble(value);
            }
            else
            {
                throw Exceptions.UnExpectedTypeException(type);
            }

            int intValue = (int)v;

            if (intValue == v)
            {
                if (intValue == 0)
                {
                    writer.Write(Constants.BC_DOUBLE_ZERO);
                    return;
                }
                else if (intValue == 1)
                {
                    writer.Write(Constants.BC_DOUBLE_ONE);
                    return;
                }
                else if (-0x80 <= intValue && intValue < 0x80)
                {
                    writer.Write(Constants.BC_DOUBLE_BYTE);
                    writer.Write((byte)intValue);
                    return;
                }
                else if (-0x8000 <= intValue && intValue < 0x8000)
                {
                    writer.Write(Constants.BC_DOUBLE_SHORT);
                    writer.Write((short)intValue);
                    return;
                }
            }

            int mills = (int)(v * 1000);

            if (0.001 * mills == v)
            {
                writer.Write(Constants.BC_DOUBLE_MILL);
                writer.Write(mills);
                return;
            }

            writer.Write(Constants.BC_DOUBLE);
            writer.Write(v);
        }
Exemplo n.º 4
0
 public void WriteLong(HessianWriter writer, long value)
 {
     if (Constants.LONG_DIRECT_MIN <= value && value <= Constants.LONG_DIRECT_MAX)
     {
         writer.Write((byte)(Constants.BC_LONG_ZERO + value));
     }
     else if (Constants.LONG_BYTE_MIN <= value && value <= Constants.LONG_BYTE_MAX)
     {
         writer.Write((byte)(Constants.BC_LONG_BYTE_ZERO + (value >> 8)));
         writer.Write((byte)value);
     }
     else if (Constants.LONG_SHORT_MIN <= value && value <= Constants.LONG_SHORT_MAX)
     {
         writer.Write((byte)(Constants.BC_LONG_SHORT_ZERO + (value >> 16)));
         writer.Write((short)value);
     }
     else if (Constants.LONG_INT_MIN <= value && value <= Constants.LONG_INT_MAX)
     {
         writer.Write((byte)(Constants.BC_LONG_INT));
         writer.Write((int)value);
     }
     else
     {
         writer.Write(Constants.BC_LONG);
         writer.Write(value);
     }
 }
Exemplo n.º 5
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);
            }
        }
Exemplo n.º 6
0
        public virtual void WriteValue(HessianWriter writer, HessianContext context, object value)
        {
            if (value == null)
            {
                writer.Write(Constants.BC_NULL);
                return;
            }

            WriteValueNotNull(writer, context, value);
        }
Exemplo n.º 7
0
        public override void WriteValueNotExisted(HessianWriter writer, HessianContext context, object value)
        {
            var definition = ClassDefinition.ForType(value.GetType());

            ClassDefinitionConverter.WriteClassDefinition(writer, context, definition, out int definitionIndex);
            if (definitionIndex <= Constants.OBJECT_DIRECT_MAX)
            {
                writer.Write((byte)(Constants.BC_OBJECT_DIRECT + definitionIndex));
            }
            else
            {
                writer.Write(Constants.BC_OBJECT);
                writer.Write(definitionIndex);
            }
            definition.VisitFields(field =>
            {
                var fieldValue = field.Getter(value);
                AutoConverter.WriteValue(writer, context, fieldValue);
            });
        }
Exemplo n.º 8
0
        public override void WriteValueNotNull(HessianWriter writer, HessianContext context, object value)
        {
            var type = value.GetType();

            if (type != typeof(bool))
            {
                throw Exceptions.UnExpectedTypeException(type);
            }

            bool b = (bool)value;

            if (b)
            {
                writer.Write(Constants.BC_TRUE);
            }
            else
            {
                writer.Write(Constants.BC_FALSE);
            }
        }
Exemplo n.º 9
0
 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);
     }
 }
Exemplo n.º 10
0
        public override void WriteValueNotExisted(HessianWriter writer, HessianContext context, object value)
        {
            Type type     = value.GetType();
            Type itemType = null;

            if (type == typeof(ArrayList))
            {
                itemType = typeof(object);
            }
            else if (IsGenericList(type))
            {
                itemType = type.GenericTypeArguments[0];
            }
            else
            {
                throw Exceptions.UnExpectedTypeException(type);
            }

            HessianConverter itemConverter = null;

            if (itemType == typeof(object))
            {
                writer.Write(Constants.BC_LIST_VARIABLE_UNTYPED);
                itemConverter = AutoConverter;
            }
            else
            {
                writer.Write(Constants.BC_LIST_VARIABLE);
                TypeConverter.WriteType(writer, context, type);
                itemConverter = AutoConverter.GetConverter(itemType);
            }

            foreach (var item in (IEnumerable)value)
            {
                itemConverter.WriteValue(writer, context, item);
            }
            writer.Write(Constants.BC_END);
        }
Exemplo n.º 11
0
        public override void WriteValueNotNull(HessianWriter writer, HessianContext context, object value)
        {
            var type = value.GetType();

            if (type != typeof(DateTime))
            {
                throw Exceptions.UnExpectedTypeException(type);
            }

            DateTime time = (DateTime)value;

            if (time.Kind == DateTimeKind.Unspecified)
            {
                time = new DateTime(time.Ticks, DateTimeKind.Utc);
            }
            else if (time.Kind == DateTimeKind.Local)
            {
                time = time.ToUniversalTime();
            }
            long timeStamp = (time.Ticks - BeginDate.Ticks) / 10000;

            if (timeStamp % 60000L == 0)
            {
                long minutes = timeStamp / 60000L;

                if ((minutes >> 31) == 0 || (minutes >> 31) == -1)
                {
                    writer.Write(Constants.BC_DATE_MINUTE);
                    writer.Write((int)minutes);
                    return;
                }
            }

            writer.Write(Constants.BC_DATE);
            writer.Write(timeStamp);
        }
Exemplo n.º 12
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;
        }
Exemplo n.º 13
0
        public override void WriteValueNotNull(HessianWriter writer, HessianContext context, object value)
        {
            byte[] bytes  = (byte[])value;
            int    length = bytes.Length;
            int    index  = 0;

            while (index + Constants.BINARY_CHUNK_SIZE < length)
            {
                writer.Write(Constants.BC_BINARY_CHUNK);
                writer.Write((short)Constants.BINARY_CHUNK_SIZE);
                writer.Write(bytes, index, Constants.BINARY_CHUNK_SIZE);
                index += Constants.BINARY_CHUNK_SIZE;
            }

            int leftSize = length - index;

            if (leftSize == 0)
            {
                if (index == 0)
                {
                    writer.Write(Constants.BC_BINARY_DIRECT);
                }
            }
            else
            {
                if (leftSize <= Constants.BINARY_DIRECT_MAX)
                {
                    writer.Write((byte)(Constants.BC_BINARY_DIRECT + leftSize));
                }
                else
                {
                    writer.Write(Constants.BC_BINARY);
                    writer.Write((short)leftSize);
                }

                writer.Write(bytes, index, leftSize);
            }
        }
Exemplo n.º 14
0
 public void WriteInt(HessianWriter writer, HessianContext context, int value)
 {
     if (Constants.INT_DIRECT_MIN <= value && value <= Constants.INT_DIRECT_MAX)
     {
         writer.Write((byte)(Constants.BC_INT_ZERO + value));
     }
     else if (Constants.INT_BYTE_MIN <= value && value <= Constants.INT_BYTE_MAX)
     {
         writer.Write((byte)(Constants.BC_INT_BYTE_ZERO + (value >> 8)));
         writer.Write((byte)value);
     }
     else if (Constants.INT_SHORT_MIN <= value && value <= Constants.INT_SHORT_MAX)
     {
         writer.Write((byte)(Constants.BC_INT_SHORT_ZERO + (value >> 16)));
         writer.Write((short)value);
     }
     else
     {
         writer.Write(Constants.BC_INT);
         writer.Write(value);
     }
 }