提交 01bfb62d 编写于 作者: G Geoff Kizer 提交者: GitHub

Merge pull request dotnet/corefx#14812 from geoffkizer/getlasterror

use SocketPal.GetLastSocketError instead of calling Marshal.GetLastWin32Error directly

Commit migrated from https://github.com/dotnet/corefx/commit/14705f76a90f2eb777082ac98e229213e3ebaa96
......@@ -62,7 +62,7 @@ internal override object PostCompletion(int numBytes)
if (errorCode == SocketError.SocketError)
{
errorCode = (SocketError)Marshal.GetLastWin32Error();
errorCode = SocketPal.GetLastSocketError();
}
if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"setsockopt handle:{handle}, AcceptSocket:{_acceptSocket}, returns:{errorCode}");
......
......@@ -111,7 +111,7 @@ private static unsafe void CompletionPortCallback(uint errorCode, uint numBytes,
try
{
// The async IO completed with a failure.
// Here we need to call WSAGetOverlappedResult() just so Marshal.GetLastWin32Error() will return the correct error.
// Here we need to call WSAGetOverlappedResult() just so GetLastSocketError() will return the correct error.
SocketFlags ignore;
bool success = Interop.Winsock.WSAGetOverlappedResult(
socket.SafeHandle,
......@@ -121,11 +121,7 @@ private static unsafe void CompletionPortCallback(uint errorCode, uint numBytes,
out ignore);
if (!success)
{
socketError = (SocketError)Marshal.GetLastWin32Error();
if (socketError == 0)
{
NetEventSource.Fail(asyncResult, $"socketError:0 numBytes:{numBytes}");
}
socketError = SocketPal.GetLastSocketError();
}
if (success)
{
......
......@@ -32,7 +32,7 @@ internal override object PostCompletion(int numBytes)
0);
if (errorCode == SocketError.SocketError)
{
errorCode = (SocketError)Marshal.GetLastWin32Error();
errorCode = SocketPal.GetLastSocketError();
}
}
catch (ObjectDisposedException)
......
......@@ -3233,9 +3233,6 @@ private SocketError DoBeginReceive(byte[] buffer, int offset, int size, SocketFl
// Throw an appropriate SocketException if the native call fails synchronously.
if (errorCode != SocketError.Success)
{
// TODO: https://github.com/dotnet/corefx/issues/5426
// NetEventSource.Fail(this, "GetLastWin32Error() returned zero.");
// Update the internal state of this socket according to the error before throwing.
UpdateStatusAfterSocketError(errorCode);
var socketException = new SocketException((int)errorCode);
......
......@@ -251,7 +251,7 @@ internal SocketError DoOperationDisconnect(Socket socket, SafeCloseSocket handle
(int)(DisconnectReuseSocket ? TransmitFileOptions.ReuseSocket : 0),
0))
{
socketError = (SocketError)Marshal.GetLastWin32Error();
socketError = SocketPal.GetLastSocketError();
}
return socketError;
......@@ -1244,7 +1244,7 @@ private unsafe void CompletionPortCallback(uint errorCode, uint numBytes, Native
try
{
// The Async IO completed with a failure.
// here we need to call WSAGetOverlappedResult() just so Marshal.GetLastWin32Error() will return the correct error.
// here we need to call WSAGetOverlappedResult() just so GetLastSocketError() will return the correct error.
bool success = Interop.Winsock.WSAGetOverlappedResult(
_currentSocket.SafeHandle,
_ptrNativeOverlapped,
......
......@@ -41,7 +41,14 @@ public static void Initialize()
public static SocketError GetLastSocketError()
{
return (SocketError)Marshal.GetLastWin32Error();
int win32Error = Marshal.GetLastWin32Error();
if (win32Error == 0)
{
NetEventSource.Fail(null, "GetLastWin32Error() returned zero.");
}
return (SocketError)win32Error;
}
public static SocketError CreateSocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, out SafeCloseSocket socket)
......@@ -62,7 +69,7 @@ public static SocketError SetBlocking(SafeCloseSocket handle, bool shouldBlock,
if (errorCode == SocketError.SocketError)
{
errorCode = (SocketError)Marshal.GetLastWin32Error();
errorCode = GetLastSocketError();
}
willBlock = intBlocking == 0;
......@@ -153,7 +160,7 @@ public static SocketError Send(SafeCloseSocket handle, IList<ArraySegment<byte>>
if ((SocketError)errorCode == SocketError.SocketError)
{
errorCode = (SocketError)Marshal.GetLastWin32Error();
errorCode = GetLastSocketError();
}
return errorCode;
......@@ -278,7 +285,7 @@ public static SocketError Receive(SafeCloseSocket handle, IList<ArraySegment<byt
if ((SocketError)errorCode == SocketError.SocketError)
{
errorCode = (SocketError)Marshal.GetLastWin32Error();
errorCode = GetLastSocketError();
}
return errorCode;
......@@ -341,7 +348,7 @@ public static SocketError ReceiveMessageFrom(Socket socket, SafeCloseSocket hand
IntPtr.Zero,
IntPtr.Zero) == SocketError.SocketError)
{
errorCode = (SocketError)Marshal.GetLastWin32Error();
errorCode = GetLastSocketError();
}
}
finally
......@@ -1018,7 +1025,7 @@ internal static SocketError Disconnect(Socket socket, SafeCloseSocket handle, bo
// This can throw ObjectDisposedException (handle, and retrieving the delegate).
if (!socket.DisconnectExBlocking(handle, IntPtr.Zero, (int)(reuseSocket ? TransmitFileOptions.ReuseSocket : 0), 0))
{
errorCode = (SocketError)Marshal.GetLastWin32Error();
errorCode = GetLastSocketError();
}
return errorCode;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册