Search
lxdream.org :: lxdream/src/drivers/net_glib.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/net_glib.c
changeset 1021:848db285a184
prev998:1754a8c6a9cf
author nkeynes
date Sun Jun 28 01:34:55 2009 +0000 (14 years ago)
permissions -rw-r--r--
last change Replace printf with DEBUG messages (patch by Wahrhaft)
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Glib-based networking support functions. Currently this is just for activity callbacks.
     5  *
     6  * Copyright (c) 2009 Nathan Keynes.
     7  *
     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.
    12  *
    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.
    17  */
    19 #include <assert.h>
    20 #include <glib.h>
    21 #include <stdlib.h>
    22 #include "netutil.h"
    24 struct net_glib_cbinfo {
    25     net_callback_t callback;
    26     void * cbdata;
    27     void (*cbdealloc)(void *);
    28 };
    30 static gboolean net_glib_callback( GIOChannel *source, GIOCondition cond, gpointer data )
    31 {
    32     struct net_glib_cbinfo *cbinfo = (struct net_glib_cbinfo *)data;
    33     return cbinfo->callback( g_io_channel_unix_get_fd(source), cbinfo->cbdata);
    34 }
    36 static void net_glib_release( void *data )
    37 {
    38     struct net_glib_cbinfo *cbinfo = (struct net_glib_cbinfo *)data;
    39     if( cbinfo->cbdealloc ) {
    40         cbinfo->cbdealloc( cbinfo->cbdata );
    41     }
    42     free(cbinfo);
    43 }
    45 /**
    46  * Register a TCP server socket listener on an already open (and listening) 
    47  * socket. The socket must not have been previously registered.
    48  * @return TRUE on success, FALSE on failure.
    49  * 
    50  * Defined in netutil.h
    51  */ 
    52 gboolean net_register_tcp_listener( int fd, net_callback_t callback, void *data, void (*dealloc)(void*) )
    53 {
    54     struct net_glib_cbinfo *cbinfo = malloc( sizeof(struct net_glib_cbinfo) );
    55     assert(cbinfo != NULL);
    57     cbinfo->callback = callback;
    58     cbinfo->cbdata = data;
    59     cbinfo->cbdealloc = dealloc;
    61     /**
    62      * Note magic here: the watch creates an event source which holds a 
    63      * reference to the channel. We unref the channel so that the channel then
    64      * is automatically released when the event source goes away.
    65      */
    66     GIOChannel *chan = g_io_channel_unix_new(fd);
    67     g_io_channel_set_encoding( chan, NULL, NULL );
    68     g_io_channel_set_buffered(chan, FALSE);
    69     g_io_add_watch_full( chan, 0, G_IO_IN, net_glib_callback, cbinfo, net_glib_release );
    70     g_io_channel_unref( chan );
    71     return TRUE;
    72 }
.