Differences From Artifact [d472e2c55c]:
- File src/OFKernelEventObserver_select.m — part of check-in [96e20b8093] at 2014-01-28 22:50:37 on branch trunk — Rename OFStreamObserver -> OFKernelEventObserver (user: js, size: 4276) [annotate] [blame] [check-ins using]
To Artifact [d61b6807c3]:
- File
src/OFKernelEventObserver_select.m
— part of check-in
[c694569d86]
at
2014-01-29 15:54:59
on branch trunk
— Generalize OFKernelEventObserver
It is no longer limited to streams now. (user: js, size: 3472) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
22 23 24 25 26 27 28 | #include <math.h> #include <sys/time.h> #import "OFKernelEventObserver.h" #import "OFKernelEventObserver+Private.h" #import "OFKernelEventObserver_select.h" | < | 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #include <math.h> #include <sys/time.h> #import "OFKernelEventObserver.h" #import "OFKernelEventObserver+Private.h" #import "OFKernelEventObserver_select.h" #import "OFArray.h" #import "autorelease.h" #import "macros.h" #import "socket_helpers.h" @implementation OFKernelEventObserver_select |
︙ | ︙ | |||
45 46 47 48 49 50 51 | return self; } - (void)OF_addFileDescriptorForReading: (int)fd { FD_SET(fd, &_readFDs); | < < < < < < < < | < < < | | | | | < < < < < < < < < < < < < < < | | | | < < < < < < < < < | 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 | return self; } - (void)OF_addFileDescriptorForReading: (int)fd { FD_SET(fd, &_readFDs); } - (void)OF_addFileDescriptorForWriting: (int)fd { FD_SET(fd, &_writeFDs); } - (void)OF_removeFileDescriptorForReading: (int)fd { FD_CLR(fd, &_readFDs); } - (void)OF_removeFileDescriptorForWriting: (int)fd { FD_CLR(fd, &_writeFDs); } - (bool)observeForTimeInterval: (of_time_interval_t)timeInterval { void *pool = objc_autoreleasePoolPush(); id *objects; fd_set readFDs; fd_set writeFDs; struct timeval timeout; size_t i, count, realEvents = 0; [self OF_processQueue]; if ([self OF_processCache]) { objc_autoreleasePoolPop(pool); return true; } objc_autoreleasePoolPop(pool); #ifdef FD_COPY FD_COPY(&_readFDs, &readFDs); FD_COPY(&_writeFDs, &writeFDs); #else readFDs = _readFDs; writeFDs = _writeFDs; #endif /* * We cast to int before assigning to tv_usec in order to avoid a * warning with Apple GCC on PPC. POSIX defines this as suseconds_t, * however, this is not available on Win32. As an int should always * satisfy the required range, we just cast to int. */ timeout.tv_sec = (time_t)timeInterval; timeout.tv_usec = (int)lrint((timeInterval - timeout.tv_sec) * 1000); if (select((int)_maxFD + 1, &readFDs, &writeFDs, NULL, (timeInterval != -1 ? &timeout : NULL)) < 1) return false; if (FD_ISSET(_cancelFD[0], &readFDs)) { char buffer; #ifndef _WIN32 OF_ENSURE(read(_cancelFD[0], &buffer, 1) > 0); #else OF_ENSURE(recvfrom(_cancelFD[0], &buffer, 1, 0, NULL, NULL) > 0); #endif } objects = [_readObjects objects]; count = [_readObjects count]; for (i = 0; i < count; i++) { int fd = [objects[i] fileDescriptorForReading]; pool = objc_autoreleasePoolPush(); if (FD_ISSET(fd, &readFDs)) { if ([_delegate respondsToSelector: @selector(objectIsReadyForReading:)]) [_delegate objectIsReadyForReading: objects[i]]; realEvents++; } objc_autoreleasePoolPop(pool); } objects = [_writeObjects objects]; count = [_writeObjects count]; for (i = 0; i < count; i++) { int fd = [objects[i] fileDescriptorForWriting]; pool = objc_autoreleasePoolPush(); if (FD_ISSET(fd, &writeFDs)) { if ([_delegate respondsToSelector: @selector(objectIsReadyForWriting:)]) [_delegate objectIsReadyForWriting: objects[i]]; realEvents++; } objc_autoreleasePoolPop(pool); } if (realEvents == 0) return false; return true; } @end |