Differences From Artifact [df6639e5af]:
- File
src/OFTCPSocket.m
— part of check-in
[d16ad96cbd]
at
2018-12-07 01:33:47
on branch trunk
— OFStream: Use a delegate for async operations
The target / selector approach had several drawbacks:
* It was inconvenient to use, as for every read or write, a target,
selector and context would need to be specified.
* It lacked any kind of type-safety and would not even warn about using
a callback method with the wrong number of parameters.
* It encouraged using a different callback method for each read or
write call, which results in code that is hard to follow and also
slower (as it needs to recreate the async operation with a new
callback every time). (user: js, size: 26324) [annotate] [blame] [check-ins using]
To Artifact [8cf85678b9]:
- File src/OFTCPSocket.m — part of check-in [27153bf8cf] at 2018-12-08 16:05:51 on branch trunk — OFTCPSocket: Use a delegate for async operations (user: js, size: 25944) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
60 61 62 63 64 65 66 | Class of_tls_socket_class = Nil; static of_run_loop_mode_t connectRunLoopMode = @"of_tcp_socket_connect_mode"; static OFString *defaultSOCKS5Host = nil; static uint16_t defaultSOCKS5Port = 1080; | | | < < | 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | Class of_tls_socket_class = Nil; static of_run_loop_mode_t connectRunLoopMode = @"of_tcp_socket_connect_mode"; static OFString *defaultSOCKS5Host = nil; static uint16_t defaultSOCKS5Port = 1080; @interface OFTCPSocket_AsyncConnectContext: OFObject <OFTCPSocketDelegate> { OFTCPSocket *_socket; OFString *_host; uint16_t _port; OFString *_SOCKS5Host; uint16_t _SOCKS5Port; id <OFTCPSocketDelegate> _delegate; #ifdef OF_HAVE_BLOCKS of_tcp_socket_async_connect_block_t _block; #endif id _exception; OFData *_socketAddresses; size_t _socketAddressesIndex; enum { |
︙ | ︙ | |||
94 95 96 97 98 99 100 | } - (instancetype)initWithSocket: (OFTCPSocket *)sock host: (OFString *)host port: (uint16_t)port SOCKS5Host: (OFString *)SOCKS5Host SOCKS5Port: (uint16_t)SOCKS5Port | | < < < | < < < < | < < | < < | 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | } - (instancetype)initWithSocket: (OFTCPSocket *)sock host: (OFString *)host port: (uint16_t)port SOCKS5Host: (OFString *)SOCKS5Host SOCKS5Port: (uint16_t)SOCKS5Port delegate: (id <OFTCPSocketDelegate>)delegate; #ifdef OF_HAVE_BLOCKS - (instancetype)initWithSocket: (OFTCPSocket *)sock host: (OFString *)host port: (uint16_t)port SOCKS5Host: (OFString *)SOCKS5Host SOCKS5Port: (uint16_t)SOCKS5Port block: (of_tcp_socket_async_connect_block_t)block; #endif - (void)didConnect; - (void)socketDidConnect: (OFTCPSocket *)sock exception: (id)exception; - (void)tryNextAddressWithRunLoopMode: (of_run_loop_mode_t)runLoopMode; - (void)resolver: (OFDNSResolver *)resolver didResolveDomainName: (OFString *)domainName socketAddresses: (OFData *)socketAddresses context: (id)context exception: (id)exception; - (void)startWithRunLoopMode: (of_run_loop_mode_t)runLoopMode; - (void)sendSOCKS5Request; @end @interface OFTCPSocket_ConnectDelegate: OFObject <OFTCPSocketDelegate> { @public bool _done; id _exception; } @end @implementation OFTCPSocket_AsyncConnectContext - (instancetype)initWithSocket: (OFTCPSocket *)sock host: (OFString *)host port: (uint16_t)port SOCKS5Host: (OFString *)SOCKS5Host SOCKS5Port: (uint16_t)SOCKS5Port delegate: (id <OFTCPSocketDelegate>)delegate { self = [super init]; @try { _socket = [sock retain]; _host = [host copy]; _port = port; _SOCKS5Host = [SOCKS5Host copy]; _SOCKS5Port = SOCKS5Port; _delegate = [delegate retain]; [_socket setDelegate: self]; } @catch (id e) { [self release]; @throw e; } |
︙ | ︙ | |||
190 191 192 193 194 195 196 | return self; } #endif - (void)dealloc { | > > > | | | < < < | | > | > > > > > > > > > | < < | 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | return self; } #endif - (void)dealloc { #ifdef OF_HAVE_BLOCKS if (_block != NULL) #endif if ([_socket delegate] == self) [_socket setDelegate: _delegate]; [_socket release]; [_host release]; [_SOCKS5Host release]; [_delegate release]; #ifdef OF_HAVE_BLOCKS [_block release]; #endif [_exception release]; [_socketAddresses release]; [_request release]; [super dealloc]; } - (void)didConnect { if (_exception == nil) [_socket setBlocking: true]; #ifdef OF_HAVE_BLOCKS if (_block != NULL) _block(_socket, _exception); else { #endif [_socket setDelegate: _delegate]; if (_exception == nil) { if ([_delegate respondsToSelector: @selector(socket:didConnectToHost:port:)]) [_delegate socket: _socket didConnectToHost: _host port: _port]; } else { if ([_delegate respondsToSelector: @selector(stream:didFailWithException:)]) [_delegate stream: _socket didFailWithException: _exception]; } #ifdef OF_HAVE_BLOCKS } #endif } - (void)socketDidConnect: (OFTCPSocket *)sock exception: (id)exception { if (exception != nil) { if (_socketAddressesIndex >= [_socketAddresses count]) { _exception = [exception retain]; [self didConnect]; } else { |
︙ | ︙ | |||
284 285 286 287 288 289 290 | } [_socket setBlocking: false]; if (![_socket of_connectSocketToAddress: &address errNo: &errNo]) { if (errNo == EINPROGRESS) { | | < | < | 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | } [_socket setBlocking: false]; if (![_socket of_connectSocketToAddress: &address errNo: &errNo]) { if (errNo == EINPROGRESS) { SEL selector = @selector(socketDidConnect:exception:); [OFRunLoop of_addAsyncConnectForTCPSocket: _socket mode: runLoopMode target: self selector: selector]; return; } else { [_socket of_closeSocket]; if (_socketAddressesIndex >= [_socketAddresses count]) { _exception = [[OFConnectionFailedException alloc] initWithHost: _host |
︙ | ︙ | |||
497 498 499 500 501 502 503 | [_socket asyncReadIntoBuffer: _buffer exactLength: 1 runLoopMode: runLoopMode]; return false; case 4: /* IPv6 */ _SOCKS5State = SOCKS5_STATE_READ_ADDRESS; [_socket asyncReadIntoBuffer: _buffer | | | | 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 | [_socket asyncReadIntoBuffer: _buffer exactLength: 1 runLoopMode: runLoopMode]; return false; case 4: /* IPv6 */ _SOCKS5State = SOCKS5_STATE_READ_ADDRESS; [_socket asyncReadIntoBuffer: _buffer exactLength: 16 + 2 runLoopMode: runLoopMode]; return false; default: _exception = [[OFConnectionFailedException alloc] initWithHost: _host port: _port socket: self errNo: EPROTONOSUPPORT]; |
︙ | ︙ | |||
565 566 567 568 569 570 571 | didFailWithException: (id)exception { _exception = [exception retain]; [self didConnect]; } @end | | | > > > > > | > | < > > | 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 | didFailWithException: (id)exception { _exception = [exception retain]; [self didConnect]; } @end @implementation OFTCPSocket_ConnectDelegate - (void)dealloc { [_exception release]; [super dealloc]; } - (void)socket: (OF_KINDOF(OFTCPSocket *))sock didConnectToHost: (OFString *)host port: (uint16_t)port { _done = true; } - (void)stream: (OF_KINDOF(OFStream *))stream didFailWithException: (id)exception { _done = true; _exception = [exception retain]; } @end @implementation OFTCPSocket @synthesize SOCKS5Host = _SOCKS5Host, SOCKS5Port = _SOCKS5Port; @dynamic delegate; + (void)setSOCKS5Host: (OFString *)host { id old = defaultSOCKS5Host; defaultSOCKS5Host = [host copy]; [old release]; } |
︙ | ︙ | |||
691 692 693 694 695 696 697 | return errNo; } - (void)connectToHost: (OFString *)host port: (uint16_t)port { void *pool = objc_autoreleasePoolPush(); | > | | > | < < < | | | > > < < < | < < < < < < | | | | | < < | | 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 | return errNo; } - (void)connectToHost: (OFString *)host port: (uint16_t)port { void *pool = objc_autoreleasePoolPush(); id <OFTCPSocketDelegate> delegate = [_delegate retain]; OFTCPSocket_ConnectDelegate *connectDelegate = [[[OFTCPSocket_ConnectDelegate alloc] init] autorelease]; OFRunLoop *runLoop = [OFRunLoop currentRunLoop]; [self setDelegate: connectDelegate]; [self asyncConnectToHost: host port: port runLoopMode: connectRunLoopMode]; while (!connectDelegate->_done) [runLoop runMode: connectRunLoopMode beforeDate: nil]; /* Cleanup */ [runLoop runMode: connectRunLoopMode beforeDate: [OFDate date]]; if (connectDelegate->_exception != nil) @throw connectDelegate->_exception; [self setDelegate: delegate]; objc_autoreleasePoolPop(pool); } - (void)asyncConnectToHost: (OFString *)host port: (uint16_t)port { [self asyncConnectToHost: host port: port runLoopMode: of_run_loop_mode_default]; } - (void)asyncConnectToHost: (OFString *)host port: (uint16_t)port runLoopMode: (of_run_loop_mode_t)runLoopMode { void *pool = objc_autoreleasePoolPush(); [[[[OFTCPSocket_AsyncConnectContext alloc] initWithSocket: self host: host port: port SOCKS5Host: _SOCKS5Host SOCKS5Port: _SOCKS5Port delegate: _delegate] autorelease] startWithRunLoopMode: runLoopMode]; objc_autoreleasePoolPop(pool); } #ifdef OF_HAVE_BLOCKS - (void)asyncConnectToHost: (OFString *)host |
︙ | ︙ | |||
772 773 774 775 776 777 778 | port: (uint16_t)port runLoopMode: (of_run_loop_mode_t)runLoopMode block: (of_tcp_socket_async_connect_block_t)block { void *pool = objc_autoreleasePoolPush(); [[[[OFTCPSocket_AsyncConnectContext alloc] | | | | | | | | 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 | port: (uint16_t)port runLoopMode: (of_run_loop_mode_t)runLoopMode block: (of_tcp_socket_async_connect_block_t)block { void *pool = objc_autoreleasePoolPush(); [[[[OFTCPSocket_AsyncConnectContext alloc] initWithSocket: self host: host port: port SOCKS5Host: _SOCKS5Host SOCKS5Port: _SOCKS5Port block: block] autorelease] startWithRunLoopMode: runLoopMode]; objc_autoreleasePoolPop(pool); } #endif - (uint16_t)bindToHost: (OFString *)host |
︙ | ︙ | |||
1003 1004 1005 1006 1007 1008 1009 | OF_SOCKET_ADDRESS_FAMILY_UNKNOWN; break; } return client; } | | < < | < < < < < < < | < | 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 | OF_SOCKET_ADDRESS_FAMILY_UNKNOWN; break; } return client; } - (void)asyncAccept { [self asyncAcceptWithRunLoopMode: of_run_loop_mode_default]; } - (void)asyncAcceptWithRunLoopMode: (of_run_loop_mode_t)runLoopMode { [OFRunLoop of_addAsyncAcceptForTCPSocket: self mode: runLoopMode delegate: _delegate]; } #ifdef OF_HAVE_BLOCKS - (void)asyncAcceptWithBlock: (of_tcp_socket_async_accept_block_t)block { [self asyncAcceptWithRunLoopMode: of_run_loop_mode_default block: block]; |
︙ | ︙ |