public override object Decode(IChannel channel, IByteBuffer buffer) { var input = new ChannelBufferStream(buffer); var objectInput = GetSerialization(channel).Deserialize(input); var obj = DecodeData(channel, objectInput); return obj; }
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")); }
public override void Encode(IChannel channel, IByteBuffer buffer, object message) { var output = new ChannelBufferStream(buffer); var objectOutput = GetSerialization(channel).Serialize(output); EncodeData(channel, objectOutput, message); objectOutput.FlushBuffer(); if (objectOutput is ICleanable) { ((ICleanable)objectOutput).cleanup(); } }
protected override object Decode(IChannel channel, IByteBuffer buffer, int readable, byte[] header) { //Console.WriteLine("decode step1:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); // check magic number. if (readable > 0 && header[0] != MagicHigh || readable > 1 && header[1] != MagicLow) { int length = header.Length; if (header.Length < readable) { header = ByteUtil.CopyOf(header, readable); buffer.ReadBytes(header, length, readable - length); } for (int i = 1; i < header.Length - 1; i++) { if (header[i] == MagicHigh && header[i + 1] == MagicLow) { buffer.SetReaderIndex(buffer.ReaderIndex - header.Length + i); header = ByteUtil.CopyOf(header, i); break; } } return(base.Decode(channel, buffer, readable, header)); } // check length. if (readable < HeaderLength) { return(DecodeResult.NeedMoreInput); } // get data length. int len = ByteUtil.Bytes2Int(header, 12); CheckPayload(channel, len); //Console.WriteLine("decode step2:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); int tt = len + HeaderLength; if (readable < tt) { return(DecodeResult.NeedMoreInput); } // limit input stream. ChannelBufferStream input = new ChannelBufferStream(buffer, len); try { //Console.WriteLine("decode step3:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); return(DecodeBody(channel, input, header)); } finally { if (input.Available() > 0) { try { if (input.Available() > 0) { input.Position = input.Available(); } } catch (IOException) { } } } }
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(); } } }