// Placed object message payload into a segment from a buffer pool.  When this get's too big, older blocks will be purged
        private ArraySegment <byte> SerializeMessageIntoPooledSegment(GeneratedBatchContainer queueMessage)
        {
            byte[] serializedPayload = this.serializationManager.SerializeToByteArray(queueMessage.Payload);
            // get size of namespace, offset, partitionkey, properties, and payload
            int size = SegmentBuilder.CalculateAppendSize(serializedPayload);

            // get segment
            ArraySegment <byte> segment;

            if (currentBuffer == null || !currentBuffer.TryGetSegment(size, out segment))
            {
                // no block or block full, get new block and try again
                currentBuffer = bufferPool.Allocate();
                //call EvictionStrategy's OnBlockAllocated method
                this.evictionStrategy.OnBlockAllocated(currentBuffer);
                // if this fails with clean block, then requested size is too big
                if (!currentBuffer.TryGetSegment(size, out segment))
                {
                    string errmsg = $"Message size is to big. MessageSize: {size}";
                    throw new ArgumentOutOfRangeException(nameof(queueMessage), errmsg);
                }
            }

            // encode namespace, offset, partitionkey, properties and payload into segment
            int writeOffset = 0;

            SegmentBuilder.Append(segment, ref writeOffset, serializedPayload);

            return(segment);
        }
        // Placed object message payload into a segment from a buffer pool.  When this get's too big, older blocks will be purged
        private ArraySegment <byte> SerializeMessageIntoPooledSegment(EventData queueMessage)
        {
            byte[] payloadBytes    = queueMessage.GetBytes();
            string streamNamespace = queueMessage.GetStreamNamespaceProperty();
            int    size            = SegmentBuilder.CalculateAppendSize(streamNamespace) +
                                     SegmentBuilder.CalculateAppendSize(queueMessage.Offset) +
                                     SegmentBuilder.CalculateAppendSize(payloadBytes);

            // get segment from current block
            ArraySegment <byte> segment;

            if (currentBuffer == null || !currentBuffer.TryGetSegment(size, out segment))
            {
                // no block or block full, get new block and try again
                currentBuffer = bufferPool.Allocate();
                currentBuffer.SetPurgeAction(PurgeAction);
                // if this fails with clean block, then requested size is too big
                if (!currentBuffer.TryGetSegment(size, out segment))
                {
                    string errmsg = String.Format(CultureInfo.InvariantCulture,
                                                  "Message size is to big. MessageSize: {0}", size);
                    throw new ArgumentOutOfRangeException("queueMessage", errmsg);
                }
            }
            // encode namespace, offset, and payload into segment
            int writeOffset = 0;

            SegmentBuilder.Append(segment, ref writeOffset, streamNamespace);
            SegmentBuilder.Append(segment, ref writeOffset, queueMessage.Offset);
            SegmentBuilder.Append(segment, ref writeOffset, payloadBytes);

            return(segment);
        }
        public void FixedBufferCanAdjustReadOffsetOnOverwrite()
        {
            int dataLength   = 100;
            int bufferLength = 30;
            int readOffset   = 10;

            int writeInitial = 10;
            int readInitial  = 5;

            FixedSizeBuffer <int> fixedSizeBuffer = new FixedSizeBuffer <int>(bufferLength);
            var numbers  = Enumerable.Range(0, dataLength).ToArray();
            var readData = new int[numbers.Length + readOffset];

            fixedSizeBuffer.Write(Enumerable.Range(5000, writeInitial).ToArray(), 0, writeInitial);
            fixedSizeBuffer.Read(readData, 0, readInitial);
            Array.Clear(readData, 0, readData.Length);

            int written = fixedSizeBuffer.Write(numbers, 0, numbers.Length);

            Assert.AreEqual(numbers.Length, written);
            Assert.AreEqual(fixedSizeBuffer.Buffered, bufferLength);

            int read = fixedSizeBuffer.Read(readData, readOffset, readData.Length);

            Assert.AreEqual(read, bufferLength);
            Assert.AreEqual(fixedSizeBuffer.Buffered, 0);

            Array.Copy(readData, readOffset, readData, 0, dataLength);
            Array.Copy(numbers, dataLength - bufferLength, numbers, 0, bufferLength);
            for (int i = 0; i < bufferLength; i++)
            {
                Assert.AreEqual(readData[i], numbers[i]);
            }
        }
Exemple #4
0
        public void FixedSizeBuffer_must_start_as_empty(int size)
        {
            var buf = FixedSizeBuffer.Create <NotUsed>(size);

            buf.IsEmpty.Should().BeTrue();
            buf.IsFull.Should().BeFalse();
        }
            /// <summary>
            ///
            /// </summary>
            /// <param name="buffer"></param>
            /// <param name="cancellationToken"></param>
            /// <returns>-1 if error. 0 if read timeout.</returns>
            public override async ValueTask <int> ReadAsync(Memory <byte> buffer, CancellationToken cancellationToken = default)
            {
                UpdateLastActive();
                if (_closed)
                {
                    return(-1);
                }

                int read = -1;

                if (null != _semaphoreReceived && await _semaphoreReceived.WaitAsync(UdpServer.CLIENT_ACTIVE_TIMEOUT + 100))
                {
                    //_logger?.LogInformation("UdpClient2 _semaphoreReceived.WaitAsync()...");
                    if (_receivedPackets.IsEmpty)//timeout.
                    {
                        return(Active ? 0 : -1);
                    }

                    FixedSizeBuffer fixedSizeBuffer = null;
                    while (!_receivedPackets.TryDequeue(out fixedSizeBuffer) && _receivedPackets.Count > 0)
                    {
                    }
                    if (null != fixedSizeBuffer)
                    {
                        Memory <byte> mem = new Memory <byte>(fixedSizeBuffer.Memory, fixedSizeBuffer.Offset, fixedSizeBuffer.SignificantLength);
                        if (mem.TryCopyTo(buffer))
                        {
                            read = mem.Length;
                        }
                        fixedSizeBuffer.Pool.Return(fixedSizeBuffer);
                    }
                }
                return(read);
            }
Exemple #6
0
        /// <summary>
        ///
        /// </summary>
        /// <returns>usually null.</returns>
        public override async Task <UdpClient2> Accept()//TODO MaxNumClient limit. //TODO blocklist.
        {
            if (null == _listenerClient)
            {
                return(null);
            }
            try
            {
                #region receive packet
                FixedSizeBuffer     buff = _bufferPool.Rent();
                ArraySegment <byte> seg  = new ArraySegment <byte>(buff.Memory, buff.Offset, buff.Memory.Length);

                //TODO cache
                IPEndPoint clientEndPoint = new IPEndPoint(_listenerClient.Client.LocalEndPoint.AddressFamily == AddressFamily.InterNetworkV6 ?
                                                           IPAddress.IPv6Any : IPAddress.Any, 0);
                var result = await _listenerClient.Client.ReceiveFromAsync(seg, SocketFlags.None, clientEndPoint);// _listenerClient.Client.LocalEndPoint);

                buff.SignificantLength = result.ReceivedBytes;
                #endregion

                //////var result = await _listenerClient.ReceiveAsync();
                var client = _clientManager.Get(result.RemoteEndPoint);
                if (null != client)//
                {
                    #region delivery packet
                    if (client.Active)
                    {
                        client.PostReceived(buff);
                        return(null);
                    }
                    else//inactive
                    {
                        DeleteClient(result.RemoteEndPoint);
                        buff.Pool.Return(buff);//drop packet.
                        return(null);
                    }
                    #endregion
                }
                else
                {
                    #region create client
                    client = CreateClient(_listenerClient.Client, result.RemoteEndPoint as IPEndPoint);
                    client.PostReceived(buff);
                    KeepClient(client);

                    _logger?.LogInformation($"UdpServer New client:[{client.EndPoint.ToString()}].");
                    return(client);

                    #endregion
                }
            }
            catch (SocketException se)
            {
                _logger?.LogError(se, "UdpServer ReceiveFromAsync error.");
                return(null);
            }
        }
 public bool ShouldPurge(ref CachedMessage cachedMessage, IDisposable purgeRequest)
 {
     var purgedResource = (FixedSizeBuffer) purgeRequest;
     // if we're purging our current buffer, don't use it any more
     if (currentBuffer != null && currentBuffer.Id == purgedResource.Id)
     {
         currentBuffer = null;
     }
     return cachedMessage.Payload.Array == purgedResource.Id;
 }
        /// <summary>
        /// Creates an new instance of the WriteableBufferingSource class.
        /// </summary>
        /// <param name="waveFormat">The WaveFormat of the source.</param>
        /// <param name="bufferSize">Buffersize in bytes</param>
        public WriteableBufferingSource(WaveFormat waveFormat, int bufferSize)
        {
            if (waveFormat == null)
                throw new ArgumentNullException("waveFormat");
            if (bufferSize <= 0 || (bufferSize % waveFormat.BlockAlign) != 0)
                throw new ArgumentException("Invalid bufferSize.");

            _waveFormat = waveFormat;
            _buffer = new FixedSizeBuffer<byte>(bufferSize);
            FillWithZeros = true;
        }
Exemple #9
0
        public void FixedBufferExample()
        {
            FixedSizeBuffer myBuffer = new FixedSizeBuffer();

            for (int i = 0; i < 10; i++)
            {
                unsafe
                {
                    Console.WriteLine(myBuffer.myDataArray[i]);
                }
            }
        }
Exemple #10
0
        public void FixedSizeBuffer_must_become_non_full_after_head_dropped_from_full_buffer(int size)
        {
            var buf = FixedSizeBuffer.Create <string>(size);

            for (var elem = 1; elem <= size; elem++)
            {
                buf.Enqueue("test");
            }

            buf.DropHead();

            buf.IsEmpty.Should().Be(size == 1);
            buf.IsFull.Should().BeFalse();
        }
        /// <inheritdoc />
        public void OnBlockAllocated(IDisposable newBlock)
        {
            var newBuffer = newBlock as FixedSizeBuffer;

            if (this.PurgeObservable.IsEmpty && this.currentBuffer != null &&
                this.inUseBuffers.Contains(this.currentBuffer) && this.inUseBuffers.Count == 1)
            {
                this.purgedBuffers.Enqueue(this.inUseBuffers.Dequeue());
            }

            this.inUseBuffers.Enqueue(newBuffer);
            this.currentBuffer = newBuffer;
            newBuffer.SetPurgeAction(this.OnFreeBlockRequest);
        }
 public void PostReceived(FixedSizeBuffer buffer)
 {
     if (_receivedPackets.Count >= MAX_BUFFE_COUNT)//too many packets to be read.
     {
         //drop the packet.
         buffer.Pool.Return(buffer);
     }
     else
     {
         _receivedPackets.Enqueue(buffer);
         _semaphoreReceived.Release();
         //_logger?.LogInformation("UdpClient2 _semaphoreReceived.Release().");
     }
 }
        /// <summary>
        /// Creates an new instance of the WriteableBufferingSource class.
        /// </summary>
        /// <param name="waveFormat">The WaveFormat of the source.</param>
        /// <param name="bufferSize">Buffersize in bytes</param>
        public WriteableBufferingSource(WaveFormat waveFormat, int bufferSize)
        {
            if (waveFormat == null)
            {
                throw new ArgumentNullException("waveFormat");
            }
            if (bufferSize <= 0 || (bufferSize % waveFormat.BlockAlign) != 0)
            {
                throw new ArgumentException("Invalid bufferSize.");
            }

            _waveFormat   = waveFormat;
            _buffer       = new FixedSizeBuffer <byte>(bufferSize);
            FillWithZeros = true;
        }
Exemple #14
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BufferSource"/> class.
        /// </summary>
        /// <param name="source">The <see cref="IWaveSource"/> to buffer.</param>
        /// <param name="bufferSize">Size of the buffer.</param>
        /// <exception cref="System.ArgumentOutOfRangeException"><paramref name="bufferSize"/> is out of range.</exception>
        public BufferSource(IWaveSource source, int bufferSize)
            : base(source)
        {
            if (bufferSize <= 0 || bufferSize % source.WaveFormat.BlockAlign != 0)
                throw new ArgumentOutOfRangeException("bufferSize");

            _buffer = new FixedSizeBuffer<byte>(bufferSize);
            _lockObject = new Object();

            _bufferThread = new Thread(BufferProc)
            {
                Priority = ThreadPriority.Normal,
                IsBackground = false
            };
            _bufferThread.Start();
        }
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         //dispose managed
         _buffer.Dispose();
         _buffer = null;
     }
 }
Exemple #16
0
        private void BufferProc(object o)
        {
            if (_stream == null || _stream.CanRead == false)
                throw new Exception("Mp3WebStream not initialized");

            MP3Frame frame = GetNextFrame(_stream);

            int channels = frame.ChannelMode == MP3ChannelMode.Stereo ? 2 : 1;
            AcmConverter converter = new AcmConverter(new MP3Format(frame.SampleRate, frame.ChannelCount, frame.FrameLength, frame.BitRate));

            _waveFormat = converter.DestinationFormat;

            byte[] buffer = new byte[16384 * 4];
            _buffer = new FixedSizeBuffer<byte>(converter.DestinationFormat.BytesPerSecond * 10);

            ManualResetEvent resetEvent = o as ManualResetEvent;
            resetEvent.Set();

            do
            {
                if (_buffer.Buffered >= _buffer.Length * 0.85 && !_disposing)
                {
                    Thread.Sleep(250);
                    continue;
                }
                try
                {
                    frame = GetNextFrame(_stream);
                    //_frameBuffer is set in GetNextFrame
                    int count = converter.Convert(_frameBuffer, frame.FrameLength, buffer, 0);
                    if (count > 0)
                    {
                        int written = _buffer.Write(buffer, 0, count);
                    }
                }
                catch (MmException)
                {
                    _disposing = true;
                    ThreadPool.QueueUserWorkItem((c) =>
                    {
                        while (_bufferThread.ThreadState != System.Threading.ThreadState.Stopped) ;
                        CreateStream(false);
                    });
                }
                catch (WebException)
                {
                    InitializeConnection();
                }
                catch (IOException)
                {
                    InitializeConnection();
                }
            } while (!_disposing);

            if (converter != null)
                converter.Dispose();
        }
Exemple #17
0
 /// <summary>
 /// Releases unmanaged and - optionally - managed resources.
 /// </summary>
 /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
 protected virtual void Dispose(bool disposing)
 {
     if (!_disposed)
     {
         _disposing = true;
         if (_bufferThread != null &&
             _bufferThread.ThreadState != ThreadState.Stopped &&
             !_bufferThread.Join(500))
             _bufferThread.Abort();
         if (_buffer != null)
         {
             _buffer.Dispose();
             _buffer = null;
         }
         if (_decoder != null)
         {
             _decoder.Dispose();
             _decoder = null;
         }
         CloseResponse();
     }
     _disposed = true;
 }
Exemple #18
0
        private void BufferProc(object o)
        {
            var resetEvent = o as EventWaitHandle;

            try
            {
                if (_stream == null || _stream.CanRead == false)
                    throw new Exception("Mp3WebStream not initialized");

                byte[] buffer = null;
                int read;

                Mp3Format format, prevFormat;
                Mp3Frame frame;

                read = ReadRawDataFromFrame(ref buffer, out frame);
                format = new Mp3Format(frame.SampleRate, frame.ChannelCount, frame.FrameLength,
                    frame.BitRate);

                _buffer = new FixedSizeBuffer<byte>(format.BytesPerSecond * 2);

                do
                {
                    _buffer.Write(buffer, 0, read);
                    read = ReadRawDataFromFrame(ref buffer, out frame);
                    prevFormat = format;
                    format = new Mp3Format(frame.SampleRate, frame.ChannelCount, frame.FrameLength,
                        frame.BitRate);
                } while (_buffer.Buffered < _buffer.Length / 10 || !format.Equals(prevFormat));

                if (resetEvent != null)
                    resetEvent.Set();

                do
                {
                    Debug.WriteLine("Buffering");
                    if (_buffer.Buffered >= _buffer.Length * 0.85 && !_disposing)
                        Thread.Sleep(250);
                    else
                    {
                        _buffer.Write(buffer, 0, read);
                        read = ReadRawDataFromFrame(ref buffer, out frame);
                    }
                } while (!_disposing);
            }
            finally
            {
                if (resetEvent != null)
                    resetEvent.Set();
            }
        }
		/// <summary>Called only by constructors. This creates the actual mesh and the buffers for verts/tris etc.</summary>
		private void Setup(){
			UV=new FixedSizeBuffer<Vector2>(4,false);
			Triangles=new FixedSizeBuffer<int>(6,true);
			Colours=new FixedSizeBuffer<Color>(4,false);
			Vertices=new FixedSizeBuffer<Vector3>(4,false);
			UV2=new FixedSizeBuffer<Vector2>(4,false);
			
			OutputMesh=new Mesh();
			OutputGameObject=new GameObject();
			OutputTransform=OutputGameObject.GetComponent<Transform>();	
			
			Renderer=OutputGameObject.AddComponent<MeshRenderer>();
			Renderer.sharedMaterial=Material;
			
			MeshFilter filter=OutputGameObject.AddComponent<MeshFilter>();
			filter.mesh=OutputMesh;
		}
		/// <summary>Let the mesh know that normals are required.</summary>
		public void RequireNormals(){
			
			Normals=new FixedSizeBuffer<Vector3>(4,false);
			
		}
		/// <summary>Let the mesh know that tangents are required.</summary>
		public void RequireTangents(){
			
			Tangents=new FixedSizeBuffer<Vector4>(4,false);
			
		}
		/// <summary>Permanently destroys this mesh.</summary>
		public void Destroy(){
			OutputMesh=null;
			OutputTransform=null;
			
			if(OutputGameObject!=null){
				GameObject.Destroy(OutputGameObject);
				OutputGameObject=null;
			}
			
			UV=null;
			UV2=null;
			Colours=null;
			Normals=null;
			Vertices=null;
			Triangles=null;
			Tangents=null;
			
		}