ObjFW  Diff

Differences From Artifact [2b70c16414]:

To Artifact [7000e17b8e]:


18
19
20
21
22
23
24

25
26
27
28
29
30
31
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32







+







#include "config.h"

#include <assert.h>
#include <errno.h>

#import "OFRunLoop.h"
#import "OFRunLoop+Private.h"
#import "OFData.h"
#import "OFDictionary.h"
#ifdef OF_HAVE_SOCKETS
# import "OFKernelEventObserver.h"
# import "OFTCPSocket.h"
# import "OFTCPSocket+Private.h"
#endif
#import "OFThread.h"
114
115
116
117
118
119
120
121
122


123
124
125
126
127
128
129
115
116
117
118
119
120
121


122
123
124
125
126
127
128
129
130







-
-
+
+








@interface OFRunLoop_WriteQueueItem: OFRunLoop_QueueItem
{
@public
# ifdef OF_HAVE_BLOCKS
	of_stream_async_write_block_t _block;
# endif
	const void *_buffer;
	size_t _length, _writtenLength;
	OFData *_data;
	size_t _writtenLength;
}
@end

@interface OFRunLoop_ConnectQueueItem: OFRunLoop_QueueItem
@end

@interface OFRunLoop_AcceptQueueItem: OFRunLoop_QueueItem
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
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
517
518

519
520
521
522
523
524
525







+
+


+
+
-
-
+
+







-
+




-
+

-
+


+
+
+
+





-
+


-
-
-
+
+
+


-
+


+
+
+
+







-


+
+

+



-







@end

@implementation OFRunLoop_WriteQueueItem
- (bool)handleObject: (id)object
{
	size_t length;
	id exception = nil;
	size_t dataLength = [_data count] * [_data itemSize];
	OFData *newData, *oldData;

	@try {
		const char *dataItems = [_data items];

		length = [object writeBuffer: (char *)_buffer + _writtenLength
				      length: _length - _writtenLength];
		length = [object writeBuffer: dataItems + _writtenLength
				      length: dataLength - _writtenLength];
	} @catch (id e) {
		length = 0;
		exception = e;
	}

	_writtenLength += length;

	if (_writtenLength != _length && exception == nil)
	if (_writtenLength != dataLength && exception == nil)
		return true;

# ifdef OF_HAVE_BLOCKS
	if (_block != NULL) {
		_length = _block(object, &_buffer, _writtenLength, exception);
		newData = _block(object, _data, _writtenLength, exception);

		if (_length == 0)
		if (newData == nil)
			return false;

		oldData = _data;
		_data = [newData copy];
		[oldData release];

		_writtenLength = 0;
		return true;
	} else {
# endif
		if (![_delegate respondsToSelector:
		    @selector(stream:didWriteBuffer:length:exception:)])
		    @selector(stream:didWriteData:bytesWritten:exception:)])
			return false;

		_length = [_delegate stream: object
			     didWriteBuffer: &_buffer
				     length: _length
		newData = [_delegate stream: object
			       didWriteData: _data
			       bytesWritten: _writtenLength
				  exception: exception];

		if (_length == 0)
		if (newData == nil)
			return false;

		oldData = _data;
		_data = [newData copy];
		[oldData release];

		_writtenLength = 0;
		return true;
# ifdef OF_HAVE_BLOCKS
	}
# endif
}

# ifdef OF_HAVE_BLOCKS
- (void)dealloc
{
	[_data release];
# ifdef OF_HAVE_BLOCKS
	[_block release];
# endif

	[super dealloc];
}
# endif
@end

@implementation OFRunLoop_ConnectQueueItem
- (bool)handleObject: (id)object
{
	id exception = nil;
	int errNo;
767
768
769
770
771
772
773
774
775

776
777
778
779
780
781

782
783
784
785
786
787
788
789
781
782
783
784
785
786
787


788
789
790
791
792
793

794

795
796
797
798
799
800
801







-
-
+





-
+
-







		queueItem->_delegate = [delegate retain];
		queueItem->_encoding = encoding;
	})
}

+ (void)of_addAsyncWriteForStream: (OFStream <OFReadyForWritingObserving> *)
				       stream
			   buffer: (const void *)buffer
			   length: (size_t)length
			     data: (OFData *)data
			     mode: (of_run_loop_mode_t)mode
			 delegate: (id <OFStreamDelegate>)delegate
{
	ADD_WRITE(OFRunLoop_WriteQueueItem, stream, mode, {
		queueItem->_delegate = [delegate retain];
		queueItem->_buffer = buffer;
		queueItem->_data = [data copy];
		queueItem->_length = length;
	})
}

+ (void)of_addAsyncConnectForTCPSocket: (OFTCPSocket *)stream
				  mode: (of_run_loop_mode_t)mode
			      delegate: (id <OFTCPSocketDelegate_Private>)
					    delegate
869
870
871
872
873
874
875
876
877

878
879
880
881

882
883
884
885
886
887
888
889
890
891
881
882
883
884
885
886
887


888
889
890
891
892
893
894


895
896
897
898
899
900
901







-
-
+




+

-
-







		queueItem->_block = [block copy];
		queueItem->_encoding = encoding;
	})
}

+ (void)of_addAsyncWriteForStream: (OFStream <OFReadyForWritingObserving> *)
				       stream
			   buffer: (const void *)buffer
			   length: (size_t)length
			     data: (OFData *)data
			     mode: (of_run_loop_mode_t)mode
			    block: (of_stream_async_write_block_t)block
{
	ADD_WRITE(OFRunLoop_WriteQueueItem, stream, mode, {
		queueItem->_data = [data copy];
		queueItem->_block = [block copy];
		queueItem->_buffer = buffer;
		queueItem->_length = length;
	})
}

+ (void)of_addAsyncAcceptForTCPSocket: (OFTCPSocket *)stream
				 mode: (of_run_loop_mode_t)mode
				block: (of_tcp_socket_async_accept_block_t)block
{