Re: [gnet] Re: GIOChannels, GNet, GOIO



"James M. Cape" <jcape ignore-your tv> writes:

> > 	I'm not worried about the mere allocation of a GObject.  I'm
> > quite comfortable with that, in fact.  What I am worried about is a
> > stack of these things, forming a nice tower of code passing that is, in
> > a word, awful.  SysV STREAMS is my example.
> > 	Consider the simple "here's my fd, please watch in GMainLoop
> > and let me call read_chars() in my callback".  In GIOChannel, there is a
> > GIOChannel structure allocated, it happily registers your callbacks, and
> > g_io_channel_get_chars() does the right thing (mere copy to your buffer
> > if you turn off encoding conversion).

The code I usually use for this is

        http://www.daimi.au.dk/~sandmann/lac-0.5/src/lacwatch.c

The API is this:

       typedef void (* LacWatchCallback) (gpointer data);

        void     lac_fd_add_watch             (gint             fd,
                                               gpointer         data);
        void     lac_fd_set_read_callback     (gint             fd,
                                               LacWatchCallback read_cb);
        void     lac_fd_set_write_callback    (gint             fd,
                                               LacWatchCallback write_cb);
        void     lac_fd_set_hangup_callback   (gint             fd,
                                               LacWatchCallback hangup_cb);
        void     lac_fd_set_error_callback    (gint             fd,
                                               LacWatchCallback error_cb);
        void     lac_fd_set_priority_callback (gint             fd,
                                               LacWatchCallback priority_cb);
        void     lac_fd_remove_watch          (gint             fd);
        gboolean lac_fd_is_watched            (gint             fd);

For example, to watch a pollable file descriptor for reads, you use

        lac_fd_add_watch (fd, your_data);
        lac_fd_set_read_callback (fd, read_callback);

then read_callback will be called every time the fd becomes
readable. To remove the read callback use

        lac_fd_set_read_callback (fd, NULL);

And when you are done:

        lac_fd_remove_watch (fd);
        close (fd);

Ie., it basically maintains a static hashtable mapping file desciptors
to callbacks. The callback is called at the appropriate time if it is
non-NULL. You can only have one of each type callback per
filedescriptor. 

This simple approach has worked very well for me in several cases. I
don't see the any benefit in making it GObject based, becuase I don't
expect languages, except possibly C++, to bind this.


Søren



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]