Search
lxdream.org :: lxdream/src/drivers/io_glib.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/io_glib.c
changeset 1077:136fc24d17ef
prev1021:848db285a184
author nkeynes
date Tue Feb 07 14:27:13 2012 +1000 (12 years ago)
permissions -rw-r--r--
last change Add wrangling for some of the core vs EXT/ARB versions of functions + macros
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/drivers/io_glib.c Tue Feb 07 14:27:13 2012 +1000
1.3 @@ -0,0 +1,85 @@
1.4 +/**
1.5 + * $Id$
1.6 + *
1.7 + * Glib-based 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 <assert.h>
1.23 +#include <glib.h>
1.24 +#include <stdlib.h>
1.25 +#include "ioutil.h"
1.26 +
1.27 +struct io_glib_cbinfo {
1.28 + io_callback_t callback;
1.29 + guint sourceid;
1.30 + void * cbdata;
1.31 + void (*cbdealloc)(void *);
1.32 +};
1.33 +
1.34 +static gboolean io_glib_callback( GIOChannel *source, GIOCondition cond, gpointer data )
1.35 +{
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.38 +}
1.39 +
1.40 +static void io_glib_release( void *data )
1.41 +{
1.42 + struct io_glib_cbinfo *cbinfo = (struct io_glib_cbinfo *)data;
1.43 + if( cbinfo->cbdealloc ) {
1.44 + cbinfo->cbdealloc( cbinfo->cbdata );
1.45 + }
1.46 + free(cbinfo);
1.47 +}
1.48 +
1.49 +/**
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.53 + *
1.54 + * Defined in netutil.h
1.55 + */
1.56 +io_listener_t io_register_tcp_listener( int fd, io_callback_t callback, void *data, void (*dealloc)(void*) )
1.57 +{
1.58 + return io_register_listener( fd, callback, data, dealloc );
1.59 +}
1.60 +
1.61 +io_listener_t io_register_listener( int fd, io_callback_t callback, void *data, void (*dealloc)(void *) )
1.62 +{
1.63 + struct io_glib_cbinfo *cbinfo = malloc( sizeof(struct io_glib_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 +
1.70 + /**
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.74 + */
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.80 + return cbinfo;
1.81 +}
1.82 +
1.83 +
1.84 +void io_unregister_listener( io_listener_t data )
1.85 +{
1.86 + struct io_glib_cbinfo *cbinfo = (struct io_glib_cbinfo *)data;
1.87 + g_source_remove(cbinfo->sourceid);
1.88 +}
.