protected override void EncodeRequestData(IChannel channel, IObjectOutput output, object data) { RpcInvocation inv = (RpcInvocation)data; output.WriteUTF(inv.GetAttachment(Constants.DubboVersionKey, DubboVersion)); output.WriteUTF(inv.GetAttachment(Constants.PathKey)); output.WriteUTF(inv.GetAttachment(Constants.VersionKey)); //Console.WriteLine("encode attachment:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); output.WriteUTF(inv.MethodName); output.WriteUTF(ReflectUtil.GetDesc(inv.ParameterTypes)); //Console.WriteLine("encode method and paramtype:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); object[] args = inv.Arguments; if (args != null) { foreach (var arg in args) { output.WriteObject(arg); } } //Console.WriteLine("encode args:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); output.WriteObject(inv.Attachments); //Console.WriteLine("encode all attachments:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); }
protected void EncodeResponse(IChannel channel, IByteBuffer buffer, Response res) { int savedWriteIndex = buffer.WriterIndex; try { ISerialization serialization = GetSerialization(channel); // header. byte[] header = new byte[HeaderLength]; // set magic number. ByteUtil.Short2Bytes(Magic, header); // set request and serialization flag. header[2] = serialization.GetContentTypeId(); if (res.IsHeartBeat()) { header[2] |= FlagEvent; } // set response status. byte status = res.Mstatus; header[3] = status; // set request id. ByteUtil.Long2Bytes(res.Mid, header, 4); buffer.SetWriterIndex(savedWriteIndex + HeaderLength); ChannelBufferStream bos = new ChannelBufferStream(buffer); IObjectOutput output = serialization.Serialize(bos); // encode response data or error message. if (status == Response.Ok) { if (res.IsHeartBeat()) { EncodeEventData(channel, output, res.Mresult); } else { EncodeResponseData(channel, output, res.Mresult); } } else { output.WriteUTF(res.MerrorMsg); } output.FlushBuffer(); bos.Flush(); bos.Close(); int len = bos.WritenBytes(); CheckPayload(channel, len); ByteUtil.Int2Bytes(len, header, 12); // write buffer.SetWriterIndex(savedWriteIndex); buffer.WriteBytes(header); // write header. buffer.SetWriterIndex(savedWriteIndex + HeaderLength + len); } catch (Exception t) { // clear buffer buffer.SetWriterIndex(savedWriteIndex); // send error message to Consumer, otherwise, Consumer will wait till timeout. if (!res.Mevent && res.Mstatus != Response.BadResponse) { Response r = new Response(res.Mid, res.Mversion); r.Mstatus = Response.BadResponse; r.MerrorMsg = t.ToString(); } } }