예제 #1
0
 public MessageDecoderResult Decode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
 {
     try
     {
         input.Skip(MIN_REQUIRED_FRAME_LENGTH);
         var command = commandParser.Parse(input);
         output.Write(command);
         return MessageDecoderResult.OK;
     }
     catch (Exception ex)
     {
         var remoteAddr = string.Empty;
         if (session != null && session.RemoteEndPoint != null) remoteAddr = session.RemoteEndPoint.ToString();
         log.Error(ex, new Dictionary<string, string> { { "RemoteAddr", remoteAddr } });
         return MessageDecoderResult.NotOK;
     }
 }
        private void ManipulateIoBuffer(IoSession session, IoBuffer buffer)
        {
            if ((buffer.Remaining > 0) && (_removeByteProbability > _rng.Next(1000)))
            {
                Debug.WriteLine(buffer.GetHexDump());

                // where to remove bytes ?
                int pos = _rng.Next(buffer.Remaining);
                // how many byte to remove ?
                int count = _rng.Next(buffer.Remaining - pos) + 1;
                if (count == buffer.Remaining)
                    count = buffer.Remaining - 1;

                IoBuffer newBuff = IoBuffer.Allocate(buffer.Remaining - count);
                for (int i = 0; i < pos; i++)
                    newBuff.Put(buffer.Get());

                buffer.Skip(count); // hole
                while (newBuff.Remaining > 0)
                    newBuff.Put(buffer.Get());
                newBuff.Flip();
                // copy the new buffer in the old one
                buffer.Rewind();
                buffer.Put(newBuff);
                buffer.Flip();

                Debug.WriteLine("Removed " + count + " bytes at position " + pos + ".");
                Debug.WriteLine(buffer.GetHexDump());
            }
            if ((buffer.Remaining > 0) && (_changeByteProbability > _rng.Next(1000)))
            {
                Debug.WriteLine(buffer.GetHexDump());

                // how many byte to change ?
                int count = _rng.Next(buffer.Remaining - 1) + 1;

                byte[] values = new byte[count];
                _rng.NextBytes(values);
                for (int i = 0; i < values.Length; i++)
                {
                    int pos = _rng.Next(buffer.Remaining);
                    buffer.Put(pos, values[i]);
                }

                Debug.WriteLine("Modified " + count + " bytes.");
                Debug.WriteLine(buffer.GetHexDump());
            }
        }