protected void EncodeRequest(IChannel channel, IByteBuffer buffer, Request req) { //Console.WriteLine("begin encode Request:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); ISerialization serialization = GetSerialization(channel); // Console.WriteLine("get serialization:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); // header. byte[] header = new byte[HeaderLength]; // set magic number. ByteUtil.Short2Bytes(Magic, header); // set request and serialization flag. header[2] = (byte)(FlagRequest | serialization.GetContentTypeId()); if (req.IsTwoWay) { header[2] |= FlagTwoway; } if (req.IsEvent) { header[2] |= FlagEvent; } // set request id. ByteUtil.Long2Bytes(req.Mid, header, 4); // encode request data. int savedWriteIndex = buffer.WriterIndex; buffer.SetWriterIndex(savedWriteIndex + HeaderLength); ChannelBufferStream bos = new ChannelBufferStream(buffer); IObjectOutput output = serialization.Serialize(bos); //Console.WriteLine("encode header:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); if (req.IsEvent) { EncodeEventData(channel, output, req.Mdata); } else { EncodeRequestData(channel, output, req.Mdata); } //Console.WriteLine("encode requestdata down:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); 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); // Console.WriteLine("encode end:" + 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(); } } }