Beispiel #1
0
        public void TestShrink2()
        {
            IoBuffer buf = IoBuffer.Allocate(36);

            buf.Put(Encoding.Default.GetBytes("012345"));
            buf.Flip();
            buf.Position        = 4;
            buf.MinimumCapacity = 8;

            IoBuffer newBuf = buf.Shrink();

            Assert.AreEqual(4, newBuf.Position);
            Assert.AreEqual(6, newBuf.Limit);
            Assert.AreEqual(9, newBuf.Capacity);
            Assert.AreEqual(8, newBuf.MinimumCapacity);

            buf = IoBuffer.Allocate(6);
            buf.Put(Encoding.Default.GetBytes("012345"));
            buf.Flip();
            buf.Position = 4;

            newBuf = buf.Shrink();
            Assert.AreEqual(4, newBuf.Position);
            Assert.AreEqual(6, newBuf.Limit);
            Assert.AreEqual(6, newBuf.Capacity);
            Assert.AreEqual(6, newBuf.MinimumCapacity);
        }
Beispiel #2
0
        static void WriteVarint32(IoBuffer buffer, uint value)
        {
            for (; value >= 0x80u; value >>= 7)
                buffer.Put((byte)(value | 0x80u));

            buffer.Put((byte)value);
        }
Beispiel #3
0
        public void TestCapacity()
        {
            IoBuffer buffer = IoBuffer.Allocate(10);

            buffer.Put(Encoding.Default.GetBytes("012345"));
            buffer.Flip();

            // See if we can decrease the capacity (we shouldn't be able to go under the minimul capacity)
            buffer.Capacity = 7;
            Assert.AreEqual(10, buffer.Capacity);

            // See if we can increase the capacity
            buffer = IoBuffer.Allocate(10);

            buffer.Put(Encoding.Default.GetBytes("012345"));
            buffer.Flip();
            buffer.Capacity = 14;
            Assert.AreEqual(14, buffer.Capacity);
            buffer.Put(0, (byte)'9');
            Assert.AreEqual((byte)'9', buffer.Get(0));
            Assert.AreEqual((byte)'9', buffer.Get(0));

            // See if we can go down when the minimum capacity is below the current capacity
            // We should not.
            buffer          = IoBuffer.Allocate(10);
            buffer.Capacity = 5;
            Assert.AreEqual(10, buffer.MinimumCapacity);
            Assert.AreEqual(10, buffer.Capacity);
        }
Beispiel #4
0
 protected override void Fill(IoBuffer buffer)
 {
     buffer.Put(Length);
     buffer.Put(FRAME_SOURCE_ADDR);
     buffer.Put(Protocol);
     buffer.Put(FRAME_SEQUENCE_VALUE);
     this.FillData(buffer);
 }
        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;
        }
Beispiel #6
0
        public void TestGetSlice()
        {
            IoBuffer buf = IoBuffer.Allocate(36);

            for (Byte i = 0; i < 36; i++)
            {
                buf.Put(i);
            }

            IoBuffer res = buf.GetSlice(1, 3);

            // The limit should be 3, the pos should be 0 and the bytes read
            // should be 0x01, 0x02 and 0x03
            Assert.AreEqual(0, res.Position);
            Assert.AreEqual(3, res.Limit);
            Assert.AreEqual(0x01, res.Get());
            Assert.AreEqual(0x02, res.Get());
            Assert.AreEqual(0x03, res.Get());

            // Now test after a flip
            buf.Flip();

            res = buf.GetSlice(1, 3);
            // The limit should be 3, the pos should be 0 and the bytes read
            // should be 0x01, 0x02 and 0x03
            Assert.AreEqual(0, res.Position);
            Assert.AreEqual(3, res.Limit);
            Assert.AreEqual(0x01, res.Get());
            Assert.AreEqual(0x02, res.Get());
            Assert.AreEqual(0x03, res.Get());
        }
Beispiel #7
0
        public void TestGetUnsigned()
        {
            IoBuffer buf = IoBuffer.Allocate(16);

            buf.Put((byte)0xA4);
            buf.Put((byte)0xD0);
            buf.Put((byte)0xB3);
            buf.Put((byte)0xCD);
            buf.Flip();

            buf.Order = ByteOrder.LittleEndian;

            buf.Mark();
            Assert.AreEqual(0xA4, buf.Get());
            buf.Reset();
            Assert.AreEqual(0xD0A4, (UInt16)buf.GetInt16());
            buf.Reset();
            Assert.AreEqual(0xCDB3D0A4L, (UInt32)buf.GetInt32());
        }
 /// <inheritdoc/>
 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;
     }
 }
Beispiel #9
0
        public void TestAutoExpandMark()
        {
            IoBuffer buf = ByteBufferAllocator.Instance.Allocate(4);

            buf.AutoExpand = true;

            buf.Put((byte)0);
            buf.Put((byte)0);
            buf.Put((byte)0);

            // Position should be 3 when we reset this buffer.
            buf.Mark();

            // Overflow it
            buf.Put((byte)0);
            buf.Put((byte)0);

            Assert.AreEqual(5, buf.Position);
            buf.Reset();
            Assert.AreEqual(3, buf.Position);
        }
Beispiel #10
0
        public void TestAutoExpand()
        {
            IoBuffer buf = ByteBufferAllocator.Instance.Allocate(1);

            buf.Put((byte)0);
            try
            {
                buf.Put((byte)0);
                Assert.Fail("Buffer can't auto expand, with autoExpand property set at false");
            }
            catch (OverflowException)
            {
                // Expected Exception as auto expand property is false
                Assert.IsTrue(true);
            }

            buf.AutoExpand = true;
            buf.Put((byte)0);
            Assert.AreEqual(2, buf.Position);
            Assert.AreEqual(2, buf.Limit);
            Assert.AreEqual(2, buf.Capacity);

            buf.AutoExpand = false;
            try
            {
                buf.Put(3, (byte)0);
                Assert.Fail("Buffer can't auto expand, with autoExpand property set at false");
            }
            catch (IndexOutOfRangeException)
            {
                // Expected Exception as auto expand property is false
                Assert.IsTrue(true);
            }

            buf.AutoExpand = true;
            buf.Put(3, (byte)0);
            Assert.AreEqual(2, buf.Position);
            Assert.AreEqual(4, buf.Limit);
            Assert.AreEqual(4, buf.Capacity);

            // Make sure the buffer is doubled up.
            buf            = ByteBufferAllocator.Instance.Allocate(1);
            buf.AutoExpand = true;
            int lastCapacity = buf.Capacity;

            for (int i = 0; i < 1048576; i++)
            {
                buf.Put((byte)0);
                if (lastCapacity != buf.Capacity)
                {
                    Assert.AreEqual(lastCapacity * 2, buf.Capacity);
                    lastCapacity = buf.Capacity;
                }
            }
        }
Beispiel #11
0
        public void TestIndexOf()
        {
            for (int i = 0; i < 2; i++)
            {
                IoBuffer buf = IoBuffer.Allocate(16);
                buf.Put((byte)0x1);
                buf.Put((byte)0x2);
                buf.Put((byte)0x3);
                buf.Put((byte)0x4);
                buf.Put((byte)0x1);
                buf.Put((byte)0x2);
                buf.Put((byte)0x3);
                buf.Put((byte)0x4);
                buf.Position = 2;
                buf.Limit    = 5;

                Assert.AreEqual(4, buf.IndexOf((byte)0x1));
                Assert.AreEqual(-1, buf.IndexOf((byte)0x2));
                Assert.AreEqual(2, buf.IndexOf((byte)0x3));
                Assert.AreEqual(3, buf.IndexOf((byte)0x4));
            }
        }
Beispiel #12
0
 /// <inheritdoc/>
 public override IoBuffer Put(Byte b)
 {
     _buf.Put(b);
     return(this);
 }
Beispiel #13
0
 protected override void Fill(IoBuffer buffer)
 {
     buffer.Put(FRAME_SOURCE_ADDR);
     buffer.Put(Respond);
     buffer.Put(FRAME_SEQUENCE_VALUE);
 }
Beispiel #14
0
 public static void WritePrimitive(IoBuffer buffer, bool value)
 {
     buffer.Put(value ? (byte)1 : (byte)0);
 }
Beispiel #15
0
 public static void WritePrimitive(IoBuffer buffer, sbyte value)
 {
     buffer.Put((byte)value);
 }
Beispiel #16
0
        public void TestGetString()
        {
            IoBuffer buf      = ByteBufferAllocator.Instance.Allocate(16);
            Encoding encoding = Encoding.UTF8;

            buf.Clear();
            buf.PutString("hello", encoding);
            buf.Put((Byte)0);
            buf.Flip();
            Assert.AreEqual("hello", buf.GetString(encoding));

            buf.Clear();
            buf.PutString("hello", encoding);
            buf.Flip();
            Assert.AreEqual("hello", buf.GetString(encoding));

            encoding = Encoding.GetEncoding("ISO-8859-1");
            buf.Clear();
            buf.Put((Byte)'A');
            buf.Put((Byte)'B');
            buf.Put((Byte)'C');
            buf.Put((Byte)0);

            buf.Position = 0;
            Assert.AreEqual("ABC", buf.GetString(encoding));
            Assert.AreEqual(4, buf.Position);

            buf.Position = 0;
            buf.Limit    = 1;
            Assert.AreEqual("A", buf.GetString(encoding));
            Assert.AreEqual(1, buf.Position);

            buf.Clear();
            Assert.AreEqual("ABC", buf.GetString(10, encoding));
            Assert.AreEqual(10, buf.Position);

            buf.Clear();
            Assert.AreEqual("A", buf.GetString(1, encoding));
            Assert.AreEqual(1, buf.Position);

            // Test a trailing garbage
            buf.Clear();
            buf.Put((Byte)'A');
            buf.Put((Byte)'B');
            buf.Put((Byte)0);
            buf.Put((Byte)'C');
            buf.Position = 0;
            Assert.AreEqual("AB", buf.GetString(4, encoding));
            Assert.AreEqual(4, buf.Position);

            buf.Clear();
            buf.FillAndReset(buf.Limit);
            encoding = Encoding.GetEncoding("UTF-16BE");
            buf.Put((Byte)0);
            buf.Put((Byte)'A');
            buf.Put((Byte)0);
            buf.Put((Byte)'B');
            buf.Put((Byte)0);
            buf.Put((Byte)'C');
            buf.Put((Byte)0);
            buf.Put((Byte)0);

            buf.Position = 0;
            Assert.AreEqual("ABC", buf.GetString(encoding));
            Assert.AreEqual(8, buf.Position);

            buf.Position = 0;
            buf.Limit    = 2;
            Assert.AreEqual("A", buf.GetString(encoding));
            Assert.AreEqual(2, buf.Position);

            buf.Position = 0;
            buf.Limit    = 3;
            Assert.AreEqual("A", buf.GetString(encoding));
            Assert.AreEqual(2, buf.Position);

            buf.Clear();
            Assert.AreEqual("ABC", buf.GetString(10, encoding));
            Assert.AreEqual(10, buf.Position);

            buf.Clear();
            Assert.AreEqual("A", buf.GetString(2, encoding));
            Assert.AreEqual(2, buf.Position);

            buf.Clear();
            try
            {
                buf.GetString(1, encoding);
                Assert.Fail();
            }
            catch (Exception)
            {
                // Expected an Exception, signifies test success
                Assert.IsTrue(true);
            }

            // Test getting strings from an empty buffer.
            buf.Clear();
            buf.Limit = 0;
            Assert.AreEqual("", buf.GetString(encoding));
            Assert.AreEqual("", buf.GetString(2, encoding));

            // Test getting strings from non-empty buffer which is filled with 0x00
            buf.Clear();
            buf.PutInt32(0);
            buf.Clear();
            buf.Limit = 4;
            Assert.AreEqual("", buf.GetString(encoding));
            Assert.AreEqual(2, buf.Position);
            Assert.AreEqual(4, buf.Limit);

            buf.Position = 0;
            Assert.AreEqual("", buf.GetString(2, encoding));
            Assert.AreEqual(2, buf.Position);
            Assert.AreEqual(4, buf.Limit);
        }
        private void ManipulateIoBuffer(IoSession session, IoBuffer buffer)
        {
            if ((buffer.Remaining > 0) && (_removeByteProbability > _rng.Next(1000)))
            {
                if (log.IsInfoEnabled)
                    log.Info(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();

                if (log.IsInfoEnabled)
                {
                    log.Info("Removed " + count + " bytes at position " + pos + ".");
                    log.Info(buffer.GetHexDump());
                }
            }
            if ((buffer.Remaining > 0) && (_changeByteProbability > _rng.Next(1000)))
            {
                if (log.IsInfoEnabled)
                    log.Info(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]);
                }

                if (log.IsInfoEnabled)
                {
                    log.Info("Modified " + count + " bytes.");
                    log.Info(buffer.GetHexDump());
                }
            }
        }
Beispiel #18
0
        public unsafe static void WritePrimitive(IoBuffer buffer, string value)
        {
            if (value == null)
            {
                WritePrimitive(buffer, (uint)0);
                return;
            }
            else if (value.Length == 0)
            {
                WritePrimitive(buffer, (uint)1);
                return;
            }

            var helper = s_stringHelper;
            if (helper == null)
                s_stringHelper = helper = new StringHelper();

            var encoder = helper.Encoder;
            var buf = helper.ByteBuffer;

            int totalChars = value.Length;
            int totalBytes;

            fixed (char* ptr = value)
                totalBytes = encoder.GetByteCount(ptr, totalChars, true);

            WritePrimitive(buffer, (uint)totalBytes + 1);
            WritePrimitive(buffer, (uint)totalChars);

            int p = 0;
            bool completed = false;

            while (completed == false)
            {
                int charsConverted;
                int bytesConverted;

                fixed (char* src = value)
                fixed (byte* dst = buf)
                {
                    encoder.Convert(src + p, totalChars - p, dst, buf.Length, true,
                        out charsConverted, out bytesConverted, out completed);
                }

                buffer.Put(buf, 0, bytesConverted);

                p += charsConverted;
            }
        }
Beispiel #19
0
 public static void WriteGuidPrefix(IoBuffer buffer, GuidPrefix obj)
 {
     buffer.Put(obj.Prefix);
 }
Beispiel #20
0
        public void TestExpandPos()
        {
            IoBuffer buffer = IoBuffer.Allocate(10);

            buffer.Put(Encoding.Default.GetBytes("012345"));
            buffer.Flip();

            Assert.AreEqual(6, buffer.Remaining);

            // See if we can expand with a lower number of remaining bytes. We should not.
            IoBuffer newBuffer = buffer.Expand(3, 2);

            Assert.AreEqual(6, newBuffer.Limit);
            Assert.AreEqual(10, newBuffer.Capacity);
            Assert.AreEqual(0, newBuffer.Position);

            // Now, let's expand the buffer above the number of current bytes but below the limit
            buffer = IoBuffer.Allocate(10);
            buffer.Put(Encoding.Default.GetBytes("012345"));
            buffer.Flip();

            newBuffer = buffer.Expand(3, 5);
            Assert.AreEqual(8, newBuffer.Limit);
            Assert.AreEqual(10, newBuffer.Capacity);
            Assert.AreEqual(0, newBuffer.Position);

            // Last, expand the buffer above the limit
            buffer = IoBuffer.Allocate(10);

            buffer.Put(Encoding.Default.GetBytes("012345"));
            buffer.Flip();
            newBuffer = buffer.Expand(3, 9);
            Assert.AreEqual(12, newBuffer.Limit);
            Assert.AreEqual(12, newBuffer.Capacity);
            Assert.AreEqual(0, newBuffer.Position);

            // Now, move forward in the buffer
            buffer = IoBuffer.Allocate(10);

            buffer.Put(Encoding.Default.GetBytes("012345"));
            buffer.Flip();
            buffer.Position = 4;

            // See if we can expand with a lower number of remaining bytes. We should not be.
            newBuffer = buffer.Expand(5, 1);
            Assert.AreEqual(6, newBuffer.Limit);
            Assert.AreEqual(10, newBuffer.Capacity);
            Assert.AreEqual(4, newBuffer.Position);

            // Expand above the current limit
            buffer = IoBuffer.Allocate(10);

            buffer.Put(Encoding.Default.GetBytes("012345"));
            buffer.Flip();
            buffer.Position = 4;
            newBuffer       = buffer.Expand(5, 2);
            Assert.AreEqual(7, newBuffer.Limit);
            Assert.AreEqual(10, newBuffer.Capacity);
            Assert.AreEqual(4, newBuffer.Position);

            // Expand above the current capacity
            buffer = IoBuffer.Allocate(10);

            buffer.Put(Encoding.Default.GetBytes("012345"));
            buffer.Flip();
            buffer.Position = 4;
            newBuffer       = buffer.Expand(5, 6);
            Assert.AreEqual(11, newBuffer.Limit);
            Assert.AreEqual(11, newBuffer.Capacity);
            Assert.AreEqual(4, newBuffer.Position);
        }
        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);
            }
        }
Beispiel #22
0
 protected override void FillData(IoBuffer buffer)
 {
     buffer.Put(addrChangeTo);
 }
Beispiel #23
0
 public static void WriteLocator(IoBuffer buffer, Locator obj)
 {
     buffer.PutInt32((int)obj.Kind);
     buffer.PutInt32(obj.Port);
     buffer.Put(obj.SocketAddressBytes);
 }
Beispiel #24
0
        public static void WritePrimitive(IoBuffer buffer, byte[] value)
        {
            if (value == null)
            {
                WritePrimitive(buffer, (uint)0);
                return;
            }

            WritePrimitive(buffer, (uint)value.Length + 1);

            buffer.Put(value, 0, value.Length);
        }
Beispiel #25
0
 public static void WriteProtocolId(IoBuffer buffer, ProtocolId obj)
 {
     buffer.Put(obj.Id);
 }
Beispiel #26
0
 /// <inheritdoc/>
 public override void Write(Byte[] buffer, Int32 offset, Int32 count)
 {
     _buf.Put(buffer, offset, count);
 }