Skip to content
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

Compress SafeSocketHandle.Unix booleans #112417

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -14,17 +14,88 @@ public partial class SafeSocketHandle
{
private int _receiveTimeout = -1;
private int _sendTimeout = -1;
private bool _nonBlocking;
private SocketAsyncContext? _asyncContext;

internal bool LastConnectFailed { get; set; }
internal bool DualMode { get; set; }
internal bool ExposedHandleOrUntrackedConfiguration { get; private set; }
internal bool PreferInlineCompletions { get; set; } = SocketAsyncEngine.InlineSocketCompletionsEnabled;
internal bool IsSocket { get; set; } = true; // (ab)use Socket class for performing async I/O on non-socket fds.
[Flags]
private enum Flags : byte
{
NonBlocking = 1,
LastConnectFailed = 2,
DualMode = 4,
ExposedHandleOrUntrackedConfiguration = 8,
PreferInlineCompletions = 16,
IsSocket = 32,
IsDisconnected = 64,
#if SYSTEM_NET_SOCKETS_APPLE_PLATFROM
internal bool TfoEnabled { get; set; }
TfoEnabled = 128
#endif
}

private Flags _flags = Flags.IsSocket | (SocketAsyncEngine.InlineSocketCompletionsEnabled ? Flags.PreferInlineCompletions : 0);

internal bool LastConnectFailed
{
get => (_flags & Flags.LastConnectFailed) != 0;
set
{
if (value) _flags |= Flags.LastConnectFailed;
else _flags &= ~Flags.LastConnectFailed;
}
}

internal bool DualMode
{
get => (_flags & Flags.DualMode) != 0;
set
{
if (value) _flags |= Flags.DualMode;
else _flags &= ~Flags.DualMode;
}
}

internal bool ExposedHandleOrUntrackedConfiguration
{
get => (_flags & Flags.ExposedHandleOrUntrackedConfiguration) != 0;
private set
{
if (value) _flags |= Flags.ExposedHandleOrUntrackedConfiguration;
else _flags &= ~Flags.ExposedHandleOrUntrackedConfiguration;
}
}

internal bool PreferInlineCompletions
{
get => (_flags & Flags.PreferInlineCompletions) != 0;
set
{
if (value) _flags |= Flags.PreferInlineCompletions;
else _flags &= ~Flags.PreferInlineCompletions;
}
}

// (ab)use Socket class for performing async I/O on non-socket fds.
internal bool IsSocket
{
get => (_flags & Flags.IsSocket) != 0;
set
{
if (value) _flags |= Flags.IsSocket;
else _flags &= ~Flags.IsSocket;
}
}

#if SYSTEM_NET_SOCKETS_APPLE_PLATFROM
internal bool TfoEnabled
{
get => (_flags & Flags.TfoEnabled) != 0;
set
{
if (value) _flags |= Flags.TfoEnabled;
else _flags &= ~Flags.TfoEnabled;
}
}
#endif

internal void RegisterConnectResult(SocketError error)
{
switch (error)
Expand Down Expand Up @@ -67,11 +138,12 @@ internal bool IsNonBlocking
{
get
{
return _nonBlocking;
return (_flags & Flags.NonBlocking) != 0;
}
set
{
_nonBlocking = value;
if (value) _flags |= Flags.NonBlocking;
else _flags &= ~Flags.NonBlocking;

// If transitioning from blocking to non-blocking, we need to set the native socket to non-blocking mode.
// If transitioning from non-blocking to blocking, we keep the native socket in non-blocking mode, and emulate
Expand Down Expand Up @@ -112,11 +184,11 @@ internal int SendTimeout
}
}

internal bool IsDisconnected { get; private set; }
internal bool IsDisconnected => (_flags & Flags.IsDisconnected) != 0;

internal void SetToDisconnected()
{
IsDisconnected = true;
_flags |= Flags.IsDisconnected;
}

/// <returns>Returns whether operations were canceled.</returns>
Expand Down
Loading