Search
lxdream.org :: lxdream/src/drivers/net_glib.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/net_glib.c
changeset 998:1754a8c6a9cf
next1021:848db285a184
author nkeynes
date Tue Mar 24 11:15:57 2009 +0000 (14 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_glib.c Tue Mar 24 11:15:57 2009 +0000
1.3 @@ -0,0 +1,72 @@
1.4 +/**
1.5 + * $Id: net_glib.c 1018 2009-03-19 12:29:06Z nkeynes $
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 "netutil.h"
1.26 +
1.27 +struct net_glib_cbinfo {
1.28 + net_callback_t callback;
1.29 + void * cbdata;
1.30 + void (*cbdealloc)(void *);
1.31 +};
1.32 +
1.33 +static gboolean net_glib_callback( GIOChannel *source, GIOCondition cond, gpointer data )
1.34 +{
1.35 + struct net_glib_cbinfo *cbinfo = (struct net_glib_cbinfo *)data;
1.36 + return cbinfo->callback( g_io_channel_unix_get_fd(source), cbinfo->cbdata);
1.37 +}
1.38 +
1.39 +static void net_glib_release( void *data )
1.40 +{
1.41 + struct net_glib_cbinfo *cbinfo = (struct net_glib_cbinfo *)data;
1.42 + if( cbinfo->cbdealloc ) {
1.43 + cbinfo->cbdealloc( cbinfo->cbdata );
1.44 + }
1.45 + free(cbinfo);
1.46 +}
1.47 +
1.48 +/**
1.49 + * Register a TCP server socket listener on an already open (and listening)
1.50 + * socket. The socket must not have been previously registered.
1.51 + * @return TRUE on success, FALSE on failure.
1.52 + *
1.53 + * Defined in netutil.h
1.54 + */
1.55 +gboolean net_register_tcp_listener( int fd, net_callback_t callback, void *data, void (*dealloc)(void*) )
1.56 +{
1.57 + struct net_glib_cbinfo *cbinfo = malloc( sizeof(struct net_glib_cbinfo) );
1.58 + assert(cbinfo != NULL);
1.59 +
1.60 + cbinfo->callback = callback;
1.61 + cbinfo->cbdata = data;
1.62 + cbinfo->cbdealloc = dealloc;
1.63 +
1.64 + /**
1.65 + * Note magic here: the watch creates an event source which holds a
1.66 + * reference to the channel. We unref the channel so that the channel then
1.67 + * is automatically released when the event source goes away.
1.68 + */
1.69 + GIOChannel *chan = g_io_channel_unix_new(fd);
1.70 + g_io_channel_set_encoding( chan, NULL, NULL );
1.71 + g_io_channel_set_buffered(chan, FALSE);
1.72 + g_io_add_watch_full( chan, 0, G_IO_IN, net_glib_callback, cbinfo, net_glib_release );
1.73 + g_io_channel_unref( chan );
1.74 + return TRUE;
1.75 +}
.