Overview
Comment: | OFTCPSocket: Allow specifying the run loop mode |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
29e9a4c8680803147c94718ae4385e83 |
User & Date: | js on 2018-10-06 18:24:10 |
Other Links: | manifest | tags |
Context
2018-10-06
| ||
18:31 | .travis.yml: Change download URL for amiga-gcc check-in: d7795b525e user: js tags: trunk | |
18:24 | OFTCPSocket: Allow specifying the run loop mode check-in: 29e9a4c868 user: js tags: trunk | |
17:44 | OFRunLoop: Fix missing nullable check-in: 3c0d72802a user: js tags: trunk | |
Changes
Modified src/OFTCPSocket.h from [0871d5445b] to [38f799c939].
︙ | ︙ | |||
12 13 14 15 16 17 18 19 20 21 22 23 24 25 | * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #import "OFStreamSocket.h" #import "socket.h" OF_ASSUME_NONNULL_BEGIN /*! @file */ | > | 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #import "OFStreamSocket.h" #import "OFRunLoop.h" #import "socket.h" OF_ASSUME_NONNULL_BEGIN /*! @file */ |
︙ | ︙ | |||
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | */ - (void)asyncConnectToHost: (OFString *)host port: (uint16_t)port target: (id)target selector: (SEL)selector context: (nullable id)context; #ifdef OF_HAVE_BLOCKS /*! * @brief Asynchronously connect the OFTCPSocket to the specified destination. * * @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: (of_tcp_socket_async_connect_block_t)block; #endif /*! * @brief Bind the socket to the specified host and port. * * @param host The host to bind to. Use `@"0.0.0.0"` for IPv4 or `@"::"` for * IPv6 to bind to all. | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 167 168 169 170 171 172 173 174 175 176 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 | */ - (void)asyncConnectToHost: (OFString *)host port: (uint16_t)port target: (id)target selector: (SEL)selector context: (nullable id)context; /*! * @brief Asynchronously connect 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 async connect * @param target The target on which to call the selector once the connection * has been established * @param selector The selector to call on the target. The signature must be * `void (OFTCPSocket *socket, id context, id exception)`. * @param context A context object to pass along to the target */ - (void)asyncConnectToHost: (OFString *)host port: (uint16_t)port runLoopMode: (of_run_loop_mode_t)runLoopMode target: (id)target selector: (SEL)selector context: (nullable id)context; #ifdef OF_HAVE_BLOCKS /*! * @brief Asynchronously connect the OFTCPSocket to the specified destination. * * @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: (of_tcp_socket_async_connect_block_t)block; /*! * @brief Asynchronously connect 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 async connect * @param block The block to execute once the connection has been established */ - (void)asyncConnectToHost: (OFString *)host port: (uint16_t)port runLoopMode: (of_run_loop_mode_t)runLoopMode block: (of_tcp_socket_async_connect_block_t)block; #endif /*! * @brief Bind the socket to the specified host and port. * * @param host The host to bind to. Use `@"0.0.0.0"` for IPv4 or `@"::"` for * IPv6 to bind to all. |
︙ | ︙ | |||
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | * id context, id exception)`. * @param context A context object to pass along to the target */ - (void)asyncAcceptWithTarget: (id)target selector: (SEL)selector context: (nullable id)context; #ifdef OF_HAVE_BLOCKS /*! * @brief Asynchronously accept an incoming connection. * * @param block The block to execute when a new connection has been accepted. * Returns whether the next incoming connection should be accepted * by the specified block as well. */ - (void)asyncAcceptWithBlock: (of_tcp_socket_async_accept_block_t)block; #endif @end #ifdef __cplusplus extern "C" { #endif extern Class _Nullable of_tls_socket_class; #ifdef __cplusplus } #endif OF_ASSUME_NONNULL_END | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | * id context, id exception)`. * @param context A context object to pass along to the target */ - (void)asyncAcceptWithTarget: (id)target selector: (SEL)selector context: (nullable id)context; /*! * @brief Asynchronously accept an incoming connection. * * @param runLoopMode The run loop mode in which to perform the async accept * @param target The target on which to execute the selector when a new * connection has been accepted. The method returns whether the * next incoming connection should be accepted by the specified * block as well. * @param selector The selector to call on the target. The signature must be * `bool (OFTCPSocket *socket, OFTCPSocket *acceptedSocket, * id context, id exception)`. * @param context A context object to pass along to the target */ - (void)asyncAcceptWithRunLoopMode: (of_run_loop_mode_t)runLoopMode target: (id)target selector: (SEL)selector context: (nullable id)context; #ifdef OF_HAVE_BLOCKS /*! * @brief Asynchronously accept an incoming connection. * * @param block The block to execute when a new connection has been accepted. * Returns whether the next incoming connection should be accepted * by the specified block as well. */ - (void)asyncAcceptWithBlock: (of_tcp_socket_async_accept_block_t)block; /*! * @brief Asynchronously accept an incoming connection. * * @param runLoopMode The run loop mode in which to perform the async accept * @param block The block to execute when a new connection has been accepted. * Returns whether the next incoming connection should be accepted * by the specified block as well. */ - (void)asyncAcceptWithRunLoopMode: (of_run_loop_mode_t)runLoopMode block: (of_tcp_socket_async_accept_block_t)block; #endif @end #ifdef __cplusplus extern "C" { #endif extern Class _Nullable of_tls_socket_class; #ifdef __cplusplus } #endif OF_ASSUME_NONNULL_END |
Modified src/OFTCPSocket.m from [6cee403849] to [1071039b7f].
︙ | ︙ | |||
107 108 109 110 111 112 113 | SOCKS5Port: (uint16_t)SOCKS5Port block: (of_tcp_socket_async_connect_block_t)block; #endif - (void)didConnect; - (void)socketDidConnect: (OFTCPSocket *)sock context: (id)context exception: (id)exception; | | | | 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | SOCKS5Port: (uint16_t)SOCKS5Port block: (of_tcp_socket_async_connect_block_t)block; #endif - (void)didConnect; - (void)socketDidConnect: (OFTCPSocket *)sock context: (id)context 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; - (size_t)socket: (OFTCPSocket *)sock didSendSOCKS5Authentication: (const void *)request bytesWritten: (size_t)bytesWritten context: (id)context exception: (id)exception; - (bool)socket: (OFTCPSocket *)sock |
︙ | ︙ | |||
244 245 246 247 248 249 250 | exception: (id)exception { if (exception != nil) { if (_socketAddressesIndex >= [_socketAddresses count]) { _exception = [exception retain]; [self didConnect]; } else { | | > | | 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | exception: (id)exception { if (exception != nil) { if (_socketAddressesIndex >= [_socketAddresses count]) { _exception = [exception retain]; [self didConnect]; } else { [self tryNextAddressWithRunLoopMode: [[OFRunLoop currentRunLoop] currentMode]]; } return; } if (_SOCKS5Host != nil) [self sendSOCKS5Request]; else [self didConnect]; } - (void)tryNextAddressWithRunLoopMode: (of_run_loop_mode_t)runLoopMode { of_socket_address_t address = *(const of_socket_address_t *) [_socketAddresses itemAtIndex: _socketAddressesIndex++]; int errNo; if (_SOCKS5Host != nil) of_socket_address_set_port(&address, _SOCKS5Port); |
︙ | ︙ | |||
279 280 281 282 283 284 285 | port: _port socket: _socket errNo: errNo]; [self didConnect]; return; } | | < | | > | > | | 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | port: _port socket: _socket errNo: errNo]; [self didConnect]; return; } [self tryNextAddressWithRunLoopMode: runLoopMode]; return; } [_socket setBlocking: false]; if (![_socket of_connectSocketToAddress: &address errNo: &errNo]) { if (errNo == EINPROGRESS) { SEL selector = @selector(socketDidConnect:context: exception:); [OFRunLoop of_addAsyncConnectForTCPSocket: _socket mode: runLoopMode target: self selector: selector context: nil]; return; } else { [_socket of_closeSocket]; if (_socketAddressesIndex >= [_socketAddresses count]) { _exception = [[OFConnectionFailedException alloc] initWithHost: _host port: _port socket: _socket errNo: errNo]; [self didConnect]; return; } [self tryNextAddressWithRunLoopMode: runLoopMode]; return; } } [self didConnect]; } - (void)resolver: (OFDNSResolver *)resolver didResolveDomainName: (OFString *)domainName socketAddresses: (OFData *)socketAddresses context: (id)context exception: (id)exception { if (exception != nil) { _exception = [exception retain]; [self didConnect]; return; } _socketAddresses = [socketAddresses copy]; [self tryNextAddressWithRunLoopMode: [[OFRunLoop currentRunLoop] currentMode]]; } - (void)startWithRunLoopMode: (of_run_loop_mode_t)runLoopMode { OFString *host; uint16_t port; if (_SOCKS5Host != nil) { if ([_host UTF8StringLength] > 255) @throw [OFOutOfRangeException exception]; |
︙ | ︙ | |||
360 361 362 363 364 365 366 | of_socket_address_parse_ip(host, port); _socketAddresses = [[OFData alloc] initWithItems: &address itemSize: sizeof(address) count: 1]; | | > > > > | 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | of_socket_address_parse_ip(host, port); _socketAddresses = [[OFData alloc] initWithItems: &address itemSize: sizeof(address) count: 1]; [self tryNextAddressWithRunLoopMode: runLoopMode]; return; } @catch (OFInvalidFormatException *e) { } [[OFThread DNSResolver] asyncResolveSocketAddressesForHost: host addressFamily: OF_SOCKET_ADDRESS_FAMILY_ANY runLoopMode: runLoopMode target: self selector: @selector(resolver: didResolveDomainName: socketAddresses:context: exception:) context: nil]; } - (void)sendSOCKS5Request { [_socket asyncWriteBuffer: "\x05\x01\x00" length: 3 runLoopMode: [[OFRunLoop currentRunLoop] currentMode] target: self selector: @selector(socket:didSendSOCKS5Authentication: bytesWritten:context:exception:) context: nil]; } - (size_t)socket: (OFTCPSocket *)sock didSendSOCKS5Authentication: (const void *)request bytesWritten: (size_t)bytesWritten context: (id)context exception: (id)exception { if (exception != nil) { _exception = [exception retain]; [self didConnect]; return 0; } [_socket asyncReadIntoBuffer: _buffer exactLength: 2 runLoopMode: [[OFRunLoop currentRunLoop] currentMode] target: self selector: @selector(socket:didReadSOCKSVersion: length:context:exception:) context: nil]; return 0; } |
︙ | ︙ | |||
450 451 452 453 454 455 456 457 458 459 460 461 462 463 | port[1] = _port & 0xFF; [request addItems: port count: 2]; /* Use request as context to retain it */ [_socket asyncWriteBuffer: [request items] length: [request count] target: self selector: @selector(socket:didSendSOCKS5Request: bytesWritten:context:exception:) context: request]; return false; } | > | 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 | port[1] = _port & 0xFF; [request addItems: port count: 2]; /* Use request as context to retain it */ [_socket asyncWriteBuffer: [request items] length: [request count] runLoopMode: [[OFRunLoop currentRunLoop] currentMode] target: self selector: @selector(socket:didSendSOCKS5Request: bytesWritten:context:exception:) context: request]; return false; } |
︙ | ︙ | |||
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 | _exception = [exception retain]; [self didConnect]; return 0; } [_socket asyncReadIntoBuffer: _buffer exactLength: 4 target: self selector: @selector(socket:didReadSOCKS5Response: length:context:exception:) context: nil]; return 0; } - (bool)socket: (OFTCPSocket *)sock didReadSOCKS5Response: (unsigned char *)response length: (size_t)length context: (id)context exception: (id)exception { if (exception != nil) { _exception = [exception retain]; [self didConnect]; return false; } if (response[0] != 5 || response[2] != 0) { | > > > | 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 | _exception = [exception retain]; [self didConnect]; return 0; } [_socket asyncReadIntoBuffer: _buffer exactLength: 4 runLoopMode: [[OFRunLoop currentRunLoop] currentMode] target: self selector: @selector(socket:didReadSOCKS5Response: length:context:exception:) context: nil]; return 0; } - (bool)socket: (OFTCPSocket *)sock didReadSOCKS5Response: (unsigned char *)response length: (size_t)length context: (id)context exception: (id)exception { of_run_loop_mode_t runLoopMode; if (exception != nil) { _exception = [exception retain]; [self didConnect]; return false; } if (response[0] != 5 || response[2] != 0) { |
︙ | ︙ | |||
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 | port: _port socket: _socket errNo: errNo]; [self didConnect]; return false; } /* Skip the rest of the response */ switch (response[3]) { case 1: /* IPv4 */ [_socket asyncReadIntoBuffer: _buffer exactLength: 4 + 2 target: self selector: @selector(socket: didReadSOCKS5Address:length: context:exception:) context: nil]; return false; case 3: /* Domain name */ [_socket asyncReadIntoBuffer: _buffer exactLength: 1 target: self selector: @selector(socket: didReadSOCKS5AddressLength: length:context:exception:) context: nil]; return false; case 4: /* IPv6 */ [_socket asyncReadIntoBuffer: _buffer exactLength: 16 + 2 target: self selector: @selector(socket: didReadSOCKS5Address:length: context:exception:) context: nil]; return false; default: | > > > > > | 551 552 553 554 555 556 557 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 | port: _port socket: _socket errNo: errNo]; [self didConnect]; return false; } runLoopMode = [[OFRunLoop currentRunLoop] currentMode]; /* Skip the rest of the response */ switch (response[3]) { case 1: /* IPv4 */ [_socket asyncReadIntoBuffer: _buffer exactLength: 4 + 2 runLoopMode: runLoopMode target: self selector: @selector(socket: didReadSOCKS5Address:length: context:exception:) context: nil]; return false; case 3: /* Domain name */ [_socket asyncReadIntoBuffer: _buffer exactLength: 1 runLoopMode: runLoopMode target: self selector: @selector(socket: didReadSOCKS5AddressLength: length:context:exception:) context: nil]; return false; case 4: /* IPv6 */ [_socket asyncReadIntoBuffer: _buffer exactLength: 16 + 2 runLoopMode: runLoopMode target: self selector: @selector(socket: didReadSOCKS5Address:length: context:exception:) context: nil]; return false; default: |
︙ | ︙ | |||
608 609 610 611 612 613 614 615 616 617 618 619 620 621 | _exception = [exception retain]; [self didConnect]; return false; } [_socket asyncReadIntoBuffer: _buffer exactLength: addressLength[0] + 2 target: self selector: @selector(socket:didReadSOCKS5Address: length:context:exception:) context: nil]; return false; } @end | > | 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 | _exception = [exception retain]; [self didConnect]; return false; } [_socket asyncReadIntoBuffer: _buffer exactLength: addressLength[0] + 2 runLoopMode: [[OFRunLoop currentRunLoop] currentMode] target: self selector: @selector(socket:didReadSOCKS5Address: length:context:exception:) context: nil]; return false; } @end |
︙ | ︙ | |||
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 | } - (void)asyncConnectToHost: (OFString *)host port: (uint16_t)port target: (id)target selector: (SEL)selector context: (id)context { void *pool = objc_autoreleasePoolPush(); [[[[OFTCPSocket_ConnectContext alloc] initWithSocket: self host: host port: port SOCKS5Host: _SOCKS5Host SOCKS5Port: _SOCKS5Port target: target selector: selector | > > > > > > > > > > > > > > > | > > > > > > > > > > > > | > | 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 | } - (void)asyncConnectToHost: (OFString *)host port: (uint16_t)port target: (id)target selector: (SEL)selector context: (id)context { [self asyncConnectToHost: host port: port runLoopMode: of_run_loop_mode_default target: target selector: selector context: context]; } - (void)asyncConnectToHost: (OFString *)host port: (uint16_t)port runLoopMode: (of_run_loop_mode_t)runLoopMode target: (id)target selector: (SEL)selector context: (id)context { void *pool = objc_autoreleasePoolPush(); [[[[OFTCPSocket_ConnectContext alloc] initWithSocket: self host: host port: port SOCKS5Host: _SOCKS5Host SOCKS5Port: _SOCKS5Port target: target selector: selector context: context] autorelease] startWithRunLoopMode: runLoopMode]; objc_autoreleasePoolPop(pool); } #ifdef OF_HAVE_BLOCKS - (void)asyncConnectToHost: (OFString *)host port: (uint16_t)port block: (of_tcp_socket_async_connect_block_t)block { [self asyncConnectToHost: host port: port runLoopMode: of_run_loop_mode_default block: block]; } - (void)asyncConnectToHost: (OFString *)host 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_ConnectContext 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 port: (uint16_t)port |
︙ | ︙ | |||
1063 1064 1065 1066 1067 1068 1069 1070 1071 | return client; } - (void)asyncAcceptWithTarget: (id)target selector: (SEL)selector context: (id)context { [OFRunLoop of_addAsyncAcceptForTCPSocket: self | > > > > > > > > > > > | > > > > > > > | | 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 | return client; } - (void)asyncAcceptWithTarget: (id)target selector: (SEL)selector context: (id)context { [self asyncAcceptWithRunLoopMode: of_run_loop_mode_default target: target selector: selector context: context]; } - (void)asyncAcceptWithRunLoopMode: (of_run_loop_mode_t)runLoopMode target: (id)target selector: (SEL)selector context: (id)context { [OFRunLoop of_addAsyncAcceptForTCPSocket: self mode: runLoopMode target: target selector: selector context: context]; } #ifdef OF_HAVE_BLOCKS - (void)asyncAcceptWithBlock: (of_tcp_socket_async_accept_block_t)block { [self asyncAcceptWithRunLoopMode: of_run_loop_mode_default block: block]; } - (void)asyncAcceptWithRunLoopMode: (of_run_loop_mode_t)runLoopMode block: (of_tcp_socket_async_accept_block_t)block { [OFRunLoop of_addAsyncAcceptForTCPSocket: self mode: runLoopMode block: block]; } #endif - (const of_socket_address_t *)remoteAddress { if (_socket == INVALID_SOCKET) |
︙ | ︙ |