Example #1
0
        private static async Task CopyToAsyncCore(Stream destination, AwaitableSocketAsyncEventArgs ea, CancellationToken cancellationToken)
        {
            try
            {
                while (true)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    int bytesRead = await ea.ReceiveAsync();

                    if (bytesRead == 0)
                    {
                        break;
                    }

                    await destination.WriteAsync(ea.Buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
                }
            }
            finally
            {
                ArrayPool <byte> .Shared.Return(ea.Buffer, clearArray : true);

                ea.Dispose();
            }
        }
Example #2
0
 /// <exception cref="ObjectDisposedException"/>
 public ValueTask <SocketReceiveResult> ReceiveAsync(Memory <byte> buffer, CancellationToken cancellationToken = default)
 {
     if (!IsDisposed)
     {
         if (!cancellationToken.IsCancellationRequested)
         {
             if (_receiveArgs.Reserve())
             {
                 return(_receiveArgs.ReceiveAsync(_socket, buffer));
             }
             else
             {
                 return(new ValueTask <SocketReceiveResult>(task: Task.FromException <SocketReceiveResult>(SimultaneouslyOperationException())));
             }
         }
         else
         {
             return(new ValueTask <SocketReceiveResult>(Task.FromCanceled <SocketReceiveResult>(cancellationToken)));
         }
     }
     else
     {
         return(DisposedValueTask <SocketReceiveResult>());
     }
 }
Example #3
0
        internal ValueTask <int> ReceiveAsync(Memory <byte> buffer, SocketFlags socketFlags, bool fromNetworkStream, CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return(ValueTask.FromCanceled <int>(cancellationToken));
            }

            AwaitableSocketAsyncEventArgs saea =
                Interlocked.Exchange(ref _singleBufferReceiveEventArgs, null) ??
                new AwaitableSocketAsyncEventArgs(this, isReceiveForCaching: true);

            Debug.Assert(saea.BufferList == null);
            saea.SetBuffer(buffer);
            saea.SocketFlags = socketFlags;
            saea.WrapExceptionsForNetworkStream = fromNetworkStream;
            return(saea.ReceiveAsync(this, cancellationToken));
        }
Example #4
0
        private static async Task CopyToAsyncCore(Stream destination, AwaitableSocketAsyncEventArgs ea, CancellationToken cancellationToken)
        {
            try
            {
                while (true)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    int bytesRead = await ea.ReceiveAsync();
                    if (bytesRead == 0)
                    {
                        break;
                    }

                    await destination.WriteAsync(ea.Buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
                }
            }
            finally
            {
                ArrayPool<byte>.Shared.Return(ea.Buffer, clearArray: true);
                ea.Dispose();
            }
        }