Esempio n. 1
0
        /// <summary>
        /// Returns an instance of NetworkBuffer, from the pool.
        /// </summary>
        /// <param name="size">The size of the underlayer array.</param>
        /// <returns></returns>
        public static Buffer Create(int size)
        {
            var buffer = Pool.Rent();

            buffer.m_buffer = BytesPool.Get(size);

            return(buffer);
        }
Esempio n. 2
0
        private void EnsureBufferSpace(int additionalSpaceInBits)
        {
            var length = m_buffer.Length;

            if (m_bitsPointer + additionalSpaceInBits > length << 3)
            {
                var tmpBuffer = m_buffer;
                var newBuffer = BytesPool.Get((int)(length * GrowingFactor) + 1);
                System.Buffer.BlockCopy(tmpBuffer, 0, newBuffer, 0, tmpBuffer.Length);
                BytesPool.Recycle(m_buffer);
                m_buffer = newBuffer;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Returns an instance of NetworkBuffer, from the pool.
        /// It is initialized with the passed data.
        /// </summary>
        /// <param name="data">The data to initialize the buffer.</param>
        /// <param name="createCopy">If true, data will be copied in a new array; else data will be used as internal buffer.</param>
        /// <returns></returns>
        public static Buffer Create(byte[] data, bool createCopy = true)
        {
            var buffer = Pool.Rent();

            if (createCopy)
            {
                buffer.m_buffer = BytesPool.Get(data.Length);
                System.Buffer.BlockCopy(data, 0, buffer.m_buffer, 0, data.Length);
            }
            else
            {
                buffer.m_buffer = data;
            }

            return(buffer);
        }