Improve winsock error handling (consistently use correct errror number)

This commit is contained in:
rfm 2024-02-13 11:19:36 +00:00
parent e6e95d81f6
commit 6488189dca
4 changed files with 65 additions and 37 deletions

View file

@ -49,7 +49,7 @@
#endif
#define BADSOCKET(X) ((X) == INVALID_SOCKET)
#define GSNETERROR WSAGetLastError()
#define GSWOULDBLOCK (GSNETERROR == WSAEWOULDBLOCK || GSNETERROR == WSAEINPROGRESS)
#define GSWOULDBLOCK(X) (WSAEWOULDBLOCK == (X) || WSAEINPROGRESS == (X))
#else
@ -70,10 +70,10 @@
#define INVALID_SOCKET -1
#define BADSOCKET(X) ((X) < 0)
#define GSNETERROR errno
#define GSWOULDBLOCK (EINPROGRESS == errno\
|| EALREADY == errno\
|| EINTR == errno\
|| EAGAIN == errno)
#define GSWOULDBLOCK(X) (EINPROGRESS == (X)\
|| EALREADY == (X)\
|| EINTR == (X)\
|| EAGAIN == (X))
#endif /* _WIN32 */

View file

@ -1706,9 +1706,9 @@ socketError(int result)
}
static inline BOOL
socketWouldBlock()
socketWouldBlock(long eno)
{
return GSWOULDBLOCK ? YES : NO;
return GSWOULDBLOCK(eno) ? YES : NO;
}
@ -2062,7 +2062,9 @@ setNonBlocking(SOCKET fd)
GSPrivateSockaddrLength(&_address.s));
if (socketError(result))
{
if (socketWouldBlock())
long eno = GSNETERROR;
if (socketWouldBlock(eno))
{
/* Need to set the status first, so that the run loop can tell
* it needs to add the stream as waiting on writable, as an
@ -2072,10 +2074,12 @@ setNonBlocking(SOCKET fd)
}
else
{
NSError *e = [NSError _systemError: eno];
/* Had an immediate connect error.
*/
[self _recordError];
[_sibling _recordError];
[self _recordError: e];
[_sibling _recordError: e];
}
#if defined(_WIN32)
WSAEventSelect(_sock, _loopID, FD_ALL_EVENTS);
@ -2228,6 +2232,8 @@ setNonBlocking(SOCKET fd)
}
if (socketError(readLen))
{
long eno = GSNETERROR;
if (_closing == YES)
{
/* If a read fails on a closing socket,
@ -2241,7 +2247,7 @@ setNonBlocking(SOCKET fd)
}
else
{
if (socketWouldBlock())
if (socketWouldBlock(eno))
{
/* We need an event from the operating system
* to tell us we can start reading again.
@ -2250,7 +2256,9 @@ setNonBlocking(SOCKET fd)
}
else
{
[self _recordError];
NSError *e = [NSError _systemError: eno];
[self _recordError: e];
}
readLen = -1;
}
@ -2344,12 +2352,14 @@ setNonBlocking(SOCKET fd)
if (error != 0)
{
NSError *e = [NSError _systemError: error];
errno = error;
[self _recordError];
[self _recordError: e];
[self _sendEvent: NSStreamEventErrorOccurred];
if ([_sibling streamStatus] == NSStreamStatusOpening)
{
[_sibling _recordError];
[_sibling _recordError: e];
[_sibling _sendEvent: NSStreamEventErrorOccurred];
}
}
@ -2357,9 +2367,10 @@ setNonBlocking(SOCKET fd)
{
if (events.lNetworkEvents & FD_WRITE)
{
NSAssert([_sibling _isOpened], NSInternalInconsistencyException);
/* Clear NSStreamStatusWriting if it was set */
[_sibling _setStatus: NSStreamStatusOpen];
if (_sibling && NSStreamStatusWriting == [_sibling streamStatus])
{
[_sibling _setStatus: NSStreamStatusOpen];
}
}
/* On winsock a socket is always writable unless it has had
@ -2423,11 +2434,11 @@ setNonBlocking(SOCKET fd)
}
else // must be an error
{
if (error)
errno = error;
[self _recordError];
NSError *e = [NSError _systemError: error];
[self _recordError: e];
myEvent = NSStreamEventErrorOccurred;
[_sibling _recordError];
[_sibling _recordError: e];
[_sibling _sendEvent: myEvent];
}
}
@ -2504,6 +2515,8 @@ setNonBlocking(SOCKET fd)
if (socketError(writeLen))
{
long eno = GSNETERROR;
if (_closing == YES)
{
/* If a write fails on a closing socket,
@ -2516,7 +2529,7 @@ setNonBlocking(SOCKET fd)
}
else
{
if (socketWouldBlock())
if (socketWouldBlock(eno))
{
/* We need an event from the operating system
* to tell us we can start writing again.
@ -2525,7 +2538,9 @@ setNonBlocking(SOCKET fd)
}
else
{
[self _recordError];
NSError *e = [NSError _systemError: eno];
[self _recordError: e];
}
writeLen = -1;
}
@ -2591,7 +2606,9 @@ setNonBlocking(SOCKET fd)
GSPrivateSockaddrLength(&_address.s));
if (socketError(result))
{
if (socketWouldBlock())
long eno = GSNETERROR;
if (socketWouldBlock(eno))
{
/*
* Need to set the status first, so that the run loop can tell
@ -2602,10 +2619,12 @@ setNonBlocking(SOCKET fd)
}
else
{
NSError *e = [NSError _systemError: eno];
/* Had an immediate connect error.
*/
[self _recordError];
[_sibling _recordError];
[self _recordError: e];
[_sibling _recordError: e];
}
#if defined(_WIN32)
WSAEventSelect(_sock, _loopID, FD_ALL_EVENTS);
@ -2827,12 +2846,13 @@ setNonBlocking(SOCKET fd)
if (error != 0)
{
errno = error;
[self _recordError];
NSError *e = [NSError _systemError: error];
[self _recordError: e];
[self _sendEvent: NSStreamEventErrorOccurred];
if ([_sibling streamStatus] == NSStreamStatusOpening)
{
[_sibling _recordError];
[_sibling _recordError: e];
[_sibling _sendEvent: NSStreamEventErrorOccurred];
}
}
@ -2902,11 +2922,11 @@ setNonBlocking(SOCKET fd)
}
else // must be an error
{
if (error)
errno = error;
[self _recordError];
NSError *e = [NSError _systemError: error];
[self _recordError: e];
myEvent = NSStreamEventErrorOccurred;
[_sibling _recordError];
[_sibling _recordError: e];
[_sibling _sendEvent: myEvent];
}
}
@ -3090,9 +3110,13 @@ setNonBlocking(SOCKET fd)
_events &= ~NSStreamEventHasBytesAvailable;
if (socketError(acceptReturn))
{ // test for real error
if (!socketWouldBlock())
long eno = GSNETERROR;
if (!socketWouldBlock(eno))
{
[self _recordError];
NSError *e = [NSError _systemError: eno];
[self _recordError: e];
}
ins = nil;
outs = nil;

View file

@ -409,7 +409,9 @@ static Class runLoopClass;
if (connect(desc, (struct sockaddr*)&sockAddr, SUN_LEN(&sockAddr)) < 0)
{
if (!GSWOULDBLOCK)
long eno = GSNETERROR;
if (!GSWOULDBLOCK(eno))
{
NSLog(@"unable to make connection to %s - %@",
sockAddr.sun_path, [NSError _last]);

View file

@ -687,7 +687,9 @@ static Class runLoopClass;
if (connect(desc, (struct sockaddr*)&sockAddr,
GSPrivateSockaddrLength(&sockAddr)) == SOCKET_ERROR)
{
if (!GSWOULDBLOCK)
long eno = GSNETERROR;
if (!GSWOULDBLOCK(eno))
{
NSLog(@"unable to make connection to %@ - %@",
GSPrivateSockaddrName(&sockAddr), [NSError _last]);