Exemplo n.º 1
0
        public SlicedArrayBuffer(IArrayBuffer <T> buffer, int index, int length)
            : base(length)
        {
            if (index < 0 ||
                index > buffer.Capacity - length)
            {
                throw new ArgumentOutOfRangeException(nameof(index), $"{buffer}.slice({index}, {length})");
            }

            var slicedByteBuf = buffer as SlicedArrayBuffer <T>;

            if (slicedByteBuf != null)
            {
                this.buffer     = slicedByteBuf.buffer;
                this.adjustment = slicedByteBuf.adjustment + index;
            }
            else if (buffer is DuplicatedArrayBuffer <T> )
            {
                this.buffer     = buffer.Unwrap();
                this.adjustment = index;
            }
            else
            {
                this.buffer     = buffer;
                this.adjustment = index;
            }
            this.sliceLength = length;
            this.SetWriterIndex(length);
        }
Exemplo n.º 2
0
        public override IArrayBuffer <T> Duplicate()
        {
            IArrayBuffer <T> duplicate = this.buffer.Slice(this.adjustment, this.sliceLength);

            duplicate.SetIndex(this.ReaderIndex, this.WriterIndex);
            return(duplicate);
        }
Exemplo n.º 3
0
        protected static IArrayBuffer <T> ToLeakAwareBuffer(IArrayBuffer <T> buf)
        {
            IResourceLeak leak;

            switch (ResourceLeakDetector.Level)
            {
            case ResourceLeakDetector.DetectionLevel.Simple:
                leak = AbstractArrayBuffer <T> .LeakDetector.Open(buf);

                if (leak != null)
                {
                    buf = new SimpleLeakAwareArrayBuffer <T>(buf, leak);
                }
                break;

            case ResourceLeakDetector.DetectionLevel.Advanced:
            case ResourceLeakDetector.DetectionLevel.Paranoid:
                leak = AbstractArrayBuffer <T> .LeakDetector.Open(buf);

                if (leak != null)
                {
                    buf = new AdvancedLeakAwareArrayBuffer <T>(buf, leak);
                }
                break;

            case ResourceLeakDetector.DetectionLevel.Disabled:
                break;

            default:
                throw new ArgumentOutOfRangeException($"Unknown type {ResourceLeakDetector.Level}");
            }
            return(buf);
        }
Exemplo n.º 4
0
        public string ReadString(Encoding encoding)
        {
            Contract.Requires(encoding != null);

            IArrayBuffer <byte> buffer = this.ArrayBuffer;

            if (buffer == null)
            {
                throw new ObjectDisposedException(
                          $"{nameof(ReadableBuffer)} has already been disposed.");
            }

            int length = buffer.ReadableCount;

            if (length == 0)
            {
                return(string.Empty);
            }

            string result = buffer.GetString(length, encoding);

            buffer.Skip(length);

            return(result);
        }
Exemplo n.º 5
0
        internal ByteBuffer Buffer()
        {
            IArrayBuffer <byte> buffer = this.ArrayAllocator.Buffer();
            var byteBuffer             = new ByteBuffer(buffer);

            return(byteBuffer);
        }
Exemplo n.º 6
0
        internal ByteBuffer Buffer(int initialCapacity, int maxCapacity)
        {
            IArrayBuffer <byte> buffer = this.ArrayAllocator.Buffer(initialCapacity, maxCapacity);
            var byteBuffer             = new ByteBuffer(buffer);

            return(byteBuffer);
        }
Exemplo n.º 7
0
        IArrayBuffer <byte> ReleasedBuffer()
        {
            IArrayBuffer <byte> newBuffer = this.allocator.Buffer(8);

            Assert.True(newBuffer.Release());
            return(newBuffer);
        }
Exemplo n.º 8
0
        public static void SetBoolean(this IArrayBuffer <byte> buffer, int start, bool value)
        {
            Contract.Requires(buffer != null);

            buffer.CheckIndex(start);
            buffer.Array[buffer.WriterIndex] = (byte)(value ? 1 : 0);
        }
Exemplo n.º 9
0
        public void TestSequentialHeapBufferTransfer1()
        {
            var valueContent          = new byte[BlockSize * 2];
            IArrayBuffer <byte> value = Unpooled.WrappedBuffer(valueContent);

            this.buffer.SetWriterIndex(0);
            for (int i = 0; i < this.buffer.Capacity - BlockSize + 1; i += BlockSize)
            {
                this.random.NextBytes(valueContent);
                Assert.Equal(0, this.buffer.ReaderIndex);
                Assert.Equal(i, this.buffer.WriterIndex);
                this.buffer.Write(value, this.random.Next(BlockSize), BlockSize);
                Assert.Equal(0, value.ReaderIndex);
                Assert.Equal(valueContent.Length, value.WriterIndex);
            }

            this.random = new Random(this.seed);
            var expectedValueContent          = new byte[BlockSize * 2];
            IArrayBuffer <byte> expectedValue = Unpooled.WrappedBuffer(expectedValueContent);

            for (int i = 0; i < this.buffer.Capacity - BlockSize + 1; i += BlockSize)
            {
                this.random.NextBytes(expectedValueContent);
                int valueOffset = this.random.Next(BlockSize);
                Assert.Equal(i, this.buffer.ReaderIndex);
                Assert.Equal(Capacity, this.buffer.WriterIndex);
                this.buffer.Read(value, valueOffset, BlockSize);
                for (int j = valueOffset; j < valueOffset + BlockSize; j++)
                {
                    Assert.Equal(expectedValue.Get(j), value.Get(j));
                }
                Assert.Equal(0, value.ReaderIndex);
                Assert.Equal(valueContent.Length, value.WriterIndex);
            }
        }
Exemplo n.º 10
0
        public void Duplicate()
        {
            for (int i = 0; i < this.buffer.Capacity; i++)
            {
                byte value = (byte)this.random.Next();
                this.buffer.Set(i, value);
            }

            int readerIndex = Capacity / 3;
            int writerIndex = Capacity * 2 / 3;

            this.buffer.SetIndex(readerIndex, writerIndex);

            // Make sure all properties are copied.
            IArrayBuffer <byte> duplicate = this.buffer.Duplicate();

            Assert.Equal(this.buffer.ReaderIndex, duplicate.ReaderIndex);
            Assert.Equal(this.buffer.WriterIndex, duplicate.WriterIndex);
            Assert.Equal(this.buffer.Capacity, duplicate.Capacity);
            for (int i = 0; i < duplicate.Capacity; i++)
            {
                Assert.Equal(this.buffer.Get(i), duplicate.Get(i));
            }

            // Make sure the this.buffer content is shared.
            this.buffer.Set(readerIndex, (byte)(this.buffer.Get(readerIndex) + 1));
            Assert.Equal(this.buffer.Get(readerIndex), duplicate.Get(readerIndex));
            duplicate.Set(1, (byte)(duplicate.Get(1) + 1));
            Assert.Equal(this.buffer.Get(1), duplicate.Get(1));
        }
Exemplo n.º 11
0
        public void RandomHeapBufferTransfer2()
        {
            var valueContent          = new byte[BlockSize * 2];
            IArrayBuffer <byte> value = Unpooled.WrappedBuffer(valueContent);

            for (int i = 0; i < this.buffer.Capacity - BlockSize + 1; i += BlockSize)
            {
                this.random.NextBytes(valueContent);
                this.buffer.Set(i, value, this.random.Next(BlockSize), BlockSize);
            }

            this.random = new Random(this.seed);
            var expectedValueContent          = new byte[BlockSize * 2];
            IArrayBuffer <byte> expectedValue = Unpooled.WrappedBuffer(expectedValueContent);

            for (int i = 0; i < this.buffer.Capacity - BlockSize + 1; i += BlockSize)
            {
                this.random.NextBytes(expectedValueContent);
                int valueOffset = this.random.Next(BlockSize);
                this.buffer.Get(i, value, valueOffset, BlockSize);
                for (int j = valueOffset; j < valueOffset + BlockSize; j++)
                {
                    Assert.Equal(expectedValue.Get(j), value.Get(j));
                }
            }
        }
Exemplo n.º 12
0
        public void RandomHeapBufferTransfer1()
        {
            var valueContent          = new byte[BlockSize];
            IArrayBuffer <byte> value = Unpooled.WrappedBuffer(valueContent);

            for (int i = 0; i < this.buffer.Capacity - BlockSize + 1; i += BlockSize)
            {
                this.random.NextBytes(valueContent);
                value.SetIndex(0, BlockSize);
                this.buffer.Set(i, value);
                Assert.Equal(BlockSize, value.ReaderIndex);
                Assert.Equal(BlockSize, value.WriterIndex);
            }

            this.random = new Random(this.seed);
            var expectedValueContent          = new byte[BlockSize];
            IArrayBuffer <byte> expectedValue = Unpooled.WrappedBuffer(expectedValueContent);

            for (int i = 0; i < this.buffer.Capacity - BlockSize + 1; i += BlockSize)
            {
                this.random.NextBytes(expectedValueContent);
                value.Clear();
                this.buffer.Get(i, value);
                Assert.Equal(0, value.ReaderIndex);
                Assert.Equal(BlockSize, value.WriterIndex);
                for (int j = 0; j < BlockSize; j++)
                {
                    Assert.Equal(expectedValue.Get(j), value.Get(j));
                }
            }
        }
Exemplo n.º 13
0
        internal WritableBuffer(IArrayBuffer <byte> buffer, bool isLittleEndian)
        {
            Contract.Requires(buffer != null);

            this.ArrayBuffer    = buffer;
            this.isLittleEndian = isLittleEndian;
        }
Exemplo n.º 14
0
        static void OnReadCallback(IntPtr handle, IntPtr nread, ref uv_buf_t buf)
        {
            var stream = HandleContext.GetTarget <StreamHandle>(handle);
            IArrayBuffer <byte> byteBuffer = stream.pipeline.GetBuffer(ref buf);

            stream.OnReadCallback(byteBuffer, (int)nread.ToInt64());
        }
Exemplo n.º 15
0
        public static bool Equals(IArrayBuffer <byte> bufferA, IArrayBuffer <byte> bufferB)
        {
            int aLen = bufferA.ReadableCount;

            return(aLen == bufferB.ReadableCount &&
                   Equals(bufferA, bufferA.ReaderIndex, bufferB, bufferB.ReaderIndex, aLen));
        }
Exemplo n.º 16
0
        public static void SetInt64(this IArrayBuffer <byte> buffer, int start, long value, bool isLittleEndian)
        {
            Contract.Requires(buffer != null);

            buffer.CheckIndex(start, sizeof(long));
            int index = buffer.ArrayOffset + start;

            if (isLittleEndian)
            {
                buffer.Array[index]     = (byte)value;
                buffer.Array[index + 1] = (byte)(value >> 8);
                buffer.Array[index + 2] = (byte)(value >> 16);
                buffer.Array[index + 3] = (byte)(value >> 24);
                buffer.Array[index + 4] = (byte)(value >> 32);
                buffer.Array[index + 5] = (byte)(value >> 40);
                buffer.Array[index + 6] = (byte)(value >> 48);
                buffer.Array[index + 7] = (byte)(value >> 56);
            }
            else
            {
                buffer.Array[index]     = (byte)(value >> 56);
                buffer.Array[index + 1] = (byte)(value >> 48);
                buffer.Array[index + 2] = (byte)(value >> 40);
                buffer.Array[index + 3] = (byte)(value >> 32);
                buffer.Array[index + 4] = (byte)(value >> 24);
                buffer.Array[index + 5] = (byte)(value >> 16);
                buffer.Array[index + 6] = (byte)(value >> 8);
                buffer.Array[index + 7] = (byte)value;
            }
        }
Exemplo n.º 17
0
        public static long GetInt64(this IArrayBuffer <byte> buffer, int start, bool isLittleEndian)
        {
            Contract.Requires(buffer != null);

            buffer.CheckIndex(start, sizeof(long));
            int index = buffer.ArrayOffset + start;

            if (isLittleEndian)
            {
                // ReSharper disable once RedundantCast
                return((long)buffer.Array[index]
                       | (long)buffer.Array[index + 1] << 8
                       | (long)buffer.Array[index + 2] << 16
                       | (long)buffer.Array[index + 3] << 24
                       | (long)buffer.Array[index + 4] << 32
                       | (long)buffer.Array[index + 5] << 40
                       | (long)buffer.Array[index + 6] << 48
                       | (long)buffer.Array[index + 7] << 56);
            }

            return(((long)buffer.Array[index] & 0xFF) << 56
                   | ((long)buffer.Array[index + 1] & 0xFF) << 48
                   | ((long)buffer.Array[index + 2] & 0xFF) << 40
                   | ((long)buffer.Array[index + 3] & 0xFF) << 32
                   | ((long)buffer.Array[index + 4] & 0xFF) << 24
                   | ((long)buffer.Array[index + 5] & 0xFF) << 16
                   | ((long)buffer.Array[index + 6] & 0xFF) << 8
                   | ((long)buffer.Array[index + 7] & 0xFF));
        }
Exemplo n.º 18
0
        public static IArrayBuffer <byte> CopiedBuffer(params IArrayBuffer <byte>[] buffers)
        {
            Contract.Requires(buffers != null);

            if (buffers.Length == 0)
            {
                return(Empty);
            }

            if (buffers.Length == 1)
            {
                return(CopiedBuffer(buffers[0]));
            }

            long newlength = 0;

            foreach (IArrayBuffer <byte> buffer in buffers)
            {
                newlength += buffer.ReadableCount;
            }

            var mergedArray = new byte[newlength];

            for (int i = 0, j = 0; i < buffers.Length; i++)
            {
                IArrayBuffer <byte> b = buffers[i];

                int bLen = b.ReadableCount;
                b.Get(b.ReaderIndex, mergedArray, j, bLen);
                j += bLen;
            }

            return(WrappedBuffer(mergedArray));
        }
Exemplo n.º 19
0
Arquivo: Udp.cs Projeto: doruu12/NetUV
        void OnReceivedCallback(IArrayBuffer <byte> byteBuffer, int status, IPEndPoint remoteEndPoint)
        {
            // status (nread)
            //     Number of bytes that have been received.
            //     0 if there is no more data to read. You may discard or repurpose the read buffer.
            //     Note that 0 may also mean that an empty datagram was received (in this case addr is not NULL).
            //     < 0 if a transmission error was detected.

            // For status = 0 (Nothing to read)
            if (status >= 0)
            {
                Contract.Assert(byteBuffer != null);

                if (Log.IsDebugEnabled)
                {
                    // ReSharper disable once PossibleNullReferenceException
                    Log.DebugFormat("{0} {1} read, buffer length = {2} status = {3}.",
                                    this.HandleType, this.InternalHandle, byteBuffer.Capacity, status);
                }

                this.InvokeRead(byteBuffer, status, remoteEndPoint);
                return;
            }

            Exception exception = NativeMethods.CreateError((uv_err_code)status);

            Log.Error($"{this.HandleType} {this.InternalHandle} read error, status = {status}", exception);

            this.bufferQueue.Clear();
            this.InvokeRead(byteBuffer, 0, remoteEndPoint, exception);
        }
Exemplo n.º 20
0
Arquivo: Udp.cs Projeto: doruu12/NetUV
        void InvokeRead(IArrayBuffer <byte> byteBuffer, int size, IPEndPoint remoteEndPoint, Exception error = null)
        {
            if (size == 0 && error == null)
            {
                // Filter out empty data received if not an error
                //
                // On windows the udp receive actually been call with empty data
                // for broadcast, on Linux, the receive is not called at all.
                //
                byteBuffer?.Release();
                return;
            }

            ReadableBuffer buffer     = size > 0 ? new ReadableBuffer(byteBuffer, size) : ReadableBuffer.Empty;
            var            completion = new DatagramReadCompletion(ref buffer, error, remoteEndPoint);

            try
            {
                this.readAction?.Invoke(this, completion);
            }
            catch (Exception exception)
            {
                Log.Warn($"{nameof(Udp)} Exception whilst invoking read callback.", exception);
            }
            finally
            {
                completion.Dispose();
            }
        }
Exemplo n.º 21
0
        public IArrayBuffer <T> ReadSlice(int length)
        {
            IArrayBuffer <T> slice = this.Slice(this.ReaderIndex, length);

            this.ReaderIndex += length;
            return(slice);
        }
Exemplo n.º 22
0
        internal uv_buf_t[] GetBuffer()
        {
            if (this.buffer == null)
            {
                throw new ObjectDisposedException(
                          $"{nameof(BufferRef)} has already been disposed.");
            }

            if (this.handle.IsAllocated ||
                this.array.IsAllocated)
            {
                throw new InvalidOperationException(
                          $"{nameof(BufferRef)} has already been initialized and not released yet.");
            }

            IArrayBuffer <byte> arrayBufer = this.buffer;

            this.array = GCHandle.Alloc(arrayBufer.Array, GCHandleType.Pinned);
            IntPtr arrayHandle = this.array.AddrOfPinnedObject();

            var bufs = new[] { new uv_buf_t(arrayHandle + this.index, this.length) };

            this.handle = GCHandle.Alloc(bufs, GCHandleType.Pinned);

            return(bufs);
        }
Exemplo n.º 23
0
        void InvokeRead(IArrayBuffer <byte> byteBuffer, int size, Exception error = null, bool completed = false)
        {
            if (size == 0)
            {
                byteBuffer?.Release();
            }

            ReadableBuffer buffer = byteBuffer != null && size > 0
                ? new ReadableBuffer(byteBuffer, size)
                : ReadableBuffer.Empty;

            var completion = new StreamReadCompletion(ref buffer, error, completed);

            try
            {
                this.streamConsumer?.Consume(this.streamHandle, completion);
            }
            catch (Exception exception)
            {
                Log.Warn($"{nameof(Pipeline)} Exception whilst invoking read callback.", exception);
            }
            finally
            {
                completion.Dispose();
            }
        }
Exemplo n.º 24
0
 public IArrayBuffer <T> Read(IArrayBuffer <T> destination, int dstIndex, int length)
 {
     this.CheckReadableCount(length);
     this.Get(this.ReaderIndex, destination, dstIndex, length);
     this.ReaderIndex += length;
     return(this);
 }
Exemplo n.º 25
0
        static int IndexOf(IArrayBuffer <byte> haystack, byte[] separator)
        {
            for (int i = haystack.ReaderIndex; i < haystack.WriterIndex; i++)
            {
                int haystackIndex = i;
                int needleIndex;
                for (needleIndex = 0; needleIndex < separator.Length; needleIndex++)
                {
                    if (haystack.Get(haystackIndex) != separator[needleIndex])
                    {
                        break;
                    }
                    else
                    {
                        haystackIndex++;
                        if (haystackIndex == haystack.WriterIndex && needleIndex != separator.Length - 1)
                        {
                            return(-1);
                        }
                    }
                }

                if (needleIndex == separator.Length)
                {
                    // Found the needle from the haystack!
                    return(i - haystack.ReaderIndex);
                }
            }

            return(-1);
        }
Exemplo n.º 26
0
        public static string GetString(this IArrayBuffer <byte> buffer, int start, byte[] separator, Encoding encoding, out int count)
        {
            Contract.Requires(buffer != null);
            Contract.Requires(encoding != null);
            Contract.Requires(separator != null && separator.Length > 0);

            buffer.CheckIndex(start);
            int index = buffer.ArrayOffset + start;

            if (buffer.ReadableCount < separator.Length)
            {
                count = buffer.ReadableCount;
                return(encoding.GetString(buffer.Array, index, count));
            }

            int frameLength = IndexOf(buffer, separator);

            if (frameLength == 0) // Leading separator
            {
                frameLength = separator.Length;
            }
            else if (frameLength < 0) // Not found
            {
                frameLength = buffer.ReadableCount;
            }

            count = frameLength;
            return(encoding.GetString(buffer.Array, index, count));
        }
Exemplo n.º 27
0
Arquivo: Udp.cs Projeto: doruu12/NetUV
        public WritableBuffer Allocate(int size)
        {
            Contract.Requires(size > 0);

            IArrayBuffer <byte> buffer = this.allocator.Buffer(size);

            return(new WritableBuffer(buffer));
        }
Exemplo n.º 28
0
        public DuplicatedArrayBuffer(IArrayBuffer <T> source)
            : base(source.MaxCapacity)
        {
            var asDuplicate = source as DuplicatedArrayBuffer <T>;

            this.buffer = asDuplicate != null ? asDuplicate.buffer : source;
            this.SetIndex(source.ReaderIndex, source.WriterIndex);
        }
Exemplo n.º 29
0
        internal void OnReadCompleted(IArrayBuffer <byte> byteBuffer, int size)
        {
            Contract.Requires(byteBuffer != null);
            Contract.Requires(size >= 0);

            this.receiveBufferSizeEstimate.Record(size);
            this.InvokeRead(byteBuffer, size);
        }
Exemplo n.º 30
0
        public static void SetDouble(this IArrayBuffer <byte> buffer, int start, double value, bool isLittleEndian)
        {
            Contract.Requires(buffer != null);

            buffer.SetInt64(start,
                            BitConverter.DoubleToInt64Bits(value),
                            BitConverter.IsLittleEndian == isLittleEndian);
        }