Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Replace StreamAsyncHelper with StreamApmExtensions in System.Net.Security #7800

Merged
merged 1 commit into from
Apr 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Common/src/System/IO/StreamApmExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.IO;
using System.Threading.Tasks;

namespace System.IO
{
/// <summary>Provides Stream.Begin/EndRead/Write wrappers for Stream.Read/WriteAsync.</summary>
internal static class StreamApmExtensions
{
public static IAsyncResult BeginRead(this Stream stream, byte[] buffer, int offset, int count, AsyncCallback callback, object state) =>
TaskToApm.Begin(stream.ReadAsync(buffer, offset, count), callback, state);

public static int EndRead(this Stream stream, IAsyncResult asyncResult) =>
TaskToApm.End<int>(asyncResult);

public static IAsyncResult BeginWrite(this Stream stream, byte[] buffer, int offset, int count, AsyncCallback callback, object state) =>
TaskToApm.Begin(stream.WriteAsync(buffer, offset, count), callback, state);

public static void EndWrite(this Stream stream, IAsyncResult asyncResult) =>
TaskToApm.End(asyncResult);
}
}
209 changes: 0 additions & 209 deletions src/Common/src/System/IO/StreamAsyncHelper.cs

This file was deleted.

13 changes: 3 additions & 10 deletions src/Common/tests/System/Net/VirtualNetwork/VirtualNetworkStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,9 @@ public override void Write(byte[] buffer, int offset, int count)

public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
try
{
cancellationToken.ThrowIfCancellationRequested();
int bytesRead = Read(buffer, offset, count);
return Task.FromResult<int>(bytesRead);
}
catch (Exception e)
{
return Task.FromException<int>(e);
}
return cancellationToken.IsCancellationRequested ?
Task.FromCanceled<int>(cancellationToken) :
Task.Run(() => Read(buffer, offset, count));
}

public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
Expand Down
6 changes: 0 additions & 6 deletions src/System.Net.Security/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,6 @@
<data name="security_ServiceNameCollection_EmptyServiceName" xml:space="preserve">
<value>A service name must not be null or empty.</value>
</data>
<data name="InvalidOperation_WrongAsyncResultOrEndReadCalledMultiple" xml:space="preserve">
<value>Either the IAsyncResult object did not come from the corresponding async method on this type, or EndRead was called multiple times with the same IAsyncResult.</value>
</data>
<data name="InvalidOperation_WrongAsyncResultOrEndWriteCalledMultiple" xml:space="preserve">
<value>Either the IAsyncResult object did not come from the corresponding async method on this type, or EndWrite was called multiple times with the same IAsyncResult.</value>
</data>
<data name="ObjectDisposed_StreamIsClosed" xml:space="preserve">
<value>Cannot access a closed Stream.</value>
</data>
Expand Down
7 changes: 5 additions & 2 deletions src/System.Net.Security/src/System.Net.Security.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,11 @@
<Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\SafeHandleZeroOrMinusOneIsInvalid.cs">
<Link>Common\Microsoft\Win32\SafeHandles\SafeHandleZeroOrMinusOneIsInvalid.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\System\IO\StreamAsyncHelper.cs">
<Link>Common\System\IO\StreamAsyncHelper.cs</Link>
<Compile Include="$(CommonPath)\System\IO\StreamApmExtensions.cs">
<Link>Common\System\IO\StreamApmExtensions.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\System\Threading\Tasks\TaskToApm.cs">
<Link>Common\System\Threading\Tasks\TaskToApm.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\System\IO\Error.cs">
<Link>Common\System\IO\Error.cs</Link>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ internal class FixedSizeReader
{
private static readonly AsyncCallback s_readCallback = new AsyncCallback(ReadCallback);

// TODO (Issue #3114): Implement this using TPL instead of APM.
private readonly StreamAsyncHelper _transportAPM;
private readonly Stream _transport;
private AsyncProtocolRequest _request;
private int _totalRead;

public FixedSizeReader(Stream transport)
{
_transport = transport;
_transportAPM = new StreamAsyncHelper(transport);
}

//
Expand Down Expand Up @@ -73,7 +70,7 @@ private void StartReading()
{
while (true)
{
IAsyncResult ar = _transportAPM.BeginRead(_request.Buffer, _request.Offset + _totalRead, _request.Count - _totalRead, s_readCallback, this);
IAsyncResult ar = _transport.BeginRead(_request.Buffer, _request.Offset + _totalRead, _request.Count - _totalRead, s_readCallback, this);
if (!ar.CompletedSynchronously)
{
#if DEBUG
Expand All @@ -82,7 +79,7 @@ private void StartReading()
break;
}

int bytes = _transportAPM.EndRead(ar);
int bytes = _transport.EndRead(ar);

if (CheckCompletionBeforeNextRead(bytes))
{
Expand Down Expand Up @@ -148,7 +145,7 @@ private static void ReadCallback(IAsyncResult transportResult)
// Async completion.
try
{
int bytes = reader._transportAPM.EndRead(transportResult);
int bytes = reader._transport.EndRead(transportResult);

if (reader.CheckCompletionBeforeNextRead(bytes))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,10 @@ public partial class NegotiateStream : AuthenticatedStream

private FixedSizeReader _FrameReader;

// TODO (Issue #3114): Implement using TPL instead of APM.
private StreamAsyncHelper _innerStreamAPM;

internal StreamAsyncHelper InnerStreamAPM
{
get
{
return _innerStreamAPM;
}
}

private void InitializeStreamPart()
{
_ReadHeader = new byte[4];
_FrameReader = new FixedSizeReader(InnerStream);
_innerStreamAPM = new StreamAsyncHelper(InnerStream);
}

private byte[] InternalBuffer
Expand Down Expand Up @@ -179,13 +167,13 @@ private void StartWriting(byte[] buffer, int offset, int count, AsyncProtocolReq
{
// prepare for the next request
asyncRequest.SetNextRequest(buffer, offset + chunkBytes, count - chunkBytes, null);
IAsyncResult ar = InnerStreamAPM.BeginWrite(outBuffer, 0, encryptedBytes, s_writeCallback, asyncRequest);
IAsyncResult ar = InnerStream.BeginWrite(outBuffer, 0, encryptedBytes, s_writeCallback, asyncRequest);
if (!ar.CompletedSynchronously)
{
return;
}

InnerStreamAPM.EndWrite(ar);
InnerStream.EndWrite(ar);
}
else
{
Expand Down Expand Up @@ -415,7 +403,7 @@ private static void WriteCallback(IAsyncResult transportResult)
try
{
NegotiateStream negoStream = (NegotiateStream)asyncRequest.AsyncObject;
negoStream.InnerStreamAPM.EndWrite(transportResult);
negoStream.InnerStream.EndWrite(transportResult);
if (asyncRequest.Count == 0)
{
// This was the last chunk.
Expand Down
Loading