Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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);
        }