ObjFW  Diff

Differences From Artifact [58de981563]:

To Artifact [2c9b5a07cf]:


22
23
24
25
26
27
28
29

30
31
32

33
34
35
36
37
38
39
22
23
24
25
26
27
28

29
30
31
32
33
34
35
36
37
38
39
40







-
+



+







#include <assert.h>
#include <unistd.h>

#import "OFStreamObserver.h"
#import "OFArray.h"
#import "OFDictionary.h"
#import "OFStream.h"
#import "OFNumber.h"
#import "OFDataArray.h"
#ifdef _WIN32
# import "OFTCPSocket.h"
#endif
#import "OFThread.h"
#import "OFAutoreleasePool.h"

#ifdef HAVE_POLL_H
# import "OFStreamObserver_poll.h"
#endif
#if defined(HAVE_SYS_SELECT_H) || defined(_WIN32)
# import "OFStreamObserver_select.h"
86
87
88
89
90
91
92

93

94
95
96
97
98
99
100
87
88
89
90
91
92
93
94

95
96
97
98
99
100
101
102







+
-
+







		struct sockaddr_in cancelAddr2;
		socklen_t cancelAddrLen;
#endif

		readStreams = [[OFMutableArray alloc] init];
		writeStreams = [[OFMutableArray alloc] init];
		queue = [[OFMutableArray alloc] init];
		queueInfo = [[OFDataArray alloc] initWithItemSize: sizeof(int)];
		queueInfo = [[OFMutableArray alloc] init];
		queueFDs = [[OFDataArray alloc] initWithItemSize: sizeof(int)];

#ifndef _WIN32
		if (pipe(cancelFD))
			@throw [OFInitializationFailedException
			    newWithClass: isa];
#else
		/* Make sure WSAStartup has been called */
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
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
285
286
287
288
289
290
291

292
293
294
295
296
297
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
337
338
339
340
341
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


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
285
286
287
288
289

290
291
292

293
294
295
296
297
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
337
338

339

340

341
342
343
344
345
346
347
348
349

350
351
352
353
354
355
356
357
358
359
360







+
+
+
+


















+
+
+
+
















-
-
+
+
+
+

-

-
+
+
+
+








-
-




-
-
+
+
+
+

-

-
+
+
+
+








-
-




-
-
+
+
+
+

-

-
+
+
+
+








-
-




-
-
+
+
+
+

-

-
+
+
+
+








-
-
+
+
+
+
+
+


-
+





-
+





-
-
-
-
-
-
+
-







-
+
+

-
+
+



+
-
+
+


-
-








-
+



-
-






-
+

-
+



-
+

-
+



-
+
-

-
+



-
+
-

-
+








-
+
+
+
+







			    newWithClass: isa];
#endif

		maxFD = cancelFD[0];
		FDToStream = [self allocMemoryForNItems: maxFD + 1
						 ofSize: sizeof(OFStream*)];
		FDToStream[cancelFD[0]] = nil;

#ifdef OF_THREADS
		mutex = [[OFMutex alloc] init];
#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];
	[queueFDs release];
#ifdef OF_THREADS
	[mutex release];
#endif

	[super dealloc];
}

- (id <OFStreamObserverDelegate>)delegate
{
	OF_GETTER(delegate, YES)
}

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

- (void)addStreamForReading: (OFStream*)stream
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFNumber *qi = [OFNumber numberWithInt: QUEUE_ADD | QUEUE_READ];
	[mutex lock];
	@try {
		int qi = QUEUE_ADD | QUEUE_READ;
		int fd = [stream fileDescriptor];

	@synchronized (queue) {
		[queue addObject: stream];
		[queueInfo addObject: qi];
		[queueInfo addItem: &qi];
		[queueFDs addItem: &fd];
	} @finally {
		[mutex unlock];
	}

#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)addStreamForWriting: (OFStream*)stream
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFNumber *qi = [OFNumber numberWithInt: QUEUE_ADD | QUEUE_WRITE];
	[mutex lock];
	@try {
		int qi = QUEUE_ADD | QUEUE_WRITE;
		int fd = [stream fileDescriptor];

	@synchronized (queue) {
		[queue addObject: stream];
		[queueInfo addObject: qi];
		[queueInfo addItem: &qi];
		[queueFDs addItem: &fd];
	} @finally {
		[mutex unlock];
	}

#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)removeStreamForReading: (OFStream*)stream
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFNumber *qi = [OFNumber numberWithInt: QUEUE_REMOVE | QUEUE_READ];
	[mutex lock];
	@try {
		int qi = QUEUE_REMOVE | QUEUE_READ;
		int fd = [stream fileDescriptor];

	@synchronized (queue) {
		[queue addObject: stream];
		[queueInfo addObject: qi];
		[queueInfo addItem: &qi];
		[queueFDs addItem: &fd];
	} @finally {
		[mutex unlock];
	}

#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)removeStreamForWriting: (OFStream*)stream
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFNumber *qi = [OFNumber numberWithInt: QUEUE_REMOVE | QUEUE_WRITE];
	[mutex lock];
	@try {
		int qi = QUEUE_REMOVE | QUEUE_WRITE;
		int fd = [stream fileDescriptor];

	@synchronized (queue) {
		[queue addObject: stream];
		[queueInfo addObject: qi];
		[queueInfo addItem: &qi];
		[queueFDs addItem: &fd];
	} @finally {
		[mutex unlock];
	}

#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)_addFileDescriptorForReading: (int)fd
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

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

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

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

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

- (void)_processQueue
{
	@synchronized (queue) {
	[mutex lock];
	@try {
		OFStream **queueCArray = [queue cArray];
		OFNumber **queueInfoCArray = [queueInfo cArray];
		int *queueInfoCArray = [queueInfo cArray];
		int *queueFDsCArray = [queueFDs cArray];
		size_t i, count = [queue count];

		for (i = 0; i < count; i++) {
			OFStream *stream = queueCArray[i];
			int action = [queueInfoCArray[i] intValue];
			int action = queueInfoCArray[i];
			int fd = queueFDsCArray[i];

			if ((action & QUEUE_ACTION) == QUEUE_ADD) {
				int fd = [queueCArray[i] fileDescriptor];

				if (fd > maxFD) {
					maxFD = fd;
					FDToStream = [self
					    resizeMemory: FDToStream
						toNItems: maxFD + 1
						  ofSize: sizeof(OFStream*)];
				}

				FDToStream[fd] = queueCArray[i];
				FDToStream[fd] = stream;
			}

			if ((action & QUEUE_ACTION) == QUEUE_REMOVE) {
				int fd = [queueCArray[i] fileDescriptor];

				/* FIXME: Maybe downsize? */
				FDToStream[fd] = nil;
			}

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

				[self _addStreamForReading: queueCArray[i]];
				[self _addFileDescriptorForReading: fd];

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

				[self _addStreamForWriting: queueCArray[i]];
				[self _addFileDescriptorForWriting: fd];

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

				[self _removeStreamForReading: queueCArray[i]];
				[self _removeFileDescriptorForReading: fd];

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

				[self _removeStreamForWriting: queueCArray[i]];
				[self _removeFileDescriptorForWriting: fd];

				break;
			default:
				assert(0);
			}
		}

		[queue removeNObjects: count];
		[queueInfo removeNObjects: count];
		[queueInfo removeNItems: count];
		[queueFDs removeNItems: count];
	} @finally {
		[mutex unlock];
	}
}

- (void)observe
{
	[self observeWithTimeout: -1];
}