Re: Watching filedescriptors



On Tue, 2005-09-06 at 17:16 +0200, Soeren Sandmann wrote:
> One thing I and many other people regularly need is to be notified
> whenever something happens on a file descriptor. This is needed when
> you write networked applications, or when you with to communicated
> with spawned child processes, and in many other circumstances.
> 
> The way you are supposed to do that with glib right now is with
> GIOChannels. Unfortunately, GIOChannels are quite painful to use, and
> somewhat overengineered.

While I agree that GIOChannels have some over-engineering issues,
I'm pretty strongly opposed to re-adding gdk_input_add() or the
equivalent.

It's not really that hard to do:

 GIOChannel *channel = g_io_channel_unix_new();
 g_io_add_watch (channel, condition, callback, data);
 g_io_channel_unref (channel);

I think most of the difficulty of GIOChannel comes more from the
problems of the underlying Unix APIs. The fact that to detect
a closed socket you have to do:

 G_IO_READ | G_IO_HUP

Then catch 0 length reads is perhaps painful, but it isn't something
we can hide.

> So I propose adding this simple API for glib 2.10:
> 
>     /*
>      * Watching file descriptors
>      */
>     typedef void (* WatchCallback) (gpointer data);
>     
>     void     fd_add_watch             (gint          fd,
>                                        gpointer      data);
>     void     fd_set_read_callback     (gint          fd,
>                                        WatchCallback read_cb);
>     void     fd_set_write_callback    (gint          fd,
>                                        WatchCallback write_cb);
>     void     fd_set_hangup_callback   (gint          fd,
>                                        WatchCallback hangup_cb);
>     void     fd_set_error_callback    (gint          fd,
>                                        WatchCallback error_cb);
>     void     fd_set_priority_callback (gint          fd,
>                                        WatchCallback priority_cb);
>     void     fd_remove_watch          (gint          fd);
>     gboolean fd_is_watched            (gint          fd);

This is utterly divorced from the main loop and the way that the
other stuff in the main loop works; you don't have destroy notification,
you can't add it to a different main loop, g_source_set_closure()
isn't there, etc.

And it doesn't strike me as being significantly more convenient than
what we have now.

Regards,
						Owen

Attachment: signature.asc
Description: This is a digitally signed message part



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