Exemplo n.º 1
0
        public void Cancel_ReceiveFirst()
        {
            using var releaser = SocketMemoryPool.Default.Rent(100, out var buffer);
            var task = connection.ReceiveAsync(buffer).AsTask();

            connection.Dispose();
            Assert.CatchAsync <OperationCanceledException> (() => task);
        }
Exemplo n.º 2
0
        public static async ReusableTask ReceiveAsync(IPeerConnection connection, SocketMemory buffer, IRateLimiter rateLimiter, SpeedMonitor peerMonitor, SpeedMonitor managerMonitor)
        {
            await MainLoop.SwitchToThreadpool();

            while (buffer.Length > 0)
            {
                int  transferred;
                bool unlimited  = rateLimiter?.Unlimited ?? true;
                int  shouldRead = unlimited ? buffer.Length : Math.Min(ChunkLength, buffer.Length);

                if (rateLimiter != null && !unlimited && !rateLimiter.TryProcess(shouldRead))
                {
                    var tcs = new ReusableTaskCompletionSource <int> ();
                    lock (receiveQueue)
                        receiveQueue.Enqueue(new QueuedIO(connection, buffer, rateLimiter, tcs));
                    transferred = await tcs.Task.ConfigureAwait(false);
                }
                else
                {
                    transferred = await connection.ReceiveAsync(buffer.Slice(0, shouldRead)).ConfigureAwait(false);
                }

                if (transferred == 0)
                {
                    throw new ConnectionClosedException("Socket receive returned 0, indicating the connection has been closed.");
                }

                peerMonitor?.AddDelta(transferred);
                managerMonitor?.AddDelta(transferred);

                buffer = buffer.Slice(transferred);
            }
        }
Exemplo n.º 3
0
        public async Task DisposeWhileReceiving()
        {
            using var releaser = SocketMemoryPool.Default.Rent(100, out var buffer);
            var task = Incoming.ReceiveAsync(buffer).AsTask();

            Incoming.Dispose();

            // All we care about is that the task is marked as 'Complete'.
            _ = await Task.WhenAny(task).WithTimeout(1000);

            Assert.IsTrue(task.IsCompleted, "#1");
            GC.KeepAlive(task.Exception);  // observe the exception (if any)
        }