Wraps Core Foundation's sockets in Objective-C clothing. More...
#import <CFSocket.h>
Instance Methods | |
(id) | - initWithSocketRef: |
Designated initialiser. More... | |
(id) | - initWithProtocolFamily:socketType:protocol: |
(id) | - initForTCPv6 |
(id) | - initForTCPv4 |
(id) | - initForTCP |
Instantiates and creates a TCP socket using Internet Protocol version 6 if available, else tries version 4. More... | |
(id) | - initWithNativeHandle: |
Initialises using a native socket handle. More... | |
(BOOL) | - setAddress:error: |
Binds an address to a socket. More... | |
(BOOL) | - connectToAddress:timeout:error: |
(void) | - invalidate |
(BOOL) | - isValid |
(NSData *) | - address |
(NSData *) | - peerAddress |
(NSSocketNativeHandle) | - nativeHandle |
(BOOL) | - setReuseAddressOption: |
(int) | - addressFamily |
Answers the socket address family. More... | |
(int) | - port |
Answers the port number. More... | |
(void) | - addToCurrentRunLoopForCommonModes |
(void) | - removeFromCurrentRunLoopForCommonModes |
(void) | - disableAcceptCallBack |
(void) | - enableAcceptCallBack |
(void) | - acceptNativeHandle: |
Handles the socket accept call-back behaviour. More... | |
(id) | - init [implementation] |
(void) | - dealloc [implementation] |
Protected Attributes | |
CFSocketRef | _socket |
CFRunLoopSourceRef | _runLoopSource |
Properties | |
id< CFSocketDelegate > | delegate |
Wraps Core Foundation's sockets in Objective-C clothing.
Handles ARC memory management issues for a socket and implements a delegation protocol. As an object, you can sub-class the socket. It papers over the toll-free bridging requirements necessary for interactions between Core Foundation and Foundation. Start by initialising a socket, either an explicit IPv6 or IPv4 TCP socket, or whichever version of TCP socket the system makes available to you. Note that IPv4 clients can also access IPv6 sockets.
Note, you can have an object class called CFSocket; it does not clash with Apple's Core Foundation C-based socket functions, externals and constants because those exist in the C name space, while CFSocket here exists in the Objective-C name space. They do not collide.
Definition at line 54 of file CFSocket.h.
- (void) acceptNativeHandle: | (NSSocketNativeHandle) | nativeHandle |
Handles the socket accept call-back behaviour.
Executes when Next Step meets Core Foundation. The argument specifies a native socket handle, an integer defining the Unix socket descriptor.
Exists to allow for optional overriding. You do not need to deploy the delegate protocol if your sub-class handles "accept native handle" events directly; though delegation usually works best.
nativeHandle | Platform-specific native socket handle. |
Definition at line 205 of file CFSocket.m.
- (NSData *) address |
Definition at line 128 of file CFSocket.m.
- (int) addressFamily |
Answers the socket address family.
For Internet-based sockets, answers either AF_INET6
or AF_INET
. The latter for IP version 4 addresses. Note, protocol and address family are one and the same for Internet addresses. Protocol families are defined in terms of their address family; the PF_
equals its corresponding AF_
manifest constant.
You can use this for polymorphic behaviour. If behaviour depends on a particular kind of socket, you can ask this method for the underlying address family and respond accordingly. This method differs from -address which binds the address first. The implementation here obtains the address family non-destructively; socket state remains unchanged.
AF_MAX
when an error occurs. On error, standard library errno
value indicates the problem. Definition at line 149 of file CFSocket.m.
- (void) addToCurrentRunLoopForCommonModes |
Definition at line 173 of file CFSocket.m.
- (BOOL) connectToAddress: | (NSData *) | addressData | |
timeout: | (NSTimeInterval) | timeout | |
error: | (NSError **) | outError | |
Definition at line 103 of file CFSocket.m.
|
implementation |
Definition at line 233 of file CFSocket.m.
- (void) disableAcceptCallBack |
Definition at line 194 of file CFSocket.m.
- (void) enableAcceptCallBack |
Definition at line 199 of file CFSocket.m.
|
implementation |
Definition at line 38 of file CFSocket.m.
- (id) initForTCP |
Instantiates and creates a TCP socket using Internet Protocol version 6 if available, else tries version 4.
Starts by creating a socket for Internet Protocol version 6. This also supports IPv4 clients via IPv4-mapped IPv6 addresses. Falls back to IPv4 only when IPv6 fails.
nil
if the socket fails to create. Definition at line 76 of file CFSocket.m.
- (id) initForTCPv4 |
Definition at line 71 of file CFSocket.m.
- (id) initForTCPv6 |
Definition at line 66 of file CFSocket.m.
- (id) initWithNativeHandle: | (NSSocketNativeHandle) | nativeHandle |
Initialises using a native socket handle.
nativeHandle | Platform-specific native socket handle. |
Definition at line 83 of file CFSocket.m.
- (id) initWithProtocolFamily: | (int) | family | |
socketType: | (int) | type | |
protocol: | (int) | protocol | |
Definition at line 60 of file CFSocket.m.
- (id) initWithSocketRef: | (CFSocketRef) | socket |
Designated initialiser.
Initialisers also create the underlying Core Foundation socket. You cannot have a partially initialised Objective-C socket. When socket creation fails, initialisation fails also. All socket initialisers follow this pattern. Hence, you cannot initialise a socket with a NULL
socket reference. In such cases, the initialiser answers nil
.
This approach creates a slight quandary. Creating a Core Foundation socket requires a socket context. The context needs to retain a bridging reference to self
, the Objective-C object encapsulating the socket. Otherwise, the socket call-back function cannot springboard from C to Objective-C when call-backs trigger. When the initialiser returns successfully however, the answer overwrites self
. What if self
changes? If it changes to nil
, no problem. But what if it changes to some other pointer address?
TODO: Add more initialisers; specifically, socket signature initialisers.
socket | Reference to a CFSocket object. |
Definition at line 44 of file CFSocket.m.
- (void) invalidate |
Definition at line 117 of file CFSocket.m.
- (BOOL) isValid |
Definition at line 123 of file CFSocket.m.
- (NSSocketNativeHandle) nativeHandle |
Definition at line 138 of file CFSocket.m.
- (NSData *) peerAddress |
Definition at line 133 of file CFSocket.m.
- (int) port |
Answers the port number.
The answer depends on the socket's address family: version 4 or
Definition at line 156 of file CFSocket.m.
- (void) removeFromCurrentRunLoopForCommonModes |
Definition at line 184 of file CFSocket.m.
- (BOOL) setAddress: | (NSData *) | addressData | |
error: | (NSError **) | outError | |
Binds an address to a socket.
Despite the innocuous-sounding method name, this method irreversibly binds the socket; assuming success. Do this early when constructing a socket. Be aware that accessing the address also binds the socket, if not already bound. You cannot therefore subsequently bind it. If you want to bind to a specific port, do so by setting the socket address before asking for the address; that is, before using the getter method.
Definition at line 89 of file CFSocket.m.
- (BOOL) setReuseAddressOption: | (BOOL) | flag |
Definition at line 143 of file CFSocket.m.
|
protected |
Definition at line 57 of file CFSocket.h.
|
protected |
Definition at line 56 of file CFSocket.h.
|
readwriteatomicweak |
Definition at line 60 of file CFSocket.h.