Пример #1
0
        public TarsStruct DirectRead(TarsStruct o, int tag, bool isRequire)
        {
            //o必须有一个无参的构造函数
            TarsStruct reff = null;

            if (SkipToTag(tag))
            {
                try
                {
                    reff = (TarsStruct)BasicClassTypeUtil.CreateObject(o.GetType());
                }
                catch (Exception e)
                {
                    throw new TarsDecodeException(e.Message);
                }

                var hd = new HeadData();
                ReadHead(hd);
                if (hd.Type != (byte)TarsStructType.StructBegin)
                {
                    throw new TarsDecodeException("type mismatch.");
                }
                reff.ReadFrom(this);
                SkipToStructEnd();
            }
            else if (isRequire)
            {
                throw new TarsDecodeException("require field not exist.");
            }

            return(reff);
        }
Пример #2
0
        public IList readList <T>(T l, int tag, bool isRequire)
        {
            // 生成代码时已经往List里面添加了一个元素,纯粹用来作为类型识别,否则java无法识别到List里面放的是什么类型的数据
            if (l == null)
            {
                return(null);
            }

            IList list = BasicClassTypeUtil.CreateObject(l.GetType()) as IList;

            if (list == null)
            {
                return(null);
            }

            object objItem = BasicClassTypeUtil.CreateListItem(list.GetType());

            Array array = readArrayImpl(objItem, tag, isRequire);

            if (array != null)
            {
                list.Clear();
                foreach (object obj in array)
                {
                    list.Add(obj);
                }

                return(list);
            }

            return(null);
        }
Пример #3
0
        public TarsStruct Read(TarsStruct o, int tag, bool isRequire)
        {
            //o必须有一个无参的构造函数
            TarsStruct reff = null;

            if (skipToTag(tag))
            {
                try
                {
                    // 必须重新创建一个,否则,会导致在同一个对象上赋值,这是由于C#的引用引起的
                    reff = (TarsStruct)BasicClassTypeUtil.CreateObject(o.GetType());
                }
                catch (Exception e)
                {
                    throw new TarsDecodeException(e.Message);
                }

                HeadData hd = new HeadData();
                readHead(hd);
                if (hd.type != (byte)TarsStructType.STRUCT_BEGIN)
                {
                    throw new TarsDecodeException("type mismatch.");
                }
                reff.ReadFrom(this);
                skipToStructEnd();
            }
            else if (isRequire)
            {
                throw new TarsDecodeException("require field not exist.");
            }
            return(reff);
        }
Пример #4
0
        public TarsStruct Read(TarsStruct s, int tag, bool isRequire)
        {
            // TarsStruct must have a no-argument constructor.
            TarsStruct ref_s = null;

            if (SkipToTag(tag))
            {
                try
                {
                    // Must be recreated, otherwise it will result in assignment on the same object,
                    // which is caused by a reference to C#.
                    ref_s = (TarsStruct)BasicClassTypeUtil.CreateObject(s.GetType());
                }
                catch (Exception ex)
                {
                    throw new TarsDecodeException(ex.Message);
                }

                HeadData head = new HeadData();
                ReadHead(head);
                if (head.type != (byte)TarsStructType.STRUCT_BEGIN)
                {
                    throw new TarsDecodeException("type mismatch.");
                }
                ref_s.ReadFrom(this);
                SkipToStructEnd();
            }
            else if (isRequire)
            {
                throw new TarsDecodeException("require field not exist.");
            }
            return(ref_s);
        }
Пример #5
0
        public TarsStruct DirectRead(TarsStruct s, int tag, bool isRequire)
        {
            // TarsStruct must have a no-argument constructor.
            TarsStruct ref_s = null;

            if (SkipToTag(tag))
            {
                try
                {
                    ref_s = (TarsStruct)BasicClassTypeUtil.CreateObject(s.GetType());
                }
                catch (Exception ex)
                {
                    throw new TarsDecodeException(ex.Message);
                }

                HeadData head = new HeadData();
                ReadHead(head);
                if (head.type != (byte)TarsStructType.STRUCT_BEGIN)
                {
                    throw new TarsDecodeException("type mismatch.");
                }
                ref_s.ReadFrom(this);
                SkipToStructEnd();
            }
            else if (isRequire)
            {
                throw new TarsDecodeException("require field not exist.");
            }

            return(ref_s);
        }
Пример #6
0
        public IList ReadList <T>(T list, int tag, bool isRequire)
        {
            // When the code is generated, an element has been added to the List,
            // which is purely used as type recognition.
            // Otherwise, java cannot recognize what type of data is placed in the List.
            if (list == null)
            {
                return(null);
            }

            IList _list = BasicClassTypeUtil.CreateObject(list.GetType()) as IList;

            if (_list == null)
            {
                return(null);
            }

            object listItem = BasicClassTypeUtil.CreateListItem(_list.GetType());
            Array  array    = ReadArrayImpl(listItem, tag, isRequire);

            if (array != null)
            {
                _list.Clear();
                foreach (object obj in array)
                {
                    _list.Add(obj);
                }
                return(_list);
            }

            return(null);
        }
Пример #7
0
 void TestTool()
 {
     int        i    = (int)BasicClassTypeUtil.CreateObject <int>();
     uint       ui   = (uint)BasicClassTypeUtil.CreateObject <uint>();
     float      f    = (float)BasicClassTypeUtil.CreateObject <float>();
     List <int> list = (List <int>)BasicClassTypeUtil.CreateObject <List <int> >();
     Dictionary <int, string>      dict     = (Dictionary <int, string>)BasicClassTypeUtil.CreateObject <Dictionary <int, string> >();
     Dictionary <int, List <int> > dictlist = (Dictionary <int, List <int> >)BasicClassTypeUtil.CreateObject <Dictionary <int, List <int> > >();
 }
Пример #8
0
        /// <summary>
        /// 检测传入的元素类型
        /// </summary>
        /// <param name="listTpye"></param>
        /// <param name="o"></param>
        private void CheckObjectType(List <string> listTpye, Object o)
        {
            if (o == null)
            {
                throw new Exception("object is null");
            }

            if (o.GetType().IsArray)
            {
                Type elementType = o.GetType().GetElementType();
                listTpye.Add("list");
                CheckObjectType(listTpye, BasicClassTypeUtil.CreateObject(elementType));
            }
            else if (o is IList)
            {
                listTpye.Add("list");

                IList list = (IList)o;
                if (list.Count > 0)
                {
                    CheckObjectType(listTpye, list[0]);
                }
                else
                {
                    listTpye.Add("?");
                }
            }
            else if (o is IDictionary)
            {
                listTpye.Add("map");
                IDictionary map = (IDictionary)o;
                if (map.Count > 0)
                {
                    foreach (object key in map.Keys)
                    {
                        listTpye.Add(BasicClassTypeUtil.CS2UniType(key.GetType().ToString()));
                        CheckObjectType(listTpye, map[key]);
                        break;
                    }
                }
                else
                {
                    listTpye.Add("?");
                    listTpye.Add("?");
                    //throw new ArgumentException("map  can not is empty");
                }
            }
            else
            {
                listTpye.Add(BasicClassTypeUtil.CS2UniType(o.GetType().ToString()));
            }
        }
Пример #9
0
        public virtual Tuple <object, object[]> DecodeReturnValue(byte[] body, RpcMethodMetadata metdata)
        {
            var    buf    = Unpooled.WrappedBuffer(body);
            var    stream = new TarsInputStream(buf);
            object result = metdata.RealReturnType == typeof(void) ? null
                : stream.Read(BasicClassTypeUtil.CreateObject(metdata.RealReturnType), metdata.ReturnInfo.Position + 1, true);
            var ps = metdata.Parameters
                     .Where(i => i.ParameterType.IsByRef)
                     .Select(i => stream.Read(BasicClassTypeUtil.CreateObject(i.ParameterType), i.Position + 1, false))
                     .ToArray();

            return(Tuple.Create(result, ps));
        }
Пример #10
0
        public virtual object[] DecodeMethodParameters(byte[] body, RpcMethodMetadata metdata)
        {
            if (metdata.Parameters.Length == 0)
            {
                return(new object[0]);
            }

            var buf    = Unpooled.WrappedBuffer(body);
            var stream = new TarsInputStream(buf);

            return(metdata.Parameters
                   .Select(i => stream.Read(BasicClassTypeUtil.CreateObject(i.ParameterType), i.Position + 1, false))
                   .ToArray());
        }
Пример #11
0
        public override object[] DecodeMethodParameters(byte[] body, RpcMethodMetadata metdata)
        {
            if (metdata.Parameters.Length == 0)
            {
                return(new object[0]);
            }
            var buf    = Unpooled.WrappedBuffer(body);
            var stream = new TarsInputStream(buf);
            var unaIn  = CreateUniAttribute();

            unaIn.DecodeTup2(stream);
            return(metdata.Parameters
                   .Select(i => unaIn.GetByClass(i.Name, BasicClassTypeUtil.CreateObject(i.ParameterType),
                                                 stream))
                   .ToArray());
        }
Пример #12
0
        public void Put <T>(string name, T t)
        {
            if (name == null)
            {
                throw new ArgumentException("put key can not be null");
            }
            if (t == null)
            {
                throw new ArgumentException("put value can not be null");
            }
            TarsOutputStream _out = new TarsOutputStream();

            _out.SetServerEncoding(EncodeName);
            _out.Write(t, 0);
            byte[] sBuffer = _out.ToByteArray();
            if (IsVersionTup3)
            {
                if (newData.ContainsKey(name))
                {
                    newData[name] = sBuffer;
                }
                else
                {
                    newData.Add(name, sBuffer);
                }
            }
            else
            {
                List <string> listType = new List <string>();
                CheckObjectType(listType, t);
                string className = BasicClassTypeUtil.TransTypeList(listType);
                Dictionary <string, byte[]> pair = new Dictionary <string, byte[]>(1)
                {
                    { className, sBuffer }
                };
                cachedData.Remove(name);
                if (data.ContainsKey(name))
                {
                    data[name] = pair;
                }
                else
                {
                    data.Add(name, pair);
                }
            }
        }
Пример #13
0
        public override Tuple <object, object[]> DecodeReturnValue(byte[] body, RpcMethodMetadata metdata)
        {
            var    unaIn  = CreateUniAttribute();
            var    buf    = Unpooled.WrappedBuffer(body);
            var    input  = new TarsInputStream(buf);
            object result = null;

            if (metdata.RealReturnType != typeof(void))
            {
                result = unaIn.GetByClass(string.Empty, BasicClassTypeUtil.CreateObject(metdata.RealReturnType), input);
            }
            var ps = metdata.Parameters
                     .Select(i => unaIn.GetByClass(i.Name, BasicClassTypeUtil.CreateObject(i.ParameterType),
                                                   input))
                     .ToArray();

            return(Tuple.Create(result, ps));
        }
Пример #14
0
        public IDictionary readMap <T>(T arg, int tag, bool isRequire)
        {
            IDictionary m = BasicClassTypeUtil.CreateObject(arg.GetType()) as IDictionary;

            if (m == null)
            {
                return(null);
            }

            Type type = m.GetType();

            Type[] argsType = type.GetGenericArguments();
            if (argsType == null || argsType.Length < 2)
            {
                return(null);
            }

            var mk = BasicClassTypeUtil.CreateObject(argsType[0]);
            var mv = BasicClassTypeUtil.CreateObject(argsType[1]);

            if (skipToTag(tag))
            {
                HeadData hd = new HeadData();
                readHead(hd);
                switch (hd.type)
                {
                case (byte)TarsStructType.MAP:
                {
                    int size = Read(0, 0, true);
                    if (size < 0)
                    {
                        throw new TarsDecodeException("size invalid: " + size);
                    }
                    for (int i = 0; i < size; ++i)
                    {
                        mk = Read(mk, 0, true);
                        mv = Read(mv, 1, true);

                        if (mk != null)
                        {
                            if (m.Contains(mk))
                            {
                                m[mk] = mv;
                            }
                            else
                            {
                                m.Add(mk, mv);
                            }
                        }
                    }
                }
                break;

                default:
                {
                    throw new TarsDecodeException("type mismatch.");
                }
                }
            }
            else if (isRequire)
            {
                throw new TarsDecodeException("require field not exist.");
            }
            return(m);
        }
Пример #15
0
        public IDictionary readMap <T>(int tag, bool isRequire)
        {
            T m = (T)BasicClassTypeUtil.CreateObject <T>();

            return(readMap <T>(m, tag, isRequire));
        }
Пример #16
0
 public object Read <T>(T o, int tag, bool isRequire)
 {
     if (o == null)
     {
         o = (T)BasicClassTypeUtil.CreateObject <T>();
     }
     if (o is Byte || o is Char)
     {
         return(Read((byte)0x0, tag, isRequire));
     }
     else if (o is char)
     {
         return(Read((char)0x0, tag, isRequire));
     }
     else if (o is Boolean)
     {
         return(Read(false, tag, isRequire));
     }
     else if (o is short)
     {
         return(Read((short)0, tag, isRequire));
     }
     else if (o is ushort)
     {
         return(Read((ushort)0, tag, isRequire));
     }
     else if (o is int)
     {
         return(Read((int)0, tag, isRequire));
     }
     else if (o is uint)
     {
         return(Read((uint)0, tag, isRequire));
     }
     else if (o is long)
     {
         return(Read((long)0, tag, isRequire));
     }
     else if (o is ulong)
     {
         return(Read((ulong)0, tag, isRequire));
     }
     else if (o is float)
     {
         return(Read((float)0, tag, isRequire));
     }
     else if (o is Double)
     {
         return(Read((double)0, tag, isRequire));
     }
     else if (o is string)
     {
         return(readString(tag, isRequire));
     }
     else if (o is TarsStruct)
     {
         object oo = o;
         return(Read((TarsStruct)oo, tag, isRequire));
     }
     else if (o != null && o.GetType().IsArray)
     {
         if (o is byte[] || o is Byte[])
         {
             return(Read((byte[])null, tag, isRequire));
         }
         else if (o is bool[])
         {
             return(Read((bool[])null, tag, isRequire));
         }
         else if (o is short[])
         {
             return(Read((short[])null, tag, isRequire));
         }
         else if (o is int[])
         {
             return(Read((int[])null, tag, isRequire));
         }
         else if (o is long[])
         {
             return(Read((long[])null, tag, isRequire));
         }
         else if (o is float[])
         {
             return(Read((float[])null, tag, isRequire));
         }
         else if (o is double[])
         {
             return(Read((double[])null, tag, isRequire));
         }
         else
         {
             object oo = o;
             return(readArray((Object[])oo, tag, isRequire));
         }
     }
     else if (o is IList)
     {
         return(readList <T>(o, tag, isRequire));
     }
     else if (o is IDictionary)
     {
         return(readMap <T>(o, tag, isRequire));
     }
     else
     {
         throw new TarsDecodeException("read object error: unsupport type." + o.ToString());
     }
 }
Пример #17
0
        /// <summary>
        /// 获取一个元素,只能用于tup版本2,如果待获取的数据为tup3,则抛异常
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <returns></returns>
        public T Get <T>(string name, TarsInputStream inputStream)
        {
            if (IsPacketTypeTup3)
            {
                throw new Exception("data is encoded by new version, please use GetByClass(String name,T proxy)");
            }

            object obj = null;

            if (!data.ContainsKey(name))
            {
                return((T)obj);
            }
            else if (cachedData.ContainsKey(name))
            {
                if (!cachedData.TryGetValue(name, out obj))
                {
                    obj = null;
                }
                return((T)obj);
            }
            else
            {
                data.TryGetValue(name, out Dictionary <string, byte[]> pair);

                string strBasicType = "";
                string className    = null;
                byte[] d            = new byte[0];

                // 找到和T类型对应的数据data
                foreach (KeyValuePair <string, byte[]> e in pair)
                {
                    className = e.Key;
                    d         = e.Value;

                    if (className == null || className == string.Empty)
                    {
                        continue;
                    }

                    // 比较基本类型
                    strBasicType = BasicClassTypeUtil.CS2UniType(typeof(T).ToString());
                    if (className.Length > 0 && className == strBasicType)
                    {
                        break;
                    }
                    if (strBasicType == "map" && className.Length >= 3 && className.Substring(0, 3).ToLower() == "map")
                    {
                        break;
                    }
                    if (typeof(T).IsArray && className.Length > 3 && className.Substring(0, 4).ToLower() == "list")
                    {
                        break;
                    }
                    if (strBasicType == "list" && className.Length > 3 && className.Substring(0, 4).ToLower() == "list")
                    {
                        break;
                    }
                }

                try
                {
                    object objtmp = GetCacheProxy <T>(className);
                    if (objtmp == null)
                    {
                        return((T)objtmp);
                    }

                    obj = DecodeData(d, objtmp, inputStream);
                    if (obj != null)
                    {
                        SaveDataCache(name, obj);
                    }
                    return((T)obj);
                }
                catch (Exception ex)
                {
                    throw ex;
                    //QTrace.Trace(this + " Get Exception: " + ex.Message);
                    //throw new ObjectCreateException(ex);
                }
            }
        }
Пример #18
0
 private Object GetCacheProxy <T>(string className)
 {
     return(BasicClassTypeUtil.CreateObject <T>());
 }