Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -147,6 +142,7 @@ public AsyncOperation(SocketAsyncContext context)
public void Reset()
{
_state = (int)State.Waiting;
Event = null;
Next = this;
#if DEBUG
_callbackQueued = 0;
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -360,13 +356,10 @@ public SendOperation(SocketAsyncContext context) : base(context) { }

protected sealed override void Abort() { }

public Action<int, byte[]?, int, SocketFlags, SocketError>? Callback
{
set => CallbackOrEvent = value;
}
public Action<int, byte[]?, int, SocketFlags, SocketError>? Callback { get; set; }

public override void InvokeCallback(bool allowPooling) =>
((Action<int, byte[]?, int, SocketFlags, SocketError>)CallbackOrEvent!)(BytesTransferred, SocketAddress, SocketAddressLen, SocketFlags.None, ErrorCode);
Callback!(BytesTransferred, SocketAddress, SocketAddressLen, SocketFlags.None, ErrorCode);
}

private sealed class BufferMemorySendOperation : SendOperation
Expand All @@ -383,7 +376,7 @@ protected override bool DoTryComplete(SocketAsyncContext context)

public override void InvokeCallback(bool allowPooling)
{
var cb = (Action<int, byte[]?, int, SocketFlags, SocketError>)CallbackOrEvent!;
var cb = Callback!;
int bt = BytesTransferred;
byte[]? sa = SocketAddress;
int sal = SocketAddressLen;
Expand Down Expand Up @@ -412,7 +405,7 @@ protected override bool DoTryComplete(SocketAsyncContext context)

public override void InvokeCallback(bool allowPooling)
{
var cb = (Action<int, byte[]?, int, SocketFlags, SocketError>)CallbackOrEvent!;
var cb = Callback!;
int bt = BytesTransferred;
byte[]? sa = SocketAddress;
int sal = SocketAddressLen;
Expand Down Expand Up @@ -450,14 +443,10 @@ public ReceiveOperation(SocketAsyncContext context) : base(context) { }

protected sealed override void Abort() { }

public Action<int, byte[]?, int, SocketFlags, SocketError>? Callback
{
set => CallbackOrEvent = value;
}
public Action<int, byte[]?, int, SocketFlags, SocketError>? Callback { get; set; }

public override void InvokeCallback(bool allowPooling) =>
((Action<int, byte[]?, int, SocketFlags, SocketError>)CallbackOrEvent!)(
BytesTransferred, SocketAddress, SocketAddressLen, ReceivedFlags, ErrorCode);
Callback!(BytesTransferred, SocketAddress, SocketAddressLen, ReceivedFlags, ErrorCode);
}

private sealed class BufferMemoryReceiveOperation : ReceiveOperation
Expand Down Expand Up @@ -496,7 +485,7 @@ protected override bool DoTryComplete(SocketAsyncContext context)

public override void InvokeCallback(bool allowPooling)
{
var cb = (Action<int, byte[]?, int, SocketFlags, SocketError>)CallbackOrEvent!;
var cb = Callback!;
int bt = BytesTransferred;
byte[]? sa = SocketAddress;
int sal = SocketAddressLen;
Expand All @@ -523,7 +512,7 @@ protected override bool DoTryComplete(SocketAsyncContext context) =>

public override void InvokeCallback(bool allowPooling)
{
var cb = (Action<int, byte[]?, int, SocketFlags, SocketError>)CallbackOrEvent!;
var cb = Callback!;
int bt = BytesTransferred;
byte[]? sa = SocketAddress;
int sal = SocketAddressLen;
Expand Down Expand Up @@ -566,17 +555,13 @@ public ReceiveMessageFromOperation(SocketAsyncContext context) : base(context) {

protected sealed override void Abort() { }

public Action<int, byte[], int, SocketFlags, IPPacketInformation, SocketError> Callback
{
set => CallbackOrEvent = value;
}
public Action<int, byte[], int, SocketFlags, IPPacketInformation, SocketError>? 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<int, byte[], int, SocketFlags, IPPacketInformation, SocketError>)CallbackOrEvent!)(
BytesTransferred, SocketAddress!, SocketAddressLen, ReceivedFlags, IPPacketInformation, ErrorCode);
Callback!(BytesTransferred, SocketAddress!, SocketAddressLen, ReceivedFlags, IPPacketInformation, ErrorCode);
}

private sealed class AcceptOperation : ReadOperation
Expand All @@ -585,10 +570,7 @@ private sealed class AcceptOperation : ReadOperation

public AcceptOperation(SocketAsyncContext context) : base(context) { }

public Action<IntPtr, byte[], int, SocketError>? Callback
{
set => CallbackOrEvent = value;
}
public Action<IntPtr, byte[], int, SocketError>? Callback { get; set; }

protected override void Abort() =>
AcceptedFileDescriptor = (IntPtr)(-1);
Expand All @@ -602,7 +584,7 @@ protected override bool DoTryComplete(SocketAsyncContext context)

public override void InvokeCallback(bool allowPooling)
{
var cb = (Action<IntPtr, byte[], int, SocketError>)CallbackOrEvent!;
var cb = Callback!;
IntPtr fd = AcceptedFileDescriptor;
byte[] sa = SocketAddress!;
int sal = SocketAddressLen;
Expand All @@ -621,10 +603,7 @@ private sealed class ConnectOperation : WriteOperation
{
public ConnectOperation(SocketAsyncContext context) : base(context) { }

public Action<SocketError> Callback
{
set => CallbackOrEvent = value;
}
public Action<SocketError>? Callback { get; set; }

protected override void Abort() { }

Expand All @@ -636,7 +615,7 @@ protected override bool DoTryComplete(SocketAsyncContext context)
}

public override void InvokeCallback(bool allowPooling) =>
((Action<SocketError>)CallbackOrEvent!)(ErrorCode);
Callback!(ErrorCode);
}

private sealed class SendFileOperation : WriteOperation
Expand All @@ -650,13 +629,10 @@ public SendFileOperation(SocketAsyncContext context) : base(context) { }

protected override void Abort() { }

public Action<long, SocketError> Callback
{
set => CallbackOrEvent = value;
}
public Action<long, SocketError>? Callback { get; set; }

public override void InvokeCallback(bool allowPooling) =>
((Action<long, SocketError>)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);
Expand Down