Search
lxdream.org :: lxdream/src/drivers/net_osx.m
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/net_osx.m
changeset 1021:848db285a184
prev998:1754a8c6a9cf
next1071:182cfe43c09e
author nkeynes
date Wed Jun 03 11:37:10 2009 +0000 (13 years ago)
permissions -rw-r--r--
last change Add missing svn:keywords property
file annotate diff log raw
nkeynes@998
     1
/**
nkeynes@1021
     2
 * $Id$
nkeynes@998
     3
 *
nkeynes@998
     4
 * OS X networking support functions. Currently this is just for activity callbacks.
nkeynes@998
     5
 *
nkeynes@998
     6
 * Copyright (c) 2009 Nathan Keynes.
nkeynes@998
     7
 *
nkeynes@998
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@998
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@998
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@998
    11
 * (at your option) any later version.
nkeynes@998
    12
 *
nkeynes@998
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@998
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@998
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@998
    16
 * GNU General Public License for more details.
nkeynes@998
    17
 */
nkeynes@998
    18
nkeynes@998
    19
#include <CoreFoundation/CoreFoundation.h>
nkeynes@998
    20
#include "netutil.h"
nkeynes@998
    21
nkeynes@998
    22
struct net_osx_cbinfo {
nkeynes@998
    23
    net_callback_t callback;
nkeynes@998
    24
    void * cbdata;
nkeynes@998
    25
    void (*cbdealloc)(void *);
nkeynes@998
    26
    CFSocketRef sockRef;
nkeynes@998
    27
    CFRunLoopSourceRef sourceRef;
nkeynes@998
    28
};
nkeynes@998
    29
nkeynes@998
    30
static void net_osx_callback( CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *unused, void *data )
nkeynes@998
    31
{
nkeynes@998
    32
    struct net_osx_cbinfo *cbinfo = (struct net_osx_cbinfo *)data;
nkeynes@998
    33
    if(!cbinfo->callback( CFSocketGetNative(s), cbinfo->cbdata) ) {
nkeynes@998
    34
        CFRunLoopRemoveSource( CFRunLoopGetCurrent(), cbinfo->sourceRef, kCFRunLoopCommonModes );
nkeynes@998
    35
        CFRelease(cbinfo->sourceRef);
nkeynes@998
    36
        cbinfo->sourceRef = NULL;
nkeynes@998
    37
        CFRelease(cbinfo->sockRef);
nkeynes@998
    38
    }
nkeynes@998
    39
}
nkeynes@998
    40
nkeynes@998
    41
static void net_osx_release( const void *data )
nkeynes@998
    42
{
nkeynes@998
    43
    struct net_osx_cbinfo *cbinfo = (struct net_osx_cbinfo *)data;
nkeynes@998
    44
    if( cbinfo->cbdealloc != NULL ) {
nkeynes@998
    45
        cbinfo->cbdealloc(cbinfo->cbdata);
nkeynes@998
    46
    }
nkeynes@998
    47
    free( cbinfo );
nkeynes@998
    48
}
nkeynes@998
    49
nkeynes@998
    50
/**
nkeynes@998
    51
 * Register a TCP server socket listener on an already open (and listening) 
nkeynes@998
    52
 * socket. The socket must not have been previously registered.
nkeynes@998
    53
 * @return TRUE on success, FALSE on failure.
nkeynes@998
    54
 * 
nkeynes@998
    55
 * Defined in netutil.h
nkeynes@998
    56
 */ 
nkeynes@998
    57
gboolean net_register_tcp_listener( int fd, net_callback_t callback, void *data, void (*dealloc)(void*) )
nkeynes@998
    58
{
nkeynes@998
    59
    CFSocketContext socketContext;
nkeynes@998
    60
    struct net_osx_cbinfo *cbinfo = malloc( sizeof(struct net_osx_cbinfo) );
nkeynes@998
    61
    assert(cbinfo != NULL);
nkeynes@998
    62
    
nkeynes@998
    63
    cbinfo->callback = callback;
nkeynes@998
    64
    cbinfo->cbdata = data;
nkeynes@998
    65
    cbinfo->cbdealloc = dealloc;
nkeynes@998
    66
    socketContext.version = 0;
nkeynes@998
    67
    socketContext.info = cbinfo;
nkeynes@998
    68
    socketContext.retain = NULL;
nkeynes@998
    69
    socketContext.release = net_osx_release;
nkeynes@998
    70
    socketContext.copyDescription = NULL;
nkeynes@998
    71
    
nkeynes@998
    72
    cbinfo->sockRef = CFSocketCreateWithNative( kCFAllocatorDefault, fd, kCFSocketReadCallBack, 
nkeynes@998
    73
            net_osx_callback, &socketContext );
nkeynes@998
    74
    cbinfo->sourceRef = CFSocketCreateRunLoopSource( kCFAllocatorDefault, cbinfo->sockRef, 0 );
nkeynes@998
    75
    CFRunLoopAddSource( CFRunLoopGetCurrent(), cbinfo->sourceRef, kCFRunLoopCommonModes );
nkeynes@998
    76
}
.