Index: src/OFSPXStreamSocket.h ================================================================== --- src/OFSPXStreamSocket.h +++ src/OFSPXStreamSocket.h @@ -29,14 +29,31 @@ #ifdef OF_HAVE_BLOCKS /** * @brief A block which is called when the socket connected. * + * @deprecated Use @ref OFSPXStreamSocketConnectedHandler instead. + * + * @param exception An exception which occurred while connecting the socket or + * `nil` on success + */ +typedef void (^OFSPXStreamSocketAsyncConnectBlock)(id _Nullable exception) + OF_DEPRECATED(ObjFW, 1, 2, "Use OFSPXStreamSocketConnectedHandler instead"); + +/** + * @brief A handler which is called when the socket connected. + * + * @param socket The socket which connected + * @param network The network of the node the socket connected to + * @param node The node the socket connected to + * @param port The port of the node to which the socket connected * @param exception An exception which occurred while connecting the socket or * `nil` on success */ -typedef void (^OFSPXStreamSocketAsyncConnectBlock)(id _Nullable exception); +typedef void (^OFSPXStreamSocketConnectedHandler)(OFSPXStreamSocket *socket, + uint32_t network, const unsigned char node[_Nonnull IPX_NODE_LEN], + uint16_t port, id _Nullable exception); #endif /** * @protocol OFSPXStreamSocketDelegate OFSPXStreamSocket.h ObjFW/ObjFW.h * @@ -132,24 +149,46 @@ #ifdef OF_HAVE_BLOCKS /** * @brief Asynchronously connect the OFSPXStreamSocket to the specified * destination. * + * @deprecated Use @ref asyncConnectToNetwork:node:port:handler: instead. + * * @param network The network on which the node to connect to is * @param node The node to connect to * @param port The port (sometimes also called socket number) on the node to * connect to * @param block The block to execute once the connection has been established */ - (void)asyncConnectToNetwork: (uint32_t)network node: (const unsigned char [_Nonnull IPX_NODE_LEN])node port: (uint16_t)port - block: (OFSPXStreamSocketAsyncConnectBlock)block; + block: (OFSPXStreamSocketAsyncConnectBlock)block + OF_DEPRECATED(ObjFW, 1, 2, + "Use -[asyncConnectToNetwork:node:port:handler:] instead"); + +/** + * @brief Asynchronously connect the OFSPXStreamSocket to the specified + * destination. + * + * @param network The network on which the node to connect to is + * @param node The node to connect to + * @param port The port (sometimes also called socket number) on the node to + * connect to + * @param handler The handler to call once the connection has been established + */ +- (void)asyncConnectToNetwork: (uint32_t)network + node: (const unsigned char [_Nonnull IPX_NODE_LEN])node + port: (uint16_t)port + handler: (OFSPXStreamSocketConnectedHandler)handler; /** * @brief Asynchronously connect the OFSPXStreamSocket to the specified * destination. + * + * @deprecated Use @ref asyncConnectToNetwork:node:port:runLoopMode:handler: + * instead. * * @param network The network on which the node to connect to is * @param node The node to connect to * @param port The port (sometimes also called socket number) on the node to * connect to @@ -159,11 +198,31 @@ */ - (void)asyncConnectToNetwork: (uint32_t)network node: (const unsigned char [_Nonnull IPX_NODE_LEN])node port: (uint16_t)port runLoopMode: (OFRunLoopMode)runLoopMode - block: (OFSPXStreamSocketAsyncConnectBlock)block; + block: (OFSPXStreamSocketAsyncConnectBlock)block + OF_DEPRECATED(ObjFW, 1, 2, + "Use -[asyncConnectToNetwork:node:port:runLoopMode:handler:] instead"); + +/** + * @brief Asynchronously connect the OFSPXStreamSocket to the specified + * destination. + * + * @param network The network on which the node to connect to is + * @param node The node to connect to + * @param port The port (sometimes also called socket number) on the node to + * connect to + * @param runLoopMode The run loop mode in which to perform the asynchronous + * connect + * @param handler The handler to call once the connection has been established + */ +- (void)asyncConnectToNetwork: (uint32_t)network + node: (const unsigned char [_Nonnull IPX_NODE_LEN])node + port: (uint16_t)port + runLoopMode: (OFRunLoopMode)runLoopMode + handler: (OFSPXStreamSocketConnectedHandler)handler; #endif /** * @brief Bind the socket to the specified network, node and port. * Index: src/OFSPXStreamSocket.m ================================================================== --- src/OFSPXStreamSocket.m +++ src/OFSPXStreamSocket.m @@ -53,20 +53,20 @@ OFSPXStreamSocket *_socket; uint32_t _network; unsigned char _node[IPX_NODE_LEN]; uint16_t _port; #ifdef OF_HAVE_BLOCKS - OFSPXStreamSocketAsyncConnectBlock _block; + OFSPXStreamSocketConnectedHandler _handler; #endif } - (instancetype)initWithSocket: (OFSPXStreamSocket *)socket network: (uint32_t)network node: (const unsigned char [IPX_NODE_LEN])node port: (uint16_t)port #ifdef OF_HAVE_BLOCKS - block: (OFSPXStreamSocketAsyncConnectBlock)block + handler: (OFSPXStreamSocketConnectedHandler)handler #endif ; - (void)startWithRunLoopMode: (OFRunLoopMode)runLoopMode; @end @@ -74,11 +74,11 @@ - (instancetype)initWithSocket: (OFSPXStreamSocket *)sock network: (uint32_t)network node: (const unsigned char [IPX_NODE_LEN])node port: (uint16_t)port #ifdef OF_HAVE_BLOCKS - block: (OFSPXStreamSocketAsyncConnectBlock)block + handler: (OFSPXStreamSocketConnectedHandler)handler #endif { self = [super init]; @try { @@ -85,11 +85,11 @@ _socket = [sock retain]; _network = network; memcpy(_node, node, IPX_NODE_LEN); _port = port; #ifdef OF_HAVE_BLOCKS - _block = [block copy]; + _handler = [handler copy]; #endif } @catch (id e) { [self release]; @throw e; } @@ -99,11 +99,11 @@ - (void)dealloc { [_socket release]; #ifdef OF_HAVE_BLOCKS - [_block release]; + [_handler release]; #endif [super dealloc]; } @@ -152,12 +152,12 @@ if (exception == nil) ((OFSPXStreamSocket *)sock).canBlock = true; #ifdef OF_HAVE_BLOCKS - if (_block != NULL) - _block(exception); + if (_handler != NULL) + _handler(_socket, _network, _node, _port, exception); else { #endif if ([delegate respondsToSelector: @selector(socket:didConnectToNetwork:node:port:exception:)]) [delegate socket: _socket @@ -277,11 +277,11 @@ initWithSocket: self network: network node: node port: port #ifdef OF_HAVE_BLOCKS - block: NULL + handler: NULL #endif ] autorelease] startWithRunLoopMode: runLoopMode]; objc_autoreleasePoolPop(pool); } @@ -290,31 +290,70 @@ - (void)asyncConnectToNetwork: (uint32_t)network node: (const unsigned char [IPX_NODE_LEN])node port: (uint16_t)port block: (OFSPXStreamSocketAsyncConnectBlock)block { + OFSPXStreamSocketConnectedHandler handler = ^ ( + OFSPXStreamSocket *socket, uint32_t network_, + const unsigned char node_[IPX_NODE_LEN], uint16_t port_, + id exception) { + block(exception); + }; + + [self asyncConnectToNetwork: network + node: node + port: port + runLoopMode: OFDefaultRunLoopMode + handler: handler]; +} + +- (void)asyncConnectToNetwork: (uint32_t)network + node: (const unsigned char [IPX_NODE_LEN])node + port: (uint16_t)port + handler: (OFSPXStreamSocketConnectedHandler)handler +{ [self asyncConnectToNetwork: network node: node port: port runLoopMode: OFDefaultRunLoopMode - block: block]; + handler: handler]; } - (void)asyncConnectToNetwork: (uint32_t)network node: (const unsigned char [IPX_NODE_LEN])node port: (uint16_t)port runLoopMode: (OFRunLoopMode)runLoopMode block: (OFSPXStreamSocketAsyncConnectBlock)block { + OFSPXStreamSocketConnectedHandler handler = ^ ( + OFSPXStreamSocket *socket, uint32_t network_, + const unsigned char node_[IPX_NODE_LEN], uint16_t port_, + id exception) { + block(exception); + }; + + [self asyncConnectToNetwork: network + node: node + port: port + runLoopMode: runLoopMode + handler: handler]; +} + +- (void)asyncConnectToNetwork: (uint32_t)network + node: (const unsigned char [IPX_NODE_LEN])node + port: (uint16_t)port + runLoopMode: (OFRunLoopMode)runLoopMode + handler: (OFSPXStreamSocketConnectedHandler)handler +{ void *pool = objc_autoreleasePoolPush(); [[[[OFSPXStreamSocketAsyncConnectDelegate alloc] initWithSocket: self network: network node: node port: port - block: block + handler: handler ] autorelease] startWithRunLoopMode: runLoopMode]; objc_autoreleasePoolPop(pool); } #endif