Index: src/OFUDPSocket.h ================================================================== --- src/OFUDPSocket.h +++ src/OFUDPSocket.h @@ -15,10 +15,11 @@ * file. */ #import "OFObject.h" #import "OFKernelEventObserver.h" +#import "OFRunLoop.h" #import "socket.h" OF_ASSUME_NONNULL_BEGIN @@ -219,10 +220,37 @@ length: (size_t)length target: (id)target selector: (SEL)selector context: (nullable id)context; +/*! + * @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 async receive + * @param target The target on which the selector should be called when the + * datagram has been received. If the method 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. + * @param selector The selector to call on the target. The signature must be + * `bool (OFUDPSocket *socket, void *buffer, size_t length, + * of_socket_address_t sender, id context, id exception)`. + * @param context A context object to pass along to the target + */ +- (void)asyncReceiveIntoBuffer: (void *)buffer + length: (size_t)length + runLoopMode: (of_run_loop_mode_t)runLoopMode + target: (id)target + selector: (SEL)selector + context: (nullable id)context; + #ifdef OF_HAVE_BLOCKS /*! * @brief Asynchronously receives a datagram and stores it into the specified * buffer. * @@ -238,10 +266,31 @@ * method. */ - (void)asyncReceiveIntoBuffer: (void *)buffer length: (size_t)length block: (of_udp_socket_async_receive_block_t)block; + +/*! + * @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 async receive + * @param block The block to call when the datagram has been received. If the + * block 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: (of_run_loop_mode_t)runLoopMode + block: (of_udp_socket_async_receive_block_t)block; #endif /*! * @brief Sends the specified datagram to the specified address. * @@ -278,10 +327,38 @@ receiver: (of_socket_address_t)receiver target: (id)target selector: (SEL)selector context: (nullable id)context; +/*! + * @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 receiver A pointer to an @ref of_socket_address_t to which the + * datagram should be sent + * @param runLoopMode The run loop mode in which to perform the async send + * @param target The target on which the selector should be called when the + * packet has been sent. The method 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. + * @param selector The selector to call on the target. The signature must be + * `size_t (OFUDPSocket *socket, const void **buffer, + * size_t bytesSent, of_socket_address_t *receiver, id context, + * id exception)`. + * @param context A context object to pass along to the target + */ +- (void)asyncSendBuffer: (const void *)buffer + length: (size_t)length + receiver: (of_socket_address_t)receiver + runLoopMode: (of_run_loop_mode_t)runLoopMode + target: (id)target + selector: (SEL)selector + context: (nullable id)context; + #ifdef OF_HAVE_BLOCKS /*! * @brief Asynchronously sends the specified datagram to the specified address. * * @param buffer The buffer to send as a datagram @@ -296,10 +373,30 @@ */ - (void)asyncSendBuffer: (const void *)buffer length: (size_t)length receiver: (of_socket_address_t)receiver block: (of_udp_socket_async_send_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 receiver A pointer to an @ref of_socket_address_t to which the + * datagram should be sent + * @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. + */ +- (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; #endif /*! * @brief Cancels all pending asynchronous requests on the socket. */ Index: src/OFUDPSocket.m ================================================================== --- src/OFUDPSocket.m +++ src/OFUDPSocket.m @@ -512,15 +512,30 @@ - (void)asyncReceiveIntoBuffer: (void *)buffer length: (size_t)length target: (id)target selector: (SEL)selector context: (id)context +{ + [self asyncReceiveIntoBuffer: buffer + length: length + runLoopMode: of_run_loop_mode_default + target: target + selector: selector + context: context]; +} + +- (void)asyncReceiveIntoBuffer: (void *)buffer + length: (size_t)length + runLoopMode: (of_run_loop_mode_t)runLoopMode + target: (id)target + selector: (SEL)selector + context: (id)context { [OFRunLoop of_addAsyncReceiveForUDPSocket: self buffer: buffer length: length - mode: of_run_loop_mode_default + mode: runLoopMode target: target selector: selector context: context]; } @@ -527,14 +542,25 @@ #ifdef OF_HAVE_BLOCKS - (void)asyncReceiveIntoBuffer: (void *)buffer length: (size_t)length block: (of_udp_socket_async_receive_block_t)block { + [self asyncReceiveIntoBuffer: buffer + length: length + runLoopMode: of_run_loop_mode_default + block: block]; +} + +- (void)asyncReceiveIntoBuffer: (void *)buffer + length: (size_t)length + runLoopMode: (of_run_loop_mode_t)runLoopMode + block: (of_udp_socket_async_receive_block_t)block +{ [OFRunLoop of_addAsyncReceiveForUDPSocket: self buffer: buffer length: length - mode: of_run_loop_mode_default + mode: runLoopMode block: block]; } #endif - (void)sendBuffer: (const void *)buffer @@ -583,16 +609,33 @@ length: (size_t)length receiver: (of_socket_address_t)receiver target: (id)target selector: (SEL)selector context: (id)context +{ + [self asyncSendBuffer: buffer + length: length + receiver: receiver + runLoopMode: of_run_loop_mode_default + target: target + selector: selector + context: context]; +} + +- (void)asyncSendBuffer: (const void *)buffer + length: (size_t)length + receiver: (of_socket_address_t)receiver + runLoopMode: (of_run_loop_mode_t)runLoopMode + target: (id)target + selector: (SEL)selector + context: (id)context { [OFRunLoop of_addAsyncSendForUDPSocket: self buffer: buffer length: length receiver: receiver - mode: of_run_loop_mode_default + mode: runLoopMode target: target selector: selector context: context]; } @@ -600,15 +643,28 @@ - (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 receiver: receiver - mode: of_run_loop_mode_default + mode: runLoopMode block: block]; } #endif - (void)cancelAsyncRequests