private void SerializeV2OrV3(Response obj, IByteBuffer buffer, TarsConvertOptions options) { options.Tag = 3; intConvert.Serialize(obj.MessageType, buffer, options); options.Tag = 4; intConvert.Serialize(obj.RequestId, buffer, options); options.Tag = 5; stringConvert.Serialize(obj.ServantName, buffer, options); options.Tag = 6; stringConvert.Serialize(obj.FuncName, buffer, options); if (options.Version == TarsCodecsVersion.V2) { var uni = new UniAttributeV2(convertRoot, headHandler) { Temp = new Dictionary <string, IDictionary <string, IByteBuffer> >() }; var type = obj.ReturnValueType.ParameterType; if (obj.ReturnValue != null && type != typeof(void)) { uni.Put(string.Empty, obj.ReturnValue, obj.ReturnValueType.ParameterType, options); } for (int i = 0; i < obj.ReturnParameterTypes.Length; i++) { var pt = obj.ReturnParameterTypes[i]; uni.Put(pt.Name, obj.ReturnParameters[i], pt.ParameterType, options); } options.Tag = 7; uni.Serialize(buffer, options); } else { var uni = new UniAttributeV3(convertRoot, headHandler) { Temp = new Dictionary <string, IByteBuffer>() }; var type = obj.ReturnValueType.ParameterType; if (obj.ReturnValue != null && type != typeof(void)) { uni.Put(string.Empty, obj.ReturnValue, obj.ReturnValueType.ParameterType, options); } for (int i = 0; i < obj.ReturnParameterTypes.Length; i++) { var pt = obj.ReturnParameterTypes[i]; uni.Put(pt.Name, obj.ReturnParameters[i], pt.ParameterType, options); } options.Tag = 7; uni.Serialize(buffer, options); } options.Tag = 8; intConvert.Serialize(obj.Timeout, buffer, options); options.Tag = 9; dictConvert.Serialize(obj.Context, buffer, options); options.Tag = 10; dictConvert.Serialize(obj.Status, buffer, options); }
public override void Serialize(T?obj, IByteBuffer buffer, TarsConvertOptions options) { var old = options.HasValue; options.HasValue = obj.HasValue; convert.Serialize(obj.GetValueOrDefault(), buffer, options); options.HasValue = old; }
public override void Serialize(IList <T> obj, IByteBuffer buffer, TarsConvertOptions options) { if (obj != null) { headHandler.Reserve(buffer, 8); headHandler.WriteHead(buffer, TarsStructType.List, options.Tag); buffer.WriteInt(obj.Count); var op = options.Create(); op.Tag = 0; foreach (var item in obj) { convert.Serialize(item, buffer, op); } } }
public override void Serialize(long obj, IByteBuffer buffer, TarsConvertOptions options) { if (!options.HasValue) { return; } if (obj >= int.MinValue && obj <= int.MaxValue) { convert.Serialize((int)obj, buffer, options); } else { headHandler.Reserve(buffer, 10); headHandler.WriteHead(buffer, TarsStructType.Long, options.Tag); buffer.WriteLong(obj); } }
public override void Serialize(int obj, IByteBuffer buffer, TarsConvertOptions options) { if (!options.HasValue) { return; } if (obj >= short.MinValue && obj <= short.MaxValue) { convert.Serialize((short)obj, buffer, options); } else { headHandler.Reserve(buffer, 6); headHandler.WriteHead(buffer, TarsStructType.Int, options.Tag); buffer.WriteInt(obj); } }
public override void Serialize(Response obj, IByteBuffer buffer, TarsConvertOptions options) { options.Tag = 1; options.Version = obj.Version; shortConvert.Serialize(obj.Version, buffer, options); options.Tag = 2; byteConvert.Serialize(obj.PacketType, buffer, options); switch (options.Version) { case TarsCodecsVersion.V2: case TarsCodecsVersion.V3: SerializeV2OrV3(obj, buffer, options); break; default: SerializeV1(obj, buffer, options); break; } }
private void SerializeV1(Response obj, IByteBuffer buffer, TarsConvertOptions options) { options.Tag = 3; intConvert.Serialize(obj.RequestId, buffer, options); options.Tag = 4; intConvert.Serialize(obj.MessageType, buffer, options); options.Tag = 5; intConvert.Serialize((int)obj.ResultStatusCode, buffer, options); var contentBuffer = Unpooled.Buffer(128); if (obj.ResultStatusCode == RpcStatusCode.ServerSuccess) { var type = obj.ReturnValueType.ParameterType; if (type != typeof(void)) { //0的位置是专门给返回值用的 options.Tag = 0; convertRoot.Serialize(obj.ReturnValue, type, contentBuffer, options); } int outResIndex = 0; foreach (var item in obj.ReturnParameterTypes) { options.Tag = item.Position + 1; convertRoot.Serialize(obj.ReturnParameters[outResIndex++], item.ParameterType, contentBuffer, options); } } options.Tag = 6; bufferConvert.Serialize(contentBuffer, buffer, options); options.Tag = 7; dictConvert.Serialize(obj.Status, buffer, options); if (obj.ResultStatusCode != RpcStatusCode.ServerSuccess) { options.Tag = 8; stringConvert.Serialize(obj.ResultDesc, buffer, options); } options.Tag = 9; dictConvert.Serialize(obj.Context, buffer, options); }
public override void Serialize(Task <T> obj, IByteBuffer buffer, TarsConvertOptions options) { convert.Serialize(obj.Result, buffer, options); }
public override void Serialize(bool obj, IByteBuffer buffer, TarsConvertOptions options) { convert.Serialize((byte)(obj ? 0x01 : 0), buffer, options); }
public override void Serialize(Request obj, IByteBuffer buffer, TarsConvertOptions options) { options.Tag = 1; options.Version = obj.Version; shortConvert.Serialize(obj.Version, buffer, options); options.Tag = 2; byteConvert.Serialize(obj.PacketType, buffer, options); options.Tag = 3; intConvert.Serialize(obj.MessageType, buffer, options); options.Tag = 4; intConvert.Serialize(obj.RequestId, buffer, options); options.Tag = 5; stringConvert.Serialize(obj.ServantName, buffer, options); options.Tag = 6; stringConvert.Serialize(obj.FuncName, buffer, options); switch (options.Version) { case TarsCodecsVersion.V3: { var uni = new UniAttributeV3(convertRoot, headHandler) { Temp = new Dictionary <string, IByteBuffer>(obj.ParameterTypes.Length) }; for (int i = 0; i < obj.ParameterTypes.Length; i++) { var p = obj.ParameterTypes[i]; uni.Put(p.Name, obj.Parameters[i], p.ParameterType, options); } options.Tag = 7; uni.Serialize(buffer, options); } break; case TarsCodecsVersion.V2: { var uni = new UniAttributeV2(convertRoot, headHandler) { Temp = new Dictionary <string, IDictionary <string, IByteBuffer> >(obj.ParameterTypes.Length) }; for (int i = 0; i < obj.ParameterTypes.Length; i++) { var p = obj.ParameterTypes[i]; uni.Put(p.Name, obj.Parameters[i], p.ParameterType, options); } options.Tag = 7; uni.Serialize(buffer, options); } break; default: { var contentBuffer = Unpooled.Buffer(128); for (int i = 0; i < obj.ParameterTypes.Length; i++) { var p = obj.ParameterTypes[i]; options.Tag = p.Position + 1; convertRoot.Serialize(obj.Parameters[i], p.ParameterType, contentBuffer, options); } options.Tag = 7; bufferConvert.Serialize(contentBuffer, buffer, options); } break; } options.Tag = 8; intConvert.Serialize(obj.Timeout, buffer, options); options.Tag = 9; dictConvert.Serialize(obj.Context, buffer, options); options.Tag = 10; dictConvert.Serialize(obj.Status, buffer, options); }