Index: src/OFRunLoop+Private.h ================================================================== --- src/OFRunLoop+Private.h +++ src/OFRunLoop+Private.h @@ -46,55 +46,57 @@ stream buffer: (void *)buffer length: (size_t)length mode: (OFRunLoopMode)mode # ifdef OF_HAVE_BLOCKS - block: (nullable OFStreamAsyncReadBlock)block + handler: (nullable OFStreamReadHandler)handler # endif delegate: (nullable id )delegate; + (void)of_addAsyncReadForStream: (OFStream *) stream buffer: (void *)buffer exactLength: (size_t)length mode: (OFRunLoopMode)mode # ifdef OF_HAVE_BLOCKS - block: (nullable OFStreamAsyncReadBlock)block + handler: (nullable OFStreamReadHandler)handler # endif delegate: (nullable id )delegate; + (void)of_addAsyncReadStringForStream: (OFStream *)stream encoding: (OFStringEncoding)encoding mode: (OFRunLoopMode)mode # ifdef OF_HAVE_BLOCKS - block: (nullable OFStreamAsyncReadStringBlock) - block + handler: (nullable OFStreamStringReadHandler) + handler # endif delegate: (nullable id ) delegate; + (void)of_addAsyncReadLineForStream: (OFStream *) stream encoding: (OFStringEncoding)encoding mode: (OFRunLoopMode)mode # ifdef OF_HAVE_BLOCKS - block: (nullable OFStreamAsyncReadLineBlock)block + handler: (nullable OFStreamStringReadHandler) + handler # endif delegate: (nullable id )delegate; + (void)of_addAsyncWriteForStream: (OFStream *) stream data: (OFData *)data mode: (OFRunLoopMode)mode # ifdef OF_HAVE_BLOCKS - block: (nullable OFStreamAsyncWriteDataBlock)block + handler: (nullable OFStreamDataWrittenHandler)handler # endif delegate: (nullable id )delegate; + (void)of_addAsyncWriteForStream: (OFStream *) stream string: (OFString *)string encoding: (OFStringEncoding)encoding mode: (OFRunLoopMode)mode # ifdef OF_HAVE_BLOCKS - block: (nullable OFStreamAsyncWriteStringBlock)block + handler: (nullable OFStreamStringWrittenHandler) + handler # endif delegate: (nullable id )delegate; # if !defined(OF_WII) && !defined(OF_NINTENDO_3DS) + (void)of_addAsyncConnectForSocket: (id)socket mode: (OFRunLoopMode)mode Index: src/OFRunLoop.m ================================================================== --- src/OFRunLoop.m +++ src/OFRunLoop.m @@ -94,11 +94,11 @@ @interface OFRunLoopReadQueueItem: OFRunLoopQueueItem { @public # ifdef OF_HAVE_BLOCKS - OFStreamAsyncReadBlock _block; + OFStreamReadHandler _handler; # endif void *_buffer; size_t _length; } @end @@ -105,11 +105,11 @@ @interface OFRunLoopExactReadQueueItem: OFRunLoopQueueItem { @public # ifdef OF_HAVE_BLOCKS - OFStreamAsyncReadBlock _block; + OFStreamReadHandler _handler; # endif void *_buffer; size_t _exactLength, _readLength; } @end @@ -116,31 +116,31 @@ @interface OFRunLoopReadStringQueueItem: OFRunLoopQueueItem { @public # ifdef OF_HAVE_BLOCKS - OFStreamAsyncReadStringBlock _block; + OFStreamStringReadHandler _handler; # endif OFStringEncoding _encoding; } @end @interface OFRunLoopReadLineQueueItem: OFRunLoopQueueItem { @public # ifdef OF_HAVE_BLOCKS - OFStreamAsyncReadLineBlock _block; + OFStreamStringReadHandler _handler; # endif OFStringEncoding _encoding; } @end @interface OFRunLoopWriteDataQueueItem: OFRunLoopQueueItem { @public # ifdef OF_HAVE_BLOCKS - OFStreamAsyncWriteDataBlock _block; + OFStreamDataWrittenHandler _handler; # endif OFData *_data; size_t _writtenLength; } @end @@ -147,11 +147,11 @@ @interface OFRunLoopWriteStringQueueItem: OFRunLoopQueueItem { @public # ifdef OF_HAVE_BLOCKS - OFStreamAsyncWriteStringBlock _block; + OFStreamStringWrittenHandler _handler; # endif OFString *_string; OFStringEncoding _encoding; size_t _writtenLength; } @@ -469,12 +469,12 @@ length = 0; exception = e; } # ifdef OF_HAVE_BLOCKS - if (_block != NULL) - return _block(length, exception); + if (_handler != NULL) + return _handler(object, _buffer, length, exception); else { # endif if (![_delegate respondsToSelector: @selector(stream:didReadIntoBuffer:length:exception:)]) return false; @@ -489,11 +489,11 @@ } # ifdef OF_HAVE_BLOCKS - (void)dealloc { - [_block release]; + [_handler release]; [super dealloc]; } # endif @end @@ -517,12 +517,12 @@ if (_readLength != _exactLength && ![object isAtEndOfStream] && exception == nil) return true; # ifdef OF_HAVE_BLOCKS - if (_block != NULL) { - if (!_block(_readLength, exception)) + if (_handler != NULL) { + if (!_handler(object, _buffer, _readLength, exception)) return false; _readLength = 0; return true; } else { @@ -545,11 +545,11 @@ } # ifdef OF_HAVE_BLOCKS - (void)dealloc { - [_block release]; + [_handler release]; [super dealloc]; } # endif @end @@ -569,12 +569,12 @@ if (string == nil && ![object isAtEndOfStream] && exception == nil) return true; # ifdef OF_HAVE_BLOCKS - if (_block != NULL) - return _block(string, exception); + if (_handler != NULL) + return _handler(object, string, exception); else { # endif if (![_delegate respondsToSelector: @selector(stream:didReadString:exception:)]) return false; @@ -588,11 +588,11 @@ } # ifdef OF_HAVE_BLOCKS - (void)dealloc { - [_block release]; + [_handler release]; [super dealloc]; } # endif @end @@ -612,12 +612,12 @@ if (line == nil && ![object isAtEndOfStream] && exception == nil) return true; # ifdef OF_HAVE_BLOCKS - if (_block != NULL) - return _block(line, exception); + if (_handler != NULL) + return _handler(object, line, exception); else { # endif if (![_delegate respondsToSelector: @selector(stream:didReadLine:exception:)]) return false; @@ -631,11 +631,11 @@ } # ifdef OF_HAVE_BLOCKS - (void)dealloc { - [_block release]; + [_handler release]; [super dealloc]; } # endif @end @@ -667,12 +667,12 @@ if (_writtenLength != dataLength && exception == nil) return true; # ifdef OF_HAVE_BLOCKS - if (_block != NULL) { - newData = _block(_writtenLength, exception); + if (_handler != NULL) { + newData = _handler(object, _data, _writtenLength, exception); if (newData == nil) return false; oldData = _data; @@ -707,11 +707,11 @@ } - (void)dealloc { # ifdef OF_HAVE_BLOCKS - [_block release]; + [_handler release]; # endif [_data release]; [super dealloc]; } @@ -744,12 +744,13 @@ if (_writtenLength != cStringLength && exception == nil) return true; # ifdef OF_HAVE_BLOCKS - if (_block != NULL) { - newString = _block(_writtenLength, exception); + if (_handler != NULL) { + newString = _handler(object, _string, _encoding, _writtenLength, + exception); if (newString == nil) return false; oldString = _string; @@ -786,11 +787,11 @@ - (void)dealloc { [_string release]; # ifdef OF_HAVE_BLOCKS - [_block release]; + [_handler release]; # endif [super dealloc]; } @end @@ -1299,19 +1300,19 @@ stream buffer: (void *)buffer length: (size_t)length mode: (OFRunLoopMode)mode # ifdef OF_HAVE_BLOCKS - block: (OFStreamAsyncReadBlock)block + handler: (OFStreamReadHandler)handler # endif delegate: (id )delegate { NEW_READ(OFRunLoopReadQueueItem, stream, mode) queueItem->_delegate = [delegate retain]; # ifdef OF_HAVE_BLOCKS - queueItem->_block = [block copy]; + queueItem->_handler = [handler copy]; # endif queueItem->_buffer = buffer; queueItem->_length = length; QUEUE_ITEM @@ -1321,19 +1322,19 @@ stream buffer: (void *)buffer exactLength: (size_t)exactLength mode: (OFRunLoopMode)mode # ifdef OF_HAVE_BLOCKS - block: (OFStreamAsyncReadBlock)block + handler: (OFStreamReadHandler)handler # endif delegate: (id )delegate { NEW_READ(OFRunLoopExactReadQueueItem, stream, mode) queueItem->_delegate = [delegate retain]; # ifdef OF_HAVE_BLOCKS - queueItem->_block = [block copy]; + queueItem->_handler = [handler copy]; # endif queueItem->_buffer = buffer; queueItem->_exactLength = exactLength; QUEUE_ITEM @@ -1342,19 +1343,19 @@ + (void)of_addAsyncReadStringForStream: (OFStream *)stream encoding: (OFStringEncoding)encoding mode: (OFRunLoopMode)mode # ifdef OF_HAVE_BLOCKS - block: (OFStreamAsyncReadStringBlock)block + handler: (OFStreamStringReadHandler)handler # endif delegate: (id )delegate { NEW_READ(OFRunLoopReadStringQueueItem, stream, mode) queueItem->_delegate = [delegate retain]; # ifdef OF_HAVE_BLOCKS - queueItem->_block = [block copy]; + queueItem->_handler = [handler copy]; # endif queueItem->_encoding = encoding; QUEUE_ITEM } @@ -1362,19 +1363,19 @@ + (void)of_addAsyncReadLineForStream: (OFStream *) stream encoding: (OFStringEncoding)encoding mode: (OFRunLoopMode)mode # ifdef OF_HAVE_BLOCKS - block: (OFStreamAsyncReadLineBlock)block + handler: (OFStreamStringReadHandler)handler # endif delegate: (id )delegate { NEW_READ(OFRunLoopReadLineQueueItem, stream, mode) queueItem->_delegate = [delegate retain]; # ifdef OF_HAVE_BLOCKS - queueItem->_block = [block copy]; + queueItem->_handler = [handler copy]; # endif queueItem->_encoding = encoding; QUEUE_ITEM } @@ -1382,19 +1383,19 @@ + (void)of_addAsyncWriteForStream: (OFStream *) stream data: (OFData *)data mode: (OFRunLoopMode)mode # ifdef OF_HAVE_BLOCKS - block: (OFStreamAsyncWriteDataBlock)block + handler: (OFStreamDataWrittenHandler)handler # endif delegate: (id )delegate { NEW_WRITE(OFRunLoopWriteDataQueueItem, stream, mode) queueItem->_delegate = [delegate retain]; # ifdef OF_HAVE_BLOCKS - queueItem->_block = [block copy]; + queueItem->_handler = [handler copy]; # endif queueItem->_data = [data copy]; QUEUE_ITEM } @@ -1403,19 +1404,19 @@ stream string: (OFString *)string encoding: (OFStringEncoding)encoding mode: (OFRunLoopMode)mode # ifdef OF_HAVE_BLOCKS - block: (OFStreamAsyncWriteStringBlock)block + handler: (OFStreamStringWrittenHandler)handler # endif delegate: (id )delegate { NEW_WRITE(OFRunLoopWriteStringQueueItem, stream, mode) queueItem->_delegate = [delegate retain]; # ifdef OF_HAVE_BLOCKS - queueItem->_block = [block copy]; + queueItem->_handler = [handler copy]; # endif queueItem->_string = [string copy]; queueItem->_encoding = encoding; QUEUE_ITEM Index: src/OFStream.h ================================================================== --- src/OFStream.h +++ src/OFStream.h @@ -42,71 +42,132 @@ #if defined(OF_HAVE_SOCKETS) && defined(OF_HAVE_BLOCKS) /** * @brief A block which is called when data was read asynchronously from a * stream. + * + * @deprecated Use @ref OFStreamReadHandler instead. * * @param length The length of the data that has been read * @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 (^OFStreamAsyncReadBlock)(size_t length, id _Nullable exception); +typedef bool (^OFStreamAsyncReadBlock)(size_t length, id _Nullable exception) + OF_DEPRECATED(ObjFW, 1, 2, "Use OFStreamReadHandler instead"); + +/** + * @brief A handler which is called when data was read asynchronously from a + * stream. + * + * @param stream The stream on which data was read + * @param buffer A buffer with the data that has been read + * @param length The length of the data that has been read + * @param exception An exception which occurred while reading or `nil` on + * success + * @return A bool whether the same handler should be used for the next read + */ +typedef bool (^OFStreamReadHandler)(OFStream *stream, void *buffer, + size_t length, id _Nullable exception); /** * @brief A block which is called when a string was read asynchronously from a * stream. * + * @param stream The stream on which a string was read * @param string The string which has been read or `nil` when the end of stream * 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 (^OFStreamAsyncReadStringBlock)(OFString *_Nullable string, - id _Nullable exception); +typedef bool (^OFStreamStringReadHandler)(OFStream *stream, + OFString *_Nullable string, id _Nullable exception); /** * @brief A block which is called when a line was read asynchronously from a * stream. + * + * @deprecated Use @ref OFStreamStringReadHandler instead. * * @param line The line which has been read or `nil` when the end of stream * 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 (^OFStreamAsyncReadLineBlock)(OFString *_Nullable line, - id _Nullable exception); + id _Nullable exception) + OF_DEPRECATED(ObjFW, 1, 2, "Use OFStreamStringReadHandler instead"); /** * @brief A block which is called when data was written asynchronously to a * stream. + * + * @deprecated Use @ref OFStreamDataWrittenHandler instead. * * @param bytesWritten The number of bytes which have been written. This * matches the length of the specified data on the * asynchronous write if no exception was encountered. * @param exception An exception which occurred while writing or `nil` on * success * @return The data to repeat the write with or nil if it should not repeat */ typedef OFData *_Nullable (^OFStreamAsyncWriteDataBlock)(size_t bytesWritten, - id _Nullable exception); + id _Nullable exception) + OF_DEPRECATED(ObjFW, 1, 2, "Use OFStreamDataWrittenHandler instead"); + +/** + * @brief A handler which is called when data was written asynchronously to a + * stream. + * + * @param stream The stream to which data was written + * @param data The data which was written to the stream + * @param bytesWritten The number of bytes which have been written. This + * matches the length of the specified data on the + * asynchronous write if no exception was encountered. + * @param exception An exception which occurred while writing or `nil` on + * success + * @return The data to repeat the write with or nil if it should not repeat + */ +typedef OFData *_Nullable (^OFStreamDataWrittenHandler)(OFStream *stream, + OFData *data, size_t bytesWritten, id _Nullable exception); /** * @brief A block which is called when a string was written asynchronously to a * stream. + * + * @deprecated Use @ref OFStreamStringWrittenHandler instead. * * @param bytesWritten The number of bytes which have been written. This * matches the length of the specified data on the * asynchronous write if no exception was encountered. * @param exception An exception which occurred while writing or `nil` on * success * @return The string to repeat the write with or nil if it should not repeat */ typedef OFString *_Nullable (^OFStreamAsyncWriteStringBlock)( - size_t bytesWritten, id _Nullable exception); + size_t bytesWritten, id _Nullable exception) + OF_DEPRECATED(ObjFW, 1, 2, "Use OFStreamStringWrittenHandler instead"); + +/** + * @brief A handler which is called when a string was written asynchronously to + * a stream. + * + * @param stream The stream to which a string was written + * @param string The string which was written to the stream + * @param encoding The encoding in which the string was written + * @param bytesWritten The number of bytes which have been written. This + * matches the length of the specified data on the + * asynchronous write if no exception was encountered. + * @param exception An exception which occurred while writing or `nil` on + * success + * @return The string to repeat the write with or nil if it should not repeat + */ +typedef OFString *_Nullable (^OFStreamStringWrittenHandler)(OFStream *stream, + OFString *string, OFStringEncoding encoding, size_t bytesWritten, + id _Nullable exception); #endif /** * @protocol OFStreamDelegate OFStream.h ObjFW/ObjFW.h * @@ -131,11 +192,11 @@ /** * @brief This method is called when a string was read asynchronously from a * stream. * - * @param stream The stream on which a line was read + * @param stream The stream on which a string was read * @param string The string which has been read or `nil` when the end of stream * occurred * @param exception An exception that occurred while reading, or nil on success * @return A bool whether the read should be repeated */ @@ -393,10 +454,12 @@ # ifdef OF_HAVE_BLOCKS /** * @brief Asynchronously reads *at most* ref `length` bytes from the stream * into a buffer. + * + * @deprecated Use @ref asyncReadIntoBuffer:length:handler: instead. * * On network streams, this might read less than the specified number of bytes. * If you want to read exactly the specified number of bytes, use * @ref asyncReadIntoBuffer:exactLength:block:. Note that a read can even * return 0 bytes - this does not necessarily mean that the stream ended, so @@ -417,15 +480,49 @@ * you want the next block in the queue to handle the data * received next, you need to return false from the block. */ - (void)asyncReadIntoBuffer: (void *)buffer length: (size_t)length - block: (OFStreamAsyncReadBlock)block; + block: (OFStreamAsyncReadBlock)block + OF_DEPRECATED(ObjFW, 1, 2, + "Use -[asyncReadIntoBuffer:length:handler:] instead"); + +/** + * @brief Asynchronously reads *at most* ref `length` bytes from the stream + * into a buffer. + * + * On network streams, this might read less than the specified number of bytes. + * If you want to read exactly the specified number of bytes, use + * @ref asyncReadIntoBuffer:exactLength:handler:. Note that a read can even + * return 0 bytes - this does not necessarily mean that the stream ended, so + * you still need to check @ref atEndOfStream. Do not assume that the stream + * ended just because a read returned 0 bytes - some streams do internal + * processing that has a result of 0 bytes. + * + * @note The stream must conform to @ref OFReadyForReadingObserving in order + * for this to work! + * + * @param buffer The buffer into which the data is read. + * The buffer must not be freed before the async read completed! + * @param length The length of the data that should be read at most. + * The buffer *must* be *at least* this big! + * @param handler The handler to call when the data has been received. + * If the handler returns true, it will be called again with the + * same buffer and maximum length when more data has been + * received. If you want the next handler in the queue to handle + * the data received next, you need to return false from the + * handler. + */ +- (void)asyncReadIntoBuffer: (void *)buffer + length: (size_t)length + handler: (OFStreamReadHandler)handler; /** * @brief Asynchronously reads *at most* `length` bytes from the stream into a * buffer. + * + * @deprecated Use @ref asyncReadIntoBuffer:length:runLoopMode:handler: instead. * * On network streams, this might read less than the specified number of bytes. * If you want to read exactly the specified number of bytes, use * @ref asyncReadIntoBuffer:exactLength:block:. Note that a read can even * return 0 bytes - this does not necessarily mean that the stream ended, so @@ -448,15 +545,51 @@ * received next, you need to return false from the block. */ - (void)asyncReadIntoBuffer: (void *)buffer length: (size_t)length runLoopMode: (OFRunLoopMode)runLoopMode - block: (OFStreamAsyncReadBlock)block; + block: (OFStreamAsyncReadBlock)block + OF_DEPRECATED(ObjFW, 1, 2, + "Use -[asyncReadIntoBuffer:length:runLoopMode:handler:] instead"); + +/** + * @brief Asynchronously reads *at most* `length` bytes from the stream into a + * buffer. + * + * On network streams, this might read less than the specified number of bytes. + * If you want to read exactly the specified number of bytes, use + * @ref asyncReadIntoBuffer:exactLength:handler:. Note that a read can even + * return 0 bytes - this does not necessarily mean that the stream ended, so + * you still need to check @ref atEndOfStream. Do not assume that the stream + * ended just because a read returned 0 bytes - some streams do internal + * processing that has a result of 0 bytes. + * + * @note The stream must conform to @ref OFReadyForReadingObserving in order + * for this to work! + * + * @param buffer The buffer into which the data is read. + * The buffer must not be freed before the async read completed! + * @param length The length of the data that should be read at most. + * The buffer *must* be *at least* this big! + * @param runLoopMode The run loop mode in which to perform the async read + * @param handler The handler to call when the data has been received. + * If the handler returns true, it will be called again with the + * same buffer and maximum length when more data has been + * received. If you want the next handler in the queue to handle + * the data received next, you need to return false from the + * handler. + */ +- (void)asyncReadIntoBuffer: (void *)buffer + length: (size_t)length + runLoopMode: (OFRunLoopMode)runLoopMode + handler: (OFStreamReadHandler)handler; /** * @brief Asynchronously reads exactly the specified `length` bytes from the * stream into a buffer. + * + * @deprecated Use @ref asyncReadIntoBuffer:exactLength:handler: instead. * * Unlike @ref asyncReadIntoBuffer:length:block:, this method does not invoke * the block when less than the specified length has been read - instead, it * waits until it got exactly the specified length, the stream has ended or an * exception occurred. @@ -473,15 +606,46 @@ * you want the next block in the queue to handle the data * received next, you need to return false from the block. */ - (void)asyncReadIntoBuffer: (void *)buffer exactLength: (size_t)length - block: (OFStreamAsyncReadBlock)block; + block: (OFStreamAsyncReadBlock)block + OF_DEPRECATED(ObjFW, 1, 2, + "Use -[asyncReadIntoBuffer:exactLength:handler:] instead"); + +/** + * @brief Asynchronously reads exactly the specified `length` bytes from the + * stream into a buffer. + * + * Unlike @ref asyncReadIntoBuffer:length:handler:, this method does not invoke + * the handler when less than the specified length has been read - instead, it + * waits until it got exactly the specified length, the stream has ended or an + * exception occurred. + * + * @note The stream must conform to @ref OFReadyForReadingObserving in order + * for this to work! + * + * @param buffer The buffer into which the data is read + * @param length The length of the data that should be read. + * The buffer *must* be *at least* this big! + * @param handler The handler to call when the data has been received. + * If the handler returns true, it will be called again with the + * same buffer and exact length when more data has been + * received. If you want the next handler in the queue to handle + * the data received next, you need to return false from the + * handler. + */ +- (void)asyncReadIntoBuffer: (void *)buffer + exactLength: (size_t)length + handler: (OFStreamReadHandler)handler; /** * @brief Asynchronously reads exactly the specified `length` bytes from the * stream into a buffer. + * + * @deprecated Use @ref asyncReadIntoBuffer:exactLength:runLoopMode:handler: + * instead. * * Unlike @ref asyncReadIntoBuffer:length:block:, this method does not invoke * the block when less than the specified length has been read - instead, it * waits until it got exactly the specified length, the stream has ended or an * exception occurred. @@ -500,11 +664,41 @@ * received next, you need to return false from the block. */ - (void)asyncReadIntoBuffer: (void *)buffer exactLength: (size_t)length runLoopMode: (OFRunLoopMode)runLoopMode - block: (OFStreamAsyncReadBlock)block; + block: (OFStreamAsyncReadBlock)block + OF_DEPRECATED(ObjFW, 1, 2, + "Use -[asyncReadIntoBuffer:exactLength:runLoopMode:handler: instead]"); + +/** + * @brief Asynchronously reads exactly the specified `length` bytes from the + * stream into a buffer. + * + * Unlike @ref asyncReadIntoBuffer:length:handler:, this method does not invoke + * the handler when less than the specified length has been read - instead, it + * waits until it got exactly the specified length, the stream has ended or an + * exception occurred. + * + * @note The stream must conform to @ref OFReadyForReadingObserving in order + * for this to work! + * + * @param buffer The buffer into which the data is read + * @param length The length of the data that should be read. + * The buffer *must* be *at least* this big! + * @param runLoopMode The run loop mode in which to perform the async read + * @param handler The handler to call when the data has been received. + * If the handler returns true, it will be called again with the + * same buffer and exact length when more data has been + * received. If you want the next handler in the queue to handle + * the data received next, you need to return false from the + * handler. + */ +- (void)asyncReadIntoBuffer: (void *)buffer + exactLength: (size_t)length + runLoopMode: (OFRunLoopMode)runLoopMode + handler: (OFStreamReadHandler)handler; # endif #endif /** * @brief Reads a uint8_t from the stream. @@ -871,34 +1065,34 @@ * occurs. * * @note The stream must conform to @ref OFReadyForReadingObserving in order * for this to work! * - * @param block The block to call when the data has been received. - * If the block returns true, it will be called again when the - * next string has been received. If you want the next block in - * the queue to handle the next string, you need to return false - * from the block. + * @param handler The handler to call when the data has been received. + * If the handler returns true, it will be called again when the + * next string has been received. If you want the next handler + * in the queue to handle the next string, you need to return + * false from the handler. */ -- (void)asyncReadStringWithBlock: (OFStreamAsyncReadStringBlock)block; +- (void)asyncReadStringWithHandler: (OFStreamStringReadHandler)handler; /** * @brief Asynchronously reads with the specified encoding until a `\0`, end of * stream or an exception occurs. * * @note The stream must conform to @ref OFReadyForReadingObserving in order * for this to work! * * @param encoding The encoding used by the stream - * @param block The block to call when the data has been received. - * If the block returns true, it will be called again when the - * next string has been received. If you want the next block in - * the queue to handle the next string, you need to return false - * from the block. + * @param handler The handler to call when the data has been received. + * If the handler returns true, it will be called again when the + * next string has been received. If you want the next handler + * in the queue to handle the next string, you need to return + * false from the handler. */ - (void)asyncReadStringWithEncoding: (OFStringEncoding)encoding - block: (OFStreamAsyncReadStringBlock)block; + handler: (OFStreamStringReadHandler)handler; /** * @brief Asynchronously reads with the specified encoding until a `\0`, end of * stream or an exception occurs. * @@ -905,23 +1099,25 @@ * @note The stream must conform to @ref OFReadyForReadingObserving in order * for this to work! * * @param encoding The encoding used by the stream * @param runLoopMode The run loop mode in which to perform the async read - * @param block The block to call when the data has been received. - * If the block returns true, it will be called again when the - * next string has been received. If you want the next block in - * the queue to handle the next string, you need to return false - * from the block. + * @param handler The handler to call when the data has been received. + * If the handler returns true, it will be called again when the + * next string has been received. If you want the next handler + * in the queue to handle the next string, you need to return + * false from the handler. */ - (void)asyncReadStringWithEncoding: (OFStringEncoding)encoding runLoopMode: (OFRunLoopMode)runLoopMode - block: (OFStreamAsyncReadStringBlock)block; + handler: (OFStreamStringReadHandler)handler; /** * @brief Asynchronously reads until a newline, `\0`, end of stream or an * exception occurs. + * + * @deprecated Use @ref asyncReadLineWithHandler: instead. * * @note The stream must conform to @ref OFReadyForReadingObserving in order * for this to work! * * @param block The block to call when the data has been received. @@ -928,15 +1124,33 @@ * If the block returns true, it will be called again when the next * line has been received. If you want the next block in the queue * to handle the next line, you need to return false from the * block. */ -- (void)asyncReadLineWithBlock: (OFStreamAsyncReadLineBlock)block; +- (void)asyncReadLineWithBlock: (OFStreamAsyncReadLineBlock)block + OF_DEPRECATED(ObjFW, 1, 2, "Use -[asyncReadLineWithHandler:] instead"); + +/** + * @brief Asynchronously reads until a newline, `\0`, end of stream or an + * exception occurs. + * + * @note The stream must conform to @ref OFReadyForReadingObserving in order + * for this to work! + * + * @param handler The handler to call when the data has been received. + * If the handler returns true, it will be called again when the + * next line has been received. If you want the next handler in + * the queue to handle the next line, you need to return false + * from the handler. + */ +- (void)asyncReadLineWithHandler: (OFStreamStringReadHandler)handler; /** * @brief Asynchronously reads with the specified encoding until a newline, * `\0`, end of stream or an exception occurs. + * + * @deprecated Use @ref asyncReadLineWithEncoding:handler: instead. * * @note The stream must conform to @ref OFReadyForReadingObserving in order * for this to work! * * @param encoding The encoding used by the stream @@ -945,15 +1159,36 @@ * line has been received. If you want the next block in the queue * to handle the next line, you need to return false from the * block. */ - (void)asyncReadLineWithEncoding: (OFStringEncoding)encoding - block: (OFStreamAsyncReadLineBlock)block; + block: (OFStreamAsyncReadLineBlock)block + OF_DEPRECATED(ObjFW, 1, 2, + "Use -[asyncReadLineWithEncoding:handler:] instead"); + +/** + * @brief Asynchronously reads with the specified encoding until a newline, + * `\0`, end of stream or an exception occurs. + * + * @note The stream must conform to @ref OFReadyForReadingObserving in order + * for this to work! + * + * @param encoding The encoding used by the stream + * @param handler The handler to call when the data has been received. + * If the handler returns true, it will be called again when the + * next line has been received. If you want the next handler in + * the queue to handle the next line, you need to return false + * from the handler. + */ +- (void)asyncReadLineWithEncoding: (OFStringEncoding)encoding + handler: (OFStreamStringReadHandler)handler; /** * @brief Asynchronously reads with the specified encoding until a newline, * `\0`, end of stream or an exception occurs. + * + * @deprecated Use @ref asyncReadLineWithEncoding:runLoopMode:handler: instead. * * @note The stream must conform to @ref OFReadyForReadingObserving in order * for this to work! * * @param encoding The encoding used by the stream @@ -964,11 +1199,32 @@ * to handle the next line, you need to return false from the * block. */ - (void)asyncReadLineWithEncoding: (OFStringEncoding)encoding runLoopMode: (OFRunLoopMode)runLoopMode - block: (OFStreamAsyncReadLineBlock)block; + block: (OFStreamAsyncReadLineBlock)block + OF_DEPRECATED(ObjFW, 1, 2, + "Use -[asyncReadLineWithEncoding:runLoopMode:handler:] instead"); + +/** + * @brief Asynchronously reads with the specified encoding until a newline, + * `\0`, end of stream or an exception occurs. + * + * @note The stream must conform to @ref OFReadyForReadingObserving in order + * for this to work! + * + * @param encoding The encoding used by the stream + * @param runLoopMode The run loop mode in which to perform the async read + * @param handler The handler to call when the data has been received. + * If the handler returns true, it will be called again when the + * next line has been received. If you want the next handler in + * the queue to handle the next line, you need to return false + * from the handler. + */ +- (void)asyncReadLineWithEncoding: (OFStringEncoding)encoding + runLoopMode: (OFRunLoopMode)runLoopMode + handler: (OFStreamStringReadHandler)handler; # endif #endif /** * @brief Tries to read a string until a `\0` appears in the stream or the end @@ -1173,10 +1429,12 @@ runLoopMode: (OFRunLoopMode)runLoopMode; # ifdef OF_HAVE_BLOCKS /** * @brief Asynchronously writes data into the stream. + * + * @deprecated Use @ref asyncWriteData:handler: instead. * * @note The stream must conform to @ref OFReadyForWritingObserving in order * for this to work! * * @param data The data which is written into the stream @@ -1183,14 +1441,31 @@ * @param block The block to call when the data has been written. It should * return the data for the next write with the same callback or * nil if it should not repeat. */ - (void)asyncWriteData: (OFData *)data - block: (OFStreamAsyncWriteDataBlock)block; + block: (OFStreamAsyncWriteDataBlock)block + OF_DEPRECATED(ObjFW, 1, 2, "Use -[asyncWriteData:handler:] instead"); + +/** + * @brief Asynchronously writes data into the stream. + * + * @note The stream must conform to @ref OFReadyForWritingObserving in order + * for this to work! + * + * @param data The data which is written into the stream + * @param handler The handler to call when the data has been written. It should + * return the data for the next write with the same callback or + * nil if it should not repeat. + */ +- (void)asyncWriteData: (OFData *)data + handler: (OFStreamDataWrittenHandler)handler; /** * @brief Asynchronously writes data into the stream. + * + * @deprecated Use @ref asyncWriteData:runLoopMode:handler: instead. * * @note The stream must conform to @ref OFReadyForWritingObserving in order * for this to work! * * @param data The data which is written into the stream @@ -1199,14 +1474,34 @@ * return the data for the next write with the same callback or * nil if it should not repeat. */ - (void)asyncWriteData: (OFData *)data runLoopMode: (OFRunLoopMode)runLoopMode - block: (OFStreamAsyncWriteDataBlock)block; + block: (OFStreamAsyncWriteDataBlock)block + OF_DEPRECATED(ObjFW, 1, 2, + "Use -[asyncWriteData:runLoopMode:handler:] instead"); + +/** + * @brief Asynchronously writes data into the stream. + * + * @note The stream must conform to @ref OFReadyForWritingObserving in order + * for this to work! + * + * @param data The data which is written into the stream + * @param runLoopMode The run loop mode in which to perform the async write + * @param handler The handler to call when the data has been written. It should + * return the data for the next write with the same callback or + * nil if it should not repeat. + */ +- (void)asyncWriteData: (OFData *)data + runLoopMode: (OFRunLoopMode)runLoopMode + handler: (OFStreamDataWrittenHandler)handler; /** * @brief Asynchronously writes a string into the stream. + * + * @deprecated Use @ref asyncWriteString:handler: instead. * * @note The stream must conform to @ref OFReadyForWritingObserving in order * for this to work! * * @param string The string which is written into the stream @@ -1213,15 +1508,32 @@ * @param block The block to call when the string has been written. It should * return the string for the next write with the same callback or * nil if it should not repeat. */ - (void)asyncWriteString: (OFString *)string - block: (OFStreamAsyncWriteStringBlock)block; + block: (OFStreamAsyncWriteStringBlock)block + OF_DEPRECATED(ObjFW, 1, 2, "Use -[asyncWriteString:handler:] instead"); + +/** + * @brief Asynchronously writes a string into the stream. + * + * @note The stream must conform to @ref OFReadyForWritingObserving in order + * for this to work! + * + * @param string The string which is written into the stream + * @param handler The handler to call when the string has been written. It + * should return the string for the next write with the same + * callback or nil if it should not repeat. + */ +- (void)asyncWriteString: (OFString *)string + handler: (OFStreamStringWrittenHandler)handler; /** * @brief Asynchronously writes a string in the specified encoding into the * stream. + * + * @deprecated Use @ref asyncWriteString:encoding:handler: instead. * * @note The stream must conform to @ref OFReadyForWritingObserving in order * for this to work! * * @param string The string which is written into the stream @@ -1231,15 +1543,37 @@ * return the string for the next write with the same callback or * nil if it should not repeat. */ - (void)asyncWriteString: (OFString *)string encoding: (OFStringEncoding)encoding - block: (OFStreamAsyncWriteStringBlock)block; + block: (OFStreamAsyncWriteStringBlock)block + OF_DEPRECATED(ObjFW, 1, 2, + "Use -[asyncWriteString:encoding:handler:] instead"); + +/** + * @brief Asynchronously writes a string in the specified encoding into the + * stream. + * + * @note The stream must conform to @ref OFReadyForWritingObserving in order + * for this to work! + * + * @param string The string which is written into the stream + * @param encoding The encoding in which the string should be written to the + * stream + * @param handler The handler to call when the string has been written. It + * should return the string for the next write with the same + * callback or nil if it should not repeat. + */ +- (void)asyncWriteString: (OFString *)string + encoding: (OFStringEncoding)encoding + handler: (OFStreamStringWrittenHandler)handler; /** * @brief Asynchronously writes a string in the specified encoding into the * stream. + * + * @deprecated Use @ref asyncWriteString:encoding:runLoopMode:handler: instead. * * @note The stream must conform to @ref OFReadyForWritingObserving in order * for this to work! * * @param string The string which is written into the stream @@ -1251,11 +1585,33 @@ * nil if it should not repeat. */ - (void)asyncWriteString: (OFString *)string encoding: (OFStringEncoding)encoding runLoopMode: (OFRunLoopMode)runLoopMode - block: (OFStreamAsyncWriteStringBlock)block; + block: (OFStreamAsyncWriteStringBlock)block + OF_DEPRECATED(ObjFW, 1, 2, + "Use -[asyncWriteString:encoding:runLoopMode:handler:] instead"); + +/** + * @brief Asynchronously writes a string in the specified encoding into the + * stream. + * + * @note The stream must conform to @ref OFReadyForWritingObserving in order + * for this to work! + * + * @param string The string which is written into the stream + * @param encoding The encoding in which the string should be written to the + * stream + * @param runLoopMode The run loop mode in which to perform the async write + * @param handler The handler to call when the string has been written. It + * should return the string for the next write with the same + * callback or nil if it should not repeat. + */ +- (void)asyncWriteString: (OFString *)string + encoding: (OFStringEncoding)encoding + runLoopMode: (OFRunLoopMode)runLoopMode + handler: (OFStreamStringWrittenHandler)handler; # endif #endif /** * @brief Writes a uint8_t into the stream. Index: src/OFStream.m ================================================================== --- src/OFStream.m +++ src/OFStream.m @@ -217,11 +217,11 @@ [OFRunLoop of_addAsyncReadForStream: stream buffer: buffer length: length mode: runLoopMode # ifdef OF_HAVE_BLOCKS - block: NULL + handler: NULL # endif delegate: _delegate]; } - (void)asyncReadIntoBuffer: (void *)buffer exactLength: (size_t)length @@ -241,65 +241,127 @@ [OFRunLoop of_addAsyncReadForStream: stream buffer: buffer exactLength: length mode: runLoopMode # ifdef OF_HAVE_BLOCKS - block: NULL + handler: NULL # endif delegate: _delegate]; } # ifdef OF_HAVE_BLOCKS - (void)asyncReadIntoBuffer: (void *)buffer length: (size_t)length block: (OFStreamAsyncReadBlock)block { + OFStreamReadHandler handler = ^ (OFStream *stream, void *buffer_, + size_t length_, id exception) { + return block(length, exception); + }; + + [self asyncReadIntoBuffer: buffer + length: length + runLoopMode: OFDefaultRunLoopMode + handler: handler]; +} + +- (void)asyncReadIntoBuffer: (void *)buffer + length: (size_t)length + handler: (OFStreamReadHandler)handler +{ [self asyncReadIntoBuffer: buffer length: length runLoopMode: OFDefaultRunLoopMode - block: block]; + handler: handler]; } - (void)asyncReadIntoBuffer: (void *)buffer length: (size_t)length runLoopMode: (OFRunLoopMode)runLoopMode block: (OFStreamAsyncReadBlock)block +{ + OFStreamReadHandler handler = ^ (OFStream *stream, void *buffer_, + size_t length_, id exception) { + return block(length, exception); + }; + + [self asyncReadIntoBuffer: buffer + length: length + runLoopMode: runLoopMode + handler: handler]; +} + +- (void)asyncReadIntoBuffer: (void *)buffer + length: (size_t)length + runLoopMode: (OFRunLoopMode)runLoopMode + handler: (OFStreamReadHandler)handler { OFStream *stream = (OFStream *)self; [OFRunLoop of_addAsyncReadForStream: stream buffer: buffer length: length mode: runLoopMode - block: block + handler: handler delegate: nil]; } - (void)asyncReadIntoBuffer: (void *)buffer exactLength: (size_t)length block: (OFStreamAsyncReadBlock)block { + OFStreamReadHandler handler = ^ (OFStream *stream, void *buffer_, + size_t length_, id exception) { + return block(length, exception); + }; + + [self asyncReadIntoBuffer: buffer + exactLength: length + runLoopMode: OFDefaultRunLoopMode + handler: handler]; +} + +- (void)asyncReadIntoBuffer: (void *)buffer + exactLength: (size_t)length + handler: (OFStreamReadHandler)handler +{ [self asyncReadIntoBuffer: buffer exactLength: length runLoopMode: OFDefaultRunLoopMode - block: block]; + handler: handler]; } - (void)asyncReadIntoBuffer: (void *)buffer exactLength: (size_t)length runLoopMode: (OFRunLoopMode)runLoopMode block: (OFStreamAsyncReadBlock)block +{ + OFStreamReadHandler handler = ^ (OFStream *stream, void *buffer_, + size_t length_, id exception) { + return block(length, exception); + }; + + [self asyncReadIntoBuffer: buffer + exactLength: length + runLoopMode: runLoopMode + handler: handler]; +} + +- (void)asyncReadIntoBuffer: (void *)buffer + exactLength: (size_t)length + runLoopMode: (OFRunLoopMode)runLoopMode + handler: (OFStreamReadHandler)handler { OFStream *stream = (OFStream *)self; [OFRunLoop of_addAsyncReadForStream: stream buffer: buffer exactLength: length mode: runLoopMode - block: block + handler: handler delegate: nil]; } # endif #endif @@ -652,11 +714,11 @@ [OFRunLoop of_addAsyncReadStringForStream: stream encoding: encoding mode: runLoopMode # ifdef OF_HAVE_BLOCKS - block: NULL + handler: NULL # endif delegate: _delegate]; } - (void)asyncReadLine @@ -679,71 +741,110 @@ [OFRunLoop of_addAsyncReadLineForStream: stream encoding: encoding mode: runLoopMode # ifdef OF_HAVE_BLOCKS - block: NULL + handler: NULL # endif delegate: _delegate]; } # ifdef OF_HAVE_BLOCKS -- (void)asyncReadStringWithBlock: (OFStreamAsyncReadStringBlock)block +- (void)asyncReadStringWithHandler: (OFStreamStringReadHandler)handler { [self asyncReadStringWithEncoding: OFStringEncodingUTF8 runLoopMode: OFDefaultRunLoopMode - block: block]; + handler: handler]; } - (void)asyncReadStringWithEncoding: (OFStringEncoding)encoding - block: (OFStreamAsyncReadStringBlock)block + handler: (OFStreamStringReadHandler)handler { [self asyncReadStringWithEncoding: encoding runLoopMode: OFDefaultRunLoopMode - block: block]; + handler: handler]; } - (void)asyncReadStringWithEncoding: (OFStringEncoding)encoding runLoopMode: (OFRunLoopMode)runLoopMode - block: (OFStreamAsyncReadStringBlock)block + handler: (OFStreamStringReadHandler)handler { OFStream *stream = (OFStream *)self; [OFRunLoop of_addAsyncReadStringForStream: stream encoding: encoding mode: runLoopMode - block: block + handler: handler delegate: nil]; } - (void)asyncReadLineWithBlock: (OFStreamAsyncReadLineBlock)block { + OFStreamStringReadHandler handler = ^ (OFStream *stream, + OFString *string, id exception) { + return block(string, exception); + }; + + [self asyncReadLineWithEncoding: OFStringEncodingUTF8 + runLoopMode: OFDefaultRunLoopMode + handler: handler]; +} + +- (void)asyncReadLineWithHandler: (OFStreamStringReadHandler)handler +{ [self asyncReadLineWithEncoding: OFStringEncodingUTF8 runLoopMode: OFDefaultRunLoopMode - block: block]; + handler: handler]; } - (void)asyncReadLineWithEncoding: (OFStringEncoding)encoding block: (OFStreamAsyncReadLineBlock)block { + OFStreamStringReadHandler handler = ^ (OFStream *stream, + OFString *string, id exception) { + return block(string, exception); + }; + + [self asyncReadLineWithEncoding: encoding + runLoopMode: OFDefaultRunLoopMode + handler: handler]; +} + +- (void)asyncReadLineWithEncoding: (OFStringEncoding)encoding + handler: (OFStreamStringReadHandler)handler +{ [self asyncReadLineWithEncoding: encoding runLoopMode: OFDefaultRunLoopMode - block: block]; + handler: handler]; } - (void)asyncReadLineWithEncoding: (OFStringEncoding)encoding runLoopMode: (OFRunLoopMode)runLoopMode block: (OFStreamAsyncReadLineBlock)block +{ + OFStreamStringReadHandler handler = ^ (OFStream *stream, + OFString *string, id exception) { + return block(string, exception); + }; + + [self asyncReadLineWithEncoding: encoding + runLoopMode: runLoopMode + handler: handler]; +} + +- (void)asyncReadLineWithEncoding: (OFStringEncoding)encoding + runLoopMode: (OFRunLoopMode)runLoopMode + handler: (OFStreamStringReadHandler)handler { OFStream *stream = (OFStream *)self; [OFRunLoop of_addAsyncReadLineForStream: stream encoding: encoding mode: runLoopMode - block: block + handler: handler delegate: nil]; } # endif #endif @@ -1148,11 +1249,11 @@ [OFRunLoop of_addAsyncWriteForStream: stream data: data mode: runLoopMode # ifdef OF_HAVE_BLOCKS - block: NULL + handler: NULL # endif delegate: _delegate]; } - (void)asyncWriteString: (OFString *)string @@ -1180,69 +1281,144 @@ [OFRunLoop of_addAsyncWriteForStream: stream string: string encoding: encoding mode: runLoopMode # ifdef OF_HAVE_BLOCKS - block: NULL + handler: NULL # endif delegate: _delegate]; } # ifdef OF_HAVE_BLOCKS - (void)asyncWriteData: (OFData *)data block: (OFStreamAsyncWriteDataBlock)block { + OFStreamDataWrittenHandler handler = ^ (OFStream *stream, OFData *data_, + size_t bytesWritten, id exception) { + return block(bytesWritten, exception); + }; + + [self asyncWriteData: data + runLoopMode: OFDefaultRunLoopMode + handler: handler]; +} + +- (void)asyncWriteData: (OFData *)data + handler: (OFStreamDataWrittenHandler)handler +{ [self asyncWriteData: data runLoopMode: OFDefaultRunLoopMode - block: block]; + handler: handler]; } - (void)asyncWriteData: (OFData *)data runLoopMode: (OFRunLoopMode)runLoopMode block: (OFStreamAsyncWriteDataBlock)block +{ + OFStreamDataWrittenHandler handler = ^ (OFStream *stream, OFData *data_, + size_t bytesWritten, id exception) { + return block(bytesWritten, exception); + }; + + [self asyncWriteData: data + runLoopMode: runLoopMode + handler: handler]; +} + +- (void)asyncWriteData: (OFData *)data + runLoopMode: (OFRunLoopMode)runLoopMode + handler: (OFStreamDataWrittenHandler)handler { OFStream *stream = (OFStream *)self; [OFRunLoop of_addAsyncWriteForStream: stream data: data mode: runLoopMode - block: block + handler: handler delegate: nil]; } - (void)asyncWriteString: (OFString *)string block: (OFStreamAsyncWriteStringBlock)block { + OFStreamStringWrittenHandler handler = ^ (OFStream *stream, + OFString *string_, OFStringEncoding encoding_, size_t bytesWritten, + id exception) { + return block(bytesWritten, exception); + }; + + [self asyncWriteString: string + encoding: OFStringEncodingUTF8 + runLoopMode: OFDefaultRunLoopMode + handler: handler]; +} + +- (void)asyncWriteString: (OFString *)string + handler: (OFStreamStringWrittenHandler)handler +{ [self asyncWriteString: string encoding: OFStringEncodingUTF8 runLoopMode: OFDefaultRunLoopMode - block: block]; + handler: handler]; } - (void)asyncWriteString: (OFString *)string encoding: (OFStringEncoding)encoding block: (OFStreamAsyncWriteStringBlock)block { + OFStreamStringWrittenHandler handler = ^ (OFStream *stream, + OFString *string_, OFStringEncoding encoding_, size_t bytesWritten, + id exception) { + return block(bytesWritten, exception); + }; + + [self asyncWriteString: string + encoding: encoding + runLoopMode: OFDefaultRunLoopMode + handler: handler]; +} + +- (void)asyncWriteString: (OFString *)string + encoding: (OFStringEncoding)encoding + handler: (OFStreamStringWrittenHandler)handler +{ [self asyncWriteString: string encoding: encoding runLoopMode: OFDefaultRunLoopMode - block: block]; + handler: handler]; } - (void)asyncWriteString: (OFString *)string encoding: (OFStringEncoding)encoding runLoopMode: (OFRunLoopMode)runLoopMode block: (OFStreamAsyncWriteStringBlock)block +{ + OFStreamStringWrittenHandler handler = ^ (OFStream *stream, + OFString *string_, OFStringEncoding encoding_, size_t bytesWritten, + id exception) { + return block(bytesWritten, exception); + }; + + [self asyncWriteString: string + encoding: encoding + runLoopMode: runLoopMode + handler: handler]; +} + +- (void)asyncWriteString: (OFString *)string + encoding: (OFStringEncoding)encoding + runLoopMode: (OFRunLoopMode)runLoopMode + handler: (OFStreamStringWrittenHandler)handler { OFStream *stream = (OFStream *)self; [OFRunLoop of_addAsyncWriteForStream: stream string: string encoding: encoding mode: runLoopMode - block: block + handler: handler delegate: nil]; } # endif #endif