예제 #1
0
        private IDictionary <TKey, TValue> ReadGenericDictionary <TKey, TValue>(Hessian2Reader reader)
        {
            var result = (IDictionary <TKey, TValue>)_creator();

            reader.AddRef(result);
            while (!reader.HasEnd())
            {
                result.Add(reader.ReadObject <TKey>(), reader.ReadObject <TValue>());
            }
            reader.ReadToEnd();
            return(result);
        }
예제 #2
0
        public override object ReadMap(Hessian2Reader reader)
        {
            if (_genericConverter != null)
            {
                return(_genericConverter(this, reader));
            }
            var result = (IDictionary)_creator();

            reader.AddRef(result);
            while (!reader.HasEnd())
            {
                result.Add(reader.ReadObject(), reader.ReadObject());
            }
            reader.ReadToEnd();
            return(result);
        }
예제 #3
0
        public override object ReadMap(Hessian2Reader reader)
        {
            string val = null;

            while (!reader.HasEnd())
            {
                if ("name" == reader.ReadString())
                {
                    val = reader.ReadString();
                }
                else
                {
                    reader.ReadObject();
                }
            }
            reader.ReadToEnd();
            if (string.IsNullOrEmpty(val))
            {
                throw new HessianException($"the value is null or empty where read enum type {Type.FullName}");
            }
            var result = Enum.Parse(Type, val);

            reader.AddRef(result);
            return(result);
        }
예제 #4
0
        public override object ReadObject(Hessian2Reader reader, ObjectDefinition definition)
        {
            var result = _creator();

            reader.AddRef(result);
            foreach (var field in definition.Fields)
            {
                if (fieldSetters.TryGetValue(field, out var setter))
                {
                    setter.SetValue(result, reader.ReadObject(setter.FieldType));
                }
                else
                {
                    reader.ReadObject();
                }
            }
            return(result);
        }
예제 #5
0
        public override object ReadObject(Hessian2Reader reader, ObjectDefinition definition)
        {
            var result = new JavaException();

            reader.AddRef(result);
            string msg = null;

            JavaStackTrace[] traces = null;
            JavaException[]  suppressedExceptions = null;
            JavaException    innerException       = null;

            foreach (var key in definition.Fields)
            {
                switch (key)
                {
                case "detailMessage":
                    msg = reader.ReadString();
                    break;

                case "stackTrace":
                    traces = reader.ReadObject <JavaStackTrace[]>();
                    break;

                case "suppressedExceptions":
                    suppressedExceptions = reader.ReadObject <JavaException[]>();
                    break;

                case "cause":
                    innerException = reader.ReadObject <JavaException>();
                    break;

                default:
                    var info = reader.ReadObject();
                    if (info != result)
                    {
                        result.Data.Add(key, reader.ReadObject());
                    }
                    break;
                }
            }

            result.ResumeException(definition.Type, msg, traces, suppressedExceptions, innerException == result ? null : innerException);
            return(result);
        }
예제 #6
0
        public override object ReadMap(Hessian2Reader reader)
        {
            var result = _creator();

            reader.AddRef(result);
            while (!reader.HasEnd())
            {
                var field = reader.ReadString();
                if (fieldSetters.TryGetValue(field, out var setter))
                {
                    setter.SetValue(result, reader.ReadObject(setter.FieldType));
                }
                else
                {
                    reader.ReadObject();
                }
            }
            reader.ReadToEnd();
            return(result);
        }
예제 #7
0
        private T[] ReadArray(Hessian2Reader reader, int length)
        {
            var result = new T[length];

            reader.AddRef(result);
            for (var i = 0; i < result.Length; i++)
            {
                result[i] = reader.ReadObject <T>();
            }
            return(result);
        }
예제 #8
0
        public override object ReadMap(Hessian2Reader reader)
        {
            var result = new JavaException();

            reader.AddRef(result);
            string msg = null;

            JavaStackTrace[] traces = null;
            JavaException[]  suppressedExceptions = null;
            JavaException    innerException       = null;

            while (!reader.HasEnd())
            {
                var key = reader.ReadString();
                switch (key)
                {
                case "detailMessage":
                    msg = reader.ReadString();
                    break;

                case "stackTrace":
                    traces = reader.ReadObject <JavaStackTrace[]>();
                    break;

                case "suppressedExceptions":
                    suppressedExceptions = reader.ReadObject <JavaException[]>();
                    break;

                case "cause":
                    innerException = reader.ReadObject <JavaException>();
                    break;

                default:
                    result.Data.Add(key, reader.ReadObject());
                    break;
                }
            }
            reader.ReadToEnd();
            result.ResumeException(string.Empty, msg, traces, suppressedExceptions, innerException);
            return(result);
        }
예제 #9
0
        private T[] ReadList(Hessian2Reader reader)
        {
            var list = new List <T>();

            reader.AddRef(list);
            while (!reader.HasEnd())
            {
                list.Add(reader.ReadObject <T>());
            }
            reader.ReadToEnd();
            return(list.ToArray());
        }
        private static ICollection <TItem> ReadGenericList <T, TItem>(Hessian2Reader reader, int length) where T : ICollection <TItem>, new()
        {
            ICollection <TItem> result = new T();

            reader.AddRef(result);
            if (length >= 0)
            {
                for (var i = 0; i < length; i++)
                {
                    result.Add(reader.ReadObject <TItem>());
                }
            }
            else
            {
                while (!reader.HasEnd())
                {
                    result.Add(reader.ReadObject <TItem>());
                }
                reader.ReadToEnd();
            }
            return(result);
        }
예제 #11
0
        private static IEnumerable <T> Read <T>(Hessian2Reader reader, int length)
        {
            var list = new List <T>();

            reader.AddRef(list);
            if (length >= 0)
            {
                for (var i = 0; i < length; i++)
                {
                    list.Add(reader.ReadObject <T>());
                }
            }
            else
            {
                while (!reader.HasEnd())
                {
                    list.Add(reader.ReadObject <T>());
                }
                reader.ReadToEnd();
            }
            return(list);
        }
        private ICollection ReadObjectList(Hessian2Reader reader, int length)
        {
            var result = new ArrayList();

            reader.AddRef(result);
            if (length >= 0)
            {
                for (var i = 0; i < length; i++)
                {
                    result.Add(reader.ReadObject());
                }
            }
            else
            {
                while (!reader.HasEnd())
                {
                    result.Add(reader.ReadObject());
                }
                reader.ReadToEnd();
            }

            return(result);
        }
예제 #13
0
        public override object ReadObject(Hessian2Reader reader, ObjectDefinition definition)
        {
            if (Type.IsGenericType)
            {
                var dic = (IDictionary <string, object>)_creator();
                reader.AddRef(dic);
                foreach (var field in definition.Fields)
                {
                    dic.Add(field, reader.ReadObject());
                }
                dic.Add("$Type", definition.Type);
                return(dic);
            }
            var hashTable = (IDictionary)_creator();

            reader.AddRef(hashTable);
            foreach (var field in definition.Fields)
            {
                hashTable.Add(field, reader.ReadObject());
            }
            hashTable.Add("$Type", definition.Type);
            return(hashTable);
        }
예제 #14
0
        public override object ReadObject(Hessian2Reader reader, ObjectDefinition definition)
        {
            string value = null;

            foreach (var name in definition.Fields)
            {
                if ("value" == (name))
                {
                    value = reader.ReadString();
                }
                else
                {
                    reader.ReadObject();
                }
            }
            var result = convertFunc(value);

            reader.AddRef(result);
            return(result);
        }
예제 #15
0
        public override object ReadObject(Hessian2Reader reader, ObjectDefinition definition)
        {
            var result = _creator();

            reader.AddRef(result);

            foreach (var propertyName in definition.Fields)
            {
                if (_propertyInfos.TryGetValue(propertyName, out var setter))
                {
                    setter(result, reader);
                }
                else
                {
                    reader.ReadObject();
                }
            }

            return(result);
        }
예제 #16
0
        public override object ReadMap(Hessian2Reader reader)
        {
            string value = null;

            while (!reader.HasEnd())
            {
                var key = reader.ReadString();
                if (key == "value")
                {
                    value = reader.ReadString();
                }
                else
                {
                    reader.ReadObject();
                }
            }
            reader.ReadToEnd();
            var result = convertFunc(value);

            reader.AddRef(result);
            return(result);
        }
예제 #17
0
        public override object ReadMap(Hessian2Reader reader)
        {
            var result = _creator();

            reader.AddRef(result);

            while (!reader.HasEnd())
            {
                var propertyName = reader.ReadString();
                if (_propertyInfos.TryGetValue(propertyName, out var setter))
                {
                    setter(result, reader);
                }
                else
                {
                    reader.ReadObject();
                }
            }

            reader.ReadToEnd();
            return(result);
        }
예제 #18
0
        public override object ReadObject(Hessian2Reader reader, ObjectDefinition definition)
        {
            string val = null;

            foreach (var name in definition.Fields)
            {
                if ("name" == name)
                {
                    val = reader.ReadString();
                }
                else
                {
                    reader.ReadObject();
                }
            }
            if (string.IsNullOrEmpty(val))
            {
                throw new HessianException($"the value is null or empty where read enum type {Type.FullName}");
            }
            var result = Enum.Parse(Type, val);

            reader.AddRef(result);
            return(result);
        }
예제 #19
0
        public override object ReadObject(Hessian2Reader reader)
        {
            switch (_type)
            {
            case BasicType.Null:
                return(reader.ReadObject());

            case BasicType.Bool:
                return(reader.ReadBool());

            case BasicType.Byte:
                return((byte)reader.ReadInt());

            case BasicType.SByte:
                return((sbyte)reader.ReadInt());

            case BasicType.Short:
                return(reader.ReadShort());

            case BasicType.UShort:
                return((ushort)reader.ReadInt());

            case BasicType.Int:
                return(reader.ReadInt());

            case BasicType.UInt:
                return((uint)reader.ReadLong());

            case BasicType.Long:
                return(reader.ReadLong());

            case BasicType.Float:
                return(reader.ReadFloat());

            case BasicType.Double:
                return(reader.ReadDouble());

            case BasicType.Char:
                var str = reader.ReadString();
                return(string.IsNullOrEmpty(str) ? (char)0 : str[0]);

            case BasicType.CharArray:
                var charsString = reader.ReadString();
                return(string.IsNullOrEmpty(charsString) ? new char[0] : charsString.ToCharArray());

            case BasicType.String:
                return(reader.ReadString());

            case BasicType.Date:
                return(reader.ReadDate());

            case BasicType.ByteArray:
                return(reader.ReadBytes());

            case BasicType.Object:
                return(reader.ReadObject());

            case BasicType.BoolArray:
            case BasicType.ShortArray:
            case BasicType.IntArray:
            case BasicType.LongArray:
            case BasicType.FloatArray:
            case BasicType.StringArray:
            case BasicType.DoubleArray:
            case BasicType.SByteArray:
            case BasicType.UShortArray:
            case BasicType.UIntArray:
            case BasicType.DateArray:
            case BasicType.ObjectArray:
                return(reader.ReadObject());

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
예제 #20
0
        public static Response DecodeResponse(Stream inputStream)
        {
            var resHeader = new byte[16];

            inputStream.ReadBytes(resHeader);

            if ((resHeader[2] & RequestFlag) != 0)
            {
                throw new ArgumentException("decode response fail. the stream is not response.");
            }

            var response = new Response
            {
                ResponseId = resHeader.ReadLong(4),
                Status     = resHeader[3],
                IsEvent    = (resHeader[2] & EventFlag) != 0,
                IsTwoWay   = (resHeader[2] & TwowayFlag) != 0
            };
            var request    = RequestTasks.GetRequestTask(response.ResponseId);
            var bodyLength = resHeader.ReadInt(12);
            var body       = new byte[bodyLength];

            inputStream.ReadBytes(body);
            var reader = new Hessian2Reader(new MemoryStream(body));

            if (response.IsOk)
            {
                if (response.IsEvent)
                {
                    response.Result = reader.ReadObject();
                }
                else
                {
                    var resultType = request?.Request.ReturnType;
                    var flag       = (byte)reader.ReadInt();
                    switch (flag)
                    {
                    case Response.Null:
                        break;

                    case Response.Value:
                        response.Result = reader.ReadObject(resultType);
                        break;

                    case Response.Exception:
                        response.Error = reader.ReadObject <JavaException>();
                        break;

                    case Response.NullWithAttachment:
                        response.Attachments = reader.ReadObject <Dictionary <string, string> >();
                        break;

                    case Response.ValueWithAttachment:
                        response.Result      = reader.ReadObject(resultType);
                        response.Attachments = reader.ReadObject <Dictionary <string, string> >();
                        break;

                    case Response.ExceptionWithAttachment:
                        response.Error       = reader.ReadObject <JavaException>();
                        response.Attachments = reader.ReadObject <Dictionary <string, string> >();
                        break;

                    default:
                        throw new IOException("Unknown result flag, expect '0' '1' '2', get " + flag);
                    }
                }
            }
            else
            {
                response.ErrorMessage = reader.ReadString();
                response.Error        = new Exception(response.ErrorMessage);
            }

            request?.Task.TrySetResult(response);
            return(response);
        }