Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,17 @@ private static void ThrowMultiConnectNotSupported()
#pragma warning disable CA1822
private Socket? GetOrCreateAcceptSocket(Socket? acceptSocket, bool checkDisconnected, string propertyName, out SafeSocketHandle? handle)
{
if (acceptSocket != null && acceptSocket._handle.HasShutdownSend)
if (acceptSocket != null)
{
throw new SocketException((int)SocketError.InvalidArgument);
}
if (acceptSocket._handle.HasShutdownSend)
{
throw new SocketException((int)SocketError.InvalidArgument);
}

if (acceptSocket != null && acceptSocket._rightEndPoint != null && (!checkDisconnected || !acceptSocket._isDisconnected))
{
throw new InvalidOperationException(SR.Format(SR.net_sockets_namedmustnotbebound, propertyName));
if (acceptSocket._rightEndPoint != null && (!checkDisconnected || !acceptSocket._isDisconnected))
{
throw new InvalidOperationException(SR.Format(SR.net_sockets_namedmustnotbebound, propertyName));
}
}

handle = null;
Expand Down Expand Up @@ -273,39 +276,7 @@ internal Socket CopyStateFromSource(Socket source)
{
object? origValue = pi.GetValue(source);
object? cloneValue = pi.GetValue(this);

if (origValue is IEnumerable origEnumerable)
{
IEnumerable? cloneEnumerable = cloneValue as IEnumerable;
Debug.Assert(cloneEnumerable != null, $"{pi.Name}. Expected enumerable cloned value.");

IEnumerator e1 = origEnumerable.GetEnumerator();
try
{
IEnumerator e2 = cloneEnumerable.GetEnumerator();
try
{
while (e1.MoveNext())
{
Debug.Assert(e2.MoveNext(), $"{pi.Name}. Cloned enumerator too short.");
Debug.Assert(Equals(e1.Current, e2.Current), $"{pi.Name}. Cloned enumerator's values don't match.");
}
Debug.Assert(!e2.MoveNext(), $"{pi.Name}. Cloned enumerator too long.");
}
finally
{
(e2 as IDisposable)?.Dispose();
}
}
finally
{
(e1 as IDisposable)?.Dispose();
}
}
else
{
Debug.Assert(Equals(origValue, cloneValue), $"{pi.Name}. Expected: {origValue}, Actual: {cloneValue}");
}
Debug.Assert(Equals(origValue, cloneValue), $"{pi.Name}. Expected: {origValue}, Actual: {cloneValue}");
}
#endif
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,20 +335,21 @@ internal void LogBuffer(int size)
private SocketError FinishOperationAccept(Internals.SocketAddress remoteSocketAddress)
{
System.Buffer.BlockCopy(_acceptBuffer!, 0, remoteSocketAddress.Buffer, 0, _acceptAddressBufferCount);
Socket? sukru = _acceptSocket;
_acceptSocket = _currentSocket!.CreateAcceptSocket(
Socket acceptedSocket = _currentSocket!.CreateAcceptSocket(
SocketPal.CreateSocket(_acceptedFileDescriptor),
_currentSocket._rightEndPoint!.Create(remoteSocketAddress));
if (sukru != null)
if (_acceptSocket is null)
{
sukru.DisposeHandle();
sukru.CopyStateFromSource(_acceptSocket);
// We keep this socket to make clean-up.
Socket temp = _acceptSocket;
_acceptSocket = sukru;
temp.ClearHandle();
temp.Dispose();
GC.SuppressFinalize(temp);
// Store the accepted socket
_acceptSocket = acceptedSocket;
}
else
{
// Copy state from the accepted socket into the caller-supplied socket and then dispose of the original.
_acceptSocket.DisposeHandle();
_acceptSocket.CopyStateFromSource(acceptedSocket);
acceptedSocket.ClearHandle();
acceptedSocket.Dispose();
}
return SocketError.Success;
}
Expand Down