Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Avoid boxing Int32 result per operation
- Change BufferAsyncResult to allow storing int result, without adding another field
- Use that in both SslStream and NegotiateStream to avoid boxing an int per read and write
- Also rename AsyncProtocolRequest.CompleteWithError to CompleteUserWithError, to avoid confusion and keep it consistent with the other CompleteUser methods.
  • Loading branch information
stephentoub committed Nov 2, 2016
commit 07b48256259838b6abb67cd43418f0cc816342e5
50 changes: 45 additions & 5 deletions src/System.Net.Security/src/System/Net/BufferAsyncResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,64 @@
// 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.Diagnostics;

namespace System.Net
{
//
// Preserve the original request buffer & sizes for user IO requests.
// This is returned as an IAsyncResult to the application.
//
internal class BufferAsyncResult : LazyAsyncResult
internal sealed class BufferAsyncResult : LazyAsyncResult
{
public byte[] Buffer;
public int Offset;
public int Count;
/// <summary>Stored into LazyAsyncResult.Result to indicate a completed result.</summary>
public static readonly object ResultSentinal = nameof(BufferAsyncResult) + "." + nameof(ResultSentinal);
/// <summary>Stores the input count or the output result of the operation.</summary>
private int _countOrResult;
#if DEBUG
/// <summary>true if <see cref="_countOrResult"/> backs <see cref="Int32Result"/>, false if <see cref="Count"/>.</summary>
private bool _countOrResultIsResult;
#endif

public BufferAsyncResult(object asyncObject, byte[] buffer, int offset, int count, object asyncState, AsyncCallback asyncCallback)
: base(asyncObject, asyncState, asyncCallback)
{
Buffer = buffer;
Offset = offset;
Count = count;
_countOrResult = count;
}

public byte[] Buffer { get; }
public int Offset { get; }
public int Count
{
get
{
#if DEBUG
Debug.Assert(!_countOrResultIsResult, "Trying to get count after it's already the result");
#endif
return _countOrResult;
}
}

public int Int32Result // Int32Result to differentiate from the base's "object Result"
{
get
{
#if DEBUG
Debug.Assert(_countOrResultIsResult, "Still represents the count, not the result");
Debug.Assert(ReferenceEquals(Result, ResultSentinal), "Expected the base object Result to be the sentinel");
#endif
return _countOrResult;
}
set
{
#if DEBUG
Debug.Assert(!_countOrResultIsResult, "Should only be set when result hasn't yet been set");
_countOrResultIsResult = true;
#endif
_countOrResult = value;
}
}
}
}
2 changes: 1 addition & 1 deletion src/System.Net.Security/src/System/Net/FixedSizeReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private static void ReadCallback(IAsyncResult transportResult)
throw;
}

request.CompleteWithError(e);
request.CompleteUserWithError(e);
}
}
}
Expand Down
31 changes: 10 additions & 21 deletions src/System.Net.Security/src/System/Net/HelperAsyncResults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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.Diagnostics;
using System.Threading;

namespace System.Net
Expand Down Expand Up @@ -88,13 +89,7 @@ public void SetNextRequest(byte[] buffer, int offset, int count, AsyncProtocolCa
_callback = callback;
}

internal object AsyncObject
{
get
{
return UserAsyncResult.AsyncObject;
}
}
internal object AsyncObject => UserAsyncResult.AsyncObject;

//
// Notify protocol so a next stage could be started.
Expand Down Expand Up @@ -137,24 +132,18 @@ public bool MustCompleteSynchronously
//
// Important: This will abandon _Callback and directly notify UserAsyncResult.
//
internal void CompleteWithError(Exception e)
{
UserAsyncResult.InvokeCallback(e);
}
internal void CompleteUserWithError(Exception e) => UserAsyncResult.InvokeCallback(e);

internal void CompleteUser()
{
UserAsyncResult.InvokeCallback();
}
internal void CompleteUser() => UserAsyncResult.InvokeCallback();

internal void CompleteUser(object userResult)
internal void CompleteUser(int userResult)
{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: capitalization

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: capitalization

Of what?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, in the debug message below? GitHub wasn't showing that on the comment view. Will lower-case the R.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorry for the confusion, I meant the R in the comment below.

UserAsyncResult.InvokeCallback(userResult);
Debug.Assert(UserAsyncResult is BufferAsyncResult, "CompleteUser(int) may only be used with a BufferAsyncResult");
var bar = (BufferAsyncResult)UserAsyncResult;
bar.Int32Result = userResult;
bar.InvokeCallback(BufferAsyncResult.ResultSentinal);
}

internal bool IsUserCompleted
{
get { return UserAsyncResult.InternalPeekCompleted; }
}
internal bool IsUserCompleted => UserAsyncResult.InternalPeekCompleted;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,7 @@ private int ProcessRead(byte[] buffer, int offset, int count, AsyncProtocolReque
Buffer.BlockCopy(InternalBuffer, InternalOffset, buffer, offset, copyBytes);
DecrementInternalBufferCount(copyBytes);
}
if (asyncRequest != null)
{
asyncRequest.CompleteUser((object)copyBytes);
}
asyncRequest?.CompleteUser(copyBytes);
return copyBytes;
}

Expand Down Expand Up @@ -283,10 +280,7 @@ private int StartFrameBody(int readBytes, byte[] buffer, int offset, int count,
if (readBytes == 0)
{
//EOF
if (asyncRequest != null)
{
asyncRequest.CompleteUser((object)0);
}
asyncRequest?.CompleteUser(0);
return 0;
}

Expand Down Expand Up @@ -368,10 +362,7 @@ private int ProcessFrameBody(int readBytes, byte[] buffer, int offset, int count
// This will adjust both the remaining internal buffer count and the offset.
DecrementInternalBufferCount(readBytes);

if (asyncRequest != null)
{
asyncRequest.CompleteUser((object)readBytes);
}
asyncRequest?.CompleteUser(readBytes);

return readBytes;
}
Expand Down Expand Up @@ -410,7 +401,7 @@ private static void WriteCallback(IAsyncResult transportResult)
throw;
}

asyncRequest.CompleteWithError(e);
asyncRequest.CompleteUserWithError(e);
}
}

Expand Down Expand Up @@ -444,7 +435,7 @@ private static void ReadCallback(AsyncProtocolRequest asyncRequest)
throw;
}

asyncRequest.CompleteWithError(e);
asyncRequest.CompleteUserWithError(e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ public override int EndRead(IAsyncResult asyncResult)
throw new IOException(SR.net_io_read, (Exception)bufferResult.Result);
}

return (int)bufferResult.Result;
return bufferResult.Int32Result;
#if DEBUG
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/System.Net.Security/src/System/Net/Security/SslState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1441,7 +1441,7 @@ private void FinishHandshake(Exception e, AsyncProtocolRequest asyncRequest)
{
if (e != null)
{
asyncRequest.CompleteWithError(e);
asyncRequest.CompleteUserWithError(e);
}
else
{
Expand Down Expand Up @@ -1725,7 +1725,7 @@ private void AsyncResumeHandshake(object state)
}
catch (Exception e)
{
request.CompleteWithError(e);
request.CompleteUserWithError(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ internal int EndRead(IAsyncResult asyncResult)
throw new IOException(SR.net_io_read, (Exception)bufferResult.Result);
}

return (int)bufferResult.Result;
return bufferResult.Int32Result;
}

internal IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState)
Expand Down Expand Up @@ -495,7 +495,7 @@ private void StartWriting(byte[] buffer, int offset, int count, AsyncProtocolReq
//
// Combined sync/async read method. For sync request asyncRequest==null.
//
private int ProcessRead(byte[] buffer, int offset, int count, LazyAsyncResult asyncResult)
private int ProcessRead(byte[] buffer, int offset, int count, BufferAsyncResult asyncResult)
{
ValidateParameters(buffer, offset, count);

Expand All @@ -522,9 +522,7 @@ private int ProcessRead(byte[] buffer, int offset, int count, LazyAsyncResult as
SkipBytes(copyBytes);
}

if (asyncRequest != null) {
asyncRequest.CompleteUser((object) copyBytes);
}
asyncRequest?.CompleteUser(copyBytes);

return copyBytes;
}
Expand Down Expand Up @@ -580,10 +578,7 @@ private int StartReading(byte[] buffer, int offset, int count, AsyncProtocolRequ

if (copyBytes != -1)
{
if (asyncRequest != null)
{
asyncRequest.CompleteUser((object)copyBytes);
}
asyncRequest?.CompleteUser(copyBytes);

return copyBytes;
}
Expand Down Expand Up @@ -633,10 +628,7 @@ private int StartFrameBody(int readBytes, byte[] buffer, int offset, int count,
{
//EOF : Reset the buffer as we did not read anything into it.
SkipBytes(InternalBufferCount);
if (asyncRequest != null)
{
asyncRequest.CompleteUser((object)0);
}
asyncRequest?.CompleteUser(0);

return 0;
}
Expand Down Expand Up @@ -728,10 +720,7 @@ private int ProcessFrameBody(int readBytes, byte[] buffer, int offset, int count
SkipBytes(readBytes);

_sslState.FinishRead(null);
if (asyncRequest != null)
{
asyncRequest.CompleteUser((object)readBytes);
}
asyncRequest?.CompleteUser(readBytes);

return readBytes;
}
Expand All @@ -752,10 +741,7 @@ private int ProcessReadErrorCode(SecurityStatusPal status, byte[] buffer, int of
if (message.CloseConnection)
{
_sslState.FinishRead(null);
if (asyncRequest != null)
{
asyncRequest.CompleteUser((object)0);
}
asyncRequest?.CompleteUser(0);

return 0;
}
Expand Down Expand Up @@ -801,7 +787,7 @@ private static void WriteCallback(IAsyncResult transportResult)
}

sslStream._sslState.FinishWrite();
asyncRequest.CompleteWithError(e);
asyncRequest.CompleteUserWithError(e);
}
}

Expand All @@ -823,7 +809,7 @@ private static void ResumeAsyncReadCallback(AsyncProtocolRequest request)
}

((SslStreamInternal)request.AsyncObject)._sslState.FinishRead(null);
request.CompleteWithError(e);
request.CompleteUserWithError(e);
}
}

Expand All @@ -845,7 +831,7 @@ private static void ResumeAsyncWriteCallback(AsyncProtocolRequest asyncRequest)
}

((SslStreamInternal)asyncRequest.AsyncObject)._sslState.FinishWrite();
asyncRequest.CompleteWithError(e);
asyncRequest.CompleteUserWithError(e);
}
}

Expand All @@ -869,7 +855,7 @@ private static void ReadHeaderCallback(AsyncProtocolRequest asyncRequest)
throw;
}

asyncRequest.CompleteWithError(e);
asyncRequest.CompleteUserWithError(e);
}
}

Expand All @@ -893,7 +879,7 @@ private static void ReadFrameCallback(AsyncProtocolRequest asyncRequest)
throw;
}

asyncRequest.CompleteWithError(e);
asyncRequest.CompleteUserWithError(e);
}
}
}
Expand Down