@@ -352,56 +352,14 @@ internal Task<int> SendAsync(IList<ArraySegment<byte>> buffers, SocketFlags sock
352352
353353 internal Task < int > SendToAsync ( ArraySegment < byte > buffer , SocketFlags socketFlags , EndPoint remoteEP )
354354 {
355- // Validate the arguments.
356- ValidateBuffer ( buffer ) ;
357- if ( remoteEP == null )
358- {
359- throw new ArgumentNullException ( nameof ( remoteEP ) ) ;
360- }
361-
362- // Get the SocketAsyncEventArgs instance to use for the operation.
363- Int32TaskSocketAsyncEventArgs saea = RentSocketAsyncEventArgs ( isReceive : false ) ;
364- if ( saea == null )
355+ var tcs = new TaskCompletionSource < int > ( this ) ;
356+ BeginSendTo ( buffer . Array , buffer . Offset , buffer . Count , socketFlags , remoteEP , iar =>
365357 {
366- // We couldn't get a cached instance, which means there's already a receive operation
367- // happening on this socket. Fall back to wrapping APM.
368- var tcs = new TaskCompletionSource < int > ( this ) ;
369- BeginSendTo ( buffer . Array , buffer . Offset , buffer . Count , socketFlags , remoteEP , iar =>
370- {
371- var innerTcs = ( TaskCompletionSource < int > ) iar . AsyncState ;
372- try { innerTcs . TrySetResult ( ( ( Socket ) innerTcs . Task . AsyncState ) . EndSendTo ( iar ) ) ; }
373- catch ( Exception e ) { innerTcs . TrySetException ( e ) ; }
374- } , tcs ) ;
375- return tcs . Task ;
376- }
377-
378- // Configure the buffer. We don't clear the buffers when returning the SAEA to the pool,
379- // so as to minimize overhead if the same buffer is used for subsequent operations (which is likely).
380- // But SAEA doesn't support having both a buffer and a buffer list configured, so clear out a buffer list
381- // if there is one before we set the desired buffer.
382- if ( saea . BufferList != null ) saea . BufferList = null ;
383- saea . SetBuffer ( buffer . Array , buffer . Offset , buffer . Count ) ;
384- saea . SocketFlags = socketFlags ;
385- saea . RemoteEndPoint = remoteEP ;
386-
387- // Initiate the send
388- Task < int > t ;
389- if ( ! SendToAsync ( saea ) )
390- {
391- // The operation completed synchronously. Get a task for it and return the SAEA for future use.
392- t = saea . SocketError == SocketError . Success ?
393- GetSuccessTask ( saea ) :
394- Task . FromException < int > ( new SocketException ( ( int ) saea . SocketError ) ) ;
395- ReturnSocketAsyncEventArgs ( saea , isReceive : false ) ;
396- }
397- else
398- {
399- // The operation completed asynchronously. Get the task for the operation,
400- // with appropriate synchronization to coordinate with the async callback
401- // that'll be completing the task.
402- t = saea . GetTaskSafe ( ) ;
403- }
404- return t ;
358+ var innerTcs = ( TaskCompletionSource < int > ) iar . AsyncState ;
359+ try { innerTcs . TrySetResult ( ( ( Socket ) innerTcs . Task . AsyncState ) . EndSendTo ( iar ) ) ; }
360+ catch ( Exception e ) { innerTcs . TrySetException ( e ) ; }
361+ } , tcs ) ;
362+ return tcs . Task ;
405363 }
406364
407365 /// <summary>Validates the supplied array segment, throwing if its array or indices are null or out-of-bounds, respectively.</summary>
@@ -532,8 +490,6 @@ private void ReturnSocketAsyncEventArgs(Int32TaskSocketAsyncEventArgs saea, bool
532490 }
533491 else
534492 {
535- saea . RemoteEndPoint = null ; // will have been set for SendToAsync methods
536-
537493 Debug . Assert ( _cachedSendEventArgs == s_rentedSentinel ) ;
538494 Volatile . Write ( ref _cachedSendEventArgs , saea ) ;
539495 }
0 commit comments