Index: src/OFSPXSocket.h ================================================================== --- src/OFSPXSocket.h +++ src/OFSPXSocket.h @@ -29,14 +29,31 @@ #ifdef OF_HAVE_BLOCKS /** * @brief A block which is called when the socket connected. * + * @deprecated Use @ref OFSPXSocketConnectedHandler instead. + * + * @param exception An exception which occurred while connecting the socket or + * `nil` on success + */ +typedef void (^OFSPXSocketAsyncConnectBlock)(id _Nullable exception) + OF_DEPRECATED(ObjFW, 1, 2, "Use OFSPXSocketConnectedHandler 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 (^OFSPXSocketAsyncConnectBlock)(id _Nullable exception); +typedef void (^OFSPXSocketConnectedHandler)(OFSPXSocket *socket, + uint32_t network, const unsigned char node[_Nonnull IPX_NODE_LEN], + uint16_t port, id _Nullable exception); #endif /** * @protocol OFSPXSocketDelegate OFSPXSocket.h ObjFW/ObjFW.h * @@ -129,23 +146,44 @@ #ifdef OF_HAVE_BLOCKS /** * @brief Asynchronously connect the OFSPXSocket to the specified destination. * + * @deprecated Use @ref asyncConnectToNetwork:node:port:handler: instead. + * * @param node The node to connect to * @param network The network on which the node to connect to is * @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: (OFSPXSocketAsyncConnectBlock)block; + block: (OFSPXSocketAsyncConnectBlock)block + OF_DEPRECATED(ObjFW, 1, 2, + "Use -[asyncConnectToNetwork:node:port:handler:] instead"); + +/** + * @brief Asynchronously connect the OFSPXSocket to the specified destination. + * + * @param node The node to connect to + * @param network The network on which the node to connect to is + * @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: (OFSPXSocketConnectedHandler)handler; /** * @brief Asynchronously connect the OFSPXSocket to the specified destination. + * + * @deprecated Use @ref asyncConnectToNetwork:node:port:runLoopMode:handler: + * instead. * * @param node The node to connect to * @param network The network on which the node to connect to is * @param port The port (sometimes also called socket number) on the node to * connect to @@ -155,11 +193,30 @@ */ - (void)asyncConnectToNetwork: (uint32_t)network node: (const unsigned char [_Nonnull IPX_NODE_LEN])node port: (uint16_t)port runLoopMode: (OFRunLoopMode)runLoopMode - block: (OFSPXSocketAsyncConnectBlock)block; + block: (OFSPXSocketAsyncConnectBlock)block + OF_DEPRECATED(ObjFW, 1, 2, + "Use -[asyncConnectToNetwork:node:port:runLoopMode:handler:] instead"); + +/** + * @brief Asynchronously connect the OFSPXSocket to the specified destination. + * + * @param node The node to connect to + * @param network The network on which the node to connect to is + * @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: (OFSPXSocketConnectedHandler)handler; #endif /** * @brief Bind the socket to the specified network, node and port. * Index: src/OFSPXSocket.m ================================================================== --- src/OFSPXSocket.m +++ src/OFSPXSocket.m @@ -52,20 +52,20 @@ OFSPXSocket *_socket; uint32_t _network; unsigned char _node[IPX_NODE_LEN]; uint16_t _port; #ifdef OF_HAVE_BLOCKS - OFSPXSocketAsyncConnectBlock _block; + OFSPXSocketConnectedHandler _handler; #endif } - (instancetype)initWithSocket: (OFSPXSocket *)socket network: (uint32_t)network node: (const unsigned char [IPX_NODE_LEN])node port: (uint16_t)port #ifdef OF_HAVE_BLOCKS - block: (OFSPXSocketAsyncConnectBlock)block + handler: (OFSPXSocketConnectedHandler)handler #endif ; - (void)startWithRunLoopMode: (OFRunLoopMode)runLoopMode; @end @@ -73,11 +73,11 @@ - (instancetype)initWithSocket: (OFSPXSocket *)sock network: (uint32_t)network node: (const unsigned char [IPX_NODE_LEN])node port: (uint16_t)port #ifdef OF_HAVE_BLOCKS - block: (OFSPXSocketAsyncConnectBlock)block + handler: (OFSPXSocketConnectedHandler)handler #endif { self = [super init]; @try { @@ -84,11 +84,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; } @@ -98,11 +98,11 @@ - (void)dealloc { [_socket release]; #ifdef OF_HAVE_BLOCKS - [_block release]; + [_handler release]; #endif [super dealloc]; } @@ -150,12 +150,12 @@ if (exception == nil) ((OFSPXSocket *)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 @@ -275,11 +275,11 @@ initWithSocket: self network: network node: node port: port #ifdef OF_HAVE_BLOCKS - block: NULL + handler: NULL #endif ] autorelease] startWithRunLoopMode: runLoopMode]; objc_autoreleasePoolPop(pool); } @@ -288,31 +288,68 @@ - (void)asyncConnectToNetwork: (uint32_t)network node: (const unsigned char [IPX_NODE_LEN])node port: (uint16_t)port block: (OFSPXSocketAsyncConnectBlock)block { + OFSPXSocketConnectedHandler handler = ^ (OFSPXSocket *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: (OFSPXSocketConnectedHandler)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: (OFSPXSocketAsyncConnectBlock)block { + OFSPXSocketConnectedHandler handler = ^ (OFSPXSocket *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: (OFSPXSocketConnectedHandler)handler +{ void *pool = objc_autoreleasePoolPush(); [[[[OFSPXSocketAsyncConnectDelegate alloc] initWithSocket: self network: network node: node port: port - block: block + handler: handler ] autorelease] startWithRunLoopMode: runLoopMode]; objc_autoreleasePoolPop(pool); } #endif