Differences From Artifact [7000e17b8e]:
- File
src/OFRunLoop.m
— part of check-in
[f218986f51]
at
2018-12-18 14:14:25
on branch trunk
— Use OFData instead of a buffer for async writes
This avoids the entire problem of keeping the buffer alive until the
write finished. (user: js, size: 27395) [annotate] [blame] [check-ins using]
To Artifact [e91294dd59]:
- File src/OFRunLoop.m — part of check-in [6b35b78f94] at 2018-12-18 16:41:11 on branch trunk — Add -[OFStream asyncWriteString:] (user: js, size: 29968) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
109 110 111 112 113 114 115 | # ifdef OF_HAVE_BLOCKS of_stream_async_read_line_block_t _block; # endif of_string_encoding_t _encoding; } @end | | > > > > > > > > > > > | | > | 109 110 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 141 | # ifdef OF_HAVE_BLOCKS of_stream_async_read_line_block_t _block; # endif of_string_encoding_t _encoding; } @end @interface OFRunLoop_WriteDataQueueItem: OFRunLoop_QueueItem { @public # ifdef OF_HAVE_BLOCKS of_stream_async_write_data_block_t _block; # endif OFData *_data; size_t _writtenLength; } @end @interface OFRunLoop_WriteStringQueueItem: OFRunLoop_QueueItem { @public # ifdef OF_HAVE_BLOCKS of_stream_async_write_string_block_t _block; # endif OFString *_string; of_string_encoding_t _encoding; size_t _writtenLength; } @end @interface OFRunLoop_ConnectQueueItem: OFRunLoop_QueueItem @end |
︙ | ︙ | |||
246 247 248 249 250 251 252 | - (void)objectIsReadyForWriting: (id)object { /* * Retain the queue so that it doesn't disappear from us because the * handler called -[cancelAsyncRequests]. */ | < | | 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | - (void)objectIsReadyForWriting: (id)object { /* * Retain the queue so that it doesn't disappear from us because the * handler called -[cancelAsyncRequests]. */ OFList *queue = [[_writeQueues objectForKey: object] retain]; assert(queue != nil); @try { if (![[queue firstObject] handleObject: object]) { of_list_object_t *listObject = [queue firstListObject]; |
︙ | ︙ | |||
442 443 444 445 446 447 448 | [_block release]; [super dealloc]; } # endif @end | | | 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 | [_block release]; [super dealloc]; } # endif @end @implementation OFRunLoop_WriteDataQueueItem - (bool)handleObject: (id)object { size_t length; id exception = nil; size_t dataLength = [_data count] * [_data itemSize]; OFData *newData, *oldData; |
︙ | ︙ | |||
509 510 511 512 513 514 515 516 517 518 519 520 521 522 | - (void)dealloc { [_data release]; # ifdef OF_HAVE_BLOCKS [_block release]; # endif [super dealloc]; } @end @implementation OFRunLoop_ConnectQueueItem - (bool)handleObject: (id)object | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 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 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 | - (void)dealloc { [_data release]; # ifdef OF_HAVE_BLOCKS [_block release]; # endif [super dealloc]; } @end @implementation OFRunLoop_WriteStringQueueItem - (bool)handleObject: (id)object { size_t length; id exception = nil; size_t cStringLength = [_string cStringLengthWithEncoding: _encoding]; OFString *newString, *oldString; @try { const char *cString = [_string cStringWithEncoding: _encoding]; length = [object writeBuffer: cString + _writtenLength length: cStringLength - _writtenLength]; } @catch (id e) { length = 0; exception = e; } _writtenLength += length; if (_writtenLength != cStringLength && exception == nil) return true; # ifdef OF_HAVE_BLOCKS if (_block != NULL) { newString = _block(object, _string, _encoding, _writtenLength, exception); if (newString == nil) return false; oldString = _string; _string = [newString copy]; [oldString release]; _writtenLength = 0; return true; } else { # endif if (![_delegate respondsToSelector: @selector(stream: didWriteString:encoding:bytesWritten:exception:)]) return false; newString = [_delegate stream: object didWriteString: _string encoding: _encoding bytesWritten: _writtenLength exception: exception]; if (newString == nil) return false; oldString = _string; _string = [newString copy]; [oldString release]; _writtenLength = 0; return true; # ifdef OF_HAVE_BLOCKS } # endif } - (void)dealloc { [_string release]; # ifdef OF_HAVE_BLOCKS [_block release]; # endif [super dealloc]; } @end @implementation OFRunLoop_ConnectQueueItem - (bool)handleObject: (id)object |
︙ | ︙ | |||
785 786 787 788 789 790 791 | + (void)of_addAsyncWriteForStream: (OFStream <OFReadyForWritingObserving> *) stream data: (OFData *)data mode: (of_run_loop_mode_t)mode delegate: (id <OFStreamDelegate>)delegate { | | > > > > > > > > > > > > > > | 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 | + (void)of_addAsyncWriteForStream: (OFStream <OFReadyForWritingObserving> *) stream data: (OFData *)data mode: (of_run_loop_mode_t)mode delegate: (id <OFStreamDelegate>)delegate { ADD_WRITE(OFRunLoop_WriteDataQueueItem, stream, mode, { queueItem->_delegate = [delegate retain]; queueItem->_data = [data copy]; }) } + (void)of_addAsyncWriteForStream: (OFStream <OFReadyForWritingObserving> *) stream string: (OFString *)string encoding: (of_string_encoding_t)encoding mode: (of_run_loop_mode_t)mode delegate: (id <OFStreamDelegate>)delegate { ADD_WRITE(OFRunLoop_WriteStringQueueItem, stream, mode, { queueItem->_delegate = [delegate retain]; queueItem->_string = [string copy]; queueItem->_encoding = encoding; }) } + (void)of_addAsyncConnectForTCPSocket: (OFTCPSocket *)stream mode: (of_run_loop_mode_t)mode delegate: (id <OFTCPSocketDelegate_Private>) delegate { ADD_WRITE(OFRunLoop_ConnectQueueItem, stream, mode, { |
︙ | ︙ | |||
883 884 885 886 887 888 889 | }) } + (void)of_addAsyncWriteForStream: (OFStream <OFReadyForWritingObserving> *) stream data: (OFData *)data mode: (of_run_loop_mode_t)mode | | | > > > > > > > > > > > > > > | 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 | }) } + (void)of_addAsyncWriteForStream: (OFStream <OFReadyForWritingObserving> *) stream data: (OFData *)data mode: (of_run_loop_mode_t)mode block: (of_stream_async_write_data_block_t)block { ADD_WRITE(OFRunLoop_WriteDataQueueItem, stream, mode, { queueItem->_data = [data copy]; queueItem->_block = [block copy]; }) } + (void)of_addAsyncWriteForStream: (OFStream <OFReadyForWritingObserving> *) stream string: (OFString *)string encoding: (of_string_encoding_t)encoding mode: (of_run_loop_mode_t)mode block: (of_stream_async_write_string_block_t)block { ADD_WRITE(OFRunLoop_WriteStringQueueItem, stream, mode, { queueItem->_string = [string copy]; queueItem->_encoding = encoding; queueItem->_block = [block copy]; }) } + (void)of_addAsyncAcceptForTCPSocket: (OFTCPSocket *)stream mode: (of_run_loop_mode_t)mode block: (of_tcp_socket_async_accept_block_t)block { ADD_READ(OFRunLoop_AcceptQueueItem, stream, mode, { queueItem->_block = [block copy]; |
︙ | ︙ |