Ejemplo n.º 1
0
 internal void WriteTo(TarsOutputStream tarsOutputStream)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 2
0
        public IByteBuffer EncodeResponse(Response message)
        {
            TarsOutputStream tos = new TarsOutputStream();

            tos.SetServerEncoding(charsetName);
            try
            {
                //data length
                tos.GetByteBuffer().WriteInt(0);
                tos.Write(message.Version, 1);
                //保证协议兼容性 PacketType填入默认值 (请求类型:一定是往返请求)
                tos.Write(TarsCodecsConstant.NORMAL, 2);
                if (message.Version == TarsCodecsConstant.VERSION)
                {
                    tos.Write(message.RequestId, 3);
                    tos.Write(message.MessageType, 4);
                    tos.Write(message.ResultStatusCode, 5);
                    //按顺序写入数据流 此处写入 参数 byte[]
                    tos.Write(EncodeResponseContent(message, charsetName), 6);
                    if (message.Status != null)
                    {
                        tos.Write(message.Status, 7);
                    }
                    if (message.ResultStatusCode != RpcStatusCode.ServerSuccess)
                    {
                        tos.Write((string.IsNullOrWhiteSpace(message.ResultDesc) ? "" : message.ResultDesc), 8);
                    }
                }
                else if (message.Version == TarsCodecsConstant.VERSION2 || message.Version == TarsCodecsConstant.VERSION3)
                {
                    tos.Write(message.MessageType, 3);
                    tos.Write(message.RequestId, 4);
                    tos.Write(message.ServantName, 5);
                    tos.Write(message.FuncName, 6);
                    tos.Write(EncodeTupResponseContent(message, charsetName), 7);
                    tos.Write(message.Timeout, 8);
                    if (message.Context != null)
                    {
                        tos.Write(message.Context, 9);
                    }
                    if (message.Status != null)
                    {
                        tos.Write(message.Status, 10);
                    }
                }
                else
                {
                    message.ResultStatusCode = RpcStatusCode.ServerEncodeErr;
                }
            }
            catch (Exception ex)
            {
                if (message.ResultStatusCode == RpcStatusCode.ServerSuccess)
                {
                    //message.ResultStatusCode = RpcStatusCode.ServerEncodeErr;
                    throw new TarsException(RpcStatusCode.ServerEncodeErr, ex);
                }
            }
            IByteBuffer buffer = tos.GetByteBuffer();
            int         length = buffer.WriterIndex;

            tos.ResetDataLength(length);
            return(Unpooled.WrappedBuffer(tos.ToByteArray()));
        }