Index: src/OFDatagramSocket.h ================================================================== --- src/OFDatagramSocket.h +++ src/OFDatagramSocket.h @@ -31,28 +31,65 @@ #ifdef OF_HAVE_BLOCKS /** * @brief A block which is called when a packet has been received. * + * @deprecated Use @ref OFDatagramSocketPacketReceivedHandler instead. + * * @param length The length of the packet * @param sender The address of the sender of the packet * @param exception An exception which occurred while receiving or `nil` on * success * @return A bool whether the same block should be used for the next receive */ typedef bool (^OFDatagramSocketAsyncReceiveBlock)(size_t length, - const OFSocketAddress *_Nonnull sender, id _Nullable exception); + const OFSocketAddress *_Nonnull sender, id _Nullable exception) + OF_DEPRECATED(ObjFW, 1, 2, + "Use OFDatagramSocketPacketReceivedHandler instead"); + +/** + * @brief A handler which is called when a packet has been received. + * + * @param socket The socket that received a packet + * @param buffer The buffer the packet was stored in + * @param length The length of the packet + * @param sender The address of the sender of the packet + * @param exception An exception which occurred while receiving or `nil` on + * success + * @return A bool whether the same block should be used for the next receive + */ +typedef bool (^OFDatagramSocketPacketReceivedHandler)(OFDatagramSocket *socket, + void *buffer, size_t length, const OFSocketAddress *_Nonnull sender, + id _Nullable exception); /** * @brief A block which is called when a packet has been sent. + * + * @deprecated Use @ref OFDatagramSocketDataSentHandler instead. * * @param exception An exception which occurred while reading or `nil` on * success * @return The data to repeat the send with or nil if it should not repeat */ typedef OFData *_Nullable (^OFDatagramSocketAsyncSendDataBlock)( - id _Nullable exception); + id _Nullable exception) + OF_DEPRECATED(ObjFW, 1, 2, + "Use OFDatagramSocketDataSentHandler instead"); + +/** + * @brief A handler which is called when a packet has been sent. + * + * @param socket The datagram socket which sent a packet + * @param data The data which was sent + * @param receiver The receiver for the packet + * @param exception An exception which occurred while reading or `nil` on + * success + * @return The data to repeat the send with or nil if it should not repeat + */ +typedef OFData *_Nullable (^OFDatagramSocketDataSentHandler)( + OFDatagramSocket *socket, OFData *data, + const OFSocketAddress *_Nonnull receiver, id _Nullable exception); #endif /** * @protocol OFDatagramSocketDelegate OFDatagramSocket.h ObjFW/ObjFW.h * @@ -200,10 +237,12 @@ #ifdef OF_HAVE_BLOCKS /** * @brief Asynchronously receives a datagram and stores it into the specified * buffer. * + * @deprecated Use @ref asyncReceiveIntoBuffer:length:handler: instead. + * * If the buffer is too small, the datagram is truncated. * * @param buffer The buffer to write the datagram to * @param length The length of the buffer * @param block The block to call when the datagram has been received. If the @@ -213,15 +252,39 @@ * the datagram received next, you need to return false from the * method. */ - (void)asyncReceiveIntoBuffer: (void *)buffer length: (size_t)length - block: (OFDatagramSocketAsyncReceiveBlock)block; + block: (OFDatagramSocketAsyncReceiveBlock)block + OF_DEPRECATED(ObjFW, 1, 2, + "Use -[asyncReceiveIntoBuffer:length:handler:] instead"); + +/** + * @brief Asynchronously receives a datagram and stores it into the specified + * buffer. + * + * If the buffer is too small, the datagram is truncated. + * + * @param buffer The buffer to write the datagram to + * @param length The length of the buffer + * @param handler The handler to call when the datagram has been received. If + * the handler returns true, it will be called again with the + * same buffer and maximum length when more datagrams have been + * received. If you want the next method in the queue to handle + * the datagram received next, you need to return false from the + * method. + */ +- (void)asyncReceiveIntoBuffer: (void *)buffer + length: (size_t)length + handler: (OFDatagramSocketPacketReceivedHandler)handler; /** * @brief Asynchronously receives a datagram and stores it into the specified * buffer. + * + * @deprecated Use @ref asyncReceiveIntoBuffer:length:runLoopMode:handler: + * instead. * * If the buffer is too small, the datagram is truncated. * * @param buffer The buffer to write the datagram to * @param length The length of the buffer @@ -235,11 +298,35 @@ * method. */ - (void)asyncReceiveIntoBuffer: (void *)buffer length: (size_t)length runLoopMode: (OFRunLoopMode)runLoopMode - block: (OFDatagramSocketAsyncReceiveBlock)block; + block: (OFDatagramSocketAsyncReceiveBlock)block + OF_DEPRECATED(ObjFW, 1, 2, + "Use -[asyncReceiveIntoBuffer:length:runLoopMode:handler:] instead"); + +/** + * @brief Asynchronously receives a datagram and stores it into the specified + * buffer. + * + * If the buffer is too small, the datagram is truncated. + * + * @param buffer The buffer to write the datagram to + * @param length The length of the buffer + * @param runLoopMode The run loop mode in which to perform the asynchronous + * receive + * @param handler The handler to call when the datagram has been received. If + * the handler returns true, it will be called again with the same + * buffer and maximum length when more datagrams have been + * received. If you want the next method in the queue to handle + * the datagram received next, you need to return false from the + * method. + */ +- (void)asyncReceiveIntoBuffer: (void *)buffer + length: (size_t)length + runLoopMode: (OFRunLoopMode)runLoopMode + handler: (OFDatagramSocketPacketReceivedHandler)handler; #endif /** * @brief Sends the specified datagram to the specified address. * @@ -279,23 +366,43 @@ #ifdef OF_HAVE_BLOCKS /** * @brief Asynchronously sends the specified datagram to the specified address. * + * @deprecated Use @ref asyncSendData:receiver:handler: instead. + * * @param data The data to send as a datagram * @param receiver A pointer to an @ref OFSocketAddress to which the datagram * should be sent. The receiver is copied. * @param block The block to call when the packet has been sent. It should * return the data for the next send with the same callback or nil * if it should not repeat. */ - (void)asyncSendData: (OFData *)data receiver: (const OFSocketAddress *)receiver - block: (OFDatagramSocketAsyncSendDataBlock)block; + block: (OFDatagramSocketAsyncSendDataBlock)block + OF_DEPRECATED(ObjFW, 1, 2, + "Use -[asyncSendData:receiver:handler:] instead"); + +/** + * @brief Asynchronously sends the specified datagram to the specified address. + * + * @param data The data to send as a datagram + * @param receiver A pointer to an @ref OFSocketAddress to which the datagram + * should be sent. The receiver is copied. + * @param handler The handler to call when the packet has been sent. It should + * return the data for the next send with the same callback or + * `nil` if it should not repeat. + */ +- (void)asyncSendData: (OFData *)data + receiver: (const OFSocketAddress *)receiver + handler: (OFDatagramSocketDataSentHandler)handler; /** * @brief Asynchronously sends the specified datagram to the specified address. + * + * @deprecated Use @ref asyncSendData:receiver:runLoopMode:handler: instead. * * @param data The data to send as a datagram * @param receiver A pointer to an @ref OFSocketAddress to which the datagram * should be sent. The receiver is copied. * @param runLoopMode The run loop mode in which to perform the asynchronous @@ -305,11 +412,30 @@ * if it should not repeat. */ - (void)asyncSendData: (OFData *)data receiver: (const OFSocketAddress *)receiver runLoopMode: (OFRunLoopMode)runLoopMode - block: (OFDatagramSocketAsyncSendDataBlock)block; + block: (OFDatagramSocketAsyncSendDataBlock)block + OF_DEPRECATED(ObjFW, 1, 2, + "Use -[asyncSendData:receiver:runLoopMode:handler:] instead"); + +/** + * @brief Asynchronously sends the specified datagram to the specified address. + * + * @param data The data to send as a datagram + * @param receiver A pointer to an @ref OFSocketAddress to which the datagram + * should be sent. The receiver is copied. + * @param runLoopMode The run loop mode in which to perform the asynchronous + * send + * @param handler The handler to call when the packet has been sent. It should + * return the data for the next send with the same callback or + * `nil` if it should not repeat. + */ +- (void)asyncSendData: (OFData *)data + receiver: (const OFSocketAddress *)receiver + runLoopMode: (OFRunLoopMode)runLoopMode + handler: (OFDatagramSocketDataSentHandler)handler; #endif /** * @brief Releases the socket from the current thread. * Index: src/OFDatagramSocket.m ================================================================== --- src/OFDatagramSocket.m +++ src/OFDatagramSocket.m @@ -257,36 +257,71 @@ [OFRunLoop of_addAsyncReceiveForDatagramSocket: self buffer: buffer length: length mode: runLoopMode # ifdef OF_HAVE_BLOCKS - block: NULL + handler: NULL # endif delegate: _delegate]; } #ifdef OF_HAVE_BLOCKS - (void)asyncReceiveIntoBuffer: (void *)buffer length: (size_t)length block: (OFDatagramSocketAsyncReceiveBlock)block { + OFDatagramSocketPacketReceivedHandler handler = ^ bool ( + OFDatagramSocket *socket, void *buffer_, size_t length_, + const OFSocketAddress *sender, id exception) { + return block(length_, sender, exception); + }; + + [self asyncReceiveIntoBuffer: buffer + length: length + runLoopMode: OFDefaultRunLoopMode + handler: handler]; +} + +- (void)asyncReceiveIntoBuffer: (void *)buffer + length: (size_t)length + handler: (OFDatagramSocketPacketReceivedHandler)handler +{ [self asyncReceiveIntoBuffer: buffer length: length runLoopMode: OFDefaultRunLoopMode - block: block]; + handler: handler]; } - (void)asyncReceiveIntoBuffer: (void *)buffer length: (size_t)length runLoopMode: (OFRunLoopMode)runLoopMode block: (OFDatagramSocketAsyncReceiveBlock)block { + OFDatagramSocketPacketReceivedHandler handler = ^ bool ( + OFDatagramSocket *socket, void *buffer_, size_t length_, + const OFSocketAddress *sender, id exception) { + return block(length_, sender, exception); + }; + + [OFRunLoop of_addAsyncReceiveForDatagramSocket: self + buffer: buffer + length: length + mode: runLoopMode + handler: handler + delegate: nil]; +} + +- (void)asyncReceiveIntoBuffer: (void *)buffer + length: (size_t)length + runLoopMode: (OFRunLoopMode)runLoopMode + handler: (OFDatagramSocketPacketReceivedHandler)handler +{ [OFRunLoop of_addAsyncReceiveForDatagramSocket: self buffer: buffer length: length mode: runLoopMode - block: block + handler: handler delegate: nil]; } #endif - (void)sendBuffer: (const void *)buffer @@ -346,36 +381,71 @@ [OFRunLoop of_addAsyncSendForDatagramSocket: self data: data receiver: receiver mode: runLoopMode # ifdef OF_HAVE_BLOCKS - block: NULL + handler: NULL # endif delegate: _delegate]; } #ifdef OF_HAVE_BLOCKS - (void)asyncSendData: (OFData *)data receiver: (const OFSocketAddress *)receiver block: (OFDatagramSocketAsyncSendDataBlock)block { + OFDatagramSocketDataSentHandler handler = ^ OFData *( + OFDatagramSocket *socket, OFData *data_, + const OFSocketAddress *receiver_, id exception) { + return block(exception); + }; + + [self asyncSendData: data + receiver: receiver + runLoopMode: OFDefaultRunLoopMode + handler: handler]; +} + +- (void)asyncSendData: (OFData *)data + receiver: (const OFSocketAddress *)receiver + handler: (OFDatagramSocketDataSentHandler)handler +{ [self asyncSendData: data receiver: receiver runLoopMode: OFDefaultRunLoopMode - block: block]; + handler: handler]; } - (void)asyncSendData: (OFData *)data receiver: (const OFSocketAddress *)receiver runLoopMode: (OFRunLoopMode)runLoopMode block: (OFDatagramSocketAsyncSendDataBlock)block { + OFDatagramSocketDataSentHandler handler = ^ OFData *( + OFDatagramSocket *socket, OFData *data_, + const OFSocketAddress *receiver_, id exception) { + return block(exception); + }; + + [OFRunLoop of_addAsyncSendForDatagramSocket: self + data: data + receiver: receiver + mode: runLoopMode + handler: handler + delegate: nil]; +} + +- (void)asyncSendData: (OFData *)data + receiver: (const OFSocketAddress *)receiver + runLoopMode: (OFRunLoopMode)runLoopMode + handler: (OFDatagramSocketDataSentHandler)handler +{ [OFRunLoop of_addAsyncSendForDatagramSocket: self data: data receiver: receiver mode: runLoopMode - block: block + handler: handler delegate: nil]; } #endif - (void)cancelAsyncRequests Index: src/OFRunLoop+Private.h ================================================================== --- src/OFRunLoop+Private.h +++ src/OFRunLoop+Private.h @@ -107,19 +107,19 @@ + (void)of_addAsyncReceiveForDatagramSocket: (OFDatagramSocket *)socket buffer: (void *)buffer length: (size_t)length mode: (OFRunLoopMode)mode # ifdef OF_HAVE_BLOCKS - block: (nullable OFDatagramSocketAsyncReceiveBlock)block + handler: (nullable OFDatagramSocketPacketReceivedHandler)handler # endif delegate: (nullable id )delegate; + (void)of_addAsyncSendForDatagramSocket: (OFDatagramSocket *)socket data: (OFData *)data receiver: (const OFSocketAddress *)receiver mode: (OFRunLoopMode)mode # ifdef OF_HAVE_BLOCKS - block: (nullable OFDatagramSocketAsyncSendDataBlock)block + handler: (nullable OFDatagramSocketDataSentHandler)handler # endif delegate: (nullable id )delegate; + (void)of_addAsyncReceiveForSequencedPacketSocket: (OFSequencedPacketSocket *)socket buffer: (void *)buffer Index: src/OFRunLoop.m ================================================================== --- src/OFRunLoop.m +++ src/OFRunLoop.m @@ -173,11 +173,11 @@ @interface OFRunLoopDatagramReceiveQueueItem: OFRunLoopQueueItem { @public # ifdef OF_HAVE_BLOCKS - OFDatagramSocketAsyncReceiveBlock _block; + OFDatagramSocketPacketReceivedHandler _handler; # endif void *_buffer; size_t _length; } @end @@ -184,11 +184,11 @@ @interface OFRunLoopDatagramSendQueueItem: OFRunLoopQueueItem { @public # ifdef OF_HAVE_BLOCKS - OFDatagramSocketAsyncSendDataBlock _block; + OFDatagramSocketDataSentHandler _handler; # endif OFData *_data; OFSocketAddress _receiver; } @end @@ -895,12 +895,12 @@ length = 0; exception = e; } # ifdef OF_HAVE_BLOCKS - if (_block != NULL) - return _block(length, &address, exception); + if (_handler != NULL) + return _handler(object, _buffer, length, &address, exception); else { # endif if (![_delegate respondsToSelector: @selector( socket:didReceiveIntoBuffer:length:sender:exception:)]) return false; @@ -916,11 +916,11 @@ } # ifdef OF_HAVE_BLOCKS - (void)dealloc { - [_block release]; + [_handler release]; [super dealloc]; } # endif @end @@ -938,12 +938,12 @@ } @catch (id e) { exception = e; } # ifdef OF_HAVE_BLOCKS - if (_block != NULL) { - newData = _block(exception); + if (_handler != NULL) { + newData = _handler(object, _data, &_receiver, exception); if (newData == nil) return false; oldData = _data; @@ -976,11 +976,11 @@ } - (void)dealloc { # ifdef OF_HAVE_BLOCKS - [_block release]; + [_handler release]; # endif [_data release]; [super dealloc]; } @@ -1452,19 +1452,19 @@ + (void)of_addAsyncReceiveForDatagramSocket: (OFDatagramSocket *)sock buffer: (void *)buffer length: (size_t)length mode: (OFRunLoopMode)mode # ifdef OF_HAVE_BLOCKS - block: (OFDatagramSocketAsyncReceiveBlock)block + handler: (OFDatagramSocketPacketReceivedHandler)handler # endif delegate: (id )delegate { NEW_READ(OFRunLoopDatagramReceiveQueueItem, sock, 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 @@ -1473,19 +1473,19 @@ + (void)of_addAsyncSendForDatagramSocket: (OFDatagramSocket *)sock data: (OFData *)data receiver: (const OFSocketAddress *)receiver mode: (OFRunLoopMode)mode # ifdef OF_HAVE_BLOCKS - block: (OFDatagramSocketAsyncSendDataBlock)block + handler: (OFDatagramSocketDataSentHandler)handler # endif delegate: (id )delegate { NEW_WRITE(OFRunLoopDatagramSendQueueItem, sock, mode) queueItem->_delegate = [delegate retain]; # ifdef OF_HAVE_BLOCKS - queueItem->_block = [block copy]; + queueItem->_handler = [handler copy]; # endif queueItem->_data = [data copy]; queueItem->_receiver = *receiver; QUEUE_ITEM