Пример #1
0
        public static bool DumpValue(StringBuilder str, object obj, string fieldName, int level, int maxDepth)
        {
            StrUtils.AppendTabs(str, level);
            if (obj == null)
            {
                str.AppendFormat("{0} = null\r\n", fieldName);
                return(true);
            }

            IEnumerable e        = obj as IEnumerable;
            string      typeName = ObjectHelper.GetTypeName(obj.GetType(), false);

            if (e == null || obj.GetType() == typeof(string))
            {
                str.AppendFormat("{0}: {1} = {2}\r\n", fieldName, typeName, obj);
            }
            else if (obj.GetType() == typeof(byte[]))
            {
                var b = obj as byte[];
                str.AppendFormat("{0}: byte[] = {{ ", fieldName);
                if (b.Length > 16)
                {
                    str.Append("\r\n");
                    StrUtils.AppendTabs(str, level + 1);
                }
                for (int i = 0; i < b.Length; i++)
                {
                    if (i % 16 == 0 && i > 0)
                    {
                        str.Append("\r\n");
                        StrUtils.AppendTabs(str, level + 1);
                    }
                    str.AppendFormat("{0:X2}", b[i]);
                    if (i != b.Length - 1)
                    {
                        str.Append(",");
                    }
                }
                str.Append(" }}");
                str.Append("\r\n");
            }
            else
            {
                int i = 0;
                str.AppendFormat("{0}: {1} = {{\r\n", fieldName, typeName);
                foreach (object o in e)
                {
                    string s = string.Format("{0}[{1}]", fieldName, i + 1);
                    if (!DumpInner(str, o, fieldName, level + 1, maxDepth))
                    {
                        return(false);
                    }
                }
                str.Append("}\r\n");
            }
            return(true);
        }
Пример #2
0
        public T EndInvoke <T>()
        {
            T   retValue;
            var request  = _trans.Request;
            var response = _trans.Response;

            if (response.ErrorCode != RpcErrorCode.OK)
            {
                throw new RpcException(response.ErrorCode, _trans.ServiceUrl, "RpcResponse Failed", response.Error);
            }

            if (typeof(T) == typeof(RpcNull) || _trans.Response.BodyBuffer == null)
            {
                retValue = default(T);
            }
            else
            {
                try {
                    retValue = _trans.Response.BodyBuffer.GetValue <T>();
                } catch (Exception ex) {
                    byte[] buffer = _trans.Response.BodyBuffer.GetByteArray();

                    _observer.RequestTracer.WarnFmt2(
                        ex,
                        request.ServiceAtComputer,
                        request.ContextUri,
                        "ResponseBuffer byte[{0}] = {1}",
                        buffer.Length,
                        StrUtils.ToHexString(buffer, 500)
                        );
                    throw new RpcException(RpcErrorCode.InvaildResponseArgs, _trans.ServiceUrl, "RpcClientContext.EndInvoke<T>, Failed", ex);
                }
            }

            TracingManager.Info(
                delegate() {
                _observer.ResponseTracer.InfoFmt2(
                    request.FromService,
                    request.ContextUri,
                    "Args={0}\r\nResults={1}",
                    ObjectHelper.DumpObject(request.BodyValue),
                    ObjectHelper.DumpObject(response.BodyValue)
                    );
            }
                );

            return(retValue);
        }
Пример #3
0
        public bool DumpInfo(T T1, StringBuilder str, string fieldName, int level, int maxDepth, Func <StringBuilder, object, string, int, int, bool> ac)
        {
            bool        isContinue = true;
            ICollection coll       = (ICollection)T1;

            StrUtils.AppendTabs(str, level);
            str.AppendFormat("{0}:{1} {{\r\n", fieldName, ObjectHelper.GetTypeName(coll.GetType(), false));
            IEnumerator ie = ((IEnumerable)coll).GetEnumerator();
            int         i  = 0;

            while (ie.MoveNext())
            {
                if (ie.Current.GetType().IsValueType || ie.Current.GetType() == typeof(string))
                {
                    StrUtils.AppendTabs(str, level);
                    str.AppendFormat("{0}[{1}]= {2}\r\n", fieldName, i, ie.Current);

                    if (str.Length > 8192)
                    {
                        str.Append("...TOO LONG...");
                        isContinue = false;
                    }

                    if (!isContinue)
                    {
                        break;
                    }
                }
                else
                {
                    isContinue = ac.Invoke(str, ie.Current, fieldName, level, maxDepth);
                    str.Append("\r\n");
                    if (!isContinue)
                    {
                        break;
                    }
                }
                i++;
            }

            StrUtils.AppendTabs(str, level);
            str.Append("}\r\n");

            return(isContinue);
        }
Пример #4
0
        public bool DumpInfo(T T1, StringBuilder str, string fieldName, int level, int maxDepth, Func <StringBuilder, object, string, int, int, bool> ac)
        {
            bool  isContinue = true;
            IList list       = (IList)T1;

            StrUtils.AppendTabs(str, level);
            str.AppendFormat("{0}:{1} {{\r\n", fieldName, ObjectHelper.GetTypeName(list.GetType(), false));
            for (int i = 0; i < list.Count; i++)
            {
                Type type = list[i].GetType();
                if (type.IsValueType || type == typeof(string))
                {
                    StrUtils.AppendTabs(str, level);
                    str.AppendFormat("{0}[{1}]= {2}\r\n", fieldName, i, list[i]);

                    if (str.Length > 8192)
                    {
                        str.Append("...TOO LONG...");
                        isContinue = false;
                    }

                    if (!isContinue)
                    {
                        break;
                    }
                }
                else
                {
                    isContinue = ac.Invoke(str, list[i], fieldName, level, maxDepth);

                    if (!isContinue)
                    {
                        break;
                    }
                }
            }
            StrUtils.AppendTabs(str, level);
            str.Append("}\r\n");

            return(isContinue);
        }
Пример #5
0
        /// <summary>
        ///		获取请求参数
        /// </summary>
        /// <typeparam name="T">请求参数类型</typeparam>
        /// <returns>请求参数</returns>
        public T GetArgs <T>()
        {
            try {
                T ret;
                if (_request.BodyBuffer == null)
                {
                    object a = null;
                    ret = (T)a;
                }
                else
                {
                    ret = _trans.Request.BodyBuffer.GetValue <T>();

                    TracingManager.Info(
                        delegate() {
                        _observer.RequestTracer.InfoFmt2(
                            _request.ServiceAtComputer,
                            _request.ContextUri,
                            "Args={0}",
                            ObjectHelper.DumpObject(_trans.Request.BodyValue)
                            );
                    }
                        );
                }
                return(ret);
            } catch (Exception ex) {
                byte[] buffer = _trans.Request.BodyBuffer.GetByteArray();

                _observer.RequestTracer.ErrorFmt2(
                    ex,
                    _request.ServiceAtComputer,
                    _request.ContextUri,
                    "RequestBuffer byte[{0}] = {1}",
                    buffer.Length,
                    StrUtils.ToHexString(buffer, 500)
                    );
                throw new RpcException(RpcErrorCode.InvaildRequestArgs, _trans.ServiceUrl, "RpcServerContext.GetArgs(), Failed", ex);
            }
        }
Пример #6
0
        public bool DumpInfo(T T1, StringBuilder str, string fieldName, int level, int maxDepth, Func <StringBuilder, object, string, int, int, bool> ac)
        {
            bool        isContinue = true;
            IDictionary dict       = (IDictionary)T1;

            StrUtils.AppendTabs(str, level);
            str.AppendFormat("{0}:{1} {{\r\n", fieldName, ObjectHelper.GetTypeName(dict.GetType(), false));
            IDictionaryEnumerator e = dict.GetEnumerator();
            bool Iskey   = false;
            bool isValue = false;

            while (e.MoveNext())
            {
                Iskey   = e.Key.GetType().IsValueType || e.Key.GetType() == typeof(string);
                isValue = e.Value.GetType().IsValueType || e.Value.GetType() == typeof(string);

                if (Iskey)
                {
                    StrUtils.AppendTabs(str, level);
                    str.AppendFormat("[{0}, ", e.Key);

                    if (str.Length > 8192)
                    {
                        str.Append("...TOO LONG...");
                        isContinue = false;
                    }

                    if (!isContinue)
                    {
                        break;
                    }
                }
                else
                {
                    isContinue = ac.Invoke(str, e.Key, "Key", level, maxDepth);
                    if (!isContinue)
                    {
                        break;
                    }
                }

                if (isValue)
                {
                    str.AppendFormat("{0}]\r\n ", e.Value);

                    if (str.Length > 8192)
                    {
                        str.Append("...TOO LONG...");
                        isContinue = false;
                    }

                    if (!isContinue)
                    {
                        break;
                    }
                }
                else
                {
                    isContinue = ac.Invoke(str, e.Value, "Value", level, maxDepth);
                    str.Append("\r\n");
                    if (!isContinue)
                    {
                        break;
                    }
                }
            }
            StrUtils.AppendTabs(str, level);
            str.Append("}\r\n");

            return(isContinue);
        }
Пример #7
0
        public static bool DumpInner(StringBuilder str, object obj, string fieldName, int level, int maxDepth)
        {
            bool isContinue = true;

            if (str.Length > 8192)
            {
                str.Append("...TOO LONG...");
                return(false);
            }

            if (level > maxDepth)
            {
                str.Append("...MAX DEPTH...");
                return(true);
            }

            if (obj == null)
            {
                DumpValue(str, obj, fieldName, level, maxDepth);
                return(true);
            }

            Type type = obj.GetType();

            if (type.IsValueType || type == typeof(string) || type == typeof(byte[]))
            {
                DumpValue(str, obj, fieldName, level, maxDepth);
                return(true);
            }
            else
            {
                Func <StringBuilder, object, string, int, int, bool> ac = DumpInner;
                bool IsAssign = IsAssignInstance(obj, str, fieldName, level, maxDepth, ac, out isContinue);

                if (IsAssign)
                {
                    return(isContinue);
                }
            }

            FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            if (fields.Length == 0)
            {
                DumpValue(str, obj, fieldName, level, maxDepth);
                return(true);
            }
            else
            {
                StrUtils.AppendTabs(str, level);
                string typeName = ObjectHelper.GetTypeName(obj.GetType(), false);
                str.AppendFormat("{0}: {1} {{\r\n", fieldName, typeName, obj);
                foreach (FieldInfo field in fields)
                {
                    object fieldObj = field.GetValue(obj);
                    isContinue = DumpInner(str, fieldObj, field.Name, level + 1, maxDepth);

                    if (!isContinue)
                    {
                        break;
                    }
                }
                StrUtils.AppendTabs(str, level);
                str.Append("}");
            }
            return(isContinue);
        }
Пример #8
0
 /// <summary>
 /// 使用utf8编码将字符串散列
 /// </summary>
 /// <param name="sourceString">要散列的字符串</param>
 /// <returns>散列后的字符串</returns>
 public static string HashString(string sourceString)
 {
     byte[] source = md5.ComputeHash(Encoding.UTF8.GetBytes(sourceString));
     return(StrUtils.ToHexString(source));
 }