diff --git a/ChangeLog b/ChangeLog index e874327c8..a6e1ce122 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2011-10-08 Richard Frith-Macdonald + + * GSFileHandle.m: + * GSNetwork.h: + * GSSocketStream.m: + * NSMessagePort.m: + * NSSocketPort.m: + * NSURLProtocol.m: + Check for EALREADY as well as EINPROGRESS as an indicator of an + incomplete system call on a non-blocking descriptor. + 2011-10-07 Richard Frith-Macdonald * Source/GSHTTPURLHandle.m: diff --git a/Source/GSFileHandle.m b/Source/GSFileHandle.m index 4e79c6258..b2366cbbf 100644 --- a/Source/GSFileHandle.m +++ b/Source/GSFileHandle.m @@ -910,7 +910,7 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr"; [self setNonBlocking: YES]; if (connect(net, &sin, GSPrivateSockaddrLength(&sin)) == -1) { - if (errno != EINPROGRESS) + if (!GSWOULDBLOCK) { NSLog(@"unable to make connection to %@ - %@", GSPrivateSockaddrName(&sin), [NSError _last]); diff --git a/Source/GSNetwork.h b/Source/GSNetwork.h index d126fa06b..ef261bfe7 100644 --- a/Source/GSNetwork.h +++ b/Source/GSNetwork.h @@ -70,7 +70,7 @@ #define INVALID_SOCKET -1 #define BADSOCKET(X) ((X) < 0) #define GSNETERROR errno -#define GSWOULDBLOCK (errno == EINPROGRESS) +#define GSWOULDBLOCK (errno == EINPROGRESS || errno == EALREADY) #endif /* __MINGW__ */ diff --git a/Source/GSSocketStream.m b/Source/GSSocketStream.m index 40fe32083..dc679bf73 100644 --- a/Source/GSSocketStream.m +++ b/Source/GSSocketStream.m @@ -1246,12 +1246,7 @@ socketError(int result) static inline BOOL socketWouldBlock() { -#if defined(__MINGW__) - int e = WSAGetLastError(); - return (e == WSAEWOULDBLOCK || e == WSAEINPROGRESS) ? YES : NO; -#else - return (errno == EWOULDBLOCK || errno == EINPROGRESS) ? YES : NO; -#endif + return GSWOULDBLOCK ? YES : NO; } diff --git a/Source/NSMessagePort.m b/Source/NSMessagePort.m index dff761a96..33e0a2492 100644 --- a/Source/NSMessagePort.m +++ b/Source/NSMessagePort.m @@ -381,7 +381,7 @@ static Class runLoopClass; if (connect(desc, (struct sockaddr*)&sockAddr, SUN_LEN(&sockAddr)) < 0) { - if (errno != EINPROGRESS) + if (!GSWOULDBLOCK) { NSLog(@"unable to make connection to %s - %@", sockAddr.sun_path, [NSError _last]); diff --git a/Source/NSSocketPort.m b/Source/NSSocketPort.m index 0f6e668b9..d080e8c46 100644 --- a/Source/NSSocketPort.m +++ b/Source/NSSocketPort.m @@ -529,11 +529,7 @@ static Class runLoopClass; if (connect(desc, (struct sockaddr*)&sockAddr, GSPrivateSockaddrLength(&sockAddr)) == SOCKET_ERROR) { -#ifdef __MINGW__ - if (WSAGetLastError() != WSAEWOULDBLOCK) -#else - if (errno != EINPROGRESS) -#endif + if (!GSWOULDBLOCK) { NSLog(@"unable to make connection to %d - %@", GSPrivateSockaddrName(&sockAddr), [NSError _last]); diff --git a/Source/NSURLProtocol.m b/Source/NSURLProtocol.m index ac767cc5b..a1f1b7c1e 100644 --- a/Source/NSURLProtocol.m +++ b/Source/NSURLProtocol.m @@ -77,6 +77,7 @@ zfree(void *opaque, void *mem) } + (void) purge: (NSNotification*)n; - (void) cache: (NSDate*)when; +- (void) close; - (NSDate*) expires; - (id) initWithHost: (NSHost*)h port: (uint16_t)p forSSL: (BOOL)s; - (NSInputStream*) inputStream; @@ -126,13 +127,28 @@ static NSLock *pairLock = nil; - (void) cache: (NSDate*)when { - ASSIGN(expires, when); + NSTimeInterval ti = [when timeIntervalSinceNow]; + + if (ti <= 0.0) + { + [self close]; + return; + } + NSAssert(ip != nil, NSGenericException); + if (ti > 120.0) + { + ASSIGN(expires, [NSDate dateWithTimeIntervalSinceNow: 120.0]); + } + else + { + ASSIGN(expires, when); + } [pairLock lock]; [pairCache addObject: self]; [pairLock unlock]; } -- (void) dealloc +- (void) close { [ip setDelegate: nil]; [op setDelegate: nil]; @@ -144,6 +160,11 @@ static NSLock *pairLock = nil; [op close]; DESTROY(ip); DESTROY(op); +} + +- (void) dealloc +{ + [self close]; DESTROY(host); DESTROY(expires); [super dealloc];