public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            if (_buffer == null)
            {
                if (input.Remaining >= _length)
                {
                    Int32 limit = input.Limit;
                    input.Limit = input.Position + _length;
                    IoBuffer product = input.Slice();
                    input.Position = input.Position + _length;
                    input.Limit = limit;
                    return FinishDecode(product, output);
                }

                _buffer = IoBuffer.Allocate(_length);
                _buffer.Put(input);
                return this;
            }

            if (input.Remaining >= _length - _buffer.Position)
            {
                Int32 limit = input.Limit;
                input.Limit = input.Position + _length - _buffer.Position;
                _buffer.Put(input);
                input.Limit = limit;
                IoBuffer product = _buffer;
                _buffer = null;
                return FinishDecode(product.Flip(), output);
            }

            _buffer.Put(input);
            return this;
        }
Exemplo n.º 2
0
 public Int32 Read(IoBuffer buffer)
 {
     using (FileStream fs = _file.OpenRead())
     {
         fs.Position = _position;
         Byte[] bytes = new Byte[buffer.Remaining];
         Int32 read = fs.Read(bytes, 0, bytes.Length);
         buffer.Put(bytes, 0, read);
         Update(read);
         return read;
     }
 }
		public void Encode (ProducerMessage message, IoBuffer buf)
		{
			Magic.WriteMagic (buf);
			buf.Put (CURRENT_VERSION.Version);
			CURRENT_VERSION.Handler.Encode (message, buf);
		}
        private void Write(IoSession session, IoBuffer data, IoBuffer buf)
        {
            try
            {
                Int32 len = data.Remaining;
                if (len >= buf.Capacity)
                {
                    /*
                     * If the request length exceeds the size of the output buffer,
                     * flush the output buffer and then write the data directly.
                     */
                    INextFilter nextFilter = session.FilterChain.GetNextFilter(this);
                    InternalFlush(nextFilter, session, buf);
                    nextFilter.FilterWrite(session, new DefaultWriteRequest(data));
                    return;
                }
                if (len > (buf.Limit - buf.Position))
                {
                    InternalFlush(session.FilterChain.GetNextFilter(this), session, buf);
                }

                lock (buf)
                {
                    buf.Put(data);
                }
            }
            catch (Exception e)
            {
                session.FilterChain.FireExceptionCaught(e);
            }
        }
        private void Encode(ProducerMessage message, IoBuffer buf, byte[] body, string codecType)
        {
            var codec = new HermesPrimitiveCodec(buf);
            var indexBeginning = buf.Position;
            codec.WriteInt(-1); // placeholder for whole length
            var indexAfterWholeLen = buf.Position;
            codec.WriteInt(-1); // placeholder for header length
            codec.WriteInt(-1); // placeholder for body length
            var indexBeforeHeader = buf.Position;

            // header begin
            codec.WriteString(message.Key);
            codec.WriteLong(message.BornTime);
            codec.WriteInt(0); //remaining retries
            codec.WriteString(codecType);

            var propertiesHolder = message.PropertiesHolder;
            WriteProperties(propertiesHolder.DurableProperties, buf, codec);
            WriteProperties(propertiesHolder.VolatileProperties, buf, codec);
            // header end

            var headerLen = buf.Position - indexBeforeHeader;

            //body begin
            var indexBeforeBody = buf.Position;
            buf.Put(body);
            var bodyLen = buf.Position - indexBeforeBody;
            //body end

            //crc
            codec.WriteLong(ChecksumUtil.Crc32(buf.GetSlice(indexBeforeHeader, headerLen + bodyLen)));
            var indexEnd = buf.Position;

            var wholeLen = indexEnd - indexAfterWholeLen;

            // refill whole length
            buf.Position = indexBeginning;
            codec.WriteInt(wholeLen);

            // refill header length
            codec.WriteInt(headerLen);

            // refill body length
            codec.WriteInt(bodyLen);

            buf.Position = indexEnd;
        }
        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());
            }
        }
Exemplo n.º 7
0
 public static void WriteMagic(IoBuffer buf)
 {
     buf.Put(MAGIC);
 }
 public void WriteBytes(IoBuffer buf)
 {
     if (null == buf)
         WriteNull();
     else
     {
         buf.Flip();
         var length = buf.Remaining;
         buf.PutInt32(length);
         buf.Put(buf);
     }
 }