Index: src/OFDNSResolver.m ================================================================== --- src/OFDNSResolver.m +++ src/OFDNSResolver.m @@ -1810,14 +1810,13 @@ break; default: @throw [OFInvalidArgumentException exception]; } - [sock asyncSendBuffer: [query->_queryData items] - length: [query->_queryData count] - receiver: query->_usedNameServer - runLoopMode: runLoopMode]; + [sock asyncSendData: query->_queryData + receiver: &query->_usedNameServer + runLoopMode: runLoopMode]; } - (void)of_queryWithIDTimedOut: (OFDNSResolverQuery *)query { OFResolveHostFailedException *exception; @@ -1868,11 +1867,11 @@ } - (bool)socket: (OF_KINDOF(OFUDPSocket *))sock didReceiveIntoBuffer: (void *)buffer_ length: (size_t)length - sender: (of_socket_address_t)sender + sender: (const of_socket_address_t *)sender exception: (id)exception { unsigned char *buffer = buffer_; OFDictionary *answerRecords = nil, *authorityRecords = nil; OFDictionary *additionalRecords = nil; @@ -1890,11 +1889,11 @@ query = [[[_queries objectForKey: ID] retain] autorelease]; if (query == nil) return true; - if (!of_socket_address_equal(&sender, &query->_usedNameServer)) + if (!of_socket_address_equal(sender, &query->_usedNameServer)) return true; [query->_cancelTimer invalidate]; [query->_cancelTimer release]; query->_cancelTimer = nil; Index: src/OFRunLoop+Private.h ================================================================== --- src/OFRunLoop+Private.h +++ src/OFRunLoop+Private.h @@ -107,16 +107,16 @@ block # endif delegate: (nullable id ) delegate; + (void)of_addAsyncSendForUDPSocket: (OFUDPSocket *)socket - buffer: (const void *)buffer - length: (size_t)length - receiver: (of_socket_address_t)receiver + data: (OFData *)data + receiver: (const of_socket_address_t *)receiver mode: (of_run_loop_mode_t)mode # ifdef OF_HAVE_BLOCKS - block: (nullable of_udp_socket_async_send_block_t) + block: (nullable + of_udp_socket_async_send_data_block_t) block # endif delegate: (nullable id ) delegate; + (void)of_cancelAsyncRequestsForObject: (id)object Index: src/OFRunLoop.m ================================================================== --- src/OFRunLoop.m +++ src/OFRunLoop.m @@ -161,14 +161,13 @@ @interface OFRunLoop_UDPSendQueueItem: OFRunLoop_QueueItem { @public # ifdef OF_HAVE_BLOCKS - of_udp_socket_async_send_block_t _block; + of_udp_socket_async_send_data_block_t _block; # endif - const void *_buffer; - size_t _length; + OFData *_data; of_socket_address_t _receiver; } @end #endif @@ -681,21 +680,21 @@ exception = e; } # ifdef OF_HAVE_BLOCKS if (_block != NULL) - return _block(object, _buffer, length, address, exception); + return _block(object, _buffer, length, &address, exception); else { # endif if (![_delegate respondsToSelector: @selector( socket:didReceiveIntoBuffer:length:sender:exception:)]) return false; return [_delegate socket: object didReceiveIntoBuffer: _buffer length: length - sender: address + sender: &address exception: exception]; # ifdef OF_HAVE_BLOCKS } # endif } @@ -712,51 +711,65 @@ @implementation OFRunLoop_UDPSendQueueItem - (bool)handleObject: (id)object { id exception = nil; + OFData *newData, *oldData; @try { - [object sendBuffer: _buffer - length: _length + [object sendBuffer: [_data items] + length: [_data count] * [_data itemSize] receiver: &_receiver]; } @catch (id e) { exception = e; } # ifdef OF_HAVE_BLOCKS if (_block != NULL) { - _length = _block(object, &_buffer, - (exception == nil ? _length : 0), &_receiver, exception); + newData = _block(object, _data, &_receiver, exception); - return (_length > 0); + if (newData == nil) + return false; + + oldData = _data; + _data = [newData copy]; + [oldData release]; + + return true; } else { # endif if (![_delegate respondsToSelector: - @selector(socket:didSendBuffer:length:receiver:exception:)]) + @selector(socket:didSendData:receiver:exception:)]) return false; - _length = [_delegate socket: object - didSendBuffer: &_buffer - length: (exception == nil ? _length : 0) + newData = [_delegate socket: object + didSendData: _data receiver: &_receiver exception: exception]; - return (_length > 0); + if (newData == nil) + return false; + + oldData = _data; + _data = [newData copy]; + [oldData release]; + + 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 #endif @implementation OFRunLoop @synthesize currentMode = _currentMode; @@ -980,28 +993,27 @@ QUEUE_ITEM } + (void)of_addAsyncSendForUDPSocket: (OFUDPSocket *)sock - buffer: (const void *)buffer - length: (size_t)length - receiver: (of_socket_address_t)receiver + data: (OFData *)data + receiver: (const of_socket_address_t *)receiver mode: (of_run_loop_mode_t)mode # ifdef OF_HAVE_BLOCKS - block: (of_udp_socket_async_send_block_t)block + block: (of_udp_socket_async_send_data_block_t) + block # endif delegate: (id )delegate { NEW_WRITE(OFRunLoop_UDPSendQueueItem, sock, mode) queueItem->_delegate = [delegate retain]; # ifdef OF_HAVE_BLOCKS queueItem->_block = [block copy]; # endif - queueItem->_buffer = buffer; - queueItem->_length = length; - queueItem->_receiver = receiver; + queueItem->_data = [data copy]; + queueItem->_receiver = *receiver; QUEUE_ITEM } # undef NEW_READ # undef NEW_WRITE Index: src/OFStream.h ================================================================== --- src/OFStream.h +++ src/OFStream.h @@ -48,12 +48,13 @@ * @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 (^of_stream_async_read_block_t)(OF_KINDOF(OFStream *) stream, - void *buffer, size_t length, id _Nullable exception); +typedef bool (^of_stream_async_read_block_t)( + OF_KINDOF(OFStream *) _Nonnull stream, void *_Nonnull buffer, + size_t length, id _Nullable exception); /*! * @brief A block which is called when a line was read asynchronously from a * stream. * @@ -62,12 +63,13 @@ * 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 (^of_stream_async_read_line_block_t)(OF_KINDOF(OFStream *) stream, - OFString *_Nullable line, id _Nullable exception); +typedef bool (^of_stream_async_read_line_block_t)( + OF_KINDOF(OFStream *) _Nonnull stream, OFString *_Nullable line, + id _Nullable exception); /*! * @brief A block which is called when data was written asynchronously to a * stream. * @@ -78,12 +80,12 @@ * @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 (^of_stream_async_write_data_block_t)( - OF_KINDOF(OFStream *) stream, OFData *_Nonnull data, size_t bytesWritten, - id _Nullable exception); + OF_KINDOF(OFStream *) _Nonnull stream, OFData *_Nonnull data, + size_t bytesWritten, id _Nullable exception); /*! * @brief A block which is called when a string was written asynchronously to a * stream. * @@ -95,11 +97,11 @@ * @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 (^of_stream_async_write_string_block_t)( - OF_KINDOF(OFStream *) stream, OFString *_Nonnull string, + OF_KINDOF(OFStream *) _Nonnull stream, OFString *_Nonnull string, of_string_encoding_t encoding, size_t bytesWritten, id _Nullable exception); #endif /*! * @protocol OFStreamDelegate OFStream.h ObjFW/OFStream.h Index: src/OFUDPSocket.h ================================================================== --- src/OFUDPSocket.h +++ src/OFUDPSocket.h @@ -37,35 +37,27 @@ * @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 (^of_udp_socket_async_receive_block_t)(OFUDPSocket *socket, - void *buffer, size_t length, of_socket_address_t sender, - id _Nullable exception); +typedef bool (^of_udp_socket_async_receive_block_t)( + OFUDPSocket *_Nonnull socket, void *_Nonnull buffer, size_t length, + const of_socket_address_t *_Nonnull sender, id _Nullable exception); /*! * @brief A block which is called when a packet has been sent. * * @param socket The UDP socket which sent a packet - * @param buffer A pointer to the buffer which was sent. This can be changed to - * point to a different buffer to be used on the next send. - * @param bytesSent The number of bytes which have been sent. This matches the - * length specified on the asynchronous send if no exception - * was encountered. - * @param receiver The receiver for the UDP packet. This may be set to a new - * receiver to which the next packet is sent. + * @param data The data which was sent + * @param receiver The receiver for the UDP packet * @param exception An exception which occurred while reading or `nil` on * success - * @return The length to repeat the send with or 0 if it should not repeat. - * The buffer and receiver may be changed, so that every time a new - * buffer, length and receiver can be specified while the callback - * stays the same. + * @return The data to repeat the send with or nil if it should not repeat */ -typedef size_t (^of_udp_socket_async_send_block_t)(OFUDPSocket *socket, - const void *_Nonnull *_Nonnull buffer, size_t bytesSent, - of_socket_address_t *_Nonnull receiver, id exception); +typedef OFData *_Nullable (^of_udp_socket_async_send_data_block_t)( + OFUDPSocket *_Nonnull socket, OFData *_Nonnull data, + const of_socket_address_t *_Nonnull receiver, id _Nullable exception); #endif /*! * @protocol OFUDPSocketDelegate OFUDPSocket.h ObjFW/OFUDPSocket.h * @@ -85,33 +77,26 @@ * @return A bool whether the same block should be used for the next receive */ - (bool)socket: (OF_KINDOF(OFUDPSocket *))socket didReceiveIntoBuffer: (void *)buffer length: (size_t)length - sender: (of_socket_address_t)sender + sender: (const of_socket_address_t *_Nonnull)sender exception: (nullable id)exception; /*! * @brief This which is called when a packet has been sent. * * @param socket The UDP socket which sent a packet - * @param buffer A pointer to the buffer which was sent. This can be changed to - * point to a different buffer to be used on the next send. - * @param length The length of the buffer that has been sent - * @param receiver The receiver for the UDP packet. This may be set to a new - * receiver to which the next packet is sent. + * @param data The data which was sent + * @param receiver The receiver for the UDP packet * @param exception An exception that occurred while sending, or nil on success - * @return The length to repeat the send with or 0 if it should not repeat. - * The buffer and receiver may be changed, so that every time a new - * buffer, length and receiver can be specified while the callback - * stays the same. + * @return The data to repeat the send with or nil if it should not repeat */ -- (size_t)socket: (OF_KINDOF(OFUDPSocket *))socket - didSendBuffer: (const void *_Nonnull *_Nonnull)buffer - length: (size_t)length - receiver: (of_socket_address_t *_Nonnull)receiver - exception: (nullable id)exception; +- (nullable OFData *)socket: (OF_KINDOF(OFUDPSocket *))socket + didSendData: (OFData *)data + receiver: (const of_socket_address_t *_Nonnull)receiver + exception: (nullable id)exception; @end /*! * @class OFUDPSocket OFUDPSocket.h ObjFW/OFUDPSocket.h * @@ -273,71 +258,59 @@ receiver: (const of_socket_address_t *)receiver; /*! * @brief Asynchronously sends the specified datagram to the specified address. * - * @param buffer The buffer to send as a datagram - * @param length The length of the buffer + * @param data The data to send as a datagram * @param receiver A pointer to an @ref of_socket_address_t to which the - * datagram should be sent + * datagram should be sent. The receiver is copied. */ -- (void)asyncSendBuffer: (const void *)buffer - length: (size_t)length - receiver: (of_socket_address_t)receiver; +- (void)asyncSendData: (OFData *)data + receiver: (const of_socket_address_t *)receiver; /*! * @brief Asynchronously sends the specified datagram to the specified address. * - * @param buffer The buffer to send as a datagram - * @param length The length of the buffer + * @param data The data to send as a datagram * @param receiver A pointer to an @ref of_socket_address_t to which the - * datagram should be sent + * datagram should be sent. The receiver is copied. * @param runLoopMode The run loop mode in which to perform the async send */ -- (void)asyncSendBuffer: (const void *)buffer - length: (size_t)length - receiver: (of_socket_address_t)receiver - runLoopMode: (of_run_loop_mode_t)runLoopMode; +- (void)asyncSendData: (OFData *)data + receiver: (const of_socket_address_t *)receiver + runLoopMode: (of_run_loop_mode_t)runLoopMode; #ifdef OF_HAVE_BLOCKS /*! * @brief Asynchronously sends the specified datagram to the specified address. * - * @param buffer The buffer to send as a datagram - * @param length The length of the buffer + * @param data The data to send as a datagram * @param receiver A pointer to an @ref of_socket_address_t to which the - * datagram should be sent + * datagram should be sent. The receiver is copied. * @param block The block to call when the packet has been sent. It should - * return the length for the next send with the same callback or 0 - * if it should not repeat. The buffer and receiver may be - * changed, so that every time a new buffer, length and receiver - * can be specified while the callback stays the same. + * return the data for the next send with the same callback or nil + * if it should not repeat. */ -- (void)asyncSendBuffer: (const void *)buffer - length: (size_t)length - receiver: (of_socket_address_t)receiver - block: (of_udp_socket_async_send_block_t)block; +- (void)asyncSendData: (OFData *)data + receiver: (const of_socket_address_t *)receiver + block: (of_udp_socket_async_send_data_block_t)block; /*! * @brief Asynchronously sends the specified datagram to the specified address. * - * @param buffer The buffer to send as a datagram - * @param length The length of the buffer + * @param data The data to send as a datagram * @param receiver A pointer to an @ref of_socket_address_t to which the - * datagram should be sent + * datagram should be sent. The receiver is copied. * @param runLoopMode The run loop mode in which to perform the async send * @param block The block to call when the packet has been sent. It should - * return the length for the next send with the same callback or 0 - * if it should not repeat. The buffer and receiver may be - * changed, so that every time a new buffer, length and receiver - * can be specified while the callback stays the same. + * return the data for the next send with the same callback or nil + * if it should not repeat. */ -- (void)asyncSendBuffer: (const void *)buffer - length: (size_t)length - receiver: (of_socket_address_t)receiver - runLoopMode: (of_run_loop_mode_t)runLoopMode - block: (of_udp_socket_async_send_block_t)block; +- (void)asyncSendData: (OFData *)data + receiver: (const of_socket_address_t *)receiver + runLoopMode: (of_run_loop_mode_t)runLoopMode + block: (of_udp_socket_async_send_data_block_t)block; #endif /*! * @brief Cancels all pending asynchronous requests on the socket. */ Index: src/OFUDPSocket.m ================================================================== --- src/OFUDPSocket.m +++ src/OFUDPSocket.m @@ -414,58 +414,50 @@ requestedLength: length bytesWritten: bytesWritten errNo: 0]; } -- (void)asyncSendBuffer: (const void *)buffer - length: (size_t)length - receiver: (of_socket_address_t)receiver -{ - [self asyncSendBuffer: buffer - length: length - receiver: receiver - runLoopMode: of_run_loop_mode_default]; +- (void)asyncSendData: (OFData *)data + receiver: (const of_socket_address_t *)receiver +{ + [self asyncSendData: data + receiver: receiver + runLoopMode: of_run_loop_mode_default]; } -- (void)asyncSendBuffer: (const void *)buffer - length: (size_t)length - receiver: (of_socket_address_t)receiver - runLoopMode: (of_run_loop_mode_t)runLoopMode +- (void)asyncSendData: (OFData *)data + receiver: (const of_socket_address_t *)receiver + runLoopMode: (of_run_loop_mode_t)runLoopMode { [OFRunLoop of_addAsyncSendForUDPSocket: self - buffer: buffer - length: length + data: data receiver: receiver mode: runLoopMode # ifdef OF_HAVE_BLOCKS block: NULL # endif delegate: _delegate]; } #ifdef OF_HAVE_BLOCKS -- (void)asyncSendBuffer: (const void *)buffer - length: (size_t)length - receiver: (of_socket_address_t)receiver - block: (of_udp_socket_async_send_block_t)block -{ - [self asyncSendBuffer: buffer - length: length - receiver: receiver - runLoopMode: of_run_loop_mode_default - block: block]; -} - -- (void)asyncSendBuffer: (const void *)buffer - length: (size_t)length - receiver: (of_socket_address_t)receiver - runLoopMode: (of_run_loop_mode_t)runLoopMode - block: (of_udp_socket_async_send_block_t)block -{ - [OFRunLoop of_addAsyncSendForUDPSocket: self - buffer: buffer - length: length +- (void)asyncSendData: (OFData *)data + receiver: (const of_socket_address_t *)receiver + block: (of_udp_socket_async_send_data_block_t)block +{ + [self asyncSendData: data + receiver: receiver + runLoopMode: of_run_loop_mode_default + block: block]; +} + +- (void)asyncSendData: (OFData *)data + receiver: (const of_socket_address_t *)receiver + runLoopMode: (of_run_loop_mode_t)runLoopMode + block: (of_udp_socket_async_send_data_block_t)block +{ + [OFRunLoop of_addAsyncSendForUDPSocket: self + data: data receiver: receiver mode: runLoopMode block: block delegate: nil]; } Index: src/socket.h ================================================================== --- src/socket.h +++ src/socket.h @@ -174,20 +174,22 @@ * * @param address1 The address to compare with the second address * @param address2 The second address * @return Whether the two addresses are equal */ -extern bool of_socket_address_equal(of_socket_address_t *address1, - of_socket_address_t *address2); +extern bool of_socket_address_equal( + const of_socket_address_t *_Nonnull address1, + const of_socket_address_t *_Nonnull address2); /*! * @brief Returns the hash for the specified of_socket_address_t. * * @param address The address to hash * @return The hash for the specified of_socket_address_t */ -extern uint32_t of_socket_address_hash(of_socket_address_t *address); +extern uint32_t of_socket_address_hash( + const of_socket_address_t *_Nonnull address); /*! * @brief Converts the specified of_socket_address_t to an IP string and port. * * @param address The address to convert to a string Index: src/socket.m ================================================================== --- src/socket.m +++ src/socket.m @@ -392,15 +392,15 @@ return of_socket_address_parse_ipv4(IP, port); } } bool -of_socket_address_equal(of_socket_address_t *address1, - of_socket_address_t *address2) +of_socket_address_equal(const of_socket_address_t *address1, + const of_socket_address_t *address2) { - struct sockaddr_in *addrIn1, *addrIn2; - struct sockaddr_in6 *addrIn6_1, *addrIn6_2; + const struct sockaddr_in *addrIn1, *addrIn2; + const struct sockaddr_in6 *addrIn6_1, *addrIn6_2; if (address1->family != address2->family) return false; switch (address1->family) { @@ -445,11 +445,11 @@ return true; } uint32_t -of_socket_address_hash(of_socket_address_t *address) +of_socket_address_hash(const of_socket_address_t *address) { uint32_t hash; OF_HASH_INIT(hash); OF_HASH_ADD(hash, address->family);