Search
lxdream.org :: lxdream/src/drivers/net_osx.m :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/net_osx.m
changeset 998:1754a8c6a9cf
next1021:848db285a184
author nkeynes
date Tue Mar 24 11:15:57 2009 +0000 (13 years ago)
permissions -rw-r--r--
last change Add preliminary implementation of the GDB remote debugging server - attaches to
either or both the SH4 and ARM
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/drivers/net_osx.m Tue Mar 24 11:15:57 2009 +0000
1.3 @@ -0,0 +1,76 @@
1.4 +/**
1.5 + * $Id: net_osx.m 1018 2009-03-19 12:29:06Z nkeynes $
1.6 + *
1.7 + * OS X networking support functions. Currently this is just for activity callbacks.
1.8 + *
1.9 + * Copyright (c) 2009 Nathan Keynes.
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify
1.12 + * it under the terms of the GNU General Public License as published by
1.13 + * the Free Software Foundation; either version 2 of the License, or
1.14 + * (at your option) any later version.
1.15 + *
1.16 + * This program is distributed in the hope that it will be useful,
1.17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.19 + * GNU General Public License for more details.
1.20 + */
1.21 +
1.22 +#include <CoreFoundation/CoreFoundation.h>
1.23 +#include "netutil.h"
1.24 +
1.25 +struct net_osx_cbinfo {
1.26 + net_callback_t callback;
1.27 + void * cbdata;
1.28 + void (*cbdealloc)(void *);
1.29 + CFSocketRef sockRef;
1.30 + CFRunLoopSourceRef sourceRef;
1.31 +};
1.32 +
1.33 +static void net_osx_callback( CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *unused, void *data )
1.34 +{
1.35 + struct net_osx_cbinfo *cbinfo = (struct net_osx_cbinfo *)data;
1.36 + if(!cbinfo->callback( CFSocketGetNative(s), cbinfo->cbdata) ) {
1.37 + CFRunLoopRemoveSource( CFRunLoopGetCurrent(), cbinfo->sourceRef, kCFRunLoopCommonModes );
1.38 + CFRelease(cbinfo->sourceRef);
1.39 + cbinfo->sourceRef = NULL;
1.40 + CFRelease(cbinfo->sockRef);
1.41 + }
1.42 +}
1.43 +
1.44 +static void net_osx_release( const void *data )
1.45 +{
1.46 + struct net_osx_cbinfo *cbinfo = (struct net_osx_cbinfo *)data;
1.47 + if( cbinfo->cbdealloc != NULL ) {
1.48 + cbinfo->cbdealloc(cbinfo->cbdata);
1.49 + }
1.50 + free( cbinfo );
1.51 +}
1.52 +
1.53 +/**
1.54 + * Register a TCP server socket listener on an already open (and listening)
1.55 + * socket. The socket must not have been previously registered.
1.56 + * @return TRUE on success, FALSE on failure.
1.57 + *
1.58 + * Defined in netutil.h
1.59 + */
1.60 +gboolean net_register_tcp_listener( int fd, net_callback_t callback, void *data, void (*dealloc)(void*) )
1.61 +{
1.62 + CFSocketContext socketContext;
1.63 + struct net_osx_cbinfo *cbinfo = malloc( sizeof(struct net_osx_cbinfo) );
1.64 + assert(cbinfo != NULL);
1.65 +
1.66 + cbinfo->callback = callback;
1.67 + cbinfo->cbdata = data;
1.68 + cbinfo->cbdealloc = dealloc;
1.69 + socketContext.version = 0;
1.70 + socketContext.info = cbinfo;
1.71 + socketContext.retain = NULL;
1.72 + socketContext.release = net_osx_release;
1.73 + socketContext.copyDescription = NULL;
1.74 +
1.75 + cbinfo->sockRef = CFSocketCreateWithNative( kCFAllocatorDefault, fd, kCFSocketReadCallBack,
1.76 + net_osx_callback, &socketContext );
1.77 + cbinfo->sourceRef = CFSocketCreateRunLoopSource( kCFAllocatorDefault, cbinfo->sockRef, 0 );
1.78 + CFRunLoopAddSource( CFRunLoopGetCurrent(), cbinfo->sourceRef, kCFRunLoopCommonModes );
1.79 +}
.