Example #1
0
 /// <exception cref="ObjectDisposedException"/>
 public ValueTask <SocketError> SendAsync(ReadOnlyMemory <byte> buffer, CancellationToken cancellationToken = default)
 {
     if (!IsDisposed)
     {
         if (!cancellationToken.IsCancellationRequested)
         {
             if (_sendArgs.Reserve())
             {
                 return(_sendArgs.SendAsync(_socket, buffer));
             }
             else
             {
                 return(new ValueTask <SocketError>(task: Task.FromException <SocketError>(SimultaneouslyOperationException())));
             }
         }
         else
         {
             return(new ValueTask <SocketError>(Task.FromCanceled <SocketError>(cancellationToken)));
         }
     }
     else
     {
         return(DisposedValueTask <SocketError>());
     }
 }
Example #2
0
        internal ValueTask <int> SendAsync(ReadOnlyMemory <byte> buffer, SocketFlags socketFlags, CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return(ValueTask.FromCanceled <int>(cancellationToken));
            }

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

            Debug.Assert(saea.BufferList == null);
            saea.SetBuffer(MemoryMarshal.AsMemory(buffer));
            saea.SocketFlags = socketFlags;
            saea.WrapExceptionsForNetworkStream = false;
            return(saea.SendAsync(this, cancellationToken));
        }