예제 #1
0
        public override Memory <byte> GetMemory(int sizeHint = 0)
        {
            if (_isCompleted)
            {
                throw new InvalidOperationException($"{nameof(ThrottledPipeWriter)} is completed.");
            }

            if (_currentBuffer.Length != 0 && _currentBuffer.Length >= sizeHint)
            {
                return(_currentBuffer.Memory);
            }

            // buffer is too small, dispose it and create another one.
            _currentBuffer.Dispose();

            if (sizeHint == 0)
            {
                sizeHint = 4096;
            }

            _currentBuffer = new ReferenceCountedMemory(_pool, sizeHint);
            Debug.Assert(_currentBuffer.Length >= sizeHint);

            return(_currentBuffer.Memory);
        }
예제 #2
0
            public ReferenceCountedMemory Move()
            {
                ReferenceCountedMemory ret = this;

                this = default;
                return(ret);
            }
예제 #3
0
        // simulates connection latency by waiting a set number of milliseconds before writing.
        private void SendWithDelay(ReferenceCountedMemory buffer)
        {
            long sendTicks = Environment.TickCount64 + _sendDelayInMs;

            Task prev = _previousSendTask;

            if (prev.IsFaulted == true)
            {
                prev.GetAwaiter().GetResult();
            }

            _previousSendTask = SendWithDelayAsync();

            async Task SendWithDelayAsync()
            {
                await prev.ConfigureAwait(false);

                prev = null;

                long ticksLeft = sendTicks - Environment.TickCount64;

                if (ticksLeft > 0)
                {
                    await Task.Delay((int)ticksLeft).ConfigureAwait(false);
                }

                ValueTask <FlushResult> writeValueTask = _baseWriter.WriteAsync(buffer.Memory);
                Task writeTask = writeValueTask.IsCompletedSuccessfully ? Task.CompletedTask : writeValueTask.AsTask();

                _pendingWriteTask = writeTask;

                await writeTask.ConfigureAwait(false);

                buffer.Dispose();
            }
        }