From 54356e0d5513ecec1c68ee4ae581bfab2b000c3b Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Tue, 26 May 2020 04:07:59 +0100 Subject: [PATCH] Avoid additional casts in SocketAsyncContext --- .../Net/Sockets/SocketAsyncContext.Unix.cs | 66 ++++++------------- 1 file changed, 21 insertions(+), 45 deletions(-) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncContext.Unix.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncContext.Unix.cs index 9934e5c97f6560..a35d181f328398 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncContext.Unix.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncContext.Unix.cs @@ -126,17 +126,12 @@ private enum State public readonly SocketAsyncContext AssociatedContext; public AsyncOperation Next = null!; // initialized by helper called from ctor - protected object? CallbackOrEvent; public SocketError ErrorCode; public byte[]? SocketAddress; public int SocketAddressLen; public CancellationTokenRegistration CancellationRegistration; - public ManualResetEventSlim? Event - { - get { return CallbackOrEvent as ManualResetEventSlim; } - set { CallbackOrEvent = value; } - } + public ManualResetEventSlim? Event { get; set; } public AsyncOperation(SocketAsyncContext context) { @@ -147,6 +142,7 @@ public AsyncOperation(SocketAsyncContext context) public void Reset() { _state = (int)State.Waiting; + Event = null; Next = this; #if DEBUG _callbackQueued = 0; @@ -240,10 +236,10 @@ public bool TryCancel() // It's our responsibility to set the error code and queue the completion. DoAbort(); - var @event = CallbackOrEvent as ManualResetEventSlim; - if (@event != null) + ManualResetEventSlim? e = Event; + if (e != null) { - @event.Set(); + e.Set(); } else { @@ -360,13 +356,10 @@ public SendOperation(SocketAsyncContext context) : base(context) { } protected sealed override void Abort() { } - public Action? Callback - { - set => CallbackOrEvent = value; - } + public Action? Callback { get; set; } public override void InvokeCallback(bool allowPooling) => - ((Action)CallbackOrEvent!)(BytesTransferred, SocketAddress, SocketAddressLen, SocketFlags.None, ErrorCode); + Callback!(BytesTransferred, SocketAddress, SocketAddressLen, SocketFlags.None, ErrorCode); } private sealed class BufferMemorySendOperation : SendOperation @@ -383,7 +376,7 @@ protected override bool DoTryComplete(SocketAsyncContext context) public override void InvokeCallback(bool allowPooling) { - var cb = (Action)CallbackOrEvent!; + var cb = Callback!; int bt = BytesTransferred; byte[]? sa = SocketAddress; int sal = SocketAddressLen; @@ -412,7 +405,7 @@ protected override bool DoTryComplete(SocketAsyncContext context) public override void InvokeCallback(bool allowPooling) { - var cb = (Action)CallbackOrEvent!; + var cb = Callback!; int bt = BytesTransferred; byte[]? sa = SocketAddress; int sal = SocketAddressLen; @@ -450,14 +443,10 @@ public ReceiveOperation(SocketAsyncContext context) : base(context) { } protected sealed override void Abort() { } - public Action? Callback - { - set => CallbackOrEvent = value; - } + public Action? Callback { get; set; } public override void InvokeCallback(bool allowPooling) => - ((Action)CallbackOrEvent!)( - BytesTransferred, SocketAddress, SocketAddressLen, ReceivedFlags, ErrorCode); + Callback!(BytesTransferred, SocketAddress, SocketAddressLen, ReceivedFlags, ErrorCode); } private sealed class BufferMemoryReceiveOperation : ReceiveOperation @@ -496,7 +485,7 @@ protected override bool DoTryComplete(SocketAsyncContext context) public override void InvokeCallback(bool allowPooling) { - var cb = (Action)CallbackOrEvent!; + var cb = Callback!; int bt = BytesTransferred; byte[]? sa = SocketAddress; int sal = SocketAddressLen; @@ -523,7 +512,7 @@ protected override bool DoTryComplete(SocketAsyncContext context) => public override void InvokeCallback(bool allowPooling) { - var cb = (Action)CallbackOrEvent!; + var cb = Callback!; int bt = BytesTransferred; byte[]? sa = SocketAddress; int sal = SocketAddressLen; @@ -566,17 +555,13 @@ public ReceiveMessageFromOperation(SocketAsyncContext context) : base(context) { protected sealed override void Abort() { } - public Action Callback - { - set => CallbackOrEvent = value; - } + public Action? Callback { get; set; } protected override bool DoTryComplete(SocketAsyncContext context) => SocketPal.TryCompleteReceiveMessageFrom(context._socket, Buffer.Span, Buffers, Flags, SocketAddress!, ref SocketAddressLen, IsIPv4, IsIPv6, out BytesTransferred, out ReceivedFlags, out IPPacketInformation, out ErrorCode); public override void InvokeCallback(bool allowPooling) => - ((Action)CallbackOrEvent!)( - BytesTransferred, SocketAddress!, SocketAddressLen, ReceivedFlags, IPPacketInformation, ErrorCode); + Callback!(BytesTransferred, SocketAddress!, SocketAddressLen, ReceivedFlags, IPPacketInformation, ErrorCode); } private sealed class AcceptOperation : ReadOperation @@ -585,10 +570,7 @@ private sealed class AcceptOperation : ReadOperation public AcceptOperation(SocketAsyncContext context) : base(context) { } - public Action? Callback - { - set => CallbackOrEvent = value; - } + public Action? Callback { get; set; } protected override void Abort() => AcceptedFileDescriptor = (IntPtr)(-1); @@ -602,7 +584,7 @@ protected override bool DoTryComplete(SocketAsyncContext context) public override void InvokeCallback(bool allowPooling) { - var cb = (Action)CallbackOrEvent!; + var cb = Callback!; IntPtr fd = AcceptedFileDescriptor; byte[] sa = SocketAddress!; int sal = SocketAddressLen; @@ -621,10 +603,7 @@ private sealed class ConnectOperation : WriteOperation { public ConnectOperation(SocketAsyncContext context) : base(context) { } - public Action Callback - { - set => CallbackOrEvent = value; - } + public Action? Callback { get; set; } protected override void Abort() { } @@ -636,7 +615,7 @@ protected override bool DoTryComplete(SocketAsyncContext context) } public override void InvokeCallback(bool allowPooling) => - ((Action)CallbackOrEvent!)(ErrorCode); + Callback!(ErrorCode); } private sealed class SendFileOperation : WriteOperation @@ -650,13 +629,10 @@ public SendFileOperation(SocketAsyncContext context) : base(context) { } protected override void Abort() { } - public Action Callback - { - set => CallbackOrEvent = value; - } + public Action? Callback { get; set; } public override void InvokeCallback(bool allowPooling) => - ((Action)CallbackOrEvent!)(BytesTransferred, ErrorCode); + Callback!(BytesTransferred, ErrorCode); protected override bool DoTryComplete(SocketAsyncContext context) => SocketPal.TryCompleteSendFile(context._socket, FileHandle, ref Offset, ref Count, ref BytesTransferred, out ErrorCode);