︙ | | | ︙ | |
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
{
@public
id _target;
SEL _selector;
id _context;
}
- (bool)handleForObject: (id)object;
@end
@interface OFRunLoop_ReadQueueItem: OFRunLoop_QueueItem
{
@public
# ifdef OF_HAVE_BLOCKS
of_stream_async_read_block_t _block;
|
|
|
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
{
@public
id _target;
SEL _selector;
id _context;
}
- (bool)handleObject: (id)object;
@end
@interface OFRunLoop_ReadQueueItem: OFRunLoop_QueueItem
{
@public
# ifdef OF_HAVE_BLOCKS
of_stream_async_read_block_t _block;
|
︙ | | | ︙ | |
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
@public
# ifdef OF_HAVE_BLOCKS
of_stream_async_read_line_block_t _block;
# endif
of_string_encoding_t _encoding;
}
@end
@interface OFRunLoop_AcceptQueueItem: OFRunLoop_QueueItem
{
@public
# ifdef OF_HAVE_BLOCKS
of_tcp_socket_async_accept_block_t _block;
# endif
|
>
>
>
>
>
>
>
>
>
>
>
|
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
@public
# ifdef OF_HAVE_BLOCKS
of_stream_async_read_line_block_t _block;
# endif
of_string_encoding_t _encoding;
}
@end
@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;
}
@end
@interface OFRunLoop_AcceptQueueItem: OFRunLoop_QueueItem
{
@public
# ifdef OF_HAVE_BLOCKS
of_tcp_socket_async_accept_block_t _block;
# endif
|
︙ | | | ︙ | |
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
|
# endif
void *_buffer;
size_t _length;
}
@end
@implementation OFRunLoop_QueueItem
- (bool)handleForObject: (id)object
{
OF_UNRECOGNIZED_SELECTOR
}
- (void)dealloc
{
[_target release];
[_context release];
[super dealloc];
}
@end
@implementation OFRunLoop_ReadQueueItem
- (bool)handleForObject: (id)object
{
size_t length;
id exception = nil;
@try {
length = [object readIntoBuffer: _buffer
length: _length];
|
|
|
|
111
112
113
114
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
|
# endif
void *_buffer;
size_t _length;
}
@end
@implementation OFRunLoop_QueueItem
- (bool)handleObject: (id)object
{
OF_UNRECOGNIZED_SELECTOR
}
- (void)dealloc
{
[_target release];
[_context release];
[super dealloc];
}
@end
@implementation OFRunLoop_ReadQueueItem
- (bool)handleObject: (id)object
{
size_t length;
id exception = nil;
@try {
length = [object readIntoBuffer: _buffer
length: _length];
|
︙ | | | ︙ | |
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
[super dealloc];
}
# endif
@end
@implementation OFRunLoop_ExactReadQueueItem
- (bool)handleForObject: (id)object
{
size_t length;
id exception = nil;
@try {
length = [object readIntoBuffer: (char *)_buffer + _readLength
length: _exactLength - _readLength];
|
|
|
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
[super dealloc];
}
# endif
@end
@implementation OFRunLoop_ExactReadQueueItem
- (bool)handleObject: (id)object
{
size_t length;
id exception = nil;
@try {
length = [object readIntoBuffer: (char *)_buffer + _readLength
length: _exactLength - _readLength];
|
︙ | | | ︙ | |
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
[super dealloc];
}
# endif
@end
@implementation OFRunLoop_ReadLineQueueItem
- (bool)handleForObject: (id)object
{
OFString *line;
id exception = nil;
@try {
line = [object tryReadLineWithEncoding: _encoding];
} @catch (id e) {
|
|
|
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
[super dealloc];
}
# endif
@end
@implementation OFRunLoop_ReadLineQueueItem
- (bool)handleObject: (id)object
{
OFString *line;
id exception = nil;
@try {
line = [object tryReadLineWithEncoding: _encoding];
} @catch (id e) {
|
︙ | | | ︙ | |
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
{
[_block release];
[super dealloc];
}
# endif
@end
@implementation OFRunLoop_AcceptQueueItem
- (bool)handleForObject: (id)object
{
OFTCPSocket *newSocket;
id exception = nil;
@try {
newSocket = [object accept];
} @catch (id e) {
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
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
|
{
[_block release];
[super dealloc];
}
# endif
@end
@implementation OFRunLoop_WriteQueueItem
# ifdef OF_HAVE_BLOCKS
- (bool)handleObject: (id)object
{
size_t length;
id exception = nil;
@try {
length = [object writeBuffer: (char *)_buffer + _writtenLength
length: _length - _writtenLength];
} @catch (id e) {
length = 0;
exception = e;
}
_writtenLength += length;
if (_writtenLength != _length && exception == nil)
return true;
# ifdef OF_HAVE_BLOCKS
if (_block != NULL)
_block(object, _buffer, _writtenLength, exception);
else {
# endif
void (*func)(id, SEL, OFStream *, const void *, size_t, id,
id) = (void (*)(id, SEL, OFStream *, const void *, size_t,
id, id))[_target methodForSelector: _selector];
func(_target, _selector, object, _buffer, _writtenLength,
_context, exception);
# ifdef OF_HAVE_BLOCKS
}
# endif
return false;
}
- (void)dealloc
{
[_block release];
[super dealloc];
}
# endif
@end
@implementation OFRunLoop_AcceptQueueItem
- (bool)handleObject: (id)object
{
OFTCPSocket *newSocket;
id exception = nil;
@try {
newSocket = [object accept];
} @catch (id e) {
|
︙ | | | ︙ | |
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
[super dealloc];
}
# endif
@end
@implementation OFRunLoop_UDPReceiveQueueItem
- (bool)handleForObject: (id)object
{
size_t length;
of_udp_socket_address_t address;
id exception = nil;
@try {
length = [object receiveIntoBuffer: _buffer
|
|
|
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
[super dealloc];
}
# endif
@end
@implementation OFRunLoop_UDPReceiveQueueItem
- (bool)handleObject: (id)object
{
size_t length;
of_udp_socket_address_t address;
id exception = nil;
@try {
length = [object receiveIntoBuffer: _buffer
|
︙ | | | ︙ | |
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
addObjectForReading: object]; \
\
queueItem = [[[type alloc] init] autorelease]; \
code \
[queue appendObject: queueItem]; \
\
objc_autoreleasePoolPop(pool);
+ (void)of_addAsyncReadForStream: (OFStream *)stream
buffer: (void *)buffer
length: (size_t)length
target: (id)target
selector: (SEL)selector
context: (id)context
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
|
addObjectForReading: object]; \
\
queueItem = [[[type alloc] init] autorelease]; \
code \
[queue appendObject: queueItem]; \
\
objc_autoreleasePoolPop(pool);
# define ADD_WRITE(type, object, code) \
void *pool = objc_autoreleasePoolPush(); \
OFRunLoop *runLoop = [self currentRunLoop]; \
OFList *queue = [runLoop->_writeQueues objectForKey: object]; \
type *queueItem; \
\
if (queue == nil) { \
queue = [OFList list]; \
[runLoop->_writeQueues setObject: queue \
forKey: object]; \
} \
\
if ([queue count] == 0) \
[runLoop->_kernelEventObserver \
addObjectForWriting: object]; \
\
queueItem = [[[type alloc] init] autorelease]; \
code \
[queue appendObject: queueItem]; \
\
objc_autoreleasePoolPop(pool);
+ (void)of_addAsyncReadForStream: (OFStream *)stream
buffer: (void *)buffer
length: (size_t)length
target: (id)target
selector: (SEL)selector
context: (id)context
|
︙ | | | ︙ | |
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
ADD_READ(OFRunLoop_ReadLineQueueItem, stream, {
queueItem->_target = [target retain];
queueItem->_selector = selector;
queueItem->_context = [context retain];
queueItem->_encoding = encoding;
})
}
+ (void)of_addAsyncAcceptForTCPSocket: (OFTCPSocket *)stream
target: (id)target
selector: (SEL)selector
context: (id)context
{
ADD_READ(OFRunLoop_AcceptQueueItem, stream, {
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
526
527
528
529
530
|
ADD_READ(OFRunLoop_ReadLineQueueItem, stream, {
queueItem->_target = [target retain];
queueItem->_selector = selector;
queueItem->_context = [context retain];
queueItem->_encoding = encoding;
})
}
+ (void)of_addAsyncWriteForStream: (OFStream *)stream
buffer: (const void *)buffer
length: (size_t)length
target: (id)target
selector: (SEL)selector
context: (id)context
{
ADD_WRITE(OFRunLoop_WriteQueueItem, stream, {
queueItem->_target = [target retain];
queueItem->_selector = selector;
queueItem->_context = [context retain];
queueItem->_buffer = buffer;
queueItem->_length = length;
})
}
+ (void)of_addAsyncAcceptForTCPSocket: (OFTCPSocket *)stream
target: (id)target
selector: (SEL)selector
context: (id)context
{
ADD_READ(OFRunLoop_AcceptQueueItem, stream, {
|
︙ | | | ︙ | |
485
486
487
488
489
490
491
492
493
494
495
496
497
498
|
block: (of_stream_async_read_line_block_t)block
{
ADD_READ(OFRunLoop_ReadLineQueueItem, stream, {
queueItem->_block = [block copy];
queueItem->_encoding = encoding;
})
}
+ (void)of_addAsyncAcceptForTCPSocket: (OFTCPSocket *)stream
block: (of_tcp_socket_async_accept_block_t)block
{
ADD_READ(OFRunLoop_AcceptQueueItem, stream, {
queueItem->_block = [block copy];
})
|
>
>
>
>
>
>
>
>
>
>
>
>
|
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
|
block: (of_stream_async_read_line_block_t)block
{
ADD_READ(OFRunLoop_ReadLineQueueItem, stream, {
queueItem->_block = [block copy];
queueItem->_encoding = encoding;
})
}
+ (void)of_addAsyncWriteForStream: (OFStream *)stream
buffer: (const void *)buffer
length: (size_t)length
block: (of_stream_async_write_block_t)block
{
ADD_WRITE(OFRunLoop_WriteQueueItem, stream, {
queueItem->_block = [block copy];
queueItem->_buffer = buffer;
queueItem->_length = length;
})
}
+ (void)of_addAsyncAcceptForTCPSocket: (OFTCPSocket *)stream
block: (of_tcp_socket_async_accept_block_t)block
{
ADD_READ(OFRunLoop_AcceptQueueItem, stream, {
queueItem->_block = [block copy];
})
|
︙ | | | ︙ | |
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
|
queueItem->_buffer = buffer;
queueItem->_length = length;
queueItem->_block = [block copy];
})
}
# endif
# undef ADD_READ
+ (void)of_cancelAsyncRequestsForObject: (id)object
{
void *pool = objc_autoreleasePoolPush();
OFRunLoop *runLoop = [self currentRunLoop];
OFList *queue;
if ((queue = [runLoop->_readQueues objectForKey: object]) != nil) {
assert([queue count] > 0);
/*
* Clear the queue now, in case this has been called from a
* handler, as otherwise, we'd do the cleanups below twice.
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
|
queueItem->_buffer = buffer;
queueItem->_length = length;
queueItem->_block = [block copy];
})
}
# endif
# undef ADD_READ
# undef ADD_WRITE
+ (void)of_cancelAsyncRequestsForObject: (id)object
{
void *pool = objc_autoreleasePoolPush();
OFRunLoop *runLoop = [self currentRunLoop];
OFList *queue;
if ((queue = [runLoop->_writeQueues objectForKey: object]) != nil) {
assert([queue count] > 0);
/*
* Clear the queue now, in case this has been called from a
* handler, as otherwise, we'd do the cleanups below twice.
*/
[queue removeAllObjects];
[runLoop->_kernelEventObserver removeObjectForWriting: object];
[runLoop->_writeQueues removeObjectForKey: object];
}
if ((queue = [runLoop->_readQueues objectForKey: object]) != nil) {
assert([queue count] > 0);
/*
* Clear the queue now, in case this has been called from a
* handler, as otherwise, we'd do the cleanups below twice.
|
︙ | | | ︙ | |
547
548
549
550
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
|
#endif
#if defined(OF_HAVE_SOCKETS)
_kernelEventObserver = [[OFKernelEventObserver alloc] init];
[_kernelEventObserver setDelegate: self];
_readQueues = [[OFMutableDictionary alloc] init];
#elif defined(OF_HAVE_THREADS)
_condition = [[OFCondition alloc] init];
#endif
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)dealloc
{
[_timersQueue release];
#ifdef OF_HAVE_THREADS
[_timersQueueLock release];
#endif
#if defined(OF_HAVE_SOCKETS)
[_kernelEventObserver release];
[_readQueues release];
#elif defined(OF_HAVE_THREADS)
[_condition release];
#endif
[super dealloc];
}
|
>
>
|
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
|
#endif
#if defined(OF_HAVE_SOCKETS)
_kernelEventObserver = [[OFKernelEventObserver alloc] init];
[_kernelEventObserver setDelegate: self];
_readQueues = [[OFMutableDictionary alloc] init];
_writeQueues = [[OFMutableDictionary alloc] init];
#elif defined(OF_HAVE_THREADS)
_condition = [[OFCondition alloc] init];
#endif
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)dealloc
{
[_timersQueue release];
#ifdef OF_HAVE_THREADS
[_timersQueueLock release];
#endif
#if defined(OF_HAVE_SOCKETS)
[_kernelEventObserver release];
[_readQueues release];
[_writeQueues release];
#elif defined(OF_HAVE_THREADS)
[_condition release];
#endif
[super dealloc];
}
|
︙ | | | ︙ | |
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
|
*/
OFList OF_GENERIC(OF_KINDOF(OFRunLoop_ReadQueueItem *)) *queue =
[[_readQueues objectForKey: object] retain];
assert(queue != nil);
@try {
if (![[queue firstObject] handleForObject: object]) {
of_list_object_t *listObject = [queue firstListObject];
/*
* The handler might have called -[cancelAsyncRequests]
* so that our queue is now empty, in which case we
* should do nothing.
*/
if (listObject != NULL) {
[queue removeListObject: listObject];
if ([queue count] == 0) {
[_kernelEventObserver
removeObjectForReading: object];
[_readQueues
removeObjectForKey: object];
}
}
}
} @finally {
[queue release];
}
}
#endif
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
|
*/
OFList OF_GENERIC(OF_KINDOF(OFRunLoop_ReadQueueItem *)) *queue =
[[_readQueues objectForKey: object] retain];
assert(queue != nil);
@try {
if (![[queue firstObject] handleObject: object]) {
of_list_object_t *listObject = [queue firstListObject];
/*
* The handler might have called -[cancelAsyncRequests]
* so that our queue is now empty, in which case we
* should do nothing.
*/
if (listObject != NULL) {
[queue removeListObject: listObject];
if ([queue count] == 0) {
[_kernelEventObserver
removeObjectForReading: object];
[_readQueues
removeObjectForKey: object];
}
}
}
} @finally {
[queue release];
}
}
- (void)objectIsReadyForWriting: (id)object
{
/*
* Retain the queue so that it doesn't disappear from us because the
* handler called -[cancelAsyncRequests].
*/
OFList OF_GENERIC(OF_KINDOF(OFRunLoop_WriteQueueItem *)) *queue =
[[_writeQueues objectForKey: object] retain];
assert(queue != nil);
@try {
if (![[queue firstObject] handleObject: object]) {
of_list_object_t *listObject = [queue firstListObject];
/*
* The handler might have called -[cancelAsyncRequests]
* so that our queue is now empty, in which case we
* should do nothing.
*/
if (listObject != NULL) {
[queue removeListObject: listObject];
if ([queue count] == 0) {
[_kernelEventObserver
removeObjectForWriting: object];
[_writeQueues
removeObjectForKey: object];
}
}
}
} @finally {
[queue release];
}
}
#endif
|
︙ | | | ︙ | |
︙ | | | ︙ | |
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
* occurred
* @param exception An exception which occurred while reading or `nil` on
* success
* @return A bool whether the same block should be used for the next read
*/
typedef bool (^of_stream_async_read_line_block_t)(OFStream *stream,
OFString *_Nullable line, id _Nullable exception);
#endif
/*!
* @class OFStream OFStream.h ObjFW/OFStream.h
*
* @brief A base class for different types of streams.
*
|
>
>
>
>
>
>
>
>
>
>
>
>
|
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
* occurred
* @param exception An exception which occurred while reading or `nil` on
* success
* @return A bool whether the same block should be used for the next read
*/
typedef bool (^of_stream_async_read_line_block_t)(OFStream *stream,
OFString *_Nullable line, id _Nullable exception);
/*!
* @brief A block which is called when data was written to the stream.
*
* @param stream The stream to which data was written
* @param buffer The buffer which was written to the stream
* @param length The length of the data that bas been written
* @param exception An exception which occurred while reading or `nil` on
* success
*/
typedef void (^of_stream_async_write_block_t)(OFStream *stream,
const void *buffer, size_t length, id _Nullable exception);
#endif
/*!
* @class OFStream OFStream.h ObjFW/OFStream.h
*
* @brief A base class for different types of streams.
*
|
︙ | | | ︙ | |
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
|
- (void)flushWriteBuffer;
/*!
* @brief Writes from a buffer into the stream.
*
* @param buffer The buffer from which the data is written into the stream
* @param length The length of the data that should be written
*/
- (void)writeBuffer: (const void *)buffer
length: (size_t)length;
/*!
* @brief Writes a uint8_t into the stream.
*
* @param int8 A uint8_t
*/
- (void)writeInt8: (uint8_t)int8;
|
>
>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
|
- (void)flushWriteBuffer;
/*!
* @brief Writes from a buffer into the stream.
*
* @param buffer The buffer from which the data is written into the stream
* @param length The length of the data that should be written
* @return The number of bytes written. This can only differ from the specified
* length in non-blocking mode.
*/
- (size_t)writeBuffer: (const void *)buffer
length: (size_t)length;
#ifdef OF_HAVE_SOCKETS
/*!
* @brief Asynchronously writes a buffer into the stream.
*
* @note The stream must implement @ref fileDescriptorForWriting and return a
* valid file descriptor in order for this to work!
*
* @param buffer The buffer from which the data is written into the stream. The
* buffer needs to be valid until the write request is completed!
* @param length The length of the data that should be written
* @param target The target on which the selector should be called when the data
* has been written.
* @param selector The selector to call on the target. The signature must be
* `void (OFStream *stream, const void *buffer, size_t length,
* id context, id exception)`.
*/
- (void)asyncWriteBuffer: (const void *)buffer
length: (size_t)length
target: (id)target
selector: (SEL)selector
context: (nullable id)context;
# ifdef OF_HAVE_BLOCKS
/*!
* @brief Asynchronously writes a buffer into the stream.
*
* @note The stream must implement @ref fileDescriptorForWriting and return a
* valid file descriptor in order for this to work!
*
* @param buffer The buffer from which the data is written into the stream. The
* buffer needs to be valid until the write request is completed!
* @param length The length of the data that should be written
* @param block The block to call when the data has been written
*/
- (void)asyncWriteBuffer: (const void *)buffer
length: (size_t)length
block: (of_stream_async_write_block_t)block;
# endif
#endif
/*!
* @brief Writes a uint8_t into the stream.
*
* @param int8 A uint8_t
*/
- (void)writeInt8: (uint8_t)int8;
|
︙ | | | ︙ | |
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
|
* @warning Do not call this directly!
*
* @note Override this method with your actual write implementation when
* subclassing!
*
* @param buffer The buffer with the data to write
* @param length The length of the data to write
*/
- (void)lowlevelWriteBuffer: (const void *)buffer
length: (size_t)length;
/*!
* @brief Returns whether the lowlevel is at the end of the stream.
*
* @warning Do not call this directly!
*
* @note Override this method with your actual end of stream checking
|
>
|
|
|
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
|
* @warning Do not call this directly!
*
* @note Override this method with your actual write implementation when
* subclassing!
*
* @param buffer The buffer with the data to write
* @param length The length of the data to write
* @return The number of bytes written
*/
- (size_t)lowlevelWriteBuffer: (const void *)buffer
length: (size_t)length;
/*!
* @brief Returns whether the lowlevel is at the end of the stream.
*
* @warning Do not call this directly!
*
* @note Override this method with your actual end of stream checking
|
︙ | | | ︙ | |
︙ | | | ︙ | |
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFNotImplementedException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"
#import "OFSetOptionFailedException.h"
#import "OFTruncatedDataException.h"
#import "of_asprintf.h"
#define MIN_READ_SIZE 512
@implementation OFStream
@synthesize of_waitingForDelimiter = _waitingForDelimiter;
|
>
|
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFNotImplementedException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"
#import "OFSetOptionFailedException.h"
#import "OFTruncatedDataException.h"
#import "OFWriteFailedException.h"
#import "of_asprintf.h"
#define MIN_READ_SIZE 512
@implementation OFStream
@synthesize of_waitingForDelimiter = _waitingForDelimiter;
|
︙ | | | ︙ | |
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
- (size_t)lowlevelReadIntoBuffer: (void *)buffer
length: (size_t)length
{
OF_UNRECOGNIZED_SELECTOR
}
- (void)lowlevelWriteBuffer: (const void *)buffer
length: (size_t)length
{
OF_UNRECOGNIZED_SELECTOR
}
- copy
{
return [self retain];
|
|
|
|
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
- (size_t)lowlevelReadIntoBuffer: (void *)buffer
length: (size_t)length
{
OF_UNRECOGNIZED_SELECTOR
}
- (size_t)lowlevelWriteBuffer: (const void *)buffer
length: (size_t)length
{
OF_UNRECOGNIZED_SELECTOR
}
- copy
{
return [self retain];
|
︙ | | | ︙ | |
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
|
length: _writeBufferLength];
[self freeMemory: _writeBuffer];
_writeBuffer = NULL;
_writeBufferLength = 0;
}
- (void)writeBuffer: (const void *)buffer
length: (size_t)length
{
if (!_writeBuffered)
[self lowlevelWriteBuffer: buffer
length: length];
else {
_writeBuffer = [self resizeMemory: _writeBuffer
size: _writeBufferLength + length];
memcpy(_writeBuffer + _writeBufferLength, buffer, length);
_writeBufferLength += length;
}
}
- (void)writeInt8: (uint8_t)int8
{
[self writeBuffer: (char *)&int8
length: 1];
}
|
|
|
|
|
|
>
>
>
>
>
>
>
>
>
|
|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
|
length: _writeBufferLength];
[self freeMemory: _writeBuffer];
_writeBuffer = NULL;
_writeBufferLength = 0;
}
- (size_t)writeBuffer: (const void *)buffer
length: (size_t)length
{
if (!_writeBuffered) {
size_t bytesWritten = [self lowlevelWriteBuffer: buffer
length: length];
if (_blocking && bytesWritten < length)
@throw [OFWriteFailedException
exceptionWithObject: self
requestedLength: length
bytesWritten: bytesWritten
errNo: 0];
return bytesWritten;
} else {
_writeBuffer = [self resizeMemory: _writeBuffer
size: _writeBufferLength + length];
memcpy(_writeBuffer + _writeBufferLength, buffer, length);
_writeBufferLength += length;
return length;
}
}
#ifdef OF_HAVE_SOCKETS
- (void)asyncWriteBuffer: (const void *)buffer
length: (size_t)length
target: (id)target
selector: (SEL)selector
context: (id)context
{
[OFRunLoop of_addAsyncWriteForStream: self
buffer: buffer
length: length
target: target
selector: selector
context: context];
}
# ifdef OF_HAVE_BLOCKS
- (void)asyncWriteBuffer: (const void *)buffer
length: (size_t)length
block: (of_stream_async_write_block_t)block
{
[OFRunLoop of_addAsyncWriteForStream: self
buffer: buffer
length: length
block: block];
}
# endif
#endif
- (void)writeInt8: (uint8_t)int8
{
[self writeBuffer: (char *)&int8
length: 1];
}
|
︙ | | | ︙ | |