Overview
Comment: | Make OFStreamObserver an abstract class. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
b1ffd7732ad27bf8a58e69c362c8df7c |
User & Date: | js on 2011-09-10 19:37:08 |
Other Links: | manifest | tags |
Context
2011-09-10
| ||
19:47 | Fix a typo. check-in: 4c71fdcf43 user: js tags: trunk | |
19:37 | Make OFStreamObserver an abstract class. check-in: b1ffd7732a user: js tags: trunk | |
18:36 | Remove -[finalize] as ARC will be implemented instead of a GC. check-in: 972a3ee40d user: js tags: trunk | |
Changes
Modified configure.ac from [899596c0f4] to [af0754cd6e].
︙ | ︙ | |||
463 464 465 466 467 468 469 | AC_DEFINE(HAVE_GMTIME_R, 1, [Whether we have gmtime_r]) ]) AC_CHECK_FUNC(localtime_r, [ AC_DEFINE(HAVE_LOCALTIME_R, 1, [Whether we have localtime_r]) ]) AC_CHECK_HEADER(poll.h, [ | | > | > | 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 | AC_DEFINE(HAVE_GMTIME_R, 1, [Whether we have gmtime_r]) ]) AC_CHECK_FUNC(localtime_r, [ AC_DEFINE(HAVE_LOCALTIME_R, 1, [Whether we have localtime_r]) ]) AC_CHECK_HEADER(poll.h, [ AC_DEFINE(HAVE_POLL_H, 1, [Whether we have poll.h]) AC_SUBST(OFSTREAMPOLLOBSERVER_M, "OFStreamPollObserver.m") ]) AC_CHECK_HEADERS(sys/select.h, [ AC_DEFINE(HAVE_SYS_SELECT_H, 1, [Whether we have sys/select.h]) AC_SUBST(OFSTREAMSELECTOBSERVER_M, "OFStreamSelectObserver.m") ]) AC_MSG_CHECKING(for getaddrinfo) AC_TRY_COMPILE([ #include <stddef.h> #ifndef _WIN32 # include <sys/types.h> |
︙ | ︙ |
Modified extra.mk.in from [fedd01b906] to [20abfc72ac].
︙ | ︙ | |||
15 16 17 18 19 20 21 22 23 24 25 26 27 28 | MACH_ALIAS_LIST = @MACH_ALIAS_LIST@ OFBLOCKTESTS_M = @OFBLOCKTESTS_M@ OBJC_PROPERTIES_M = @OBJC_PROPERTIES_M@ OBJC_SYNC_M = @OBJC_SYNC_M@ OFHTTPREQUESTTESTS_M = @OFHTTPREQUESTTESTS_M@ OFPLUGIN_M = @OFPLUGIN_M@ OFPLUGINTESTS_M = @OFPLUGINTESTS_M@ OFTHREAD_M = @OFTHREAD_M@ OFTHREADTESTS_M = @OFTHREADTESTS_M@ PROPERTIESTESTS_M = @PROPERTIESTESTS_M@ REEXPORT_LIBOBJC = @REEXPORT_LIBOBJC@ TESTPLUGIN = @TESTPLUGIN@ TESTS = @TESTS@ TEST_LAUNCHER = @TEST_LAUNCHER@ | > > | 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | MACH_ALIAS_LIST = @MACH_ALIAS_LIST@ OFBLOCKTESTS_M = @OFBLOCKTESTS_M@ OBJC_PROPERTIES_M = @OBJC_PROPERTIES_M@ OBJC_SYNC_M = @OBJC_SYNC_M@ OFHTTPREQUESTTESTS_M = @OFHTTPREQUESTTESTS_M@ OFPLUGIN_M = @OFPLUGIN_M@ OFPLUGINTESTS_M = @OFPLUGINTESTS_M@ OFSTREAMPOLLOBSERVER_M = @OFSTREAMPOLLOBSERVER_M@ OFSTREAMSELECTOBSERVER_M = @OFSTREAMSELECTOBSERVER_M@ OFTHREAD_M = @OFTHREAD_M@ OFTHREADTESTS_M = @OFTHREADTESTS_M@ PROPERTIESTESTS_M = @PROPERTIESTESTS_M@ REEXPORT_LIBOBJC = @REEXPORT_LIBOBJC@ TESTPLUGIN = @TESTPLUGIN@ TESTS = @TESTS@ TEST_LAUNCHER = @TEST_LAUNCHER@ |
︙ | ︙ |
Modified src/Makefile from [3905b7ae47] to [2728704c39].
︙ | ︙ | |||
75 76 77 78 79 80 81 82 83 84 85 86 87 88 | SRCS += OFArray_adjacent.m \ OFCountedSet_hashtable.m \ OFDictionary_hashtable.m \ OFMutableArray_adjacent.m \ OFMutableDictionary_hashtable.m \ OFMutableSet_hashtable.m \ OFSet_hashtable.m \ ${ASPRINTF_M} \ ${FOUNDATION_COMPAT_M} \ iso_8859_15.m \ windows_1252.m \ ${OBJC_PROPERTIES_M} \ ${OBJC_SYNC_M} | > > | 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | SRCS += OFArray_adjacent.m \ OFCountedSet_hashtable.m \ OFDictionary_hashtable.m \ OFMutableArray_adjacent.m \ OFMutableDictionary_hashtable.m \ OFMutableSet_hashtable.m \ OFSet_hashtable.m \ ${OFSTREAMPOLLOBSERVER_M} \ ${OFSTREAMSELECTOBSERVER_M} \ ${ASPRINTF_M} \ ${FOUNDATION_COMPAT_M} \ iso_8859_15.m \ windows_1252.m \ ${OBJC_PROPERTIES_M} \ ${OBJC_SYNC_M} |
︙ | ︙ |
Modified src/OFStreamObserver.h from [923c073c02] to [70764cc054].
︙ | ︙ | |||
10 11 12 13 14 15 16 | * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ | < < < < < < < | 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #import "OFObject.h" #ifdef _WIN32 # ifndef _WIN32_WINNT # define _WIN32_WINNT 0x0501 # endif # include <windows.h> # include <ws2tcpip.h> #endif @class OFStream; @class OFMutableArray; @class OFMutableDictionary; /** * \brief A protocol that needs to be implemented by delegates for * OFStreamObserver. */ |
︙ | ︙ | |||
76 77 78 79 80 81 82 | */ @interface OFStreamObserver: OFObject { OFMutableArray *readStreams; OFMutableArray *writeStreams; OFMutableArray *queue, *queueInfo; id <OFStreamObserverDelegate> delegate; | < < < < < < < < < | 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | */ @interface OFStreamObserver: OFObject { OFMutableArray *readStreams; OFMutableArray *writeStreams; OFMutableArray *queue, *queueInfo; id <OFStreamObserverDelegate> delegate; int cancelFD[2]; #ifdef _WIN32 struct sockaddr_in cancelAddr; #endif } #ifdef OF_HAVE_PROPERTIES |
︙ | ︙ | |||
172 173 174 175 176 177 178 179 180 181 182 | * Observes all streams until an event happens on a stream or the timeout is * reached. * * \param timeout The time to wait for an event, in milliseconds * \return A boolean whether events occurred during the timeinterval */ - (BOOL)observeWithTimeout: (int)timeout; @end @interface OFObject (OFStreamObserverDelegate) <OFStreamObserverDelegate> @end | > > > > | 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | * Observes all streams until an event happens on a stream or the timeout is * reached. * * \param timeout The time to wait for an event, in milliseconds * \return A boolean whether events occurred during the timeinterval */ - (BOOL)observeWithTimeout: (int)timeout; /// \cond internal - (BOOL)_processCache; /// \endcond @end @interface OFObject (OFStreamObserverDelegate) <OFStreamObserverDelegate> @end |
Modified src/OFStreamObserver.m from [154bacad74] to [317abd8f85].
︙ | ︙ | |||
15 16 17 18 19 20 21 | */ #include "config.h" #define OF_STREAM_OBSERVER_M #define __NO_EXT_QNX | < < < < < < < < > > > > > > > > < < < < > > > > > > > > > > > > > > > > > > < < < < < < < < < < < | 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | */ #include "config.h" #define OF_STREAM_OBSERVER_M #define __NO_EXT_QNX #include <assert.h> #include <unistd.h> #import "OFStreamObserver.h" #import "OFArray.h" #import "OFDictionary.h" #import "OFStream.h" #import "OFNumber.h" #ifdef _WIN32 # import "OFTCPSocket.h" #endif #import "OFAutoreleasePool.h" #ifdef HAVE_POLL_H # import "OFStreamPollObserver.h" #endif #if defined(HAVE_SYS_SELECT_H) || defined(_WIN32) # import "OFStreamSelectObserver.h" #endif #import "OFInitializationFailedException.h" #import "OFNotImplementedException.h" #import "OFOutOfRangeException.h" #import "macros.h" enum { QUEUE_ADD = 0, QUEUE_REMOVE = 1, QUEUE_READ = 0, QUEUE_WRITE = 2 }; @implementation OFStreamObserver + observer { return [[[self alloc] init] autorelease]; } #if defined(HAVE_POLL_H) + alloc { if (self == [OFStreamObserver class]) return [OFStreamPollObserver alloc]; return [super alloc]; } #elif defined(HAVE_SYS_SELECT_H) || defined(_WIN32) + alloc { if (self == [OFStreamObserver class]) return [OFStreamSelectObserver alloc]; return [super alloc]; } #endif - init { self = [super init]; @try { #ifdef _WIN32 struct sockaddr_in cancelAddr2; socklen_t cancelAddrLen; #endif readStreams = [[OFMutableArray alloc] init]; writeStreams = [[OFMutableArray alloc] init]; queue = [[OFMutableArray alloc] init]; queueInfo = [[OFMutableArray alloc] init]; #ifndef _WIN32 if (pipe(cancelFD)) @throw [OFInitializationFailedException newWithClass: isa]; #else /* Make sure WSAStartup has been called */ |
︙ | ︙ | |||
118 119 120 121 122 123 124 | cancelAddrLen = sizeof(cancelAddr); if (getsockname(cancelFD[0], (struct sockaddr*)&cancelAddr, &cancelAddrLen)) @throw [OFInitializationFailedException newWithClass: isa]; #endif | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | cancelAddrLen = sizeof(cancelAddr); if (getsockname(cancelFD[0], (struct sockaddr*)&cancelAddr, &cancelAddrLen)) @throw [OFInitializationFailedException newWithClass: isa]; #endif } @catch (id e) { [self release]; @throw e; } return self; } - (void)dealloc { close(cancelFD[0]); close(cancelFD[1]); [(id)delegate release]; [readStreams release]; [writeStreams release]; [queue release]; [queueInfo release]; [super dealloc]; } - (id <OFStreamObserverDelegate>)delegate { OF_GETTER(delegate, YES) } - (void)setDelegate: (id <OFStreamObserverDelegate>)delegate_ { OF_SETTER(delegate, delegate_, YES, NO) } - (void)addStreamToObserveForReading: (OFStream*)stream { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; OFNumber *qi = [OFNumber numberWithInt: QUEUE_ADD | QUEUE_READ]; @synchronized (queue) { [queue addObject: stream]; |
︙ | ︙ | |||
324 325 326 327 328 329 330 | assert(sendto(cancelFD[1], "", 1, 0, (struct sockaddr*)&cancelAddr, sizeof(cancelAddr)) > 0); #endif [pool release]; } | | < < < < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | | < < < < | > | < < | | > > < < < < < < < < < < < < | < < > > < < < < < < < < < < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | assert(sendto(cancelFD[1], "", 1, 0, (struct sockaddr*)&cancelAddr, sizeof(cancelAddr)) > 0); #endif [pool release]; } - (void)observe { [self observeWithTimeout: -1]; } - (BOOL)observeWithTimeout: (int)timeout { @throw [OFNotImplementedException newWithClass: isa selector: _cmd]; } - (BOOL)_processCache { OFAutoreleasePool *pool; OFStream **cArray = [readStreams cArray]; size_t i, count = [readStreams count]; BOOL foundInCache = NO; pool = [[OFAutoreleasePool alloc] init]; for (i = 0; i < count; i++) { if ([cArray[i] pendingBytes] > 0) { [delegate streamDidBecomeReadyForReading: cArray[i]]; foundInCache = YES; [pool releaseObjects]; } } [pool release]; /* * As long as we have data in the cache for any stream, we don't want * to block. */ if (foundInCache) return YES; return NO; } @end @implementation OFObject (OFStreamObserverDelegate) - (void)streamDidBecomeReadyForReading: (OFStream*)stream { } |
︙ | ︙ |
Added src/OFStreamPollObserver.h version [6fe3772d9f].
> > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | /* * Copyright (c) 2008, 2009, 2010, 2011 * Jonathan Schleifer <js@webkeks.org> * * 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 * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #import "OFStreamObserver.h" @class OFDataArray; @interface OFStreamPollObserver: OFStreamObserver { OFDataArray *FDs; OFMutableDictionary *FDToStream; } @end |
Added src/OFStreamPollObserver.m version [bf3ce8d9d0].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | /* * Copyright (c) 2008, 2009, 2010, 2011 * Jonathan Schleifer <js@webkeks.org> * * 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 * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #define __NO_EXT_QNX #include <assert.h> #include <unistd.h> #include <poll.h> #import "OFStreamPollObserver.h" #import "OFStream.h" #import "OFArray.h" #import "OFDictionary.h" #import "OFDataArray.h" #import "OFNumber.h" #import "OFAutoreleasePool.h" #import "OFOutOfRangeException.h" enum { QUEUE_ADD = 0, QUEUE_REMOVE = 1, QUEUE_READ = 0, QUEUE_WRITE = 2 }; @implementation OFStreamPollObserver - init { self = [super init]; @try { struct pollfd p = { 0, POLLIN, 0 }; FDs = [[OFDataArray alloc] initWithItemSize: sizeof(struct pollfd)]; FDToStream = [[OFMutableDictionary alloc] init]; p.fd = cancelFD[0]; [FDs addItem: &p]; } @catch (id e) { [self release]; @throw e; } return self; } - (void)dealloc { [FDToStream release]; [FDs release]; [super dealloc]; } - (void)_addStream: (OFStream*)stream withEvents: (short)events { struct pollfd *FDsCArray = [FDs cArray]; size_t i, count = [FDs count]; int fileDescriptor = [stream fileDescriptor]; BOOL found = NO; for (i = 0; i < count; i++) { if (FDsCArray[i].fd == fileDescriptor) { FDsCArray[i].events |= events; found = YES; } } if (!found) { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; struct pollfd p = { fileDescriptor, events | POLLERR, 0 }; [FDs addItem: &p]; [FDToStream setObject: stream forKey: [OFNumber numberWithInt: fileDescriptor]]; [pool release]; } } - (void)_removeStream: (OFStream*)stream withEvents: (short)events { struct pollfd *FDsCArray = [FDs cArray]; size_t i, nFDs = [FDs count]; int fileDescriptor = [stream fileDescriptor]; for (i = 0; i < nFDs; i++) { if (FDsCArray[i].fd == fileDescriptor) { OFAutoreleasePool *pool; FDsCArray[i].events &= ~events; if ((FDsCArray[i].events & ~POLLERR) != 0) return; pool = [[OFAutoreleasePool alloc] init]; [FDs removeItemAtIndex: i]; [FDToStream removeObjectForKey: [OFNumber numberWithInt: fileDescriptor]]; [pool release]; } } } - (void)_processQueue { @synchronized (queue) { OFStream **queueCArray = [queue cArray]; OFNumber **queueInfoCArray = [queueInfo cArray]; size_t i, count = [queue count]; for (i = 0; i < count; i++) { switch ([queueInfoCArray[i] intValue]) { case QUEUE_ADD | QUEUE_READ: [readStreams addObject: queueCArray[i]]; [self _addStream: queueCArray[i] withEvents: POLLIN]; break; case QUEUE_ADD | QUEUE_WRITE: [writeStreams addObject: queueCArray[i]]; [self _addStream: queueCArray[i] withEvents: POLLOUT]; break; case QUEUE_REMOVE | QUEUE_READ: [readStreams removeObjectIdenticalTo: queueCArray[i]]; [self _removeStream: queueCArray[i] withEvents: POLLIN]; break; case QUEUE_REMOVE | QUEUE_WRITE: [writeStreams removeObjectIdenticalTo: queueCArray[i]]; [self _removeStream: queueCArray[i] withEvents: POLLOUT]; break; default: assert(0); } } [queue removeNObjects: count]; [queueInfo removeNObjects: count]; } } - (BOOL)observeWithTimeout: (int)timeout { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; struct pollfd *FDsCArray; size_t i, nFDs; [self _processQueue]; if ([self _processCache]) return YES; FDsCArray = [FDs cArray]; nFDs = [FDs count]; #ifdef OPEN_MAX if (nFDs > OPEN_MAX) @throw [OFOutOfRangeException newWithClass: isa]; #endif if (poll(FDsCArray, (nfds_t)nFDs, timeout) < 1) return NO; for (i = 0; i < nFDs; i++) { OFNumber *num; OFStream *stream; if (FDsCArray[i].revents & POLLIN) { if (FDsCArray[i].fd == cancelFD[0]) { char buffer; assert(read(cancelFD[0], &buffer, 1) > 0); FDsCArray[i].revents = 0; continue; } num = [OFNumber numberWithInt: FDsCArray[i].fd]; stream = [FDToStream objectForKey: num]; [delegate streamDidBecomeReadyForReading: stream]; [pool releaseObjects]; } if (FDsCArray[i].revents & POLLOUT) { num = [OFNumber numberWithInt: FDsCArray[i].fd]; stream = [FDToStream objectForKey: num]; [delegate streamDidBecomeReadyForReading: stream]; [pool releaseObjects]; } if (FDsCArray[i].revents & POLLERR) { num = [OFNumber numberWithInt: FDsCArray[i].fd]; stream = [FDToStream objectForKey: num]; [delegate streamDidReceiveException: stream]; [pool releaseObjects]; } FDsCArray[i].revents = 0; } [pool release]; return YES; } @end |
Added src/OFStreamSelectObserver.h version [19d96302e5].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | /* * Copyright (c) 2008, 2009, 2010, 2011 * Jonathan Schleifer <js@webkeks.org> * * 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 * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #ifdef OF_HAVE_SYS_SELECT_H # include <sys/select.h> #endif #import "OFStreamObserver.h" @interface OFStreamSelectObserver: OFStreamObserver { fd_set readFDs; fd_set writeFDs; fd_set exceptFDs; int nFDs; } @end |
Added src/OFStreamSelectObserver.m version [c9a34f66b3].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | /* * Copyright (c) 2008, 2009, 2010, 2011 * Jonathan Schleifer <js@webkeks.org> * * 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 * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #include <string.h> #include <assert.h> #include <unistd.h> #define __NO_EXT_QNX #import "OFStreamSelectObserver.h" #import "OFStream.h" #import "OFArray.h" #import "OFNumber.h" #import "OFAutoreleasePool.h" #ifdef _WIN32 # define close(sock) closesocket(sock) #endif enum { QUEUE_ADD = 0, QUEUE_REMOVE = 1, QUEUE_READ = 0, QUEUE_WRITE = 2 }; @implementation OFStreamSelectObserver - init { self = [super init]; @try { FD_ZERO(&readFDs); FD_ZERO(&writeFDs); FD_SET(cancelFD[0], &readFDs); nFDs = cancelFD[0] + 1; } @catch (id e) { [self release]; @throw e; } return self; } - (void)_addStream: (OFStream*)stream withFDSet: (fd_set*)FDSet { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; int fileDescriptor = [stream fileDescriptor]; FD_SET(fileDescriptor, FDSet); FD_SET(fileDescriptor, &exceptFDs); if (fileDescriptor >= nFDs) nFDs = fileDescriptor + 1; [pool release]; } - (void)_removeStream: (OFStream*)stream withFDSet: (fd_set*)FDSet otherFDSet: (fd_set*)otherFDSet { int fileDescriptor = [stream fileDescriptor]; FD_CLR(fileDescriptor, FDSet); if (!FD_ISSET(fileDescriptor, otherFDSet)) FD_CLR(fileDescriptor, &exceptFDs); } - (void)_processQueue { @synchronized (queue) { OFStream **queueCArray = [queue cArray]; OFNumber **queueInfoCArray = [queueInfo cArray]; size_t i, count = [queue count]; for (i = 0; i < count; i++) { switch ([queueInfoCArray[i] intValue]) { case QUEUE_ADD | QUEUE_READ: [readStreams addObject: queueCArray[i]]; [self _addStream: queueCArray[i] withFDSet: &readFDs]; break; case QUEUE_ADD | QUEUE_WRITE: [writeStreams addObject: queueCArray[i]]; [self _addStream: queueCArray[i] withFDSet: &writeFDs]; break; case QUEUE_REMOVE | QUEUE_READ: [readStreams removeObjectIdenticalTo: queueCArray[i]]; [self _removeStream: queueCArray[i] withFDSet: &readFDs otherFDSet: &writeFDs]; break; case QUEUE_REMOVE | QUEUE_WRITE: [writeStreams removeObjectIdenticalTo: queueCArray[i]]; [self _removeStream: queueCArray[i] withFDSet: &writeFDs otherFDSet: &readFDs]; break; default: assert(0); } } [queue removeNObjects: count]; [queueInfo removeNObjects: count]; } } - (BOOL)observeWithTimeout: (int)timeout { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; OFStream **cArray; fd_set readFDs_; fd_set writeFDs_; fd_set exceptFDs_; struct timeval time; size_t i, count; [self _processQueue]; if ([self _processCache]) return YES; # ifdef FD_COPY FD_COPY(&readFDs, &readFDs_); FD_COPY(&writeFDs, &writeFDs_); FD_COPY(&exceptFDs, &exceptFDs_); # else readFDs_ = readFDs; writeFDs_ = writeFDs; exceptFDs_ = exceptFDs; # endif time.tv_sec = timeout / 1000; time.tv_usec = (timeout % 1000) * 1000; if (select(nFDs, &readFDs_, &writeFDs_, &exceptFDs_, (timeout != -1 ? &time : NULL)) < 1) return NO; if (FD_ISSET(cancelFD[0], &readFDs_)) { char buffer; #ifndef _WIN32 assert(read(cancelFD[0], &buffer, 1) > 0); #else assert(recvfrom(cancelFD[0], &buffer, 1, 0, NULL, NULL) > 0); #endif } cArray = [readStreams cArray]; count = [readStreams count]; for (i = 0; i < count; i++) { int fileDescriptor = [cArray[i] fileDescriptor]; if (FD_ISSET(fileDescriptor, &readFDs_)) { [delegate streamDidBecomeReadyForReading: cArray[i]]; [pool releaseObjects]; } if (FD_ISSET(fileDescriptor, &exceptFDs_)) { [delegate streamDidReceiveException: cArray[i]]; [pool releaseObjects]; /* * Prevent calling it twice in case the FD is in both * sets. */ FD_CLR(fileDescriptor, &exceptFDs_); } } cArray = [writeStreams cArray]; count = [writeStreams count]; for (i = 0; i < count; i++) { int fileDescriptor = [cArray[i] fileDescriptor]; if (FD_ISSET(fileDescriptor, &writeFDs_)) { [delegate streamDidBecomeReadyForWriting: cArray[i]]; [pool releaseObjects]; } if (FD_ISSET(fileDescriptor, &exceptFDs_)) { [delegate streamDidReceiveException: cArray[i]]; [pool releaseObjects]; } } [pool release]; return YES; } @end |