@@ -73,14 +73,10 @@ #ifdef OF_HAVE_SOCKETS @interface OFRunLoop_QueueItem: OFObject { @public id _delegate; - /* TODO: Remove once everything is moved to using delegates */ - id _target; - SEL _selector; - id _context; } - (bool)handleObject: (id)object; @end @@ -301,12 +297,10 @@ } - (void)dealloc { [_delegate release]; - [_target release]; - [_context release]; [super dealloc]; } @end @@ -545,22 +539,22 @@ @implementation OFRunLoop_ConnectQueueItem - (bool)handleObject: (id)object { id exception = nil; int errNo; - void (*func)(id, SEL, OFTCPSocket *, id, id); if ((errNo = [object of_socketError]) != 0) exception = [OFConnectionFailedException exceptionWithHost: nil port: 0 socket: object errNo: errNo]; - func = (void (*)(id, SEL, OFTCPSocket *, id, id)) - [_target methodForSelector: _selector]; - func(_target, _selector, object, _context, exception); + if ([_delegate respondsToSelector: + @selector(of_socketDidConnect:exception:)]) + [_delegate of_socketDidConnect: object + exception: exception]; return false; } @end @@ -631,18 +625,27 @@ # ifdef OF_HAVE_BLOCKS if (_block != NULL) return _block(object, _buffer, length, address, exception); else { # endif - bool (*func)(id, SEL, OFUDPSocket *, void *, size_t, - of_socket_address_t, id, id) = - (bool (*)(id, SEL, OFUDPSocket *, void *, size_t, - of_socket_address_t, id, id)) - [_target methodForSelector: _selector]; - - return func(_target, _selector, object, _buffer, length, - address, _context, exception); + if (exception == nil) { + if (![_delegate respondsToSelector: @selector(socket: + didReceiveIntoBuffer:length:sender:)]) + return false; + + return [_delegate socket: object + didReceiveIntoBuffer: _buffer + length: length + sender: address]; + } else { + if ([_delegate respondsToSelector: + @selector(socket:didFailToReceiveWithException:)]) + [_delegate socket: object + didFailToReceiveWithException: exception]; + + return false; + } # ifdef OF_HAVE_BLOCKS } # endif } @@ -675,21 +678,29 @@ (exception == nil ? _length : 0), &_receiver, exception); return (_length > 0); } else { # endif - size_t (*func)(id, SEL, OFUDPSocket *, const void *, size_t, - of_socket_address_t *, id, id) = - (size_t (*)(id, SEL, OFUDPSocket *, const void *, size_t, - of_socket_address_t *, id, id)) - [_target methodForSelector: _selector]; - - _length = func(_target, _selector, object, &_buffer, - (exception == nil ? _length : 0), &_receiver, _context, - exception); - - return (_length > 0); + if (exception == nil) { + if (![_delegate respondsToSelector: + @selector(socket:didSendBuffer:length:receiver:)]) + return false; + + _length = [_delegate socket: object + didSendBuffer: &_buffer + length: _length + receiver: &_receiver]; + + return (_length > 0); + } else { + if ([_delegate respondsToSelector: + @selector(socket:didFailToSendWithException:)]) + [_delegate socket: object + didFailToSendWithException: exception]; + + return false; + } # ifdef OF_HAVE_BLOCKS } # endif } @@ -828,16 +839,15 @@ }) } + (void)of_addAsyncConnectForTCPSocket: (OFTCPSocket *)stream mode: (of_run_loop_mode_t)mode - target: (id)target - selector: (SEL)selector + delegate: (id ) + delegate { ADD_WRITE(OFRunLoop_ConnectQueueItem, stream, mode, { - queueItem->_target = [target retain]; - queueItem->_selector = selector; + queueItem->_delegate = [delegate retain]; }) } + (void)of_addAsyncAcceptForTCPSocket: (OFTCPSocket *)stream mode: (of_run_loop_mode_t)mode @@ -850,18 +860,14 @@ + (void)of_addAsyncReceiveForUDPSocket: (OFUDPSocket *)sock buffer: (void *)buffer length: (size_t)length mode: (of_run_loop_mode_t)mode - target: (id)target - selector: (SEL)selector - context: (id)context + delegate: (id )delegate { ADD_READ(OFRunLoop_UDPReceiveQueueItem, sock, mode, { - queueItem->_target = [target retain]; - queueItem->_selector = selector; - queueItem->_context = [context retain]; + queueItem->_delegate = [delegate retain]; queueItem->_buffer = buffer; queueItem->_length = length; }) } @@ -868,18 +874,14 @@ + (void)of_addAsyncSendForUDPSocket: (OFUDPSocket *)sock buffer: (const void *)buffer length: (size_t)length receiver: (of_socket_address_t)receiver mode: (of_run_loop_mode_t)mode - target: (id)target - selector: (SEL)selector - context: (id)context + delegate: (id )delegate { ADD_WRITE(OFRunLoop_UDPSendQueueItem, sock, mode, { - queueItem->_target = [target retain]; - queueItem->_selector = selector; - queueItem->_context = [context retain]; + queueItem->_delegate = [delegate retain]; queueItem->_buffer = buffer; queueItem->_length = length; queueItem->_receiver = receiver; }) }