Пример #1
0
        public void length_is_updated_to_include_bytes_written()
        {
            BufferPool pool = new BufferPool(1, BufferManager);

            byte[] data = { 1, 2, 3, 4, 5 };
            pool.Append(data);
            Assert.IsTrue(pool.Length == 5);
        }
Пример #2
0
        public void pool_can_expand_capacity()
        {
            BufferPool pool            = new BufferPool(1, BufferManager);
            int        initialCapacity = pool.Capacity;

            byte[] data = new byte[initialCapacity + 25];
            pool.Append(data);
            Assert.AreEqual(initialCapacity * 2, pool.Capacity);
        }
Пример #3
0
        public void data_is_written_to_the_internal_buffer()
        {
            BufferPool pool = new BufferPool(1, BufferManager);

            byte[] data = { 1, 2, 3, 4, 5 };
            pool.Append(data);
            for (byte i = 0; i < 5; i++)
            {
                Assert.AreEqual(i + 1, pool[i]);
            }
        }
Пример #4
0
        /// <summary>
        /// Parses a stream chunking based on length-prefixed framing. Calls are re-entrant and hold state internally.
        /// </summary>
        /// <param name="bytes">A byte array of data to append</param>
        private void Parse(ArraySegment <byte> bytes)
        {
            byte[] data = bytes.Array;
            for (int i = bytes.Offset; i < bytes.Offset + bytes.Count;)
            {
                if (_headerBytes < PrefixLength)
                {
                    _packageLength |= (data[i] << (_headerBytes * 8)); // little-endian order
                    ++_headerBytes;
                    i += 1;
                    if (_headerBytes == PrefixLength)
                    {
                        if (_packageLength <= 0 || _packageLength > _maxPackageSize)
                        {
                            Log.Error("FRAMING ERROR! Data:");
                            Log.Error(Common.Utils.Helper.FormatBinaryDump(bytes));
                            throw new PackageFramingException(string.Format("Package size is out of bounds: {0} (max: {1}).",
                                                                            _packageLength, _maxPackageSize));
                        }

                        _messageBuffer = new BufferPool(_bufferManager);
                    }
                }
                else
                {
                    int copyCnt = Math.Min(bytes.Count + bytes.Offset - i, _packageLength - _messageBuffer.Length);
                    _messageBuffer.Append(bytes.Array, i, copyCnt);
                    i += copyCnt;

                    if (_messageBuffer.Length == _packageLength)
                    {
                        if (_receivedHandler != null)
                        {
                            _receivedHandler(_messageBuffer);
                        }
                        _messageBuffer = null;
                        _headerBytes   = 0;
                        _packageLength = 0;
                    }
                }
            }
        }
Пример #5
0
        /// <summary>
        /// Parses a stream chunking based on length-prefixed framing. Calls are re-entrant and hold state internally.
        /// </summary>
        /// <param name="bytes">A byte array of data to append</param>
        private void Parse(ArraySegment <byte> bytes)
        {
            byte[] data = bytes.Array;
            for (int i = bytes.Offset; i < bytes.Offset + bytes.Count;)
            {
                if (_headerBytes < HeaderLength)
                {
                    _packageLength |= (data[i] << (_headerBytes * 8)); // little-endian order
                    ++_headerBytes;
                    i += 1;
                    if (_headerBytes == HeaderLength)
                    {
                        if (_packageLength <= 0 || _packageLength > _maxPackageSize)
                        {
                            throw new PackageFramingException(string.Format("Package size is out of bounds: {0} (max: {1}).", _packageLength, _maxPackageSize));
                        }

                        _messageBuffer = new BufferPool();
                    }
                }
                else
                {
                    int copyCnt = Math.Min(bytes.Count + bytes.Offset - i, _packageLength - _messageBuffer.Length);
                    _messageBuffer.Append(bytes.Array, i, copyCnt);
                    i += copyCnt;

                    if (_messageBuffer.Length == _packageLength)
                    {
                        if (_receivedHandler != null)
                        {
                            _receivedHandler(_messageBuffer);
                        }
                        _messageBuffer = null;
                        _headerBytes   = 0;
                        _packageLength = 0;
                    }
                }
            }
        }
        /// <summary>
        /// Parses a stream chunking based on length-prefixed framing. Calls are re-entrant and hold state internally.
        /// </summary>
        /// <param name="bytes">A byte array of data to append</param>
        private void Parse(ArraySegment <byte> bytes)
        {
            byte[] data = bytes.Array;
            for (int i = bytes.Offset; i < bytes.Offset + bytes.Count;)
            {
                if (_headerBytes < HeaderLength)
                {
                    _packageLength |= (data[i] << (_headerBytes * 8)); // little-endian order
                    ++_headerBytes;
                    i += 1;
                    if (_headerBytes == HeaderLength)
                    {
                        if (_packageLength == 0)
                        {
                            throw new InvalidOperationException("Package should not be 0 sized.");
                        }

                        _messageBuffer = new BufferPool();
                    }
                }
                else
                {
                    int copyCnt = Math.Min(bytes.Count + bytes.Offset - i, _packageLength - _messageBuffer.Length);
                    _messageBuffer.Append(bytes.Array, i, copyCnt);
                    i += copyCnt;

                    if (_messageBuffer.Length == _packageLength)
                    {
                        if (_receivedHandler != null)
                        {
                            _receivedHandler(_messageBuffer);
                        }
                        _messageBuffer = null;
                        _headerBytes   = 0;
                        _packageLength = 0;
                    }
                }
            }
        }
Пример #7
0
 public void pool_can_expand_capacity()
 {
     BufferPool pool = new BufferPool(1, BufferManager);
     int initialCapacity = pool.Capacity;
     byte[] data = new byte[initialCapacity + 25];
     pool.Append(data);
     Assert.AreEqual(initialCapacity * 2, pool.Capacity);
 }
Пример #8
0
 public void data_is_written_to_the_internal_buffer()
 {
     BufferPool pool = new BufferPool(1, BufferManager);
     byte[] data = { 1, 2, 3, 4, 5 };
     pool.Append(data);
     for (byte i = 0; i < 5; i++)
     {
         Assert.AreEqual(i + 1, pool[i]);
     }
 }
Пример #9
0
 public void length_is_updated_to_include_bytes_written()
 {
     BufferPool pool = new BufferPool(1, BufferManager);
     byte[] data = { 1, 2, 3, 4, 5 };
     pool.Append(data);
     Assert.IsTrue(pool.Length == 5);
 }
Пример #10
0
 public void a_null_byte_array_throws_an_argumentnullexception()
 {
     BufferPool pool = new BufferPool(1, BufferManager);
     pool.Append(null);
 }
Пример #11
0
 public void writing_multiple_bytes_throws_objectdisposedexception()
 {
     Assert.Throws <ObjectDisposedException>(() => { m_DisposedPool.Append(new byte[] { 1, 2, 3, 4 }); });
 }
Пример #12
0
        public void a_null_byte_array_throws_an_argumentnullexception()
        {
            BufferPool pool = new BufferPool(1, BufferManager);

            Assert.Throws <ArgumentNullException>(() => { pool.Append(null); });
        }
Пример #13
0
 public void writing_multiple_bytes_throws_objectdisposedexception()
 {
     m_DisposedPool.Append(new byte[] { 1, 2, 3, 4 });
 }
Пример #14
0
        public void a_null_byte_array_throws_an_argumentnullexception()
        {
            BufferPool pool = new BufferPool(1, BufferManager);

            pool.Append(null);
        }