Re: How to watch file descriptor activity via GMainLoop?



Just use g_io_channel_add_watch(), which installs a GSource for you which
holds an internal reference to the io channel (you can simplify your refcounting
a bit if it makes sense in your code to pass ownership of your iochannel to
the installed io watch sources, g_source_remove() will then unref your
iochannel)

You'll probably use some struct or object holding the identifier of
your clients, ip or
any other metadata, that you might maintain in a table or whatnot, you can
pass this into your io watch sources as user data to compute input with.

And then you may decide you need to run your connections in separate threads,
that would just be a matter of creating a per-thread GMainloop and GMainContext
and adding your io watch sources to the correct context.

Cheers,
              -Tristan

On Thu, Jan 29, 2009 at 11:25 AM, Toralf Lund <toralf procaptura com> wrote:
> I'm trying to integrate a raw socket based communication link into a main
> loop using GMainLoop - on the server-side, i.e. the process that listens for
> connections on the sockets. Does anyone have any good examples on how this
> might be done? I suppose I want a callback, doing accept() on the socket,
> every time there is activity on the main file descriptor for the socket, and
> subsequently when data arrives on the filedescriptor(s) from accept() - but
> how exactly do I set this up?
>
> Maybe GPollFd and  g_source_add_poll() are the keys here, but the
> documentation is not very clear on how to use these. Also, a normal
> GSourceFunc type callback does not quite seem suitable for this, in that no
> reference to the file descriptor will be passed (is there an alternative?)
> Also, the following note got me wondering [From the g_source_add_poll()
> doc]:
>
>   The event source's check function will typically test the
>   /|revents|/ field in the GPollFD
>
> <http://library.gnome.org/devel/glib/stable/glib-The-Main-Event-Loop.html#GPollFD>
>   struct and return |TRUE|
>
> <http://library.gnome.org/devel/glib/stable/glib-Standard-Macros.html#TRUE--CAPS>
>   if events need to be processed.
>
> Shouldn't the function do the actual event handling, and return true or
> false based on whether or not the GMainLoop should keep watching for
> activity?
>
> I'm confused... Help, anyone?
>
> - Toralf
>
>
>
> This email and any files contained therein is confidential and may contain
> privileged information.  If you are not the named addressee(s) or you have
> otherwise received this in error, you should not distribute or copy this
> e-mail or use any of its content for any purpose. Please notify the sender
> immediately by e-mail if you have received this e-mail in error and delete
> it from your system
> _______________________________________________
> gtk-list mailing list
> gtk-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtk-list
>


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