1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/drivers/io_glib.c Sun Mar 04 21:10:12 2012 +1000
1.7 + * Glib-based networking support functions. Currently this is just for activity callbacks.
1.9 + * Copyright (c) 2009 Nathan Keynes.
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.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.22 +#include <assert.h>
1.24 +#include <stdlib.h>
1.25 +#include "ioutil.h"
1.27 +struct io_glib_cbinfo {
1.28 + io_callback_t callback;
1.31 + void (*cbdealloc)(void *);
1.34 +static gboolean io_glib_callback( GIOChannel *source, GIOCondition cond, gpointer data )
1.36 + struct io_glib_cbinfo *cbinfo = (struct io_glib_cbinfo *)data;
1.37 + return cbinfo->callback( g_io_channel_unix_get_fd(source), cbinfo->cbdata);
1.40 +static void io_glib_release( void *data )
1.42 + struct io_glib_cbinfo *cbinfo = (struct io_glib_cbinfo *)data;
1.43 + if( cbinfo->cbdealloc ) {
1.44 + cbinfo->cbdealloc( cbinfo->cbdata );
1.50 + * Register a TCP server socket listener on an already open (and listening)
1.51 + * socket. The socket must not have been previously registered.
1.52 + * @return TRUE on success, FALSE on failure.
1.54 + * Defined in netutil.h
1.56 +io_listener_t io_register_tcp_listener( int fd, io_callback_t callback, void *data, void (*dealloc)(void*) )
1.58 + return io_register_listener( fd, callback, data, dealloc );
1.61 +io_listener_t io_register_listener( int fd, io_callback_t callback, void *data, void (*dealloc)(void *) )
1.63 + struct io_glib_cbinfo *cbinfo = malloc( sizeof(struct io_glib_cbinfo) );
1.64 + assert(cbinfo != NULL);
1.66 + cbinfo->callback = callback;
1.67 + cbinfo->cbdata = data;
1.68 + cbinfo->cbdealloc = dealloc;
1.71 + * Note magic here: the watch creates an event source which holds a
1.72 + * reference to the channel. We unref the channel so that the channel then
1.73 + * is automatically released when the event source goes away.
1.75 + GIOChannel *chan = g_io_channel_unix_new(fd);
1.76 + g_io_channel_set_encoding( chan, NULL, NULL );
1.77 + g_io_channel_set_buffered(chan, FALSE);
1.78 + cbinfo->sourceid = g_io_add_watch_full( chan, 0, G_IO_IN, io_glib_callback, cbinfo, io_glib_release );
1.79 + g_io_channel_unref( chan );
1.84 +void io_unregister_listener( io_listener_t data )
1.86 + struct io_glib_cbinfo *cbinfo = (struct io_glib_cbinfo *)data;
1.87 + g_source_remove(cbinfo->sourceid);