internal static void Write(object e, int tag, TarsOutputStream jos)
        {
            TarsStructInfo info = TarsHelper.GetStructInfo(e.GetType());

            if (info == null)
            {
                throw new TarsDecodeException("the class type[" + e.GetType().FullName + "] no attribute Struct");
            }
            jos.WriteHead(TarsStructBase.STRUCT_BEGIN, tag);
            List <TarsStructPropertyInfo> propertysList = info.PropertyList;

            foreach (var propertyInfo in propertysList)
            {
                Object value = propertyInfo.PropertyAccessor.GetValue(e);
                if (value == null && propertyInfo.IsRequire)
                {
                    throw new TarsEncodeException(propertyInfo.Name + " is require tag ,order=" + propertyInfo.Order);
                }
                if (value != null)
                {
                    jos.Write(value, propertyInfo.Order);
                }
            }
            jos.WriteHead(TarsStructBase.STRUCT_END, 0);
        }
 public void DecodeRequestContent(Request req)
 {
     if (req.ParameterTypes.Length > 0)
     {
         if (req.Version == TarsCodecsConstant.VERSION)
         {
             if (req.Parameters == null)
                 req.Parameters = new object[req.ParameterTypes.Length];
             //todo buffer to byte[]
             TarsInputStream jis = new TarsInputStream((byte[])req.Buffer);
             //todo charset
             for (int i = 0; i < req.ParameterTypes.Length; i++)
             {
                 Type type = TarsHelper.GetSourceType(req.ParameterTypes[i].ParameterType);
                 req.Parameters[i] = jis.Read(type, i, false);
             }
         }
         else if (req.Version == TarsCodecsConstant.VERSION3 || req.Version == TarsCodecsConstant.VERSION2)
         {
             if (req.Parameters == null)
                 throw new ArgumentException("params name should not be empty");
             UniAttribute unaIn = new UniAttribute(req.Version);
             unaIn.Decode((byte[])req.Buffer);
             for (int i = 0; i < req.ParameterTypes.Length; i++)
             {
                 req.Parameters[i] = unaIn.GetByType(req.ParameterTypes[i].Name, TarsHelper.GetSourceType(req.ParameterTypes[i].ParameterType));
             }
         }
         else
         {
             throw new TarsException(RpcStatusCode.ServerDecodeErr, "un supported protocol, version = " + req.Version);
         }
         req.Buffer = null;
     }
 }
Example #3
0
        internal static object Read(Type type, int tag, bool isRequire, TarsInputStream jis)
        {
            TarsStructInfo info = TarsHelper.GetStructInfo(type);

            if (info == null)
            {
                throw new TarsDecodeException("the class type[" + type.FullName + "] no attribute Struct");
            }
            if (jis.SkipToTag(tag))
            {
                HeadData hd = new HeadData();
                jis.ReadHead(hd);
                if (hd.Type != TarsStructBase.STRUCT_BEGIN)
                {
                    throw new TarsDecodeException("type mismatch.");
                }
                Object result = info.ConstructorInvoker.Invoke();
                List <TarsStructPropertyInfo> propertysList = info.PropertyList;
                foreach (var propertyInfo in propertysList)
                {
                    Object value = jis.Read(propertyInfo.Type, propertyInfo.Order, propertyInfo.IsRequire);
                    propertyInfo.PropertyAccessor.SetValue(result, value);
                }
                jis.SkipToStructEnd();
                return(result);
            }
            else if (isRequire)
            {
                throw new TarsDecodeException("require field not exist.");
            }
            return(null);
        }
 public void DecodeResponseContent(Response resp)
 {
     TarsInputStream tos = new TarsInputStream((byte[])resp.Buffer);
     var returnType = resp.ReturnValueType.ParameterType;
     if (returnType != typeof(void))
     {
         if (returnType == typeof(Task) || returnType == typeof(ValueType))
         {
             resp.ReturnValue = Task.CompletedTask;
         }
         else
         {
             if (returnType.BaseType == typeof(Task))
             {
                 var resultType = returnType.GenericTypeArguments[0];
                 resp.ReturnValue = Task.FromResult(tos.Read(resultType, 0, true));
             }
             else if (returnType.BaseType == typeof(ValueType))
             {
                 var resultType = returnType.GenericTypeArguments[0];
                 var resultItem = tos.Read(resultType, 0, true);
                 resp.ReturnValue = Activator.CreateInstance(returnType, new object[1] { resultItem });
             }
             else
             {
                 resp.ReturnValue = tos.Read(returnType, 0, true);
             }
         }
     }
     if (resp.ReturnParameterTypes.Length > 0)
     {
         if (resp.ReturnParameters == null)
             resp.ReturnParameters = new object[resp.ReturnParameterTypes.Length];
         for (int i = 0; i < resp.ReturnParameterTypes.Length; i++)
         {
             Type type = TarsHelper.GetSourceType(resp.ReturnParameterTypes[i].ParameterType);
             resp.ReturnParameters[i] = tos.Read(type, i + 1, true);
         }
     }
     resp.Buffer = null;
 }