4 * OS X networking support functions. Currently this is just for activity callbacks.
6 * Copyright (c) 2009 Nathan Keynes.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <CoreFoundation/CoreFoundation.h>
22 struct io_osx_cbinfo {
24 io_callback_t callback;
26 void (*cbdealloc)(void *);
28 CFRunLoopSourceRef sourceRef;
30 struct io_osx_cbinfo *next;
33 static struct io_osx_cbinfo *cbinfo_list = NULL;
35 void io_unregister_callback( struct io_osx_cbinfo *cbinfo )
37 CFRunLoopRemoveSource( CFRunLoopGetCurrent(), cbinfo->sourceRef, kCFRunLoopCommonModes );
38 CFRelease(cbinfo->sourceRef);
39 cbinfo->sourceRef = NULL;
40 CFRelease(cbinfo->fdRef); /* Note this implicitly releases the cbinfo itself as well */
43 static void io_osx_net_callback( CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *unused, void *data )
45 struct io_osx_cbinfo *cbinfo = (struct io_osx_cbinfo *)data;
46 if(!cbinfo->callback( CFSocketGetNative(s), cbinfo->cbdata) ) {
47 io_unregister_callback(cbinfo);
51 static void io_osx_fd_callback( CFFileDescriptorRef f, CFOptionFlags type, void *data )
53 struct io_osx_cbinfo *cbinfo = (struct io_osx_cbinfo *)data;
54 if(!cbinfo->callback( CFFileDescriptorGetNativeDescriptor(f), cbinfo->cbdata) ) {
55 io_unregister_callback(cbinfo);
59 static void io_osx_release( const void *data )
61 struct io_osx_cbinfo *cbinfo = (struct io_osx_cbinfo *)data;
62 if( cbinfo->cbdealloc != NULL ) {
63 cbinfo->cbdealloc(cbinfo->cbdata);
69 * Register a TCP server socket listener on an already open (and listening)
70 * socket. The socket must not have been previously registered.
71 * @return TRUE on success, FALSE on failure.
73 * Defined in netutil.h
75 io_listener_t io_register_tcp_listener( int fd, io_callback_t callback, void *data, void (*dealloc)(void*) )
77 CFSocketContext socketContext;
78 struct io_osx_cbinfo *cbinfo = malloc( sizeof(struct io_osx_cbinfo) );
79 assert(cbinfo != NULL);
81 cbinfo->callback = callback;
82 cbinfo->cbdata = data;
83 cbinfo->cbdealloc = dealloc;
84 socketContext.version = 0;
85 socketContext.info = cbinfo;
86 socketContext.retain = NULL;
87 socketContext.release = io_osx_release;
88 socketContext.copyDescription = NULL;
90 CFSocketRef ref = CFSocketCreateWithNative( kCFAllocatorDefault, fd, kCFSocketReadCallBack,
91 io_osx_net_callback, &socketContext );
93 cbinfo->sourceRef = CFSocketCreateRunLoopSource( kCFAllocatorDefault, ref, 0 );
94 CFRunLoopAddSource( CFRunLoopGetCurrent(), cbinfo->sourceRef, kCFRunLoopCommonModes );
100 * Register a file descriptor listener on an already open (and listening)
101 * file descriptor. The file descriptor must not have been previously registered.
102 * @return TRUE on success, FALSE on failure.
105 io_listener_t io_register_listener( int fd, io_callback_t callback, void *data, void (*dealloc)(void *) )
107 CFFileDescriptorContext fdContext;
108 struct io_osx_cbinfo *cbinfo = malloc( sizeof(struct io_osx_cbinfo) );
109 assert(cbinfo != NULL);
111 cbinfo->callback = callback;
112 cbinfo->cbdata = data;
113 cbinfo->cbdealloc = dealloc;
114 fdContext.version = 0;
115 fdContext.retain = NULL;
116 fdContext.info = cbinfo;
117 fdContext.release = io_osx_release;
118 fdContext.copyDescription = NULL;
120 CFFileDescriptorRef ref = CFFileDescriptorCreate( kCFAllocatorDefault, fd, FALSE,
121 io_osx_fd_callback, &fdContext);
123 cbinfo->sourceRef = CFFileDescriptorCreateRunLoopSource( kCFAllocatorDefault, ref, 0 );
124 CFRunLoopAddSource( CFRunLoopGetCurrent(), cbinfo->sourceRef, kCFRunLoopCommonModes );
128 void io_unregister_listener( io_listener_t data )
130 struct io_osx_cbinfo *cbinfo = (struct io_osx_cbinfo *)data;
131 io_unregister_callback(cbinfo);
.