@@ -20,10 +20,11 @@ #include #include #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" @@ -116,12 +117,12 @@ { @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 @@ -448,62 +449,75 @@ @implementation OFRunLoop_WriteQueueItem - (bool)handleObject: (id)object { size_t length; id exception = nil; + size_t dataLength = [_data count] * [_data itemSize]; + OFData *newData, *oldData; @try { - length = [object writeBuffer: (char *)_buffer + _writtenLength - length: _length - _writtenLength]; + const char *dataItems = [_data items]; + + 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 { @@ -769,19 +783,17 @@ }) } + (void)of_addAsyncWriteForStream: (OFStream *) stream - buffer: (const void *)buffer - length: (size_t)length + data: (OFData *)data mode: (of_run_loop_mode_t)mode delegate: (id )delegate { ADD_WRITE(OFRunLoop_WriteQueueItem, stream, mode, { queueItem->_delegate = [delegate retain]; - queueItem->_buffer = buffer; - queueItem->_length = length; + queueItem->_data = [data copy]; }) } + (void)of_addAsyncConnectForTCPSocket: (OFTCPSocket *)stream mode: (of_run_loop_mode_t)mode @@ -871,19 +883,17 @@ }) } + (void)of_addAsyncWriteForStream: (OFStream *) 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