ObjFW  Diff

Differences From Artifact [b904df7bb8]:

To Artifact [b6d33523f1]:


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#import "OFArray.h"
#import "OFDictionary.h"
#import "OFStream.h"
#import "OFDataArray.h"
#ifdef _WIN32
# import "OFTCPSocket.h"
#endif
#ifdef OF_THREADS
# import "OFMutex.h"
#endif

#ifdef HAVE_KQUEUE
# import "OFStreamObserver_kqueue.h"
#endif
#ifdef HAVE_POLL_H







|







28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#import "OFArray.h"
#import "OFDictionary.h"
#import "OFStream.h"
#import "OFDataArray.h"
#ifdef _WIN32
# import "OFTCPSocket.h"
#endif
#ifdef OF_HAVE_THREADS
# import "OFMutex.h"
#endif

#ifdef HAVE_KQUEUE
# import "OFStreamObserver_kqueue.h"
#endif
#ifdef HAVE_POLL_H
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
#endif

		maxFD = cancelFD[0];
		FDToStream = [self allocMemoryWithSize: sizeof(OFStream*)
						 count: maxFD + 1];
		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]);

	[readStreams release];
	[writeStreams release];
	[queue release];
	[queueInfo release];
	[queueFDs release];
#ifdef OF_THREADS
	[mutex release];
#endif

	[super dealloc];
}

- (id <OFStreamObserverDelegate>)delegate
{
	return delegate;
}

- (void)setDelegate: (id <OFStreamObserverDelegate>)delegate_
{
	delegate = delegate_;
}

- (void)addStreamForReading: (OFStream*)stream
{
#ifdef OF_THREADS
	[mutex lock];
	@try {
#endif

		int qi = QUEUE_ADD | QUEUE_READ;
		int fd = [stream fileDescriptorForReading];

		[queue addObject: stream];
		[queueInfo addItem: &qi];
		[queueFDs addItem: &fd];
#ifdef OF_THREADS
	} @finally {

		[mutex unlock];
	}
#endif


	[self cancel];
}

- (void)addStreamForWriting: (OFStream*)stream
{
#ifdef OF_THREADS
	[mutex lock];
	@try {
#endif

		int qi = QUEUE_ADD | QUEUE_WRITE;
		int fd = [stream fileDescriptorForWriting];

		[queue addObject: stream];
		[queueInfo addItem: &qi];
		[queueFDs addItem: &fd];
#ifdef OF_THREADS
	} @finally {

		[mutex unlock];
	}
#endif


	[self cancel];
}

- (void)removeStreamForReading: (OFStream*)stream
{
#ifdef OF_THREADS
	[mutex lock];
	@try {
#endif

		int qi = QUEUE_REMOVE | QUEUE_READ;
		int fd = [stream fileDescriptorForReading];

		[queue addObject: stream];
		[queueInfo addItem: &qi];
		[queueFDs addItem: &fd];
#ifdef OF_THREADS
	} @finally {

		[mutex unlock];
	}
#endif


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

- (void)removeStreamForWriting: (OFStream*)stream
{
#ifdef OF_THREADS
	[mutex lock];
	@try {
#endif

		int qi = QUEUE_REMOVE | QUEUE_WRITE;
		int fd = [stream fileDescriptorForWriting];

		[queue addObject: stream];
		[queueInfo addItem: &qi];
		[queueFDs addItem: &fd];
#ifdef OF_THREADS
	} @finally {

		[mutex unlock];
	}
#endif


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







|




















|


















|

<

>






<

>

<

>






|

<

>






<

>

<

>






|

<

>






<

>

<

>











|

<

>






<

>

<

>







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

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

#ifdef OF_HAVE_THREADS
		mutex = [[OFMutex alloc] init];
#endif
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	close(cancelFD[0]);
	close(cancelFD[1]);

	[readStreams release];
	[writeStreams release];
	[queue release];
	[queueInfo release];
	[queueFDs release];
#ifdef OF_HAVE_THREADS
	[mutex release];
#endif

	[super dealloc];
}

- (id <OFStreamObserverDelegate>)delegate
{
	return delegate;
}

- (void)setDelegate: (id <OFStreamObserverDelegate>)delegate_
{
	delegate = delegate_;
}

- (void)addStreamForReading: (OFStream*)stream
{
#ifdef OF_HAVE_THREADS
	[mutex lock];

#endif
	@try {
		int qi = QUEUE_ADD | QUEUE_READ;
		int fd = [stream fileDescriptorForReading];

		[queue addObject: stream];
		[queueInfo addItem: &qi];
		[queueFDs addItem: &fd];

	} @finally {
#ifdef OF_HAVE_THREADS
		[mutex unlock];

#endif
	}

	[self cancel];
}

- (void)addStreamForWriting: (OFStream*)stream
{
#ifdef OF_HAVE_THREADS
	[mutex lock];

#endif
	@try {
		int qi = QUEUE_ADD | QUEUE_WRITE;
		int fd = [stream fileDescriptorForWriting];

		[queue addObject: stream];
		[queueInfo addItem: &qi];
		[queueFDs addItem: &fd];

	} @finally {
#ifdef OF_HAVE_THREADS
		[mutex unlock];

#endif
	}

	[self cancel];
}

- (void)removeStreamForReading: (OFStream*)stream
{
#ifdef OF_HAVE_THREADS
	[mutex lock];

#endif
	@try {
		int qi = QUEUE_REMOVE | QUEUE_READ;
		int fd = [stream fileDescriptorForReading];

		[queue addObject: stream];
		[queueInfo addItem: &qi];
		[queueFDs addItem: &fd];

	} @finally {
#ifdef OF_HAVE_THREADS
		[mutex unlock];

#endif
	}

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

- (void)removeStreamForWriting: (OFStream*)stream
{
#ifdef OF_HAVE_THREADS
	[mutex lock];

#endif
	@try {
		int qi = QUEUE_REMOVE | QUEUE_WRITE;
		int fd = [stream fileDescriptorForWriting];

		[queue addObject: stream];
		[queueInfo addItem: &qi];
		[queueFDs addItem: &fd];

	} @finally {
#ifdef OF_HAVE_THREADS
		[mutex unlock];

#endif
	}

#ifndef _WIN32
	OF_ENSURE(write(cancelFD[1], "", 1) > 0);
#else
	OF_ENSURE(sendto(cancelFD[1], "", 1, 0, (struct sockaddr*)&cancelAddr,
	    sizeof(cancelAddr)) > 0);
#endif
302
303
304
305
306
307
308
309
310
311
312

313
314
315
316
317
318
319
{
	[self doesNotRecognizeSelector: _cmd];
	abort();
}

- (void)OF_processQueue
{
#ifdef OF_THREADS
	[mutex lock];
	@try {
#endif

		OFStream **queueObjects = [queue objects];
		int *queueInfoItems = [queueInfo items];
		int *queueFDsItems = [queueFDs items];
		size_t i, count = [queue count];

		for (i = 0; i < count; i++) {
			OFStream *stream = queueObjects[i];







|

<

>







302
303
304
305
306
307
308
309
310

311
312
313
314
315
316
317
318
319
{
	[self doesNotRecognizeSelector: _cmd];
	abort();
}

- (void)OF_processQueue
{
#ifdef OF_HAVE_THREADS
	[mutex lock];

#endif
	@try {
		OFStream **queueObjects = [queue objects];
		int *queueInfoItems = [queueInfo items];
		int *queueFDsItems = [queueFDs items];
		size_t i, count = [queue count];

		for (i = 0; i < count; i++) {
			OFStream *stream = queueObjects[i];
366
367
368
369
370
371
372
373
374

375
376
377

378
379
380
381
382
383
384
				assert(0);
			}
		}

		[queue removeAllObjects];
		[queueInfo removeAllItems];
		[queueFDs removeAllItems];
#ifdef OF_THREADS
	} @finally {

		[mutex unlock];
	}
#endif

}

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








<

>

<

>







366
367
368
369
370
371
372

373
374
375

376
377
378
379
380
381
382
383
384
				assert(0);
			}
		}

		[queue removeAllObjects];
		[queueInfo removeAllItems];
		[queueFDs removeAllItems];

	} @finally {
#ifdef OF_HAVE_THREADS
		[mutex unlock];

#endif
	}
}

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