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; } }
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; }
public Response DecodeResponse(IByteBuffer input) { if (input.Capacity < TarsCodecsConstant.HEAD_SIZE) { return(null); } TarsInputStream tos = new TarsInputStream(input); int length = tos.GetDataLength() - TarsCodecsConstant.HEAD_SIZE; if (length > TarsCodecsConstant.PACKAGE_MAX_LENGTH || length <= 0) { throw new ProtocolException("the length header of the package must be between 0~10M bytes. data length:" + length); } //校验数据 if (input.Capacity - input.ReaderIndex < length) { return(null); } Response response = new Response(); response.Version = tos.ReadShort(1, true); int packetType = tos.ReadByte(2, true); response.RequestId = tos.ReadInt(3, true); response.MessageType = tos.ReadInt(4, true); response.ResultStatusCode = (RpcStatusCode)tos.ReadInt(5, true); if (response.ResultStatusCode == RpcStatusCode.ServerSuccess) { response.Buffer = tos.ReadByteArray(6, true); } response.Status = tos.ReadMap(7, false); if (response.ResultStatusCode != RpcStatusCode.ServerSuccess) { response.ResultDesc = tos.ReadString(8, false); } return(response); }
public Request DecodeRequest(IByteBuffer input) { if (input.Capacity < TarsCodecsConstant.HEAD_SIZE) { return(null); } int length = input.ReadInt() - TarsCodecsConstant.HEAD_SIZE; if (length > TarsCodecsConstant.PACKAGE_MAX_LENGTH || length <= 0) { throw new ProtocolException("the length header of the package must be between 0~10M bytes. data length:" + length); } if (input.Capacity < length) { return(null); } try { TarsInputStream jis = new TarsInputStream(input); Request request = new Request(); request.Version = jis.ReadShort(1, true); request.PacketType = jis.ReadByte(2, true); request.MessageType = jis.ReadInt(3, true); request.RequestId = jis.ReadInt(4, true); request.ServantName = jis.ReadString(5, true); request.FuncName = jis.ReadString(6, true); request.Buffer = jis.ReadByteArray(7, true); //数据 request.Timeout = jis.ReadInt(8, true); //超时时间 request.Context = jis.ReadMap(9, true); request.Status = jis.ReadMap(10, true); return(request); } catch (Exception ex) { throw new TarsException(RpcStatusCode.ServerDecodeErr, ex); } }
public abstract void ReadFrom(TarsInputStream stream);