29 #import <sys/socket.h>
32 #import <netinet/in.h>
44 - (id)initWithSocketRef:(CFSocketRef)socket
46 if ((
self = [super
init]))
60 - (id)initWithProtocolFamily:(
int)family socketType:(
int)type protocol:(
int)protocol
62 CFSocketContext context = { .info = (__bridge
void *)
self };
63 return [
self initWithSocketRef:CFSocketCreate(kCFAllocatorDefault, family, type, protocol, kCFSocketAcceptCallBack, __CFSocketCallOut, &context)];
83 - (id)initWithNativeHandle:(NSSocketNativeHandle)nativeHandle
85 CFSocketContext context = { .info = (__bridge
void *)
self };
86 return [
self initWithSocketRef:CFSocketCreateWithNative(kCFAllocatorDefault, nativeHandle, kCFSocketAcceptCallBack, __CFSocketCallOut, &context)];
89 - (BOOL)setAddress:(NSData *)addressData error:(NSError **)outError
91 CFSocketError error = CFSocketSetAddress(
_socket, (__bridge CFDataRef)addressData);
92 BOOL success = (error == kCFSocketSuccess);
95 if (outError && *outError == nil)
97 *outError = [NSError errorWithDomain:CFSocketErrorDomain code:error userInfo:nil];
103 - (BOOL)connectToAddress:(NSData *)addressData timeout:(NSTimeInterval)timeout error:(NSError **)outError
105 CFSocketError error = CFSocketConnectToAddress(
_socket, (__bridge CFDataRef)addressData, timeout);
106 BOOL success = (error == kCFSocketSuccess);
109 if (outError && *outError == nil)
111 *outError = [NSError errorWithDomain:CFSocketErrorDomain code:error userInfo:nil];
125 return CFSocketIsValid(
_socket) !=
false;
130 return CFBridgingRelease(CFSocketCopyAddress(
_socket));
135 return CFBridgingRelease(CFSocketCopyPeerAddress(
_socket));
140 return CFSocketGetNative(
_socket);
143 - (BOOL)setReuseAddressOption:(BOOL)flag
145 int option = (flag == NO) ? 0 : 1;
146 return 0 == setsockopt([
self nativeHandle], SOL_SOCKET, SO_REUSEADDR, (
void *)&option,
sizeof(option));
151 uint8_t sockaddr[SOCK_MAXADDRLEN];
152 socklen_t len =
sizeof(sockaddr);
153 return 0 == getsockname([
self nativeHandle], (
struct sockaddr *)sockaddr, &len) && len >= offsetof(
struct sockaddr, sa_data) ? ((
struct sockaddr *)sockaddr)->sa_family : AF_MAX;
162 port = ntohs(((
struct sockaddr_in *)[[
self address] bytes])->sin_port);
165 port = ntohs(((
struct sockaddr_in6 *)[[
self address] bytes])->sin6_port);
180 CFRunLoopAddSource(CFRunLoopGetCurrent(),
_runLoopSource, kCFRunLoopCommonModes);
188 CFRunLoopRemoveSource(CFRunLoopGetCurrent(),
_runLoopSource, kCFRunLoopCommonModes);
196 CFSocketDisableCallBacks(
_socket, kCFSocketAcceptCallBack);
202 CFSocketEnableCallBacks(
_socket, kCFSocketAcceptCallBack);
205 - (void)acceptNativeHandle:(NSSocketNativeHandle)nativeHandle
210 if ([
delegate respondsToSelector:
@selector(socket:acceptNativeHandle:)])
212 [
delegate socket:self acceptNativeHandle:nativeHandle];
214 else if ([
delegate respondsToSelector:
@selector(socket:acceptStreamPair:)])
219 [
delegate socket:self acceptStreamPair:streamPair];
256 case kCFSocketAcceptCallBack:
264 [(__bridge
CFSocket *)
info acceptNativeHandle:*(CFSocketNativeHandle *)data];