ObjFW  Check-in [6343e04dcd]

Overview
Comment:Pass exceptions on async I/O.

This makes it possible to handle exceptions that occurred during async
I/O in the specified handler.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 6343e04dcd00c9d9c55fdcc84658687e30bb9c33c3d86f4cf888a36e309dbebc
User & Date: js on 2012-10-17 20:16:10
Other Links: manifest | tags
Context
2012-10-17
20:39
Small fix in instance.m. check-in: 879bf12fd0 user: js tags: trunk
20:16
Pass exceptions on async I/O. check-in: 6343e04dcd user: js tags: trunk
2012-10-16
14:49
OFConstantString: Add a few missing new methods. check-in: e49548ce0f user: js tags: trunk
Changes

Modified Makefile from [f76483636b] to [99b6edd0ce].

19
20
21
22
23
24
25
26
27
28
		objfw-${PACKAGE_VERSION}.tar.xz
	mkdir objfw-${PACKAGE_VERSION}
	git --work-tree=objfw-${PACKAGE_VERSION} checkout .
	rm objfw-${PACKAGE_VERSION}/.gitignore
	cp configure config.h.in objfw-${PACKAGE_VERSION}/
	tar cf objfw-${PACKAGE_VERSION}.tar objfw-${PACKAGE_VERSION}
	rm -fr objfw-${PACKAGE_VERSION}
	xz -9 objfw-${PACKAGE_VERSION}.tar
	rm -f objfw-${PACKAGE_VERSION}.tar
	gpg -b objfw-${PACKAGE_VERSION}.tar.xz || true







|


19
20
21
22
23
24
25
26
27
28
		objfw-${PACKAGE_VERSION}.tar.xz
	mkdir objfw-${PACKAGE_VERSION}
	git --work-tree=objfw-${PACKAGE_VERSION} checkout .
	rm objfw-${PACKAGE_VERSION}/.gitignore
	cp configure config.h.in objfw-${PACKAGE_VERSION}/
	tar cf objfw-${PACKAGE_VERSION}.tar objfw-${PACKAGE_VERSION}
	rm -fr objfw-${PACKAGE_VERSION}
	xz objfw-${PACKAGE_VERSION}.tar
	rm -f objfw-${PACKAGE_VERSION}.tar
	gpg -b objfw-${PACKAGE_VERSION}.tar.xz || true

Modified src/OFRunLoop.m from [0f13518563] to [5d7da15761].

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
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
396
397
398
399
400
401
402
403

404


405



406

407

408
409
410

411
412
413
414
415
416
417
418
419
420
421
422
423
424

425
426
427
428
429

430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446




447




448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463

464

465
466
467
468
469
470
471
472
473
474
475
476
	OF_ENSURE(queue != nil);

	listObject = [queue firstListObject];

	if ([listObject->object isKindOfClass:
	    [OFRunLoop_ReadQueueItem class]]) {
		OFRunLoop_ReadQueueItem *queueItem = listObject->object;




		size_t length = [stream readIntoBuffer: queueItem->buffer
						length: queueItem->length];





#ifdef OF_HAVE_BLOCKS
		if (queueItem->block != NULL) {
			if (!queueItem->block(stream, queueItem->buffer,
				length)) {
				[queue removeListObject: listObject];

				if ([queue count] == 0) {
					[streamObserver
					    removeStreamForReading: stream];
					[readQueues removeObjectForKey: stream];
				}
			}
		} else {
#endif
			BOOL (*func)(id, SEL, OFStream*, void*, size_t) =
			    (BOOL(*)(id, SEL, OFStream*, void*, size_t))

			    [queueItem->target methodForSelector:
			    queueItem->selector];

			if (!func(queueItem->target, queueItem->selector,
			    stream, queueItem->buffer, length)) {
				[queue removeListObject: listObject];

				if ([queue count] == 0) {
					[streamObserver
					    removeStreamForReading: stream];
					[readQueues removeObjectForKey: stream];
				}
			}
#ifdef OF_HAVE_BLOCKS
		}
#endif
	} else if ([listObject->object isKindOfClass:
	    [OFRunLoop_ExactReadQueueItem class]]) {
		OFRunLoop_ExactReadQueueItem *queueItem = listObject->object;
		size_t length = [stream




		    readIntoBuffer: (char*)queueItem->buffer +
				    queueItem->readLength
			    length: queueItem->exactLength -
				    queueItem->readLength];





		queueItem->readLength += length;
		if (queueItem->readLength == queueItem->exactLength ||
		    [stream isAtEndOfStream]) {
#ifdef OF_HAVE_BLOCKS
			if (queueItem->block != NULL) {
				if (queueItem->block(stream, queueItem->buffer,
				    queueItem->readLength))
					queueItem->readLength = 0;
				else {
					[queue removeListObject: listObject];

					if ([queue count] == 0) {
						[streamObserver
						    removeStreamForReading:
						    stream];
						[readQueues
						    removeObjectForKey: stream];
					}
				}
			} else {
#endif
				BOOL (*func)(id, SEL, OFStream*, void*,
				    size_t) = (BOOL(*)(id, SEL, OFStream*,

				    void*, size_t))[queueItem->target
				    methodForSelector: queueItem->selector];

				if (func(queueItem->target,
				    queueItem->selector, stream,
				    queueItem->buffer, queueItem->readLength))

					queueItem->readLength = 0;
				else {
					[queue removeListObject: listObject];

					if ([queue count] == 0) {
						[streamObserver
						    removeStreamForReading:
						    stream];
						[readQueues
						    removeObjectForKey: stream];
					}
				}
#ifdef OF_HAVE_BLOCKS
			}
#endif
		}
	} else if ([listObject->object isKindOfClass:
	    [OFRunLoop_ReadLineQueueItem class]]) {
		OFRunLoop_ReadLineQueueItem *queueItem = listObject->object;
		OFString *line;




		line = [stream tryReadLineWithEncoding: queueItem->encoding];





		if (line != nil || [stream isAtEndOfStream]) {

#ifdef OF_HAVE_BLOCKS
			if (queueItem->block != NULL) {
				if (!queueItem->block(stream, line)) {

					[queue removeListObject: listObject];

					if ([queue count] == 0) {
						[streamObserver
						    removeStreamForReading:
						    stream];
						[readQueues
						    removeObjectForKey: stream];
					}
				}
			} else {
#endif
				BOOL (*func)(id, SEL, OFStream*, OFString*) =
				    (BOOL(*)(id, SEL, OFStream*, OFString*))

				    [queueItem->target methodForSelector:
				    queueItem->selector];

				if (!func(queueItem->target,
				    queueItem->selector, stream, line)) {

					[queue removeListObject: listObject];

					if ([queue count] == 0) {
						[streamObserver
						    removeStreamForReading:
						    stream];
						[readQueues
						    removeObjectForKey: stream];
					}
				}
#ifdef OF_HAVE_BLOCKS
			}
#endif
		}
	} else if ([listObject->object isKindOfClass:
	    [OFRunLoop_AcceptQueueItem class]]) {
		OFRunLoop_AcceptQueueItem *queueItem = listObject->object;




		OFTCPSocket *newSocket = [(OFTCPSocket*)stream accept];





#ifdef OF_HAVE_BLOCKS
		if (queueItem->block != NULL) {
			if (!queueItem->block((OFTCPSocket*)stream,
			    newSocket)) {
				[queue removeListObject: listObject];

				if ([queue count] == 0) {
					[streamObserver
					    removeStreamForReading: stream];
					[readQueues removeObjectForKey: stream];
				}
			}
		} else {
#endif
			BOOL (*func)(id, SEL, OFTCPSocket*, OFTCPSocket*) =

			    (BOOL(*)(id, SEL, OFTCPSocket*, OFTCPSocket*))

			    [queueItem->target methodForSelector:
			    queueItem->selector];

			if (!func(queueItem->target, queueItem->selector,
			    (OFTCPSocket*)stream, newSocket)) {
				[queue removeListObject: listObject];

				if ([queue count] == 0) {
					[streamObserver
					    removeStreamForReading: stream];
					[readQueues removeObjectForKey: stream];
				}







>
>
>
>
|
|
>
>
>
>




|










|
|
>




|














|
>
>
>
>
|
|
|
|
>
>
>
>



|



|















|
>
|




|
>




















>

>
>
|
>
>
>
|
>
|
>


|
>












|
|
>




|
>

















>
>
>
>
|
>
>
>
>




|










|
>
|
>




|







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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
	OF_ENSURE(queue != nil);

	listObject = [queue firstListObject];

	if ([listObject->object isKindOfClass:
	    [OFRunLoop_ReadQueueItem class]]) {
		OFRunLoop_ReadQueueItem *queueItem = listObject->object;
		size_t length;
		OFException *exception = nil;

		@try {
			length = [stream readIntoBuffer: queueItem->buffer
						 length: queueItem->length];
		} @catch (OFException *e) {
			length = 0;
			exception = e;
		}

#ifdef OF_HAVE_BLOCKS
		if (queueItem->block != NULL) {
			if (!queueItem->block(stream, queueItem->buffer,
			    length, exception)) {
				[queue removeListObject: listObject];

				if ([queue count] == 0) {
					[streamObserver
					    removeStreamForReading: stream];
					[readQueues removeObjectForKey: stream];
				}
			}
		} else {
#endif
			BOOL (*func)(id, SEL, OFStream*, void*, size_t,
			    OFException*) = (BOOL(*)(id, SEL, OFStream*, void*,
			    size_t, OFException*))
			    [queueItem->target methodForSelector:
			    queueItem->selector];

			if (!func(queueItem->target, queueItem->selector,
			    stream, queueItem->buffer, length, exception)) {
				[queue removeListObject: listObject];

				if ([queue count] == 0) {
					[streamObserver
					    removeStreamForReading: stream];
					[readQueues removeObjectForKey: stream];
				}
			}
#ifdef OF_HAVE_BLOCKS
		}
#endif
	} else if ([listObject->object isKindOfClass:
	    [OFRunLoop_ExactReadQueueItem class]]) {
		OFRunLoop_ExactReadQueueItem *queueItem = listObject->object;
		size_t length;
		OFException *exception = nil;

		@try {
			length = [stream
			    readIntoBuffer: (char*)queueItem->buffer +
					    queueItem->readLength
				    length: queueItem->exactLength -
					    queueItem->readLength];
		} @catch (OFException *e) {
			length = 0;
			exception = e;
		}

		queueItem->readLength += length;
		if (queueItem->readLength == queueItem->exactLength ||
		    [stream isAtEndOfStream] || exception != nil) {
#ifdef OF_HAVE_BLOCKS
			if (queueItem->block != NULL) {
				if (queueItem->block(stream, queueItem->buffer,
				    queueItem->readLength, exception))
					queueItem->readLength = 0;
				else {
					[queue removeListObject: listObject];

					if ([queue count] == 0) {
						[streamObserver
						    removeStreamForReading:
						    stream];
						[readQueues
						    removeObjectForKey: stream];
					}
				}
			} else {
#endif
				BOOL (*func)(id, SEL, OFStream*, void*,
				    size_t, OFException*) = (BOOL(*)(id, SEL,
				    OFStream*, void*, size_t, OFException*))
				    [queueItem->target
				    methodForSelector: queueItem->selector];

				if (func(queueItem->target,
				    queueItem->selector, stream,
				    queueItem->buffer, queueItem->readLength,
				    exception))
					queueItem->readLength = 0;
				else {
					[queue removeListObject: listObject];

					if ([queue count] == 0) {
						[streamObserver
						    removeStreamForReading:
						    stream];
						[readQueues
						    removeObjectForKey: stream];
					}
				}
#ifdef OF_HAVE_BLOCKS
			}
#endif
		}
	} else if ([listObject->object isKindOfClass:
	    [OFRunLoop_ReadLineQueueItem class]]) {
		OFRunLoop_ReadLineQueueItem *queueItem = listObject->object;
		OFString *line;
		OFException *exception = nil;

		@try {
			line = [stream
			    tryReadLineWithEncoding: queueItem->encoding];
		} @catch (OFException *e) {
			line = nil;
			exception = e;
		}

		if (line != nil || [stream isAtEndOfStream] ||
		    exception != nil) {
#ifdef OF_HAVE_BLOCKS
			if (queueItem->block != NULL) {
				if (!queueItem->block(stream, line,
				    exception)) {
					[queue removeListObject: listObject];

					if ([queue count] == 0) {
						[streamObserver
						    removeStreamForReading:
						    stream];
						[readQueues
						    removeObjectForKey: stream];
					}
				}
			} else {
#endif
				BOOL (*func)(id, SEL, OFStream*, OFString*,
				    OFException*) = (BOOL(*)(id, SEL, OFStream*,
				    OFString*, OFException*))
				    [queueItem->target methodForSelector:
				    queueItem->selector];

				if (!func(queueItem->target,
				    queueItem->selector, stream, line,
				    exception)) {
					[queue removeListObject: listObject];

					if ([queue count] == 0) {
						[streamObserver
						    removeStreamForReading:
						    stream];
						[readQueues
						    removeObjectForKey: stream];
					}
				}
#ifdef OF_HAVE_BLOCKS
			}
#endif
		}
	} else if ([listObject->object isKindOfClass:
	    [OFRunLoop_AcceptQueueItem class]]) {
		OFRunLoop_AcceptQueueItem *queueItem = listObject->object;
		OFTCPSocket *newSocket;
		OFException *exception = nil;

		@try {
			newSocket = [(OFTCPSocket*)stream accept];
		} @catch (OFException *e) {
			newSocket = nil;
			exception = e;
		}

#ifdef OF_HAVE_BLOCKS
		if (queueItem->block != NULL) {
			if (!queueItem->block((OFTCPSocket*)stream,
			    newSocket, exception)) {
				[queue removeListObject: listObject];

				if ([queue count] == 0) {
					[streamObserver
					    removeStreamForReading: stream];
					[readQueues removeObjectForKey: stream];
				}
			}
		} else {
#endif
			BOOL (*func)(id, SEL, OFTCPSocket*, OFTCPSocket*,
			    OFException*) =
			    (BOOL(*)(id, SEL, OFTCPSocket*, OFTCPSocket*,
			    OFException*))
			    [queueItem->target methodForSelector:
			    queueItem->selector];

			if (!func(queueItem->target, queueItem->selector,
			    (OFTCPSocket*)stream, newSocket, exception)) {
				[queue removeListObject: listObject];

				if ([queue count] == 0) {
					[streamObserver
					    removeStreamForReading: stream];
					[readQueues removeObjectForKey: stream];
				}

Modified src/OFStream.h from [8627416ee2] to [6c1eafc699].

24
25
26
27
28
29
30

31
32
33

34

35
36
37
38
39
40
41
#include <stdarg.h>

#import "OFObject.h"
#import "OFString.h"

@class OFStream;
@class OFDataArray;


#ifdef OF_HAVE_BLOCKS
typedef BOOL (^of_stream_async_read_block_t)(OFStream*, void*, size_t);

typedef BOOL (^of_stream_async_read_line_block_t)(OFStream*, OFString*);

#endif

/**
 * \brief A base class for different types of streams.
 *
 * \warning Even though the OFCopying protocol is implemented, it does
 *	    <i>not</i> return an independent copy of the stream, but instead







>


|
>
|
>







24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdarg.h>

#import "OFObject.h"
#import "OFString.h"

@class OFStream;
@class OFDataArray;
@class OFException;

#ifdef OF_HAVE_BLOCKS
typedef BOOL (^of_stream_async_read_block_t)(OFStream*, void*, size_t,
    OFException*);
typedef BOOL (^of_stream_async_read_line_block_t)(OFStream*, OFString*,
    OFException*);
#endif

/**
 * \brief A base class for different types of streams.
 *
 * \warning Even though the OFCopying protocol is implemented, it does
 *	    <i>not</i> return an independent copy of the stream, but instead
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
162
163
164
 * \param target The target on which the selector should be called when the
 *		 data has been received. If the method returns YES, it will be
 *		 called again with the same buffer and maximum length when more
 *		 data has been received. If you want the next method in the
 *		 queue to handle the data received next, you need to return NO
 *		 from the method.
 * \param selector The selector to call on the target. The signature must be
 *		   BOOL (OFStream *stream, void *buffer, size_t size).

 */
- (void)asyncReadIntoBuffer: (void*)buffer
		     length: (size_t)length
		     target: (id)target
		   selector: (SEL)selector;

/**
 * \brief Asyncronously reads exactly the specified length bytes from the
 *	  stream into a buffer.
 *
 * Unlike asyncReadIntoBuffer:length:block, this method does not call the
 * method when less than the specified length has been read - instead, it waits
 * until it got exactly the specified length or the stream has ended.

 *
 * \param buffer The buffer into which the data is read
 * \param length The length of the data that should be read.
 *		 The buffer <i>must</i> be <i>exactly</i> this big!
 * \param target The target on which the selector should be called when the
 *		 data has been received. If the method returns YES, it will be
 *		 called again with the same buffer and exact length when more
 *		 data has been received. If you want the next method in the
 *		 queue to handle the data received next, you need to return NO
 *		 from the method.
 * \param selector The selector to call on the target. The signature must be
 *		   BOOL (OFStream *stream, void *buffer, size_t size).

 */
 - (void)asyncReadIntoBuffer: (void*)buffer
		 exactLength: (size_t)length
		      target: (id)target
		    selector: (SEL)selector;

#ifdef OF_HAVE_BLOCKS







|
>












|
>











|
>







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
 * \param target The target on which the selector should be called when the
 *		 data has been received. If the method returns YES, it will be
 *		 called again with the same buffer and maximum length when more
 *		 data has been received. If you want the next method in the
 *		 queue to handle the data received next, you need to return NO
 *		 from the method.
 * \param selector The selector to call on the target. The signature must be
 *		   BOOL (OFStream *stream, void *buffer, size_t size,
 *		   OFException *exception).
 */
- (void)asyncReadIntoBuffer: (void*)buffer
		     length: (size_t)length
		     target: (id)target
		   selector: (SEL)selector;

/**
 * \brief Asyncronously reads exactly the specified length bytes from the
 *	  stream into a buffer.
 *
 * Unlike asyncReadIntoBuffer:length:block, this method does not call the
 * method when less than the specified length has been read - instead, it waits
 * until it got exactly the specified length, the stream has ended or an
 * exception occurred.
 *
 * \param buffer The buffer into which the data is read
 * \param length The length of the data that should be read.
 *		 The buffer <i>must</i> be <i>exactly</i> this big!
 * \param target The target on which the selector should be called when the
 *		 data has been received. If the method returns YES, it will be
 *		 called again with the same buffer and exact length when more
 *		 data has been received. If you want the next method in the
 *		 queue to handle the data received next, you need to return NO
 *		 from the method.
 * \param selector The selector to call on the target. The signature must be
 *		   BOOL (OFStream *stream, void *buffer, size_t size,
 *		   OFException *exception).
 */
 - (void)asyncReadIntoBuffer: (void*)buffer
		 exactLength: (size_t)length
		      target: (id)target
		    selector: (SEL)selector;

#ifdef OF_HAVE_BLOCKS
188
189
190
191
192
193
194
195

196
197
198
199
200
201
202

/**
 * \brief Asyncronously reads exactly the specified length bytes from the
 *	  stream into a buffer.
 *
 * Unlike asyncReadIntoBuffer:length:block, this method does not invoke the
 * block when less than the specified length has been read - instead, it waits
 * until it got exactly the specified length or the stream has ended.

 *
 * \param buffer The buffer into which the data is read
 * \param length The length of the data that should be read.
 *		 The buffer <i>must</i> be <i>exactly</i> this big!
 * \param block The block to call when the data has been received.
 *		If the block returns YES, it will be called again with the same
 *		buffer and exact length when more data has been received. If







|
>







194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209

/**
 * \brief Asyncronously reads exactly the specified length bytes from the
 *	  stream into a buffer.
 *
 * Unlike asyncReadIntoBuffer:length:block, this method does not invoke the
 * block when less than the specified length has been read - instead, it waits
 * until it got exactly the specified length, the stream has ended or an
 * exception occurred.
 *
 * \param buffer The buffer into which the data is read
 * \param length The length of the data that should be read.
 *		 The buffer <i>must</i> be <i>exactly</i> this big!
 * \param block The block to call when the data has been received.
 *		If the block returns YES, it will be called again with the same
 *		buffer and exact length when more data has been received. If
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567

568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583

584
585
586
587
588
589
590
591

592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
 * \param encoding The encoding used by the stream
 * \return The line that was read, autoreleased, or nil if the end of the
 *	   stream has been reached.
 */
- (OFString*)readLineWithEncoding: (of_string_encoding_t)encoding;

/**
 * \brief Asyncronously reads with the specified encoding until a newline, \\0
 *	  or end of stream occurs.
 *
 * \param target The target on which to call the selector when the data has
 *		 been received. If the method returns YES, it will be called
 *		 again when the next line has been received. If you want the
 *		 next method in the queue to handle the next line, you need to
 *		 return NO from the method
 * \param selector The selector to call on the target. The signature must be
 *		   BOOL (OFStream *stream, OFString *line).

 */
- (void)asyncReadLineWithTarget: (id)target
		       selector: (SEL)selector;

/**
 * \brief Asyncronously reads with the specified encoding until a newline, \\0
 *	  or end of stream occurs.
 *
 * \param encoding The encoding used by the stream
 * \param target The target on which to call the selector when the data has
 *		 been received. If the method returns YES, it will be called
 *		 again when the next line has been received. If you want the
 *		 next method in the queue to handle the next line, you need to
 *		 return NO from the method
 * \param selector The selector to call on the target. The signature must be
 *		   BOOL (OFStream *stream, OFString *line).

 */
- (void)asyncReadLineWithEncoding: (of_string_encoding_t)encoding
			   target: (id)target
			 selector: (SEL)selector;

#ifdef OF_HAVE_BLOCKS
/**
 * \brief Asyncronously reads until a newline, \\0 or end of stream occurs.

 *
 * \param block The block to call when the data has been received.
 *		If the block returns YES, it will be called again when the next
 *		line has been received. If you want the next block in the queue
 *		to handle the next line, you need to return NO from the block.
 */
- (void)asyncReadLineWithBlock: (of_stream_async_read_line_block_t)block;

/**
 * \brief Asyncronously reads with the specified encoding until a newline, \\0
 *	  or end of stream occurs.
 *
 * \param encoding The encoding used by the stream
 * \param block The block to call when the data has been received.
 *		If the block returns YES, it will be called again when the next
 *		line has been received. If you want the next block in the queue
 *		to handle the next line, you need to return NO from the block.
 */







|
|







|
>





|
|








|
>







|
>









|
|







558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
 * \param encoding The encoding used by the stream
 * \return The line that was read, autoreleased, or nil if the end of the
 *	   stream has been reached.
 */
- (OFString*)readLineWithEncoding: (of_string_encoding_t)encoding;

/**
 * \brief Asyncronously reads until a newline, \\0, end of stream or an
 *	  exception occurs.
 *
 * \param target The target on which to call the selector when the data has
 *		 been received. If the method returns YES, it will be called
 *		 again when the next line has been received. If you want the
 *		 next method in the queue to handle the next line, you need to
 *		 return NO from the method
 * \param selector The selector to call on the target. The signature must be
 *		   BOOL (OFStream *stream, OFString *line,
 *		   OFException *exception).
 */
- (void)asyncReadLineWithTarget: (id)target
		       selector: (SEL)selector;

/**
 * \brief Asyncronously reads with the specified encoding until a newline, \\0,
 *	  end of stream or an exception occurs.
 *
 * \param encoding The encoding used by the stream
 * \param target The target on which to call the selector when the data has
 *		 been received. If the method returns YES, it will be called
 *		 again when the next line has been received. If you want the
 *		 next method in the queue to handle the next line, you need to
 *		 return NO from the method
 * \param selector The selector to call on the target. The signature must be
 *		   BOOL (OFStream *stream, OFString *line,
 *		   OFException *exception).
 */
- (void)asyncReadLineWithEncoding: (of_string_encoding_t)encoding
			   target: (id)target
			 selector: (SEL)selector;

#ifdef OF_HAVE_BLOCKS
/**
 * \brief Asyncronously reads until a newline, \\0, end of stream or an
 *	  exception occurs.
 *
 * \param block The block to call when the data has been received.
 *		If the block returns YES, it will be called again when the next
 *		line has been received. If you want the next block in the queue
 *		to handle the next line, you need to return NO from the block.
 */
- (void)asyncReadLineWithBlock: (of_stream_async_read_line_block_t)block;

/**
 * \brief Asyncronously reads with the specified encoding until a newline, \\0,
 *	  end of stream or an exception occurs.
 *
 * \param encoding The encoding used by the stream
 * \param block The block to call when the data has been received.
 *		If the block returns YES, it will be called again when the next
 *		line has been received. If you want the next block in the queue
 *		to handle the next line, you need to return NO from the block.
 */

Modified src/OFTCPSocket.h from [cd657843ce] to [facc710023].

33
34
35
36
37
38
39
40
41

42
43
44
45
46
47
48
# include <ws2tcpip.h>
#endif

@class OFTCPSocket;
@class OFString;

#ifdef OF_HAVE_BLOCKS
typedef void (^of_tcpsocket_async_connect_block_t)(OFTCPSocket*);
typedef BOOL (^of_tcpsocket_async_accept_block_t)(OFTCPSocket*, OFTCPSocket*);

#endif

/**
 * \brief A class which provides functions to create and use TCP sockets.
 *
 * To connect to a server, create a socket and connect it.
 * To create a server, create a socket, bind it and listen on it.







|
|
>







33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# include <ws2tcpip.h>
#endif

@class OFTCPSocket;
@class OFString;

#ifdef OF_HAVE_BLOCKS
typedef void (^of_tcpsocket_async_connect_block_t)(OFTCPSocket*, OFException*);
typedef BOOL (^of_tcpsocket_async_accept_block_t)(OFTCPSocket*, OFTCPSocket*,
    OFException*);
#endif

/**
 * \brief A class which provides functions to create and use TCP sockets.
 *
 * To connect to a server, create a socket and connect it.
 * To create a server, create a socket, bind it and listen on it.
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
 * \brief Asyncronously connect the OFTCPSocket to the specified destination.
 *
 * \param host The host to connect to
 * \param port The port on the host to connect to
 * \param target The target on which to call the selector once the connection
 *		 has been established
 * \param selector The selector to call on the target. The signature must be
 *		   void (OFTCPSocket *socket).
 */
- (void)asyncConnectToHost: (OFString*)host
		      port: (uint16_t)port
		    target: (id)target
		  selector: (SEL)selector;

#ifdef OF_HAVE_BLOCKS







|







134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
 * \brief Asyncronously connect the OFTCPSocket to the specified destination.
 *
 * \param host The host to connect to
 * \param port The port on the host to connect to
 * \param target The target on which to call the selector once the connection
 *		 has been established
 * \param selector The selector to call on the target. The signature must be
 *		   void (OFTCPSocket *socket, OFException *exception).
 */
- (void)asyncConnectToHost: (OFString*)host
		      port: (uint16_t)port
		    target: (id)target
		  selector: (SEL)selector;

#ifdef OF_HAVE_BLOCKS
192
193
194
195
196
197
198
199

200
201
202
203
204
205
206
 * \brief Asyncronously ccept an incoming connection.
 *
 * \param target The target on which to execute the selector when a new
 *		 connection has been accepted. The method returns whether the
 *		 next incoming connection should be accepted by the specified
 *		 block as well.
 * \param selector The selector to call on the target. The signature must be
 *		   BOOL (OFTCPSocket *socket, OFTCPSocket *acceptedSocket).

 */
- (void)asyncAcceptWithTarget: (id)target
		     selector: (SEL)selector;

#ifdef OF_HAVE_BLOCKS
/**
 * \brief Asyncronously ccept an incoming connection.







|
>







193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
 * \brief Asyncronously ccept an incoming connection.
 *
 * \param target The target on which to execute the selector when a new
 *		 connection has been accepted. The method returns whether the
 *		 next incoming connection should be accepted by the specified
 *		 block as well.
 * \param selector The selector to call on the target. The signature must be
 *		   BOOL (OFTCPSocket *socket, OFTCPSocket *acceptedSocket,
 *		   OFException *exception).
 */
- (void)asyncAcceptWithTarget: (id)target
		     selector: (SEL)selector;

#ifdef OF_HAVE_BLOCKS
/**
 * \brief Asyncronously ccept an incoming connection.

Modified src/OFTCPSocket.m from [0f410ad5c9] to [29f21ecbe7].

84
85
86
87
88
89
90

91
92
93
94
95
96
97
	OFString *host;
	uint16_t port;
	id target;
	SEL selector;
#ifdef OF_HAVE_BLOCKS
	of_tcpsocket_async_connect_block_t connectBlock;
#endif

}

- initWithSourceThread: (OFThread*)sourceThread
		socket: (OFTCPSocket*)socket
		  host: (OFString*)host
		  port: (uint16_t)port
		target: (id)target







>







84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
	OFString *host;
	uint16_t port;
	id target;
	SEL selector;
#ifdef OF_HAVE_BLOCKS
	of_tcpsocket_async_connect_block_t connectBlock;
#endif
	OFException *exception;
}

- initWithSourceThread: (OFThread*)sourceThread
		socket: (OFTCPSocket*)socket
		  host: (OFString*)host
		  port: (uint16_t)port
		target: (id)target
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
	[sourceThread release];
	[sock release];
	[host release];
	[target release];
#ifdef OF_HAVE_BLOCKS
	[connectBlock release];
#endif


	[super dealloc];
}

- (void)didConnect
{
	[self join];

#ifdef OF_HAVE_BLOCKS
	if (connectBlock != NULL)
		connectBlock(sock);
	else {
#endif
		void (*func)(id, SEL, OFTCPSocket*) =
		    (void(*)(id, SEL, OFTCPSocket*))[target
		    methodForSelector: selector];

		func(target, selector, sock);
#ifdef OF_HAVE_BLOCKS
	}
#endif
}

- (id)main
{
	void *pool = objc_autoreleasePoolPush();


	[sock connectToHost: host
		       port: port];




	[self performSelector: @selector(didConnect)
		     onThread: sourceThread
		waitUntilDone: NO];

	objc_autoreleasePoolPop(pool);








>










|


|
|


|









>
|
|
>
>
>







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
	[sourceThread release];
	[sock release];
	[host release];
	[target release];
#ifdef OF_HAVE_BLOCKS
	[connectBlock release];
#endif
	[exception release];

	[super dealloc];
}

- (void)didConnect
{
	[self join];

#ifdef OF_HAVE_BLOCKS
	if (connectBlock != NULL)
		connectBlock(sock, exception);
	else {
#endif
		void (*func)(id, SEL, OFTCPSocket*, OFException*) =
		    (void(*)(id, SEL, OFTCPSocket*, OFException*))[target
		    methodForSelector: selector];

		func(target, selector, sock, exception);
#ifdef OF_HAVE_BLOCKS
	}
#endif
}

- (id)main
{
	void *pool = objc_autoreleasePoolPush();

	@try {
		[sock connectToHost: host
			       port: port];
	} @catch (OFException *e) {
		exception = [[e retain] autorelease];
	}

	[self performSelector: @selector(didConnect)
		     onThread: sourceThread
		waitUntilDone: NO];

	objc_autoreleasePoolPop(pool);