Differences From Artifact [367b0cdd47]:
- File
src/OFTCPSocket.m
— part of check-in
[e54c8c0368]
at
2014-01-16 23:38:42
on branch trunk
— Add +[OFString nativeOSEncoding].
This replaces OF_STRING_ENCODING_NATIVE, as a define was not flexible
enough (determining the native OS encoding at startup was not possible). (user: js, size: 17417) [annotate] [blame] [check-ins using]
To Artifact [f0c5480103]:
- File
src/OFTCPSocket.m
— part of check-in
[81d47f4398]
at
2014-01-25 19:33:57
on branch trunk
— Move socket includes and helpers to separate files
The new file socket.h includes all headers required for sockets on the
used platform, while the file socket_helpers.h defines the BSD API
functions to the platform specific functions if necessary.This cleans up the classes dealing with sockets a lot and also reduces
code duplication. (user: js, size: 15858) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
21 22 23 24 25 26 27 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> | < < < < < < < < < < | 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #import "OFTCPSocket.h" #import "OFTCPSocket+SOCKS5.h" #import "OFString.h" #import "OFThread.h" #import "OFTimer.h" #import "OFRunLoop.h" #import "OFRunLoop+Private.h" |
︙ | ︙ | |||
52 53 54 55 56 57 58 | #import "OFListenFailedException.h" #import "OFNotConnectedException.h" #import "OFNotImplementedException.h" #import "OFSetOptionFailedException.h" #import "autorelease.h" #import "macros.h" | | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #import "OFListenFailedException.h" #import "OFNotConnectedException.h" #import "OFNotImplementedException.h" #import "OFSetOptionFailedException.h" #import "autorelease.h" #import "macros.h" #import "socket_helpers.h" #if defined(OF_HAVE_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO) # import "OFMutex.h" # import "OFDataArray.h" static OFMutex *mutex = nil; #endif /* References for static linking */ void _references_to_categories_of_OFTCPSocket(void) { _OFTCPSocket_SOCKS5_reference = 1; } |
︙ | ︙ | |||
298 299 300 301 302 303 304 | - init { self = [super init]; @try { _socket = INVALID_SOCKET; | | | 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | - init { self = [super init]; @try { _socket = INVALID_SOCKET; _sockAddrLen = sizeof(struct sockaddr_storage); _SOCKS5Host = [defaultSOCKS5Host copy]; _SOCKS5Port = defaultSOCKS5Port; } @catch (id e) { [self release]; @throw e; } |
︙ | ︙ | |||
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 | close(_socket); _socket = INVALID_SOCKET; @throw [OFBindFailedException exceptionWithHost: host port: port socket: self]; } - (void)listenWithBackLog: (int)backLog { if (_socket == INVALID_SOCKET) @throw [OFNotConnectedException exceptionWithSocket: self]; if (listen(_socket, backLog) == -1) @throw [OFListenFailedException exceptionWithSocket: self backLog: backLog]; _listening = true; } | > > > > > < < < < < < < < < < | < < | > | < < < < | 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 | close(_socket); _socket = INVALID_SOCKET; @throw [OFBindFailedException exceptionWithHost: host port: port socket: self]; } - (void)listen { [self listenWithBackLog: SOMAXCONN]; } - (void)listenWithBackLog: (int)backLog { if (_socket == INVALID_SOCKET) @throw [OFNotConnectedException exceptionWithSocket: self]; if (listen(_socket, backLog) == -1) @throw [OFListenFailedException exceptionWithSocket: self backLog: backLog]; _listening = true; } - (instancetype)accept { OFTCPSocket *client = [[[[self class] alloc] init] autorelease]; if ((client->_socket = accept(_socket, (struct sockaddr*)&client->_sockAddr, &client->_sockAddrLen)) == INVALID_SOCKET) @throw [OFAcceptFailedException exceptionWithSocket: self]; return client; } - (void)asyncAcceptWithTarget: (id)target selector: (SEL)selector { |
︙ | ︙ | |||
742 743 744 745 746 747 748 | @throw [OFSetOptionFailedException exceptionWithStream: self]; } - (OFString*)remoteAddress { char *host; | | | | | | 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 | @throw [OFSetOptionFailedException exceptionWithStream: self]; } - (OFString*)remoteAddress { char *host; if (_socket == INVALID_SOCKET) @throw [OFNotConnectedException exceptionWithSocket: self]; #ifdef HAVE_THREADSAFE_GETADDRINFO host = [self allocMemoryWithSize: NI_MAXHOST]; @try { if (getnameinfo((struct sockaddr*)&_sockAddr, _sockAddrLen, host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST | NI_NUMERICSERV)) @throw [OFAddressTranslationFailedException exceptionWithSocket: self]; return [OFString stringWithUTF8String: host]; } @finally { [self freeMemory: host]; } #else # ifdef OF_HAVE_THREADS [mutex lock]; @try { # endif host = inet_ntoa(((struct sockaddr_in*)&_sockAddr)->sin_addr); if (host == NULL) @throw [OFAddressTranslationFailedException exceptionWithSocket: self]; return [OFString stringWithUTF8String: host]; # ifdef OF_HAVE_THREADS |
︙ | ︙ | |||
792 793 794 795 796 797 798 | } - (void)close { [super close]; _listening = false; | < < | | 721 722 723 724 725 726 727 728 729 730 | } - (void)close { [super close]; _listening = false; _sockAddrLen = sizeof(struct sockaddr_storage); } @end |