Index: src/OFRunLoop.m ================================================================== --- src/OFRunLoop.m +++ src/OFRunLoop.m @@ -284,25 +284,31 @@ if (_writtenLength != _length && exception == nil) return true; # ifdef OF_HAVE_BLOCKS - if (_block != NULL) - _block(object, _buffer, _writtenLength, exception); - else { + if (_block != NULL) { + if (!_block(object, _buffer, _writtenLength, exception)) + return false; + + _writtenLength = 0; + return true; + } else { # endif - void (*func)(id, SEL, OFStream *, const void *, size_t, id, - id) = (void (*)(id, SEL, OFStream *, const void *, size_t, + bool (*func)(id, SEL, OFStream *, const void *, size_t, id, + id) = (bool (*)(id, SEL, OFStream *, const void *, size_t, id, id))[_target methodForSelector: _selector]; - func(_target, _selector, object, _buffer, _writtenLength, - _context, exception); + if (!func(_target, _selector, object, _buffer, _writtenLength, + _context, exception)) + return false; + + _writtenLength = 0; + return true; # ifdef OF_HAVE_BLOCKS } # endif - - return false; } - (void)dealloc { [_block release]; Index: src/OFStream.h ================================================================== --- src/OFStream.h +++ src/OFStream.h @@ -69,12 +69,14 @@ * @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 + * @return A bool whether another write with the same buffer, length and + * callback block should be performed */ -typedef void (^of_stream_async_write_block_t)(OFStream *stream, +typedef bool (^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 @@ -802,14 +804,16 @@ * 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 target The target on which the selector should be called when the + * data has been received. If the method returns true, the same + * buffer and length will be written again and same method will + * be called again. * @param selector The selector to call on the target. The signature must be - * `void (OFStream *stream, const void *buffer, size_t length, + * `bool (OFStream *stream, const void *buffer, size_t length, * id context, id exception)`. */ - (void)asyncWriteBuffer: (const void *)buffer length: (size_t)length target: (id)target