Index: src/OFAsyncIPSocketConnector.m ================================================================== --- src/OFAsyncIPSocketConnector.m +++ src/OFAsyncIPSocketConnector.m @@ -74,11 +74,12 @@ [_socket setCanBlock: true]; #ifdef OF_HAVE_BLOCKS if (_handler != NULL) { if ([_socket isKindOfClass: [OFTCPSocket class]]) - ((OFTCPSocketAsyncConnectBlock)_handler)(_exception); + ((OFTCPSocketConnectedHandler)_handler)(_socket, _host, + _port, _exception); # ifdef OF_HAVE_SCTP else if ([_socket isKindOfClass: [OFSCTPSocket class]]) ((OFSCTPSocketConnectedHandler)_handler)(_socket, _host, _port, _exception); # endif Index: src/OFTCPSocket.h ================================================================== --- src/OFTCPSocket.h +++ src/OFTCPSocket.h @@ -29,14 +29,29 @@ #ifdef OF_HAVE_BLOCKS /** * @brief A block which is called when the socket connected. * + * @deprecated Use @ref OFTCPSocketConnecetedHandler instead. + * + * @param exception An exception which occurred while connecting the socket or + * `nil` on success + */ +typedef void (^OFTCPSocketAsyncConnectBlock)(id _Nullable exception) + OF_DEPRECATED(ObjFW, 1, 2, "Use OFTCPSocketConnecetedHandler instead"); + +/** + * @brief A handler which is called when the socket connected. + * + * @param socket The socket which connected + * @param host The host connected to + * @param port The port on the host connected to * @param exception An exception which occurred while connecting the socket or * `nil` on success */ -typedef void (^OFTCPSocketAsyncConnectBlock)(id _Nullable exception); +typedef void (^OFTCPSocketConnectedHandler)(OFTCPSocket *socket, + OFString *host, uint16_t port, id _Nullable exception); #endif /** * @protocol OFTCPSocketDelegate OFTCPSocket.h ObjFW/ObjFW.h * @@ -187,20 +202,37 @@ #ifdef OF_HAVE_BLOCKS /** * @brief Asynchronously connects the OFTCPSocket to the specified destination. * + * @deprecated Use @ref asyncConnectToHost:port:handler: instead. + * * @param host The host to connect to * @param port The port on the host to connect to * @param block The block to execute once the connection has been established */ - (void)asyncConnectToHost: (OFString *)host port: (uint16_t)port - block: (OFTCPSocketAsyncConnectBlock)block; + block: (OFTCPSocketAsyncConnectBlock)block + OF_DEPRECATED(ObjFW, 1, 2, + "Use -[asyncConnectToHost:port:handler:] instead"); + +/** + * @brief Asynchronously connects the OFTCPSocket to the specified destination. + * + * @param host The host to connect to + * @param port The port on the host to connect to + * @param handler The handler to call once the connection has been established + */ +- (void)asyncConnectToHost: (OFString *)host + port: (uint16_t)port + handler: (OFTCPSocketConnectedHandler)handler; /** * @brief Asynchronously connects the OFTCPSocket to the specified destination. + * + * @deprecated Use @ref asyncConnectToHost:port:runLoopMode:handler: instead. * * @param host The host to connect to * @param port The port on the host to connect to * @param runLoopMode The run loop mode in which to perform the asynchronous * connect @@ -207,11 +239,27 @@ * @param block The block to execute once the connection has been established */ - (void)asyncConnectToHost: (OFString *)host port: (uint16_t)port runLoopMode: (OFRunLoopMode)runLoopMode - block: (OFTCPSocketAsyncConnectBlock)block; + block: (OFTCPSocketAsyncConnectBlock)block + OF_DEPRECATED(ObjFW, 1, 2, + "Use -[asyncConnectToHost:port:runLoopMode:handler:] instead"); + +/** + * @brief Asynchronously connects the OFTCPSocket to the specified destination. + * + * @param host The host to connect to + * @param port The port on the host 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)asyncConnectToHost: (OFString *)host + port: (uint16_t)port + runLoopMode: (OFRunLoopMode)runLoopMode + handler: (OFTCPSocketConnectedHandler)handler; #endif /** * @brief Binds the socket to the specified host and port. * Index: src/OFTCPSocket.m ================================================================== --- src/OFTCPSocket.m +++ src/OFTCPSocket.m @@ -229,11 +229,11 @@ initWithSocket: self host: host port: port delegate: _delegate #ifdef OF_HAVE_BLOCKS - block: NULL + handler: NULL #endif ] autorelease]; host = _SOCKS5Host; port = _SOCKS5Port; } else @@ -253,20 +253,51 @@ #ifdef OF_HAVE_BLOCKS - (void)asyncConnectToHost: (OFString *)host port: (uint16_t)port block: (OFTCPSocketAsyncConnectBlock)block { + OFTCPSocketConnectedHandler handler = ^ (OFTCPSocket *socket, + OFString *host_, uint16_t port_, id exception) { + block(exception); + }; + + [self asyncConnectToHost: host + port: port + runLoopMode: OFDefaultRunLoopMode + handler: handler]; +} + +- (void)asyncConnectToHost: (OFString *)host + port: (uint16_t)port + handler: (OFTCPSocketConnectedHandler)handler +{ [self asyncConnectToHost: host port: port runLoopMode: OFDefaultRunLoopMode - block: block]; + handler: handler]; } - (void)asyncConnectToHost: (OFString *)host port: (uint16_t)port runLoopMode: (OFRunLoopMode)runLoopMode block: (OFTCPSocketAsyncConnectBlock)block +{ + OFTCPSocketConnectedHandler handler = ^ (OFTCPSocket *socket, + OFString *host_, uint16_t port_, id exception) { + block(exception); + }; + + [self asyncConnectToHost: host + port: port + runLoopMode: runLoopMode + handler: handler]; +} + +- (void)asyncConnectToHost: (OFString *)host + port: (uint16_t)port + runLoopMode: (OFRunLoopMode)runLoopMode + handler: (OFTCPSocketConnectedHandler)handler { void *pool = objc_autoreleasePoolPush(); id delegate = nil; if (_SOCKS5Host != nil) { @@ -273,21 +304,22 @@ delegate = [[[OFTCPSocketSOCKS5Connector alloc] initWithSocket: self host: host port: port delegate: nil - block: block] autorelease]; + handler: handler] autorelease]; host = _SOCKS5Host; port = _SOCKS5Port; } [[[[OFAsyncIPSocketConnector alloc] initWithSocket: self host: host port: port delegate: delegate - handler: (delegate == nil ? block : NULL)] autorelease] + handler: (delegate == nil + ? handler : NULL)] autorelease] startWithRunLoopMode: runLoopMode]; objc_autoreleasePoolPop(pool); } #endif Index: src/OFTCPSocketSOCKS5Connector.h ================================================================== --- src/OFTCPSocketSOCKS5Connector.h +++ src/OFTCPSocketSOCKS5Connector.h @@ -28,11 +28,11 @@ OFTCPSocket *_socket; OFString *_host; uint16_t _port; id _Nullable _delegate; #ifdef OF_HAVE_BLOCKS - OFTCPSocketAsyncConnectBlock _Nullable _block; + OFTCPSocketConnectedHandler _Nullable _handler; #endif id _Nullable _exception; uint_least8_t _SOCKS5State; /* Longest read is domain name (max 255 bytes) + port */ unsigned char _buffer[257]; @@ -42,12 +42,12 @@ - (instancetype)initWithSocket: (OFTCPSocket *)sock host: (OFString *)host port: (uint16_t)port delegate: (nullable id )delegate #ifdef OF_HAVE_BLOCKS - block: (nullable OFTCPSocketAsyncConnectBlock)block + handler: (nullable OFTCPSocketConnectedHandler)handler #endif ; - (void)didConnect; @end OF_ASSUME_NONNULL_END Index: src/OFTCPSocketSOCKS5Connector.m ================================================================== --- src/OFTCPSocketSOCKS5Connector.m +++ src/OFTCPSocketSOCKS5Connector.m @@ -41,11 +41,11 @@ - (instancetype)initWithSocket: (OFTCPSocket *)sock host: (OFString *)host port: (uint16_t)port delegate: (id )delegate #ifdef OF_HAVE_BLOCKS - block: (OFTCPSocketAsyncConnectBlock)block + handler: (OFTCPSocketConnectedHandler)handler #endif { self = [super init]; @try { @@ -52,11 +52,11 @@ _socket = [sock retain]; _host = [host copy]; _port = port; _delegate = [delegate retain]; #ifdef OF_HAVE_BLOCKS - _block = [block copy]; + _handler = [handler copy]; #endif _socket.delegate = self; } @catch (id e) { [self release]; @@ -73,11 +73,11 @@ [_socket release]; [_host release]; [_delegate release]; #ifdef OF_HAVE_BLOCKS - [_block release]; + [_handler release]; #endif [_exception release]; [_request release]; [super dealloc]; @@ -86,12 +86,12 @@ - (void)didConnect { _socket.delegate = _delegate; #ifdef OF_HAVE_BLOCKS - if (_block != NULL) - _block(_exception); + if (_handler != NULL) + _handler(_socket, _host, _port, _exception); else { #endif if ([_delegate respondsToSelector: @selector(socket:didConnectToHost:port:exception:)]) [_delegate socket: _socket