@@ -1,8 +1,8 @@ /* - * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 - * Jonathan Schleifer + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 + * Jonathan Schleifer * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in @@ -60,11 +60,11 @@ _maxFD = (int)_cancelFD[0]; return self; } -- (void)OF_addObjectForReading: (id)object +- (void)OF_addObjectForReading: (id )object { int fd = [object fileDescriptorForReading]; if (fd < 0 || fd > INT_MAX - 1) @throw [OFOutOfRangeException exception]; @@ -78,11 +78,11 @@ _maxFD = fd; FD_SET(fd, &_readFDs); } -- (void)OF_addObjectForWriting: (id)object +- (void)OF_addObjectForWriting: (id )object { int fd = [object fileDescriptorForWriting]; if (fd < 0 || fd > INT_MAX - 1) @throw [OFOutOfRangeException exception]; @@ -96,48 +96,57 @@ _maxFD = fd; FD_SET(fd, &_writeFDs); } -- (void)OF_removeObjectForReading: (id)object +- (void)OF_removeObjectForReading: (id )object { /* TODO: Adjust _maxFD */ int fd = [object fileDescriptorForReading]; - if (fd < 0 || fd >= FD_SETSIZE) + if (fd < 0) + @throw [OFOutOfRangeException exception]; + +#ifndef _WIN32 + if (fd >= FD_SETSIZE) @throw [OFOutOfRangeException exception]; +#endif FD_CLR(fd, &_readFDs); } -- (void)OF_removeObjectForWriting: (id)object +- (void)OF_removeObjectForWriting: (id )object { /* TODO: Adjust _maxFD */ int fd = [object fileDescriptorForWriting]; - if (fd < 0 || fd >= FD_SETSIZE) + if (fd < 0) + @throw [OFOutOfRangeException exception]; + +#ifndef _WIN32 + if (fd >= FD_SETSIZE) @throw [OFOutOfRangeException exception]; +#endif FD_CLR(fd, &_writeFDs); } - (void)observeForTimeInterval: (of_time_interval_t)timeInterval { - void *pool = objc_autoreleasePoolPush(); id const *objects; fd_set readFDs; fd_set writeFDs; struct timeval timeout; int events; size_t i, count; - [self OF_processQueueAndStoreRemovedIn: nil]; - [self OF_processReadBuffers]; + [self OF_processQueue]; - objc_autoreleasePoolPop(pool); + if ([self OF_processReadBuffers]) + return; #ifdef FD_COPY FD_COPY(&_readFDs, &readFDs); FD_COPY(&_writeFDs, &writeFDs); #else @@ -166,11 +175,11 @@ errNo: errno]; if (FD_ISSET(_cancelFD[0], &readFDs)) { char buffer; -#ifndef _WIN32 +#ifdef OF_HAVE_PIPE OF_ENSURE(read(_cancelFD[0], &buffer, 1) == 1); #else OF_ENSURE(recvfrom(_cancelFD[0], &buffer, 1, 0, NULL, NULL) == 1); #endif @@ -178,14 +187,12 @@ objects = [_readObjects objects]; count = [_readObjects count]; for (i = 0; i < count; i++) { - int fd; - - pool = objc_autoreleasePoolPush(); - fd = [objects[i] fileDescriptorForReading]; + void *pool = objc_autoreleasePoolPush(); + int fd = [objects[i] fileDescriptorForReading]; if (FD_ISSET(fd, &readFDs) && [_delegate respondsToSelector: @selector(objectIsReadyForReading:)]) [_delegate objectIsReadyForReading: objects[i]]; @@ -194,18 +201,16 @@ objects = [_writeObjects objects]; count = [_writeObjects count]; for (i = 0; i < count; i++) { - int fd; - - pool = objc_autoreleasePoolPush(); - fd = [objects[i] fileDescriptorForWriting]; + void *pool = objc_autoreleasePoolPush(); + int fd = [objects[i] fileDescriptorForWriting]; if (FD_ISSET(fd, &writeFDs) && [_delegate respondsToSelector: @selector(objectIsReadyForWriting:)]) [_delegate objectIsReadyForWriting: objects[i]]; objc_autoreleasePoolPop(pool); } } @end