-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Replace ATP pattern with async/await in SmtpClient (part 1) #115366
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.IO; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace System.Net.Mail | ||
| { | ||
| internal interface IReadWriteAdapter | ||
| { | ||
| static abstract ValueTask<int> ReadAsync(Stream stream, Memory<byte> buffer, CancellationToken cancellationToken); | ||
| static abstract ValueTask<int> ReadAtLeastAsync(Stream stream, Memory<byte> buffer, int minimumBytes, bool throwOnEndOfStream, CancellationToken cancellationToken); | ||
| static abstract ValueTask WriteAsync(Stream stream, ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken); | ||
| static abstract Task FlushAsync(Stream stream, CancellationToken cancellationToken); | ||
| static abstract Task WaitAsync(TaskCompletionSource<bool> waiter); | ||
| } | ||
|
|
||
| internal readonly struct AsyncReadWriteAdapter : IReadWriteAdapter | ||
| { | ||
| public static ValueTask<int> ReadAsync(Stream stream, Memory<byte> buffer, CancellationToken cancellationToken) => | ||
| stream.ReadAsync(buffer, cancellationToken); | ||
|
|
||
| public static ValueTask<int> ReadAtLeastAsync(Stream stream, Memory<byte> buffer, int minimumBytes, bool throwOnEndOfStream, CancellationToken cancellationToken) => | ||
| stream.ReadAtLeastAsync(buffer, minimumBytes, throwOnEndOfStream, cancellationToken); | ||
|
|
||
| public static ValueTask WriteAsync(Stream stream, ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken) => | ||
| stream.WriteAsync(buffer, cancellationToken); | ||
|
|
||
| public static Task FlushAsync(Stream stream, CancellationToken cancellationToken) => stream.FlushAsync(cancellationToken); | ||
|
|
||
| public static Task WaitAsync(TaskCompletionSource<bool> waiter) => waiter.Task; | ||
| } | ||
|
|
||
| internal readonly struct SyncReadWriteAdapter : IReadWriteAdapter | ||
| { | ||
| public static ValueTask<int> ReadAsync(Stream stream, Memory<byte> buffer, CancellationToken cancellationToken) => | ||
| new ValueTask<int>(stream.Read(buffer.Span)); | ||
|
|
||
| public static ValueTask<int> ReadAtLeastAsync(Stream stream, Memory<byte> buffer, int minimumBytes, bool throwOnEndOfStream, CancellationToken cancellationToken) => | ||
| new ValueTask<int>(stream.ReadAtLeast(buffer.Span, minimumBytes, throwOnEndOfStream)); | ||
|
|
||
| public static ValueTask WriteAsync(Stream stream, ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken) | ||
| { | ||
| stream.Write(buffer.Span); | ||
| return default; | ||
| } | ||
|
|
||
| public static Task FlushAsync(Stream stream, CancellationToken cancellationToken) | ||
| { | ||
| stream.Flush(); | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| public static Task WaitAsync(TaskCompletionSource<bool> waiter) | ||
| { | ||
| waiter.Task.GetAwaiter().GetResult(); | ||
| return Task.CompletedTask; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea of the SyncReadWriteAdapter is that it always returns a completed task, so that calling code can be fully synchronous (and can assert that task.IsCompleted). This file was copied from SslStream implementation, where it is used to deduplicate sync/async logic (in fact, it was the inspiration for this refactoring). Followup PR removes this copy of the ReadWriteAdapters and shares the same file. |
||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.