ObjFW  Check-in [c279948fb8]

Overview
Comment:Nicer OFStreamObserver API.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: c279948fb8f7147f3a2530a342de4dbb05d64f63aef2ed2d0a3729b29520f96e
User & Date: js on 2011-09-19 12:44:09
Other Links: manifest | tags
Context
2011-09-19
13:07
Rename -[bindToPort:onHost:] to -[bindToHost:port:].
This way it's consistent with -[connectToHost:port].
check-in: b8517c63ca user: js tags: trunk
12:44
Nicer OFStreamObserver API. check-in: c279948fb8 user: js tags: trunk
12:34
BOOL naming conventions. check-in: d43eb177bc user: js tags: trunk
Changes

Modified src/OFStreamObserver.h from [d581b21593] to [753eef6385].

41
42
43
44
45
46
47
48

49
50
51
52
53
54
55

56
57
58
59
60
61
62
41
42
43
44
45
46
47

48
49
50
51
52
53
54

55
56
57
58
59
60
61
62







-
+






-
+







@optional
#endif
/**
 * \brief This callback is called when a stream did get ready for reading.
 *
 * \param stream The stream which did become ready for reading
 */
- (void)streamDidBecomeReadyForReading: (OFStream*)stream;
- (void)streamIsReadyForReading: (OFStream*)stream;

/**
 * \brief This callback is called when a stream did get ready for writing.
 *
 * \param stream The stream which did become ready for writing
 */
- (void)streamDidBecomeReadyForWriting: (OFStream*)stream;
- (void)streamIsReadyForWriting: (OFStream*)stream;

/**
 * \brief This callback is called when an exception occurred on the stream.
 *
 * \param stream The stream on which an exception occurred
 */
- (void)streamDidReceiveException: (OFStream*)stream;
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
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







-
+











-
+









-
+









-
+







 * It is recommended that the stream you add is set to non-blocking mode.
 *
 * If there is an -[observe] call blocking, it will be canceled. The reason for
 * this is to prevent blocking even though the new added stream is ready.
 *
 * \param stream The stream to observe for reading
 */
- (void)addStreamToObserveForReading: (OFStream*)stream;
- (void)addStreamForReading: (OFStream*)stream;

/**
 * \brief Adds a stream to observe for writing.
 *
 * It is recommended that the stream you add is set to non-blocking mode.
 *
 * If there is an -[observe] call blocking, it will be canceled. The reason for
 * this is to prevent blocking even though the new added stream is ready.
 *
 * \param stream The stream to observe for writing
 */
- (void)addStreamToObserveForWriting: (OFStream*)stream;
- (void)addStreamForWriting: (OFStream*)stream;

/**
 * \brief Removes a stream to observe for reading.
 *
 * If there is an -[observe] call blocking, it will be canceled. The reason for
 * this is to prevent the removed stream from still being observed.
 *
 * \param stream The stream to remove from observing for reading
 */
- (void)removeStreamToObserveForReading: (OFStream*)stream;
- (void)removeStreamForReading: (OFStream*)stream;

/**
 * \brief Removes a stream to observe for writing.
 *
 * If there is an -[observe] call blocking, it will be canceled. The reason for
 * this is to prevent the removed stream from still being observed.
 *
 * \param stream The stream to remove from observing for writing
 */
- (void)removeStreamToObserveForWriting: (OFStream*)stream;
- (void)removeStreamForWriting: (OFStream*)stream;

/**
 * \brief Observes all streams and blocks until an event happens on a stream.
 */
- (void)observe;

/**

Modified src/OFStreamObserver.m from [547cd504c0] to [26a8940bc6].

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
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
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
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







-
+



















-
+



















-
+



















-
+



















-
+
+
+
+
+
+
+





-
+





-
-
-
-
-
-
-
+







}

- (void)setDelegate: (id <OFStreamObserverDelegate>)delegate_
{
	OF_SETTER(delegate, delegate_, YES, NO)
}

- (void)addStreamToObserveForReading: (OFStream*)stream
- (void)addStreamForReading: (OFStream*)stream
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFNumber *qi = [OFNumber numberWithInt: QUEUE_ADD | QUEUE_READ];

	@synchronized (queue) {
		[queue addObject: stream];
		[queueInfo addObject: qi];
	}

#ifndef _WIN32
	assert(write(cancelFD[1], "", 1) > 0);
#else
	assert(sendto(cancelFD[1], "", 1, 0, (struct sockaddr*)&cancelAddr,
	    sizeof(cancelAddr)) > 0);
#endif

	[pool release];
}

- (void)addStreamToObserveForWriting: (OFStream*)stream
- (void)addStreamForWriting: (OFStream*)stream
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFNumber *qi = [OFNumber numberWithInt: QUEUE_ADD | QUEUE_WRITE];

	@synchronized (queue) {
		[queue addObject: stream];
		[queueInfo addObject: qi];
	}

#ifndef _WIN32
	assert(write(cancelFD[1], "", 1) > 0);
#else
	assert(sendto(cancelFD[1], "", 1, 0, (struct sockaddr*)&cancelAddr,
	    sizeof(cancelAddr)) > 0);
#endif

	[pool release];
}

- (void)removeStreamToObserveForReading: (OFStream*)stream
- (void)removeStreamForReading: (OFStream*)stream
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFNumber *qi = [OFNumber numberWithInt: QUEUE_REMOVE | QUEUE_READ];

	@synchronized (queue) {
		[queue addObject: stream];
		[queueInfo addObject: qi];
	}

#ifndef _WIN32
	assert(write(cancelFD[1], "", 1) > 0);
#else
	assert(sendto(cancelFD[1], "", 1, 0, (struct sockaddr*)&cancelAddr,
	    sizeof(cancelAddr)) > 0);
#endif

	[pool release];
}

- (void)removeStreamToObserveForWriting: (OFStream*)stream
- (void)removeStreamForWriting: (OFStream*)stream
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFNumber *qi = [OFNumber numberWithInt: QUEUE_REMOVE | QUEUE_WRITE];

	@synchronized (queue) {
		[queue addObject: stream];
		[queueInfo addObject: qi];
	}

#ifndef _WIN32
	assert(write(cancelFD[1], "", 1) > 0);
#else
	assert(sendto(cancelFD[1], "", 1, 0, (struct sockaddr*)&cancelAddr,
	    sizeof(cancelAddr)) > 0);
#endif

	[pool release];
}

- (void)_addStreamToObserveForReading: (OFStream*)strea;
- (void)_addStreamForReading: (OFStream*)strea;
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (void)_addStreamForWriting: (OFStream*)stream
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (void)_addStreamToObserveForWriting: (OFStream*)stream
- (void)_removeStreamForReading: (OFStream*)stream
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (void)_removeStreamToObserveForReading: (OFStream*)stream
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (void)_removeStreamToObserveForWriting: (OFStream*)stream
- (void)_removeStreamForWriting: (OFStream*)stream
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (void)_processQueue
{
298
299
300
301
302
303
304
305
306

307
308
309
310
311
312
313

314
315
316
317
318
319
320
321

322
323
324
325
326
327
328
329

330
331
332
333
334
335
336
298
299
300
301
302
303
304


305
306
307
308
309
310


311
312
313
314
315
316
317


318
319
320
321
322
323
324


325
326
327
328
329
330
331
332







-
-
+





-
-
+






-
-
+






-
-
+







				FDToStream[fd] = nil;
			}

			switch (action) {
			case QUEUE_ADD | QUEUE_READ:
				[readStreams addObject: queueCArray[i]];

				[self _addStreamToObserveForReading:
				    queueCArray[i]];
				[self _addStreamForReading: queueCArray[i]];

				break;
			case QUEUE_ADD | QUEUE_WRITE:
				[writeStreams addObject: queueCArray[i]];

				[self _addStreamToObserveForWriting:
				    queueCArray[i]];
				[self _addStreamForWriting: queueCArray[i]];

				break;
			case QUEUE_REMOVE | QUEUE_READ:
				[readStreams removeObjectIdenticalTo:
				    queueCArray[i]];

				[self _removeStreamToObserveForReading:
				    queueCArray[i]];
				[self _removeStreamForReading: queueCArray[i]];

				break;
			case QUEUE_REMOVE | QUEUE_WRITE:
				[writeStreams removeObjectIdenticalTo:
				    queueCArray[i]];

				[self _removeStreamToObserveForWriting:
				    queueCArray[i]];
				[self _removeStreamForWriting: queueCArray[i]];

				break;
			default:
				assert(0);
			}
		}

357
358
359
360
361
362
363
364

365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384

385
386
387
388

389
390
391
392
393
394
395
353
354
355
356
357
358
359

360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379

380
381
382
383

384
385
386
387
388
389
390
391







-
+



















-
+



-
+







	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]];
			[delegate streamIsReadyForReading: 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
- (void)streamIsReadyForReading: (OFStream*)stream
{
}

- (void)streamDidBecomeReadyForWriting: (OFStream*)stream
- (void)streamIsReadyForWriting: (OFStream*)stream
{
}

- (void)streamDidReceiveException: (OFStream*)stream
{
}
@end

Modified src/OFStreamObserver_poll.m from [103d3eeb79] to [8b9acbd834].

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
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







-
+





-
+





-
+





-
+







				[FDs removeItemAtIndex: i];

			break;
		}
	}
}

- (void)_addStreamToObserveForReading: (OFStream*)stream
- (void)_addStreamForReading: (OFStream*)stream
{
	[self _addStream: stream
	      withEvents: POLLIN];
}

- (void)_addStreamToObserveForWriting: (OFStream*)stream
- (void)_addStreamForWriting: (OFStream*)stream
{
	[self _addStream: stream
	      withEvents: POLLOUT];
}

- (void)_removeStreamToObserveForReading: (OFStream*)stream
- (void)_removeStreamForReading: (OFStream*)stream
{
	[self _removeStream: stream
		 withEvents: POLLIN];
}

- (void)_removeStreamToObserveForWriting: (OFStream*)stream
- (void)_removeStreamForWriting: (OFStream*)stream
{
	[self _removeStream: stream
		 withEvents: POLLOUT];
}

- (BOOL)observeWithTimeout: (int)timeout
{
154
155
156
157
158
159
160
161

162
163
164
165
166
167

168
169
170
171
172
173
174
154
155
156
157
158
159
160

161
162
163
164
165
166

167
168
169
170
171
172
173
174







-
+





-
+








				assert(read(cancelFD[0], &buffer, 1) > 0);
				FDsCArray[i].revents = 0;

				continue;
			}

			[delegate streamDidBecomeReadyForReading:
			[delegate streamIsReadyForReading:
			    FDToStream[FDsCArray[i].fd]];
			[pool releaseObjects];
		}

		if (FDsCArray[i].revents & POLLOUT) {
			[delegate streamDidBecomeReadyForReading:
			[delegate streamIsReadyForReading:
			    FDToStream[FDsCArray[i].fd]];
			[pool releaseObjects];
		}

		if (FDsCArray[i].revents & POLLERR) {
			[delegate streamDidReceiveException:
			    FDToStream[FDsCArray[i].fd]];

Modified src/OFStreamObserver_select.m from [e296c3f2eb] to [ab1097e618].

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
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







-
+







-
+







-
+









-
+







	FD_ZERO(&writeFDs);

	FD_SET(cancelFD[0], &readFDs);

	return self;
}

- (void)_addStreamToObserveForReading: (OFStream*)stream
- (void)_addStreamForReading: (OFStream*)stream
{
	int fd = [stream fileDescriptor];

	FD_SET(fd, &readFDs);
	FD_SET(fd, &exceptFDs);
}

- (void)_addStreamToObserveForWriting: (OFStream*)stream
- (void)_addStreamForWriting: (OFStream*)stream
{
	int fd = [stream fileDescriptor];

	FD_SET(fd, &writeFDs);
	FD_SET(fd, &exceptFDs);
}

- (void)_removeStreamToObserveForReading: (OFStream*)stream
- (void)_removeStreamForReading: (OFStream*)stream
{
	int fd = [stream fileDescriptor];

	FD_CLR(fd, &readFDs);

	if (!FD_ISSET(fd, &writeFDs))
		FD_CLR(fd, &exceptFDs);
}

- (void)_removeStreamToObserveForWriting: (OFStream*)stream
- (void)_removeStreamForWriting: (OFStream*)stream
{
	int fd = [stream fileDescriptor];

	FD_CLR(fd, &writeFDs);

	if (!FD_ISSET(fd, &readFDs))
		FD_CLR(fd, &exceptFDs);
121
122
123
124
125
126
127
128

129
130
131
132
133
134
135
121
122
123
124
125
126
127

128
129
130
131
132
133
134
135







-
+







	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]];
			[delegate streamIsReadyForReading: cArray[i]];
			[pool releaseObjects];
		}

		if (FD_ISSET(fileDescriptor, &exceptFDs_)) {
			[delegate streamDidReceiveException: cArray[i]];
			[pool releaseObjects];

144
145
146
147
148
149
150
151

152
153
154
155
156
157
158
144
145
146
147
148
149
150

151
152
153
154
155
156
157
158







-
+







	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]];
			[delegate streamIsReadyForWriting: cArray[i]];
			[pool releaseObjects];
		}

		if (FD_ISSET(fileDescriptor, &exceptFDs_)) {
			[delegate streamDidReceiveException: cArray[i]];
			[pool releaseObjects];
		}