20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
-
+
|
#include <string.h>
#include <unistd.h>
#import "OFStreamObserver_select.h"
#import "OFStream.h"
#import "OFArray.h"
#import "OFAutoreleasePool.h"
#import "autorelease.h"
#import "macros.h"
@implementation OFStreamObserver_select
- init
{
self = [super init];
|
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
|
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
|
-
+
-
+
+
+
-
+
-
-
+
+
-
+
-
-
-
+
+
+
+
-
+
-
-
+
-
-
+
-
-
+
+
-
-
|
if (!FD_ISSET(fd, &readFDs))
FD_CLR(fd, &exceptFDs);
}
- (BOOL)observeWithTimeout: (int)timeout
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
void *pool = objc_autoreleasePoolPush();
OFStream **objects;
fd_set readFDs_;
fd_set writeFDs_;
fd_set exceptFDs_;
struct timeval time;
size_t i, count;
[self _processQueue];
if ([self _processCache]) {
[pool release];
objc_autoreleasePoolPop(pool);
return YES;
}
objc_autoreleasePoolPop(pool);
#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((int)maxFD + 1, &readFDs_, &writeFDs_, &exceptFDs_,
(timeout != -1 ? &time : NULL)) < 1) {
(timeout != -1 ? &time : NULL)) < 1)
[pool release];
return NO;
}
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 = [readStreams objects];
count = [readStreams count];
for (i = 0; i < count; i++) {
int fileDescriptor = [objects[i] fileDescriptor];
pool = objc_autoreleasePoolPush();
if (FD_ISSET(fileDescriptor, &readFDs_)) {
if (FD_ISSET(fileDescriptor, &readFDs_))
[delegate streamIsReadyForReading: objects[i]];
[pool releaseObjects];
}
if (FD_ISSET(fileDescriptor, &exceptFDs_)) {
[delegate streamDidReceiveException: objects[i]];
[pool releaseObjects];
/*
* Prevent calling it twice in case the FD is in both
* sets.
*/
FD_CLR(fileDescriptor, &exceptFDs_);
}
objc_autoreleasePoolPop(pool);
}
objects = [writeStreams objects];
count = [writeStreams count];
for (i = 0; i < count; i++) {
int fileDescriptor = [objects[i] fileDescriptor];
pool = objc_autoreleasePoolPush();
if (FD_ISSET(fileDescriptor, &writeFDs_)) {
if (FD_ISSET(fileDescriptor, &writeFDs_))
[delegate streamIsReadyForWriting: objects[i]];
[pool releaseObjects];
}
if (FD_ISSET(fileDescriptor, &exceptFDs_)) {
if (FD_ISSET(fileDescriptor, &exceptFDs_))
[delegate streamDidReceiveException: objects[i]];
[pool releaseObjects];
}
objc_autoreleasePoolPop(pool);
}
[pool release];
return YES;
}
@end
|