Пример #1
0
        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)
                    {
                    }
                }
            }
        }